dirty_seed 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/lib/dirty_seed.rb +15 -11
  4. data/lib/dirty_seed/assigners/assigner.rb +73 -0
  5. data/lib/dirty_seed/assigners/base.rb +86 -0
  6. data/lib/dirty_seed/assigners/{dirty_boolean.rb → boolean.rb} +2 -2
  7. data/lib/dirty_seed/assigners/{dirty_date.rb → date.rb} +2 -2
  8. data/lib/dirty_seed/assigners/fakers.yml +64 -1
  9. data/lib/dirty_seed/assigners/float.rb +21 -0
  10. data/lib/dirty_seed/assigners/integer.rb +20 -0
  11. data/lib/dirty_seed/assigners/min_max_helper.rb +115 -0
  12. data/lib/dirty_seed/assigners/{dirty_number.rb → number.rb} +2 -2
  13. data/lib/dirty_seed/assigners/string.rb +39 -0
  14. data/lib/dirty_seed/assigners/{dirty_time.rb → time.rb} +2 -2
  15. data/lib/dirty_seed/{dirty_association.rb → association.rb} +6 -17
  16. data/lib/dirty_seed/attribute.rb +62 -0
  17. data/lib/dirty_seed/data_model.rb +17 -18
  18. data/lib/dirty_seed/method_missing_helper.rb +1 -1
  19. data/lib/dirty_seed/{dirty_model.rb → model.rb} +81 -33
  20. data/lib/dirty_seed/version.rb +1 -1
  21. data/spec/dummy/app/models/bravo.rb +2 -1
  22. data/spec/dummy/db/test.sqlite3 +0 -0
  23. data/spec/dummy/log/development.log +2 -0
  24. data/spec/dummy/log/test.log +62192 -0
  25. data/spec/lib/dirty_seed/assigners/assigner_spec.rb +19 -0
  26. data/spec/lib/dirty_seed/assigners/base_spec.rb +81 -0
  27. data/spec/lib/dirty_seed/assigners/boolean_spec.rb +13 -0
  28. data/spec/lib/dirty_seed/assigners/date_spec.rb +15 -0
  29. data/spec/lib/dirty_seed/assigners/float_spec.rb +43 -0
  30. data/spec/lib/dirty_seed/assigners/integer_spec.rb +43 -0
  31. data/spec/lib/dirty_seed/assigners/string_spec.rb +47 -0
  32. data/spec/lib/dirty_seed/assigners/time_spec.rb +15 -0
  33. data/spec/lib/dirty_seed/association_spec.rb +64 -0
  34. data/spec/lib/dirty_seed/attribute_spec.rb +49 -0
  35. data/spec/lib/dirty_seed/data_model_spec.rb +8 -36
  36. data/spec/lib/dirty_seed/{dirty_model_spec.rb → model_spec.rb} +13 -13
  37. data/spec/support/helpers.rb +5 -5
  38. metadata +37 -33
  39. data/lib/dirty_seed/assigners/dirty_assigner.rb +0 -95
  40. data/lib/dirty_seed/assigners/dirty_float.rb +0 -35
  41. data/lib/dirty_seed/assigners/dirty_integer.rb +0 -16
  42. data/lib/dirty_seed/assigners/dirty_string.rb +0 -74
  43. data/lib/dirty_seed/dirty_attribute.rb +0 -75
  44. data/spec/lib/dirty_seed/assigners/dirty_assigner_spec.rb +0 -68
  45. data/spec/lib/dirty_seed/assigners/dirty_boolean_spec.rb +0 -13
  46. data/spec/lib/dirty_seed/assigners/dirty_date_spec.rb +0 -15
  47. data/spec/lib/dirty_seed/assigners/dirty_float_spec.rb +0 -43
  48. data/spec/lib/dirty_seed/assigners/dirty_integer_spec.rb +0 -43
  49. data/spec/lib/dirty_seed/assigners/dirty_string_spec.rb +0 -53
  50. data/spec/lib/dirty_seed/assigners/dirty_time_spec.rb +0 -15
  51. data/spec/lib/dirty_seed/dirty_association_spec.rb +0 -64
  52. data/spec/lib/dirty_seed/dirty_attribute_spec.rb +0 -49
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe DirtySeed::DirtyModel do
5
+ RSpec.describe DirtySeed::Model do
6
6
  let(:data_model) { DirtySeed::DataModel }
