fixture_bot 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +24 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +230 -0
- data/Rakefile +28 -0
- data/lib/fixture_bot/extension.rb +14 -0
- data/lib/fixture_bot/fixture_creator.rb +70 -0
- data/lib/fixture_bot/helpers.rb +27 -0
- data/lib/fixture_bot/minitest.rb +23 -0
- data/lib/fixture_bot/rspec.rb +12 -0
- data/lib/fixture_bot/table_loader.rb +21 -0
- data/lib/fixture_bot/version.rb +5 -0
- data/lib/fixture_bot.rb +103 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 882daa77b1a9840f2179c979b5bd51e09782e7ef011d28dc761bbbdd65789942
|
4
|
+
data.tar.gz: 0c8957cc5ff3f0c412ae7e9d85acc41baf407b7090de7f2e55450e77573ae13e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4c795d92c941773f0af08ffa32d289a0d6b7ae2421cfc3cb6934f2c94724d53c64078d0b179e25761ec534a8a5a96b67333146eb4bdbb0ee926a0a6ba2a85ec
|
7
|
+
data.tar.gz: 9869573d964e08fb7f7632f3a624688f48a066f325a87baa51ab789120cc286d8f165001f0954817aeaf4151aaec6e803c1e7247b288dbc13824a103fb94fb6b
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
inherit_gem:
|
2
|
+
rubocop-gp:
|
3
|
+
- .rubocop.yml
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 3.1
|
7
|
+
|
8
|
+
Lint/ConstantDefinitionInBlock:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
RSpec/LeakyConstantDeclaration:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Rails/ApplicationRecord:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Rails/TimeZone:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Layout/EmptyLinesAroundModuleBody:
|
21
|
+
EnforcedStyle: no_empty_lines
|
22
|
+
|
23
|
+
Layout/EmptyLinesAroundClassBody:
|
24
|
+
EnforcedStyle: no_empty_lines
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
gem "bundler"
|
7
|
+
gem "minitest-utils"
|
8
|
+
gem "rails"
|
9
|
+
gem "rake"
|
10
|
+
gem "rspec-rails"
|
11
|
+
gem "rubocop-gp", github: "corp-gp/rubocop-gp"
|
12
|
+
gem "simplecov"
|
13
|
+
gem "sqlite3", "~> 1.4"
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Ermolaev Andrey
|
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,230 @@
|
|
1
|
+
# factory_bot-preload
|
2
|
+
|
3
|
+
[![Travis-CI](https://travis-ci.org/fnando/factory_bot-preload.svg)](https://travis-ci.org/fnando/factory_bot-preload)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/fnando/factory_bot-preload/badges/gpa.svg)](https://codeclimate.com/github/fnando/factory_bot-preload)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/fnando/factory_bot-preload/badges/coverage.svg)](https://codeclimate.com/github/fnando/factory_bot-preload/coverage)
|
6
|
+
[![Gem](https://img.shields.io/gem/v/factory_bot-preload.svg)](https://rubygems.org/gems/factory_bot-preload)
|
7
|
+
[![Gem](https://img.shields.io/gem/dt/factory_bot-preload.svg)](https://rubygems.org/gems/factory_bot-preload)
|
8
|
+
|
9
|
+
We all love Rails fixtures because they're fast, but we hate to deal with YAML/CSV/SQL files. Here enters [factory_bot](https://rubygems.org/gems/factory_bot) (FB).
|
10
|
+
|
11
|
+
Now, you can easily create records by using predefined factories. The problem is that hitting the database everytime to create records is pretty slow. And believe me, you'll feel the pain when you have lots of tests/specs.
|
12
|
+
|
13
|
+
So here enters Factory Bot Preload (FBP). You can define which factories will be preloaded, so you don't have to recreate it every time (that will work for 99.37% of the time, according to statistics I just made up).
|
14
|
+
|
15
|
+
### Installation
|
16
|
+
|
17
|
+
Add both FB and FBP to your Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
source "https://rubygems.org"
|
21
|
+
|
22
|
+
gem "rails"
|
23
|
+
|
24
|
+
group :test, :development do
|
25
|
+
gem "factory_bot"
|
26
|
+
gem "fixture_bot", require: false
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Notice that adding `require: false` is important; otherwise you won't be able to
|
31
|
+
run commands such as `rails db:test:prepare`.
|
32
|
+
|
33
|
+
### RSpec Setup
|
34
|
+
|
35
|
+
On your `spec/spec_helper.rb` file, make sure that transactional fixtures are
|
36
|
+
enabled. Here's is my file without all those RSpec comments:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
ENV["RAILS_ENV"] ||= "test"
|
40
|
+
require File.expand_path("../../config/environment", __FILE__)
|
41
|
+
require "rspec/rails"
|
42
|
+
|
43
|
+
# First, load fixture_bot/preload.
|
44
|
+
require "fixture_bot/preload"
|
45
|
+
|
46
|
+
# Then load your factories
|
47
|
+
Dir[Rails.root.join("spec/support/factories/**/*.rb")].each do |file|
|
48
|
+
require file
|
49
|
+
end
|
50
|
+
|
51
|
+
RSpec.configure do |config|
|
52
|
+
config.use_transactional_fixtures = true
|
53
|
+
config.mock_with :rspec
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
### Minitest Setup
|
58
|
+
|
59
|
+
On your `test/test_helper.rb` file, make sure that transaction fixtures are
|
60
|
+
enabled. Here's what your file may look like:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
ENV["RAILS_ENV"] ||= "test"
|
64
|
+
require_relative "../config/environment"
|
65
|
+
require "rails/test_help"
|
66
|
+
|
67
|
+
module ActiveSupport
|
68
|
+
class TestCase
|
69
|
+
self.use_instantiated_fixtures = true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# First, load fixture_bot/preload.
|
74
|
+
require "fixture_bot/preload"
|
75
|
+
|
76
|
+
# Then load your factories.
|
77
|
+
Dir["./test/support/factories/**/*.rb"].each do |file|
|
78
|
+
require file
|
79
|
+
end
|
80
|
+
|
81
|
+
# Finally, setup minitest.
|
82
|
+
# Your factories won't behave correctly unless you
|
83
|
+
# call `FixtureBot.minitest` after loading them.
|
84
|
+
FixtureBot.minitest
|
85
|
+
```
|
86
|
+
|
87
|
+
### Usage
|
88
|
+
|
89
|
+
Create your factories and load it from your setup file (either
|
90
|
+
`test/test_helper.rb` or `spec/spec_helper.rb`) You may have something like
|
91
|
+
this:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
FactoryBot.define do
|
95
|
+
factory :user do
|
96
|
+
name "John Doe"
|
97
|
+
sequence(:email) {|n| "john#{n}@example.org" }
|
98
|
+
sequence(:username) {|n| "john#{n}" }
|
99
|
+
password "test"
|
100
|
+
password_confirmation "test"
|
101
|
+
end
|
102
|
+
|
103
|
+
factory :projects do
|
104
|
+
name "My Project"
|
105
|
+
association :user
|
106
|
+
end
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
To define your preloadable factories, just use the `preload` method:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
FactoryBot.define do
|
114
|
+
factory :user do
|
115
|
+
name "John Doe"
|
116
|
+
sequence(:email) {|n| "john#{n}@example.org" }
|
117
|
+
sequence(:username) {|n| "john#{n}" }
|
118
|
+
password "test"
|
119
|
+
password_confirmation "test"
|
120
|
+
end
|
121
|
+
|
122
|
+
factory :projects do
|
123
|
+
name "My Project"
|
124
|
+
association :user
|
125
|
+
end
|
126
|
+
|
127
|
+
preload do
|
128
|
+
fixture(:john) { create(:user) }
|
129
|
+
fixture(:myapp) { create(:project, user: users(:john)) }
|
130
|
+
end
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
You can also use preloaded factories on factory definitions.
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
FactoryBot.define do
|
138
|
+
factory :user do
|
139
|
+
# ...
|
140
|
+
end
|
141
|
+
|
142
|
+
factory :projects do
|
143
|
+
name "My Project"
|
144
|
+
user { users(:john) }
|
145
|
+
end
|
146
|
+
|
147
|
+
preload do
|
148
|
+
fixture(:john) { create(:user) }
|
149
|
+
fixture(:myapp) { create(:project, user: users(:john)) }
|
150
|
+
end
|
151
|
+
end
|
152
|
+
```
|
153
|
+
|
154
|
+
|
155
|
+
If you need to create records with specifying id, use `fixture_stub_const`, it stub const in model to next value from generated primary key
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
class User
|
159
|
+
ADMIN_ID = 10
|
160
|
+
|
161
|
+
def can_edit_article?
|
162
|
+
id == ADMIN_ID
|
163
|
+
end
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
FactoryBot.define do
|
169
|
+
factory :user do
|
170
|
+
# ...
|
171
|
+
end
|
172
|
+
|
173
|
+
preload do
|
174
|
+
fixture(:john) { create(:user) }
|
175
|
+
fixture_stub_const(:admin, :ADMIN_ID) { create(:user) }
|
176
|
+
end
|
177
|
+
end
|
178
|
+
```
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
describe User do
|
182
|
+
let(:admin) { users(:admin) }
|
183
|
+
|
184
|
+
it "admin can edit article" do
|
185
|
+
expect(admin).to be_can_edit_article
|
186
|
+
end
|
187
|
+
end
|
188
|
+
````
|
189
|
+
|
190
|
+
|
191
|
+
Like Rails fixtures, FBP will define methods for each model. You can use it on
|
192
|
+
your examples and alike.
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
require "test_helper"
|
196
|
+
|
197
|
+
class UserTest < ActiveSupport::TestCase
|
198
|
+
test "returns john's record" do
|
199
|
+
assert_instance_of User, users(:john)
|
200
|
+
end
|
201
|
+
|
202
|
+
test "returns myapp's record" do
|
203
|
+
assert_equal users(:john), projects(:myapp).user
|
204
|
+
end
|
205
|
+
end
|
206
|
+
```
|
207
|
+
|
208
|
+
Or if you're using RSpec:
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
require "spec_helper"
|
212
|
+
|
213
|
+
describe User do
|
214
|
+
let(:user) { users(:john) }
|
215
|
+
|
216
|
+
it "returns john's record" do
|
217
|
+
users(:john).should be_an(User)
|
218
|
+
end
|
219
|
+
|
220
|
+
it "returns myapp's record" do
|
221
|
+
projects(:myapp).user.should == users(:john)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
```
|
225
|
+
|
226
|
+
That's it!
|
227
|
+
|
228
|
+
## License
|
229
|
+
|
230
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
|
5
|
+
case ENV["TEST_FRAMEWORK"]
|
6
|
+
when "rspec"
|
7
|
+
require "rspec/core/rake_task"
|
8
|
+
RSpec::Core::RakeTask.new
|
9
|
+
|
10
|
+
task default: :spec
|
11
|
+
when "minitest"
|
12
|
+
require "rake/testtask"
|
13
|
+
|
14
|
+
Rake::TestTask.new(:test) do |t|
|
15
|
+
t.libs << "test"
|
16
|
+
t.libs << "lib"
|
17
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
18
|
+
t.verbose = false
|
19
|
+
t.warning = false
|
20
|
+
end
|
21
|
+
|
22
|
+
task default: :test
|
23
|
+
else
|
24
|
+
task :default do
|
25
|
+
system "TEST_FRAMEWORK=rspec bundle exec rake spec"
|
26
|
+
system "TEST_FRAMEWORK=minitest bundle exec rake test"
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FactoryBot
|
4
|
+
module Syntax
|
5
|
+
module Default
|
6
|
+
class DSL
|
7
|
+
def preload(table, &block)
|
8
|
+
::FixtureBot::FixtureCreator.tables[table] ||= []
|
9
|
+
::FixtureBot::FixtureCreator.tables[table] << block
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FixtureBot
|
4
|
+
module FixtureCreator
|
5
|
+
extend ::FactoryBot::Syntax::Methods
|
6
|
+
extend ::FixtureBot::Helpers
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :tables, :record_ids, :fixtures, :fixtures_with_id
|
10
|
+
end
|
11
|
+
|
12
|
+
self.tables = {}
|
13
|
+
self.record_ids = {}
|
14
|
+
|
15
|
+
module_function
|
16
|
+
|
17
|
+
def load_to_db
|
18
|
+
tmp_fixtures = []
|
19
|
+
tmp_fixtures_with_id = []
|
20
|
+
|
21
|
+
tables.each do |table, blocks|
|
22
|
+
table_preload = TableLoader.new(table)
|
23
|
+
|
24
|
+
blocks.each do |block|
|
25
|
+
table_preload.instance_eval(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
tmp_fixtures.concat(table_preload.fixtures)
|
29
|
+
tmp_fixtures_with_id.concat(table_preload.fixtures_with_id)
|
30
|
+
end
|
31
|
+
|
32
|
+
self.fixtures = tmp_fixtures.to_h
|
33
|
+
self.fixtures_with_id = tmp_fixtures_with_id.to_h
|
34
|
+
|
35
|
+
::ActiveRecord::Base.connection.transaction requires_new: true do
|
36
|
+
fixtures_with_id.each do |key, block|
|
37
|
+
table, fixture_name = key.split("/")
|
38
|
+
fixture_with_id(table.to_sym, fixture_name.to_sym, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
fixtures.each do |key, block|
|
42
|
+
table, fixture_name = key.split("/")
|
43
|
+
fixture(table.to_sym, fixture_name.to_sym, &block)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private def force_load_fixture(table, name)
|
49
|
+
if (block = fixtures["#{table}/#{name}"] || fixtures_with_id["#{table}/#{name}"])
|
50
|
+
fixture(table, name, &block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private def fixture(table, name, &)
|
55
|
+
record_ids[table] ||= {}
|
56
|
+
record_ids[table][name] ||=
|
57
|
+
begin
|
58
|
+
model = instance_eval(&)
|
59
|
+
[model.class.name, model.id]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private def fixture_with_id(table, name, &)
|
64
|
+
_, record_id = fixture(table, name, &)
|
65
|
+
::ActiveRecord::Base.connection.execute <<~SQL
|
66
|
+
SELECT setval(pg_get_serial_sequence('#{table}', 'id'), GREATEST(#{record_id}, nextval(pg_get_serial_sequence('#{table}', 'id'))))
|
67
|
+
SQL
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FixtureBot
|
4
|
+
module Helpers
|
5
|
+
def self.included(_base)
|
6
|
+
::FixtureBot::FixtureCreator.tables.each_key do |table|
|
7
|
+
module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
8
|
+
# def users(name)
|
9
|
+
# fixture_get(name, :users)
|
10
|
+
# end
|
11
|
+
def #{table}(name)
|
12
|
+
fixture_get(name, :#{table})
|
13
|
+
end
|
14
|
+
RUBY
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private def fixture_get(name, table)
|
19
|
+
model_name, model_id = FixtureCreator.record_ids.dig(table, name) || FixtureCreator.force_load_fixture(table, name)
|
20
|
+
if model_id
|
21
|
+
Object.const_get(model_name).find(model_id)
|
22
|
+
else
|
23
|
+
raise "Couldn't find fixture #{table}/#{name}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "minitest"
|
4
|
+
require "factory_bot/syntax/methods"
|
5
|
+
|
6
|
+
module FixtureBot
|
7
|
+
module Preload
|
8
|
+
def self.minitest
|
9
|
+
::FixtureBot.clean
|
10
|
+
::FixtureBot.run
|
11
|
+
end
|
12
|
+
|
13
|
+
module MinitestSetup
|
14
|
+
def setup
|
15
|
+
::FixtureBot.reload_factories
|
16
|
+
super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
::Minitest::Test.include Helpers
|
21
|
+
::Minitest::Test.prepend MinitestSetup
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FixtureBot
|
4
|
+
class TableLoader
|
5
|
+
attr_reader :table, :fixtures, :fixtures_with_id
|
6
|
+
|
7
|
+
def initialize(table)
|
8
|
+
@table = table
|
9
|
+
@fixtures = []
|
10
|
+
@fixtures_with_id = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def fixture(name, &block)
|
14
|
+
@fixtures << ["#{table}/#{name}", block]
|
15
|
+
end
|
16
|
+
|
17
|
+
def fixture_with_id(name, &block)
|
18
|
+
@fixtures_with_id << ["#{table}/#{name}", block]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/fixture_bot.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "factory_bot"
|
4
|
+
require "active_record"
|
5
|
+
|
6
|
+
module FixtureBot
|
7
|
+
require "fixture_bot/helpers"
|
8
|
+
require "fixture_bot/fixture_creator"
|
9
|
+
require "fixture_bot/table_loader"
|
10
|
+
require "fixture_bot/version"
|
11
|
+
require "fixture_bot/rspec" if defined?(RSpec)
|
12
|
+
require "fixture_bot/minitest" if defined?(Minitest)
|
13
|
+
require "fixture_bot/extension"
|
14
|
+
|
15
|
+
module_function
|
16
|
+
|
17
|
+
def after_load_fixtures(&block)
|
18
|
+
@after_load_fixtures = block
|
19
|
+
end
|
20
|
+
|
21
|
+
def run
|
22
|
+
cached_mtime, cached_record_ids = cached_fixtures
|
23
|
+
if cached_mtime && cached_mtime == max_mtime_fixtures
|
24
|
+
puts "Cache load fixtures".yellow
|
25
|
+
|
26
|
+
FixtureBot::FixtureCreator.record_ids = Marshal.load(cached_record_ids)
|
27
|
+
define_fixture_helpers
|
28
|
+
else
|
29
|
+
puts "Full load fixtures".yellow
|
30
|
+
|
31
|
+
clean_db
|
32
|
+
load_models
|
33
|
+
define_fixture_helpers
|
34
|
+
FixtureBot::FixtureCreator.load_to_db
|
35
|
+
@after_load_fixtures&.call
|
36
|
+
caching_max_mtime_fixtures(Marshal.dump(FixtureBot::FixtureCreator.record_ids))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def max_mtime_fixtures
|
41
|
+
@max_mtime_fixtures ||=
|
42
|
+
FactoryBot.definition_file_paths.flat_map { |path|
|
43
|
+
directory_path = File.expand_path(path)
|
44
|
+
|
45
|
+
if File.directory?(directory_path)
|
46
|
+
Dir[File.join(directory_path, "**", "*.rb")].map { |file| File.mtime(file) }
|
47
|
+
end
|
48
|
+
}.compact.max.round(6)
|
49
|
+
end
|
50
|
+
|
51
|
+
def cached_fixtures
|
52
|
+
connection.query(<<-SQL).first
|
53
|
+
CREATE TABLE IF NOT EXISTS __factory_bot_preload_cache_v1(fixtures_time timestamptz, fixtures_dump bytea);
|
54
|
+
SELECT fixtures_time, fixtures_dump FROM __factory_bot_preload_cache_v1
|
55
|
+
SQL
|
56
|
+
end
|
57
|
+
|
58
|
+
def caching_max_mtime_fixtures(dump_record_ids)
|
59
|
+
connection.execute <<-SQL
|
60
|
+
TRUNCATE TABLE __factory_bot_preload_cache_v1;
|
61
|
+
INSERT INTO __factory_bot_preload_cache_v1 VALUES ('#{max_mtime_fixtures.iso8601(6)}', '#{connection.raw_connection.escape_bytea(dump_record_ids)}')
|
62
|
+
SQL
|
63
|
+
end
|
64
|
+
|
65
|
+
def load_models
|
66
|
+
return unless defined?(Rails)
|
67
|
+
|
68
|
+
Dir[Rails.application.root.join("app/models/**/*.rb")].each do |file|
|
69
|
+
require_dependency file
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def define_fixture_helpers
|
74
|
+
::FactoryBot::SyntaxRunner.include(::FixtureBot::Helpers)
|
75
|
+
end
|
76
|
+
|
77
|
+
RESERVED_TABLES = %w[
|
78
|
+
ar_internal_metadata
|
79
|
+
schema_migrations
|
80
|
+
].freeze
|
81
|
+
|
82
|
+
def clean_db
|
83
|
+
tables = connection.tables - RESERVED_TABLES
|
84
|
+
|
85
|
+
query =
|
86
|
+
case connection.adapter_name
|
87
|
+
when "SQLite"
|
88
|
+
tables.map { |table| "DELETE FROM #{connection.quote_table_name(table)}" }.join(";")
|
89
|
+
when "PostgreSQL"
|
90
|
+
"TRUNCATE TABLE #{tables.map { |table| connection.quote_table_name(table) }.join(',')} RESTART IDENTITY CASCADE"
|
91
|
+
else
|
92
|
+
"TRUNCATE TABLE #{tables.map { |table| connection.quote_table_name(table) }.join(',')}"
|
93
|
+
end
|
94
|
+
|
95
|
+
connection.disable_referential_integrity do
|
96
|
+
connection.execute(query)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def connection
|
101
|
+
::ActiveRecord::Base.connection
|
102
|
+
end
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fixture_bot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ermolaev Andrey
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-09-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: factory_bot
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Fast and easy to maintain fixtures inside factories
|
42
|
+
email:
|
43
|
+
- andruhafirst@yandex.ru
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/fixture_bot.rb
|
55
|
+
- lib/fixture_bot/extension.rb
|
56
|
+
- lib/fixture_bot/fixture_creator.rb
|
57
|
+
- lib/fixture_bot/helpers.rb
|
58
|
+
- lib/fixture_bot/minitest.rb
|
59
|
+
- lib/fixture_bot/rspec.rb
|
60
|
+
- lib/fixture_bot/table_loader.rb
|
61
|
+
- lib/fixture_bot/version.rb
|
62
|
+
homepage: https://github.com/corp-gp/fixture_bot
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata:
|
66
|
+
allowed_push_host: https://rubygems.org
|
67
|
+
homepage_uri: https://github.com/corp-gp/fixture_bot
|
68
|
+
source_code_uri: https://github.com/corp-gp/fixture_bot
|
69
|
+
rubygems_mfa_required: 'true'
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 3.1.0
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.3.17
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: fixtures inside factories
|
89
|
+
test_files: []
|