Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix class level loading #70

Merged
merged 7 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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