7
7
 
8
8
  describe '#initialize(model:)' do
@@ -16,12 +16,12 @@ RSpec.describe DirtySeed::DirtyModel do
16
16
  expect { data_model.alfa.seed(3) }.to change(Alfa, :count).by(3)
17
17
  end
18
18
 
19
- it 'counts the number of successfully seeded instances' do
19
+ it 'stores the number of successfully seeded instances' do
20
20
  data_model.alfa.seed(3)
21
- expect(data_model.alfa.count).to eq 3
21
+ expect(data_model.alfa.instances.count).to eq 3
22
22
  end
23
23
 
24
- it 'logs the errors' do
24
+ it 'stores the errors' do
25
25
  Alfa.create && data_model.juliett.seed(3)
26
26
  expect(data_model.juliett.errors).to match_array(
27
27
  [
@@ -33,11 +33,11 @@ RSpec.describe DirtySeed::DirtyModel do
33
33
  end
34
34
  end
35
35
 
36
- describe '#dirty_associations' do
37
- it 'returns dirty_associations' do
38
- expect(data_model.alfa.dirty_associations).to be_empty
39
- expect(data_model.delta.dirty_associations.count).to eq 2
40
- expect(data_model.echo.dirty_associations.count).to eq 1
36
+ describe '#associations' do
37
+ it 'returns associations' do
38
+ expect(data_model.alfa.associations).to be_empty
39
+ expect(data_model.delta.associations.count).to eq 2
40
+ expect(data_model.echo.associations.count).to eq 1
41
41
  end
42
42
  end
43
43
 
@@ -49,15 +49,15 @@ RSpec.describe DirtySeed::DirtyModel do
49
49
  end
50
50
  end
51
51
 
52
- describe '#dirty_attributes' do
53
- it 'instantiates and returns dirty_attributes' do
52
+ describe '#attributes' do
53
+ it 'instantiates and returns attributes' do
54
54
  expectations = %i[a_boolean an_integer a_decimal a_string a_date a_time a_datetime]
55
- expect(data_model.alfa.dirty_attributes.map(&:name)).to match_array(expectations)
55
+ expect(data_model.alfa.attributes.map(&:name)).to match_array(expectations)
56
56
  end
57
57
 
58
58
  it 'does not instantiate a dirty attribute for "protected columns"' do
59
59
  expect(
60
- data_model.alfa.dirty_attributes.map(&:name) & described_class::PROTECTED_COLUMNS
60
+ data_model.alfa.attributes.map(&:name) & described_class::PROTECTED_COLUMNS
61
61
  ).to be_empty
62
62
  end
63
63
  end
@@ -8,15 +8,15 @@ def build_column(name: nil, type: :boolean)
8
8
  )
9
9
  end
10
10
 
11
- def build_dirty_attribute(name: nil, dirty_model: nil, type: :boolean)
12
- DirtySeed::DirtyAttribute.new(
13
- dirty_model || build_dirty_model,
11
+ def build_attribute(name: nil, model: nil, type: :boolean)
12
+ DirtySeed::Attribute.new(
13
+ model || build_model,
14
14
  build_column(name: name, type: type)
15
15
  )
16
16
  end
17
17
 
18
- def build_dirty_model(model: Alfa)
19
- DirtySeed::DirtyModel.new(model)
18
+ def build_model(model: Alfa)
19
+ DirtySeed::Model.new(model)
20
20
  end
21
21
 
22
22
  private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dirty_seed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edouard Piron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-25 00:00:00.000000000 Z
11
+ date: 2020-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
@@ -69,22 +69,24 @@ files:
69
69
  - README.md
70
70
  - Rakefile
71
71
  - lib/dirty_seed.rb
72
- - lib/dirty_seed/assigners/dirty_assigner.rb
73
- - lib/dirty_seed/assigners/dirty_boolean.rb
74
- - lib/dirty_seed/assigners/dirty_date.rb
75
- - lib/dirty_seed/assigners/dirty_float.rb
76
- - lib/dirty_seed/assigners/dirty_integer.rb
77
- - lib/dirty_seed/assigners/dirty_number.rb
78
- - lib/dirty_seed/assigners/dirty_string.rb
79
- - lib/dirty_seed/assigners/dirty_time.rb
72
+ - lib/dirty_seed/assigners/assigner.rb
73
+ - lib/dirty_seed/assigners/base.rb
74
+ - lib/dirty_seed/assigners/boolean.rb
75
+ - lib/dirty_seed/assigners/date.rb
80
76
  - lib/dirty_seed/assigners/fakers.yml
77
+ - lib/dirty_seed/assigners/float.rb
78
+ - lib/dirty_seed/assigners/integer.rb
79
+ - lib/dirty_seed/assigners/min_max_helper.rb
80
+ - lib/dirty_seed/assigners/number.rb
81
+ - lib/dirty_seed/assigners/string.rb
82
+ - lib/dirty_seed/assigners/time.rb
83
+ - lib/dirty_seed/association.rb
84
+ - lib/dirty_seed/attribute.rb
81
85
  - lib/dirty_seed/data_model.rb
82
- - lib/dirty_seed/dirty_association.rb
83
- - lib/dirty_seed/dirty_attribute.rb
84
- - lib/dirty_seed/dirty_model.rb
85
86
  - lib/dirty_seed/engine.rb
86
87
  - lib/dirty_seed/exceptions.rb
87
88
  - lib/dirty_seed/method_missing_helper.rb
89
+ - lib/dirty_seed/model.rb
88
90
  - lib/dirty_seed/sorter.rb
89
91
  - lib/dirty_seed/version.rb
90
92
  - lib/tasks/dirty_seed_tasks.rake
@@ -159,17 +161,18 @@ files:
159
161
  - spec/dummy/public/apple-touch-icon.png
160
162
  - spec/dummy/public/favicon.ico
161
163
  - spec/dummy/tmp/development_secret.txt
162
- - spec/lib/dirty_seed/assigners/dirty_assigner_spec.rb
163
- - spec/lib/dirty_seed/assigners/dirty_boolean_spec.rb
164
- - spec/lib/dirty_seed/assigners/dirty_date_spec.rb
165
- - spec/lib/dirty_seed/assigners/dirty_float_spec.rb
166
- - spec/lib/dirty_seed/assigners/dirty_integer_spec.rb
167
- - spec/lib/dirty_seed/assigners/dirty_string_spec.rb
168
- - spec/lib/dirty_seed/assigners/dirty_time_spec.rb
164
+ - spec/lib/dirty_seed/assigners/assigner_spec.rb
165
+ - spec/lib/dirty_seed/assigners/base_spec.rb
166
+ - spec/lib/dirty_seed/assigners/boolean_spec.rb
167
+ - spec/lib/dirty_seed/assigners/date_spec.rb
168
+ - spec/lib/dirty_seed/assigners/float_spec.rb
169
+ - spec/lib/dirty_seed/assigners/integer_spec.rb
170
+ - spec/lib/dirty_seed/assigners/string_spec.rb
171
+ - spec/lib/dirty_seed/assigners/time_spec.rb
172
+ - spec/lib/dirty_seed/association_spec.rb
173
+ - spec/lib/dirty_seed/attribute_spec.rb
169
174
  - spec/lib/dirty_seed/data_model_spec.rb
170
- - spec/lib/dirty_seed/dirty_association_spec.rb
171
- - spec/lib/dirty_seed/dirty_attribute_spec.rb
172
- - spec/lib/dirty_seed/dirty_model_spec.rb
175
+ - spec/lib/dirty_seed/model_spec.rb
173
176
  - spec/lib/dirty_seed/sorter_spec.rb
174
177
  - spec/lib/tasks/dirty_seed_tasks_spec.rb
175
178
  - spec/rails_helper.rb
@@ -198,18 +201,19 @@ signing_key:
198
201
  specification_version: 4
199
202
  summary: Summary of DirtySeed.
200
203
  test_files:
201
- - spec/lib/dirty_seed/assigners/dirty_assigner_spec.rb
202
- - spec/lib/dirty_seed/assigners/dirty_time_spec.rb
203
- - spec/lib/dirty_seed/assigners/dirty_string_spec.rb
204
- - spec/lib/dirty_seed/assigners/dirty_float_spec.rb
205
- - spec/lib/dirty_seed/assigners/dirty_date_spec.rb
206
- - spec/lib/dirty_seed/assigners/dirty_boolean_spec.rb
207
- - spec/lib/dirty_seed/assigners/dirty_integer_spec.rb
204
+ - spec/lib/dirty_seed/assigners/boolean_spec.rb
205
+ - spec/lib/dirty_seed/assigners/string_spec.rb
206
+ - spec/lib/dirty_seed/assigners/date_spec.rb
207
+ - spec/lib/dirty_seed/assigners/base_spec.rb
208
+ - spec/lib/dirty_seed/assigners/integer_spec.rb
209
+ - spec/lib/dirty_seed/assigners/time_spec.rb
210
+ - spec/lib/dirty_seed/assigners/assigner_spec.rb
211
+ - spec/lib/dirty_seed/assigners/float_spec.rb
208
212
  - spec/lib/dirty_seed/data_model_spec.rb
209
- - spec/lib/dirty_seed/dirty_model_spec.rb
210
- - spec/lib/dirty_seed/dirty_association_spec.rb
211
- - spec/lib/dirty_seed/dirty_attribute_spec.rb
213
+ - spec/lib/dirty_seed/association_spec.rb
212
214
  - spec/lib/dirty_seed/sorter_spec.rb
215
+ - spec/lib/dirty_seed/model_spec.rb
216
+ - spec/lib/dirty_seed/attribute_spec.rb
213
217
  - spec/lib/tasks/dirty_seed_tasks_spec.rb
214
218
  - spec/dummy/config.ru
215
219
  - spec/dummy/public/apple-touch-icon.png
@@ -1,95 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'faker'
4
- require 'regexp-examples'
5
-
6
- module DirtySeed
7
- module Assigners
8
- # Draws a value matching validators
9
- class DirtyAssigner
10
- attr_reader :dirty_attribute
11
-
12
- # Initializes an instance
13
- # @param attribute [DirtySeed::DirtyAttribute]
14
- # @return [DirtySeed::Assigners::DirtyAssigner]
15
- def initialize(dirty_attribute)
16
- @dirty_attribute = dirty_attribute
17
- end
18
-
19
- # Returns a random value depending on the attribute type
20
- # @return [void]
21
- # @note This method should be overrided
22
- def value
23
- return if absent?
24
- return inclusion_options.sample if inclusion_options
25
-
26
- define_value
27
- end
28
-
29
- private
30
-
31
- # Returns an validators related to the current attribute
32
- # @return [Array<ActiveModel::Validations::EachValidators>]
33
- def validators
34
- @validators ||= dirty_attribute.validators
35
- end
36
-
37
- # Returns nil, should be overrided by inherited classes
38
- # @return [nil]
39
- def define_value; end
40
-
41
- # Returns true if value should be absent
42
- # Returns [Boolean]
43
- def absent?
44
- validators.any? do |validator|
45
- validator.is_a? ActiveRecord::Validations::AbsenceValidator
46
- end
47
- end
48
-
49
- # Returns the inclusion options if value should be included in options
50
- # e.g. `validates :status, inclusion: { in: %w[todo done aborted] }`
51
- # @return [Array<>]
52
- def inclusion_options
53
- inclusion_validator =
54
- validators.find do |validator|
55
- validator.is_a? ActiveModel::Validations::InclusionValidator
56
- end
57
- inclusion_validator&.options&.dig(:in)
58
- end
59
-
60
- # Returns true if the value should be unique
61
- # @return [Boolean]
62
- def unique?
63
- @unique ||=
64
- validators.any? do |validator|
65
- validator.is_a? ActiveRecord::Validations::UniquenessValidator
66
- end
67
- end
68
-
69
- # Returns a value matching the requirements
70
- # @param category [Symbol] fake category
71
- # @param method [Symbol] fake method
72
- # @param options [Hash] options used by faker
73
- # @return [Object]
74
- def faker_value(category:, method:, options: nil)
75
- if options
76
- faker_process(category).public_send(method, options)
77
- else
78
- faker_process(category).public_send(method)
79
- end
80
- rescue Faker::UniqueGenerator::RetryLimitExceeded
81
- nil
82
- end
83
-
84
- # Returns a faker process
85
- # @param category [Symbol] fake category
86
- # @return [Faker::?]
87
- def faker_process(category)
88
- return @faker_method if @faker_method
89
-
90
- action = "::Faker::#{category}".constantize
91
- @faker_method = unique? ? action.unique : action
92
- end
93
- end
94
- end
95
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DirtySeed
4
- module Assigners
5
- # Draws an Float matching validators
6
- class DirtyFloat < DirtyNumber
7
- # Returns a value matching all validators
8
- # @return [Float]
9
- def define_value
10
- super
11
- faker_value(
12
- category: :Number,
13
- method: :between,
14
- options: { from: min, to: max - 1 }
15
- ) + decimals
16
- end
17
-
18
- private
19
-
20
- # Returns a value between 0 and 1
21
- # @return [Float]
22
- def decimals
23
- rand(0..Float(1))
24
- end
25
-
26
- # Defines the gap to add to min when "greater_than" or to substract to max when "less_than"
27
- # For example if value should be greater_than 0, then the min is 0.01
28
- # and if the value should be lesser_than 1, then the max is 0.99
29
- # @return [Float]
30
- def gap
31
- 0.01
32
- end
33
- end
34
- end
35
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DirtySeed
4
- module Assigners
5
- # Draws an integer matching validators
6
- class DirtyInteger < DirtyNumber
7
- # Defines the gap to add to min when "greater_than" or to substract to max when "less_than"
8
- # For example if value should be greater_than 0, then the min is 1
9
- # and if the value should be lesser_than 1_000, then the max is 999
10
- # @return [Integer]
11
- def gap
12
- 1
13
- end
14
- end
15
- end
16
- end
@@ -1,74 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DirtySeed
4
- module Assigners
5
- # Draws a String matching validators
6
- class DirtyString < DirtyAssigner
7
- # Returns a value matching all validators
8
- # @return [String]
9
- # @note First try to guess attribute meaning by its name and use Faker to return a coherent value
10
- # Then eventually return a value matching the regex validation
11
- def define_value
12
- specific_value || regex_value || default
13
- end
14
-
15
- private
16
-
17
- # Returns a specific value if the attribute name is specific
18
- # @note Verify that, if there is a regex validation, the value matches it
19
- # @return [String]
20
- def specific_value
21
- return unless specific
22
-
23
- try = faker_value(specific)
24
- try if !regex || regex.match?(try)
25
- end
26
-
27
- # Returns a value matching the pattern
28
- # @note Rescue from unreadable regex
29
- # @return [String]
30
- def regex_value
31
- return unless regex
32
-
33
- regex.random_example
34
- rescue RegexpExamples::IllegalSyntaxError
35
- nil
36
- end
37
-
38
- # Returns the specific options if the attribute name is specific
39
- # @return [Hash, nil]
40
- def specific
41
- @specific ||= specific_attributes[dirty_attribute.name]
42
- end
43
-
44
- # Returns specific attributes
45
- # @return [Hash]
46
- # @example
47
- # { address: { category: 'Address', method: 'full_address' } }
48
- def specific_attributes
49
- YAML.safe_load(
50
- File.read(
51
- DirtySeed::Engine.root.join('lib', 'dirty_seed', 'assigners', 'fakers.yml')
52
- )
53
- ).deep_symbolize_keys
54
- end
55
-
56
- # Returns a standard string
57
- # @return [String]
58
- def default
59
- ::Faker::Lorem.unique.sentence(word_count: 3, supplemental: false, random_words_to_add: 4)
60
- end
61
-
62
- # Returns the regex pattern if value should respect a format
63
- # e.g. `validates :email, format: { with: /\w{10}@(hotmail|gmail)\.com/ }`
64
- # @return [Array<>]
65
- def regex
66
- regex_validator =
67
- validators.find do |validator|
68
- validator.is_a? ActiveModel::Validations::FormatValidator
69
- end
70
- regex_validator&.options&.dig(:with)
71
- end
72
- end
73
- end
74
- end
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DirtySeed
4
- # Represents an Active Record attribute
5
- class DirtyAttribute
6
- extend ::DirtySeed::MethodMissingHelper
7
- forward_missing_methods_to :column
8
-
9
- attr_reader :dirty_model, :column
10
-
11
- TYPE_SYMMETRIES = {
12
- binary: :integer,
13
- datetime: :time,
14
- decimal: :float,
15
- text: :string
16
- }.freeze
17
- private_constant :TYPE_SYMMETRIES
18
-
19
- # Initializes an instance
20
- # @param dirty_model [DirtySeed::DirtyModel]
21
- # @param column [ActiveRecord::ConnectionAdapters::Column]
22
- # @return [DirtySeed::DirtyAttribute]
23
- def initialize(dirty_model, column)
24
- @dirty_model = dirty_model
25
- @column = column
26
- end
27
-
28
- # Assigns a value to the attribute
29
- # @param instance [Object] an instance of a class inheriting from ApplicationRecord
30
- # @param sequence [Integer]
31
- # @return [void]
32
- def assign_value(instance)
33
- instance.assign_attributes(name => value)
34
- rescue ArgumentError => e
35
- dirty_model.errors << e
36
- end
37
-
38
- # Returns a value matching type and validators
39
- # @return [Object, nil]
40
- def value
41
- assigner&.value
42
- end
43
-
44
- # Returns the attribute assigner
45
- # @return [DirtySeed::DirtyAssigner]
46
- def assigner
47
- @assigner ||=
48
- "DirtySeed::Assigners::Dirty#{type.capitalize}".constantize.new(self)
49
- # If attribute type is not currently handled (json, array...) return nil
50
- rescue NameError
51
- nil
52
- end
53
-
54
- # Returns attribute name
55
- # @return [Symbol]
56
- def name
57
- column.name.to_sym
58
- end
59
-
60
- # Returns attribute type
61
- # @return [Symbol]
62
- def type
63
- sql_type = column.sql_type_metadata.type
64
- TYPE_SYMMETRIES[sql_type] || sql_type
65
- end
66
-
67
- # Returns an validators related to the current attribute
68
- # @return [Array<ActiveModel::Validations::EachValidators>]
69
- def validators
70
- dirty_model.validators.select do |validator|
71
- validator.attributes.include? name
72
- end
73
- end
74
- end
75
- end