From ed60c74c787d831c06adeba1826fabc170be5fd6 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Mon, 1 Jan 2024 22:22:42 +0100 Subject: [PATCH 1/7] Fix class level loading When doing a `self.class.seed` from within a test, we'd be within the Test Class scope, so the `@loader` variable would assign there and and not on `Oaken::Seeds.loader`. The old structure was a little muddy anyway, so this attempts a rewrite that's hopefully clearer. Now we'll always be within `Oaken::Seeds`' `singleton_class`. Note: originally the point about injecting `load_onto Oaken::Seeds` was so you could potentially have other seeds modules, so you could have your app seeds split up ala Packwerk. But it doesn't really work and it's kinda unclear how I'd make it work, so it's probably better to yank it in a bit. --- lib/oaken/seeds.rb | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/oaken/seeds.rb b/lib/oaken/seeds.rb index f934be1..4888714 100644 --- a/lib/oaken/seeds.rb +++ b/lib/oaken/seeds.rb @@ -20,22 +20,23 @@ def self.register(type, key = nil) end def self.provider = Oaken::Stored::ActiveRecord - singleton_class.attr_reader :loader - delegate :entry, to: :loader + class << self + # Expose a class `seed` method for individual test classes to use. + # TODO: support parallelization somehow. + def included(klass) = klass.singleton_class.delegate(:seed, to: Oaken::Seeds) - module Loading def seed(*directories) - Oaken.lookup_paths.each do |path| - directories.each do |directory| - @loader = Oaken::Loader.new Pathname(path).join(directory.to_s) - @loader.load_onto Oaken::Seeds - end + Oaken.lookup_paths.product(directories).each do |path, directory| + load_from Pathname(path).join(directory.to_s) end end - end - extend Loading - def self.included(klass) - klass.extend Loading + private def load_from(path) + @loader = Oaken::Loader.new path + @loader.load_onto self + ensure + @loader = nil + end + def entry = @loader.entry end end From ca5fb60fd70e08831575c5343c7cc4c66bd61404 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Tue, 2 Jan 2024 00:17:07 +0100 Subject: [PATCH 2/7] Add some doc for what this do --- lib/oaken/seeds.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/oaken/seeds.rb b/lib/oaken/seeds.rb index 4888714..f17eef8 100644 --- a/lib/oaken/seeds.rb +++ b/lib/oaken/seeds.rb @@ -25,6 +25,22 @@ class << self # TODO: support parallelization somehow. def included(klass) = klass.singleton_class.delegate(:seed, to: Oaken::Seeds) + # Set up a general seed rule or perform a one-off seed for a test file. + # + # You can set up a general seed rule in `db/seeds.rb` like this: + # + # Oaken.prepare do + # seed :accounts # Seeds from `db/seeds/accounts/**/*.rb` and `db/seeds//accounts/**/*.rb` + # end + # + # Then if you need a test specific scenario, we recommend putting them in `db/seeds/test/cases`. + # + # Say you have `db/seeds/test/cases/pagination.rb`, you can load it like this: + # + # # test/integration/pagination_test.rb + # class PaginationTest < ActionDispatch::TestCase + # seed "cases/pagination" + # end def seed(*directories) Oaken.lookup_paths.product(directories).each do |path, directory| load_from Pathname(path).join(directory.to_s) From f0c7fd105737ed295cbf7b1fb6b9b901d93a79b6 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sat, 6 Jan 2024 19:16:53 +0100 Subject: [PATCH 3/7] Make edge-case seed loading an instance method --- lib/oaken/seeds.rb | 13 +++++++++---- test/dummy/test/integration/pagination_test.rb | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/oaken/seeds.rb b/lib/oaken/seeds.rb index f17eef8..f42464e 100644 --- a/lib/oaken/seeds.rb +++ b/lib/oaken/seeds.rb @@ -21,10 +21,6 @@ def self.register(type, key = nil) def self.provider = Oaken::Stored::ActiveRecord class << self - # Expose a class `seed` method for individual test classes to use. - # TODO: support parallelization somehow. - def included(klass) = klass.singleton_class.delegate(:seed, to: Oaken::Seeds) - # Set up a general seed rule or perform a one-off seed for a test file. # # You can set up a general seed rule in `db/seeds.rb` like this: @@ -55,4 +51,13 @@ def seed(*directories) end def entry = @loader.entry end + + # Call `seed` in tests to load individual case files: + # + # class PaginationTest < ActionDispatch::IntegrationTest + # setup do + # seed "cases/pagination" # Loads `db/seeds/{,test}/cases/pagination{,**/*}.rb` + # end + # end + delegate :seed, to: Oaken::Seeds end diff --git a/test/dummy/test/integration/pagination_test.rb b/test/dummy/test/integration/pagination_test.rb index 27c655f..a6aaef5 100644 --- a/test/dummy/test/integration/pagination_test.rb +++ b/test/dummy/test/integration/pagination_test.rb @@ -1,7 +1,7 @@ require "test_helper" class PaginationTest < ActiveSupport::TestCase - seed "cases/pagination" + setup { seed "cases/pagination" } test "pagination sorta" do assert_operator Order.count, :>=, 100 From 20bbcf0fa07f4b103b0ae6464e2a193d904fb275 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sat, 6 Jan 2024 19:18:55 +0100 Subject: [PATCH 4/7] Fix docs to point to instance method --- lib/oaken/seeds.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/oaken/seeds.rb b/lib/oaken/seeds.rb index f42464e..df17ed9 100644 --- a/lib/oaken/seeds.rb +++ b/lib/oaken/seeds.rb @@ -34,8 +34,8 @@ class << self # Say you have `db/seeds/test/cases/pagination.rb`, you can load it like this: # # # test/integration/pagination_test.rb - # class PaginationTest < ActionDispatch::TestCase - # seed "cases/pagination" + # class PaginationTest < ActionDispatch::IntegrationTest + # setup { seed "cases/pagination" } # end def seed(*directories) Oaken.lookup_paths.product(directories).each do |path, directory| From 8ea4e419944f9ec5b713095411dac64d62b6f61a Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sat, 6 Jan 2024 19:20:35 +0100 Subject: [PATCH 5/7] See if this will fix the tests; we'll be removing this harness soon --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4d3145b..8f3a27d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,7 +26,7 @@ jobs: bundler-cache: true - name: Uncached tests - run: bin/rails test + run: OAKEN_RESET=1 bin/rails test working-directory: test/dummy - name: Cached tests From 8d6ff211c9a189e1c8835eb2fe9947d2383dfbb9 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sat, 6 Jan 2024 19:23:00 +0100 Subject: [PATCH 6/7] Supply it like this --- .github/workflows/main.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8f3a27d..d670bd6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,9 +26,7 @@ jobs: bundler-cache: true - name: Uncached tests - run: OAKEN_RESET=1 bin/rails test - working-directory: test/dummy - - - name: Cached tests run: bin/rails test working-directory: test/dummy + env: + OAKEN_RESET: "1" From 5c8ec0ae9b2493ea8f38b3aee62a36b0d0bc3f7f Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sat, 6 Jan 2024 19:26:43 +0100 Subject: [PATCH 7/7] We gotta avoid the cache since we've rolledback the transaction --- .github/workflows/main.yml | 2 -- lib/oaken/seeds.rb | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d670bd6..9962d32 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,5 +28,3 @@ jobs: - name: Uncached tests run: bin/rails test working-directory: test/dummy - env: - OAKEN_RESET: "1" diff --git a/lib/oaken/seeds.rb b/lib/oaken/seeds.rb index df17ed9..8d82cc8 100644 --- a/lib/oaken/seeds.rb +++ b/lib/oaken/seeds.rb @@ -59,5 +59,8 @@ def entry = @loader.entry # seed "cases/pagination" # Loads `db/seeds/{,test}/cases/pagination{,**/*}.rb` # end # end - delegate :seed, to: Oaken::Seeds + def seed(...) + Oaken.store_path.rmtree # TODO: Remove after we yank the store stuff. + Oaken::Seeds.seed(...) + end end