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
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe DirtySeed::Assigners::DirtyAssigner do
6
- let(:dirty_attribute) { build_dirty_attribute(type: :integer) }
7
-
8
- describe '#initialize' do
9
- it 'instantiates an instance' do
10
- expect(described_class.new(dirty_attribute)).to be_a described_class
11
- end
12
- end
13
-
14
- describe 'absent' do
15
- context 'when value should be absent' do
16
- it 'returns nil' do
17
- bravos = DirtySeed::DataModel.bravo.seed(10)
18
- expect(bravos.map(&:an_absent_value).all?(&:nil?)).to be true
19
- end
20
- end
21
- end
22
-
23
- describe 'inclusion' do
24
- context 'when value should be one of options' do
25
- let(:bravos) { DirtySeed::DataModel.bravo.seed(10) }
26
-
27
- it 'returns a value from options [strings]' do
28
- expect(
29
- bravos.map(&:a_string_from_options).all? { |v| v.in? %w[foo bar zed] }
30
- ).to be true
31
- end
32
-
33
- it 'returns a value from options [integers]' do
34
- expect(
35
- bravos.map(&:an_integer_from_options).all? { |v| v.in? [15, 30, 45] }
36
- ).to be true
37
- end
38
- end
39
- end
40
-
41
- describe 'uniqueness' do
42
- context 'when value should be unique' do
43
- it 'returns unique values or nil' do
44
- bravos = DirtySeed::DataModel.bravo.seed(20)
45
- expect(bravos.map(&:a_unique_value).count(&:nil?)).to eq 10
46
- expect(bravos.map(&:a_unique_value).compact).to eq bravos.map(&:a_unique_value).compact.uniq
47
- end
48
- end
49
- end
50
-
51
- describe '#unique?', skip: 'has been set private' do
52
- let(:assigner) { described_class.new(dirty_attribute) }
53
-
54
- context 'when there is a uniqueness validation' do
55
- it 'returns true' do
56
- validator = ActiveModel::Validations::NumericalityValidator.new(attributes: :fake, uniqueness: true)
57
- allow(assigner.dirty_attribute).to receive(:validators).and_return([validator])
58
- expect(assigner.unique?).to be true
59
- end
60
- end
61
-
62
- context 'when there is no uniqueness validation' do
63
- it 'returns false' do
64
- expect(assigner.unique?).to be false
65
- end
66
- end
67
- end
68
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe DirtySeed::Assigners::DirtyBoolean do
6
- let(:dirty_attribute) { build_dirty_attribute(type: :boolean) }
7
-
8
- describe '#define_value' do
9
- it 'returns a Boolean' do
10
- expect(described_class.new(dirty_attribute).define_value).to be_in([true, false])
11
- end
12
- end
13
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe DirtySeed::Assigners::DirtyDate do
6
- let(:dirty_attribute) { build_dirty_attribute(type: :date) }
7
-
8
- describe '#define_value' do
9
- context 'when there are no validators' do
10
- it 'returns a Date' do
11
- expect(described_class.new(dirty_attribute).define_value).to be_a Date
12
- end
13
- end
14
- end
15
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe DirtySeed::Assigners::DirtyFloat do
6
- let(:dirty_attribute) { build_dirty_attribute(type: :float) }
7
-
8
- describe '#define_value' do
9
- context 'when there are no validators' do
10
- it 'returns an integer' do
11
- expect(described_class.new(dirty_attribute).define_value).to be_a Float
12
- end
13
- end
14
-
15
- context 'when there is greater_than validator' do
16
- it 'returns an integer greater than the requirement' do
17
- assigner = described_class.new(dirty_attribute)
18
- validator = ActiveModel::Validations::NumericalityValidator.new(attributes: :fake, greater_than: 1_000)
19
- allow(assigner.dirty_attribute).to receive(:validators).and_return([validator])
20
- 5.times { expect(assigner.define_value).to be >= 1_000 }
21
- end
22
- end
23
-
24
- context 'when there is less_than validator' do
25
- it 'returns an integer less than the requirement' do
26
- assigner = described_class.new(dirty_attribute)
27
- validator = ActiveModel::Validations::NumericalityValidator.new(attributes: :fake, less_than: -1_000)
28
- allow(assigner.dirty_attribute).to receive(:validators).and_return([validator])
29
- 5.times { expect(assigner.define_value).to be < -1_000 }
30
- end
31
- end
32
-
33
- context 'when there are less_than and greater_than validators' do
34
- it 'returns an integer greater than and less than the requirements' do
35
- greater_than = ActiveModel::Validations::NumericalityValidator.new(attributes: :fake, greater_than: 0)
36
- less_than = ActiveModel::Validations::NumericalityValidator.new(attributes: :fake, less_than: 1)
37
- assigner = described_class.new(dirty_attribute)
38
- allow(assigner.dirty_attribute).to(receive(:validators).and_return([greater_than, less_than]))
39
- 5.times { expect(assigner.define_value).to be_between(0, 1).exclusive }
40
- end
41
- end
42
- end
43
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe DirtySeed::Assigners::DirtyInteger do
6
- let(:dirty_attribute) { build_dirty_attribute(type: :integer) }
7
-
8
- describe '#define_value' do
9
- context 'when there are no validators' do
10
- it 'returns an integer' do
11
- expect(described_class.new(dirty_attribute).define_value).to be_an Integer
12
- end
13
- end
14
-
15
- context 'when there is greater_than validator' do
16
- it 'returns an integer greater than the requirement' do
17
- assigner = described_class.new(dirty_attribute)
18
- validator = ActiveModel::Validations::NumericalityValidator.new(attributes: :fake, greater_than: 1_000)
19
- allow(assigner.dirty_attribute).to receive(:validators).and_return([validator])
20
- 5.times { expect(assigner.define_value).to be >= 1_000 }
21
- end
22
- end
23
-
24
- context 'when there is less_than validator' do
25
- it 'returns an integer less than the requirement' do
26
- assigner = described_class.new(dirty_attribute)
27
- validator = ActiveModel::Validations::NumericalityValidator.new(attributes: :fake, less_than: -1_000)
28
- allow(assigner.dirty_attribute).to receive(:validators).and_return([validator])
29
- 5.times { expect(assigner.define_value).to be < -1_000 }
30
- end
31
- end
32
-
33
- context 'when there are less_than and greater_than validators' do
34
- it 'returns an integer greater than and less than the requirements' do
35
- greater_than = ActiveModel::Validations::NumericalityValidator.new(attributes: :fake, greater_than: 1)
36
- less_than = ActiveModel::Validations::NumericalityValidator.new(attributes: :fake, less_than: 5)
37
- assigner = described_class.new(dirty_attribute)
38
- allow(assigner.dirty_attribute).to receive(:validators).and_return([greater_than, less_than])
39
- 5.times { expect(assigner.define_value).to be_between(1, 5).exclusive }
40
- end
41
- end
42
- end
43
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe DirtySeed::Assigners::DirtyString do
6
- let(:data_model) { DirtySeed::DataModel }
7
- let(:dirty_attribute) { build_dirty_attribute(type: :string) }
8
-
9
- describe '#define_value' do
10
- context 'when there are no validators' do
11
- it 'returns a String' do
12
- expect(described_class.new(dirty_attribute).define_value).to be_a String
13
- end
14
- end
15
-
16
- context 'when value should match pattern' do
17
- it 'returns a value matching the pattern' do
18
- dirty_attribute = build_dirty_attribute(type: :string)
19
- assigner = described_class.new(dirty_attribute)
20
- regex = /\w{10}@(hotmail|gmail)\.com/
21
- validator = ActiveModel::Validations::FormatValidator.new(attributes: :fake, with: regex)
22
- allow(assigner.dirty_attribute).to receive(:validators).and_return([validator])
23
- expect(assigner.value).to match(regex)
24
- end
25
- end
26
-
27
- context 'when meaning can be guessed' do
28
- it 'returns a meaningfull String [email]' do
29
- email_attribute = build_dirty_attribute(name: :email, type: :string)
30
- assigner = described_class.new(email_attribute)
31
- expect(assigner.value).to match(URI::MailTo::EMAIL_REGEXP)
32
- end
33
-
34
- it 'returns a meaningfull String [latitude]' do
35
- latitude_attribute = build_dirty_attribute(name: :latitude, type: :string)
36
- assigner = described_class.new(latitude_attribute)
37
- expect(assigner.value.to_s).to match(/-?\d+\.\d+/)
38
- end
39
-
40
- it 'returns a meaningfull String [locale]' do
41
- locale_attribute = build_dirty_attribute(name: :locale, type: :string)
42
- assigner = described_class.new(locale_attribute)
43
- expect(assigner.value).to match(/[A-Z]{2}/)
44
- end
45
-
46
- it 'returns a meaningfull String [uuid]' do
47
- uuid_attribute = build_dirty_attribute(name: :uuid, type: :string)
48
- assigner = described_class.new(uuid_attribute)
49
- expect(assigner.value).to match(/([a-z]|-|\d){36}/)
50
- end
51
- end
52
- end
53
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe DirtySeed::Assigners::DirtyTime do
6
- let(:dirty_attribute) { build_dirty_attribute(type: :time) }
7
-
8
- describe '#define_value' do
9
- context 'when there are no validators' do
10
- it 'returns a Time' do
11
- expect(described_class.new(dirty_attribute).define_value).to be_a Time
12
- end
13
- end
14
- end
15
- end
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe DirtySeed::DirtyAssociation do
6
- let(:dirty_model) { DirtySeed::DataModel.charlie }
7
- let(:reflection) { Charlie.reflections['alfa'] }
8
- let(:dirty_association) { described_class.new(dirty_model, reflection) }
9
-
10
- describe '#initialize' do
11
- it 'instantiates an instance' do
12
- expect(dirty_association).to be_a described_class
13
- end
14
- end
15
-
16
- describe '#dirty_model' do
17
- it 'returns dirty model' do
18
- expect(dirty_association.dirty_model).to eq dirty_model
19
- end
20
- end
21
-
22
- describe '#name' do
23
- it 'returns attribute name' do
24
- expect(dirty_association.name).to eq :alfa
25
- end
26
- end
27
-
28
- describe '#attribute' do
29
- it 'returns attribute of this association' do
30
- expect(dirty_association.attribute).to eq :alfa_id
31
- end
32
- end
33
-
34
- describe '#associated_models' do
35
- context 'when the reflection is regular' do
36
- it 'returns belongs_to association' do
37
- expect(dirty_association.associated_models).to eq [Alfa]
38
- end
39
- end
40
-
41
- context 'when the reflection is polymorphic' do
42
- it 'returns models associated with has_many or has_one' do
43
- dirty_model = DirtySeed::DataModel.echo
44
- reflection = Echo.reflections['echoable']
45
- dirty_association = described_class.new(dirty_model, reflection)
46
- expect(dirty_association.associated_models).to eq [Alfa, Charlie]
47
- end
48
- end
49
-
50
- context 'when the reflection is cyclic (a belongs to b and b optionnally belongs to a)' do
51
- it 'returns models accepting this one as polymorphic' do
52
- dirty_model = DirtySeed::DataModel.hotel
53
- reflection = Hotel.reflections['india']
54
- dirty_association = described_class.new(dirty_model, reflection)
55
- expect(dirty_association.associated_models).to eq [India]
56
-
57
- dirty_model = DirtySeed::DataModel.india
58
- reflection = India.reflections['hotel']
59
- dirty_association = described_class.new(dirty_model, reflection)
60
- expect(dirty_association.associated_models).to eq [Hotel]
61
- end
62
- end
63
- end
64
- end
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe DirtySeed::DirtyAttribute do
6
- let(:dirty_attribute) { described_class.new(build_dirty_model, build_column) }
7
-
8
- describe '#initialize' do
9
- it 'instantiates an instance' do
10
- expect(dirty_attribute).to be_a described_class
11
- end
12
- end
13
-
14
- describe '#dirty_model' do
15
- it 'returns dirty model' do
16
- expect(dirty_attribute.dirty_model).to be_a DirtySeed::DirtyModel
17
- end
18
- end
19
-
20
- describe '#type' do
21
- context 'when column is a boolean' do
22
- it 'returns :boolean' do
23
- dirty_attribute = described_class.new(build_dirty_model, build_column(type: :boolean))
24
- expect(dirty_attribute.type).to eq :boolean
25
- end
26
- end
27
-
28
- context 'when column is an integer' do
29
- it 'returns :integer' do
30
- dirty_attribute = described_class.new(build_dirty_model, build_column(type: :integer))
31
- expect(dirty_attribute.type).to eq :integer
32
- end
33
- end
34
-
35
- context 'when column is a decimal' do
36
- it 'returns :float' do
37
- dirty_attribute = described_class.new(build_dirty_model, build_column(type: :float))
38
- expect(dirty_attribute.type).to eq :float
39
- end
40
- end
41
-
42
- context 'when column is a string' do
43
- it 'returns :string' do
44
- dirty_attribute = described_class.new(build_dirty_model, build_column(type: :string))
45
- expect(dirty_attribute.type).to eq :string
46
- end
47
- end
48
- end
49
- end