Skip to content

Commit

Permalink
Fix class level loading (#70)
Browse files Browse the repository at this point in the history
* 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.

* Add some doc for what this do

* Make edge-case seed loading an instance method

* Fix docs to point to instance method

* See if this will fix the tests; we'll be removing this harness soon

* Supply it like this

* We gotta avoid the cache since we've rolledback the transaction
  • Loading branch information
kaspth authored Jan 6, 2024
1 parent 8eff21f commit 1f8d335
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@ jobs:
- name: Uncached tests
run: bin/rails test
working-directory: test/dummy

- name: Cached tests
run: bin/rails test
working-directory: test/dummy
49 changes: 37 additions & 12 deletions lib/oaken/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,47 @@ def self.register(type, key = nil)
end
def self.provider = Oaken::Stored::ActiveRecord

singleton_class.attr_reader :loader
delegate :entry, to: :loader

module Loading
class << self
# 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/<Rails.env>/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::IntegrationTest
# setup { seed "cases/pagination" }
# end
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

private def load_from(path)
@loader = Oaken::Loader.new path
@loader.load_onto self
ensure
@loader = nil
end
def entry = @loader.entry
end
extend Loading

def self.included(klass)
klass.extend Loading
# 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
def seed(...)
Oaken.store_path.rmtree # TODO: Remove after we yank the store stuff.
Oaken::Seeds.seed(...)
end
end
2 changes: 1 addition & 1 deletion test/dummy/test/integration/pagination_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 1f8d335

Please sign in to comment.