seed_fixtures 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/lib/seed_fixtures/core.rb +17 -0
- data/lib/seed_fixtures/implementation.rb +45 -0
- data/lib/seed_fixtures/version.rb +3 -0
- data/lib/seed_fixtures.rb +9 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a3b2f17ba33d7339f567a27880b5d61d2f088aa98e1f1897c39336cdde951235
|
4
|
+
data.tar.gz: 2243b6f740b5ade12981754fb84e76429cb47c2a2de970dc5027eaf98e28cf14
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 76450f3299478c7dab95719f7c7ca7f636b70116f2b5a2ff5d1c05631223f47d5ffbf8a6ae6a3f6a3d54383816f65bff086029bf95f6da156f88f51fd0a6b07a
|
7
|
+
data.tar.gz: aace060b83430c20e8297019d6c4ea4d5bec3aaf435df4af77ca828c8201d7b2169b5ce8597d84fde40de292d0e49528414b53e066f70bc4d1d18f149517d4a4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Alex Ghiculescu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# seed_fixtures
|
2
|
+
|
3
|
+
[](https://rubygems.org/gems/seed_fixtures)
|
4
|
+
[](https://github.com/ghiculescu/seed-fixtures/actions/workflows/ci.yml)
|
5
|
+
[](https://codeclimate.com/github/ghiculescu/seed-fixtures)
|
6
|
+
|
7
|
+
Use your `seeds.rb` as fixtures when running tests.
|
8
|
+
|
9
|
+
Currently only Rails 7 is supported. PRs to support older Rails versions are welcome!
|
10
|
+
|
11
|
+
## Quick start
|
12
|
+
|
13
|
+
1. Add `seed_fixtures` to the `test` group of your Gemfile, then run `bundle install`.
|
14
|
+
|
15
|
+
2. In your `test_helper.rb`:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class ActiveSupport::TestCase
|
19
|
+
# Add this line:
|
20
|
+
fixtures_from_seeds
|
21
|
+
|
22
|
+
# Comment out or remove this line:
|
23
|
+
# fixtures :all
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
Notes:
|
28
|
+
|
29
|
+
- You cannot use standard Rails fixtures if you are using this gem. Make sure you remove the `fixtures` call that's included in `test_helper.rb` by default!
|
30
|
+
- Ensure your `test_helper.rb` includes this: `require "rails/test_help"`.
|
31
|
+
|
32
|
+
3. Add `.seeds_hash` to your `.gitignore` file.
|
33
|
+
|
34
|
+
## What it does
|
35
|
+
|
36
|
+
- When tests run, the contents of `db/seeds.rb` will be loaded into the test database.
|
37
|
+
- To improve performance, keeps a hash of everything your app's `db/` folder in the `.seeds_hash` file. This way, seeds are only reloaded into the database if you change your seeds file or your schema.
|
38
|
+
- Adds a `parallel_setup` hook, so if you run parallel tests, seeds will be loaded correctly. (This is needed because parallel databases [are truncated before each run](https://github.com/rails/rails/issues/46820).)
|
39
|
+
|
40
|
+
## How do I access seed data in my test?
|
41
|
+
|
42
|
+
Just like anything else in the database...
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
user = User.find_by(name: "Alex")
|
46
|
+
```
|
47
|
+
|
48
|
+
Or if you like, define helper methods for easy lookup.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
module SeedFixtureLookups
|
52
|
+
def user_alex
|
53
|
+
@user_alex ||= User.find_by(name: "Alex")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class ActiveSupport::TestCase
|
58
|
+
include SeedFixtureLookups
|
59
|
+
end
|
60
|
+
|
61
|
+
# in a test...
|
62
|
+
test "alex exists" do
|
63
|
+
assert_not_nil user_alex
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
## I thought seeds are for development, not testing!?
|
68
|
+
|
69
|
+
Not everyone agrees this is a good idea. And for many apps, it's not! But for some apps it is a very neat way of writing a robust test suite. If your app is one of those, give this a try.
|
70
|
+
|
71
|
+
If you're interested, [here's a discusion that touches on pros and cons of this approach](https://discuss.rubyonrails.org/t/should-dbprepare-also-call-db-seed-by-default/74835).
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
|
3
|
+
module SeedFixtures
|
4
|
+
module Core
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def fixtures_from_seeds
|
9
|
+
ActiveSupport.on_load(:active_support_test_case) do
|
10
|
+
prepend SeedFixtures::Implementation
|
11
|
+
end
|
12
|
+
|
13
|
+
parallelize_setup { SeedFixtures::Implementation.truncate_and_seed }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
|
3
|
+
module SeedFixtures
|
4
|
+
module Implementation
|
5
|
+
class << self
|
6
|
+
def truncate_and_seed
|
7
|
+
ActiveRecord::Tasks::DatabaseTasks.truncate_all
|
8
|
+
Rails.application.load_seed
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def load_fixtures(*)
|
13
|
+
return if defined?(@@has_loaded_fixtures)
|
14
|
+
|
15
|
+
unless_seeds_in_db_match_files do
|
16
|
+
SeedFixtures::Implementation.truncate_and_seed
|
17
|
+
end
|
18
|
+
|
19
|
+
@@has_loaded_fixtures = true
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def unless_seeds_in_db_match_files
|
25
|
+
File.write(hash_file_path, "") unless File.exist?(hash_file_path)
|
26
|
+
|
27
|
+
expected_hash = File.read(hash_file_path).strip
|
28
|
+
if expected_hash == calculate_seeds_hash
|
29
|
+
Rails.logger.debug "Seeds not changed since last run, skipping seeds generation. Delete #{hash_file_path} to force seeds to generate."
|
30
|
+
else
|
31
|
+
Rails.logger.debug "Generating seeds!"
|
32
|
+
yield
|
33
|
+
File.write(hash_file_path, calculate_seeds_hash)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def hash_file_path
|
38
|
+
".seeds_hash"
|
39
|
+
end
|
40
|
+
|
41
|
+
def calculate_seeds_hash
|
42
|
+
`find db* -type f | perl -e 'print sort <>' - | xargs md5sum | cut -c1-32 | md5sum | cut -c1-32`.strip
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seed_fixtures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Ghiculescu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-01-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- alexghiculescu@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- lib/seed_fixtures.rb
|
37
|
+
- lib/seed_fixtures/core.rb
|
38
|
+
- lib/seed_fixtures/implementation.rb
|
39
|
+
- lib/seed_fixtures/version.rb
|
40
|
+
homepage: https://github.com/ghiculescu/seed-fixtures
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
bug_tracker_uri: https://github.com/ghiculescu/seed-fixtures/issues
|
45
|
+
changelog_uri: https://github.com/ghiculescu/seed-fixtures/releases
|
46
|
+
source_code_uri: https://github.com/ghiculescu/seed-fixtures
|
47
|
+
homepage_uri: https://github.com/ghiculescu/seed-fixtures
|
48
|
+
rubygems_mfa_required: 'true'
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '2.6'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubygems_version: 3.3.26
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Use seeds.rb as test fixtures
|
68
|
+
test_files: []
|