fixturizer 0.4.1 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b32aa16a54c913b66e8d68df00c968faea177694473669e1041e5f120158641
4
- data.tar.gz: 7896981fd9376c0929896894f306bd47653058aecaffb9f293df3143f9ebe3e0
3
+ metadata.gz: cf7a1159047eed0d85251205266b374018fc8f7c7b7c312f91db96eb632a87ae
4
+ data.tar.gz: 965f93e4be42f6207e00647b2d456451425421b04eea4d7ceea984c5a2a7ce98
5
5
  SHA512:
6
- metadata.gz: 11f9ea54665b8be2fe7c2db16b0805d683d348f144d6cd7367dbecf04b562c78a6429b394c2511913882b6a926f267829f2efda6442364548fb38d629415b76f
7
- data.tar.gz: 1a4714aa619eeef6ff7142a848b5d8d04d452a577353827d34ea463d0bebc4e77592256ce631139bdd2b9ab086eecc00ab388269ffefb7585c01fa2c0b12373e
6
+ metadata.gz: 0ef1bea30d4ce6398a6eb0f2df942ecb26ad25f9828339b8fb3e9067c18fe07a7bb7220ddba5ae3596c313d2202dd06dabaf40cf51ec370754b69796386d72f0
7
+ data.tar.gz: 2f46d5b7386d853503abc3b95576f1fbea6fe602f5a1608e4812a7639568ee50f303f2bed11daa0632ab344b3bd717757cbc4f9aff05c3f894926fb2a2f3fc0c
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
@@ -3,30 +3,28 @@
3
3
  module Fixturizer
4
4
  module Adapters
5
5
  module Mongoid
6
- def injector(model_infos:, item:)
7
- model = Object.const_get(model_infos[:class])
8
- model.create!(item) unless model.where(model_infos[:unicity] => item[model_infos[:unicity]]).exists?
9
- end
10
-
11
- def binder(item:)
12
- result = {}
13
- item.each do |element|
14
- model_class = @models[element[:collection]][:class]
15
- model = Object.const_get(model_class)
16
- if element[:index].include?(:at)
17
- result[element[:fkey]] = model.all[element[:index][:at] - 1][element[:pkey]].to_s
18
- elsif element[:index].include?(:search_key)
19
- raise 'Links configuration failure' unless element[:index].include?(:for)
6
+ def inject_data
7
+ list = (@orders.is_a? Array)? @order : @generated.keys
8
+ list.each do |name|
9
+ records = @generated[name]
10
+ records.each do |record|
11
+ link = record.dig(:link,:to)
12
+ by = record.dig(:link,:by)
13
+ pattern = record.dig(:link,:search_by)
14
+ if link.is_a? Symbol then
15
+ model = Object.const_get(@models[link][:class]).find_by(**pattern).send by
16
+ model.create!(record[:data]) unless model.where(@models[link][:unicity] => record[:data][@models[link][:unicity]]).exists?
17
+ else
18
+ model = Object.const_get(@models[name][:class])
19
+ model.create!(record[:data]) unless model.where(@models[name][:unicity] => record[:data][@models[name][:unicity]]).exists?
20
+ end
20
21
 
21
- result[element[:fkey]] =
22
- model.where({ element[:index][:search_key] => element[:index][:for] }).first[element[:pkey]]
23
- else
24
- raise 'Links configuration failure'
25
22
  end
23
+
26
24
  end
27
- result
28
25
  end
29
26
 
27
+
30
28
  def drop_database
31
29
  ::Mongoid.purge!
32
30
  true
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'yaml'
4
+
3
5
  module Fixturizer
4
6
  class Configuration
5
- attr_reader :rules, :models, :datasets, :models_order, :models_type
7
+ attr_reader :rules, :models, :datasets, :models_order, :models_type, :filename
6
8
 
7
9
  def initialize(filename:)
10
+ @filename = filename
8
11
  @content = read_file(filename:)
9
12
  @rules = @content[:fixtures][:rules]
10
13
  @models_type = @content[:fixtures].dig(:models, :type)
@@ -19,4 +22,215 @@ module Fixturizer
19
22
  YAML.load(File.readlines(filename).join)
20
23
  end
21
24
  end
25
+
26
+ class ConfigurationLinter
27
+ attr_reader :content
28
+
29
+ KEYS_BY_LEVEL = {:fixtures => [:rules,:models,:datasets],
30
+ :dataset => [:definition, :rules],
31
+ :models => [:type, :order, :definitions],
32
+ :model_definition => [:rules,:class,:unicity, :collection],
33
+ :collection => [:attributes,:links]
34
+
35
+ }
36
+
37
+
38
+ def initialize(filename:)
39
+ @status = {status: :ok, warn: [], error: [], cleaned: []}
40
+ @filename = filename
41
+ end
42
+
43
+
44
+ def display
45
+ puts "Running linter for Configuration file : #{@filename} :"
46
+ validate
47
+ puts " => Status : #{@status[:status].to_s.upcase} "
48
+ unless @status[:error].empty? then
49
+ puts "Errors : "
50
+ @status[:error].each do |error|
51
+ puts " * #{error}"
52
+ end
53
+ end
54
+ unless @status[:warn].empty? then
55
+ puts "Warnings : "
56
+ @status[:warn].each do |warning|
57
+ puts " * #{warning}"
58
+ end
59
+ end
60
+ end
61
+
62
+
63
+ def validate!
64
+ validate
65
+ unless @status[:status] == :ok
66
+ puts "Configuration error, run rake fixturizer:configuration:lint to show"
67
+ exit 10
68
+ end
69
+ end
70
+
71
+
72
+ private
73
+
74
+ def validate
75
+ begin
76
+ @content = read_file filename: @filename
77
+ [:validate_root,:validate_section, :validate_rules,:validate_datasets,:validate_models].each do |method|
78
+ self.send method if @status[:status] = :ok
79
+ end
80
+ rescue RuntimeError => e
81
+ @status[:status] = :ko
82
+ @status[:error].push "Malformed YAML file #{e.message}"
83
+ rescue Errno::ENOENT
84
+ @status[:status] = :ko
85
+ @status[:error].push "Missing YAML file"
86
+ rescue NoMethodError => e
87
+ @status[:status] = :ko
88
+ @status[:error].push "Malformed YAML file #{e.message}"
89
+ end
90
+ return @status
91
+ end
92
+
93
+ def read_file(filename:)
94
+ YAML.load(File.readlines(filename).join)
95
+ end
96
+
97
+ def validate_root
98
+ if @content.dig(:fixtures).nil?
99
+ @status[:error].push "General root error : key is not Symbol :fixtures or empty"
100
+ @status[:status] = :ko
101
+ end
102
+ end
103
+
104
+ def validate_section
105
+ full_nil = []
106
+ [:datasets,:rules,:models].each do |root|
107
+ unless @content.dig(:fixtures, root).is_a? Hash or @content.dig(:fixtures,root).nil?
108
+ @status[:error].push "//:fixtures/#{root} is not a Hash"
109
+ @status[:status] = :ko
110
+ end
111
+ full_nil.push root if @content.dig(:fixtures,root).nil?
112
+ end
113
+ if full_nil.size == 3
114
+ @status[:error].push "All sections are empty : #{full_nil}"
115
+ @status[:status] = :ko
116
+ end
117
+ end
118
+
119
+ def validate_rules
120
+ return if @content.dig(:fixtures,:rules).nil?
121
+ count = @status[:error].size
122
+ @content.dig(:fixtures,:rules).each do |name, rule|
123
+ @status[:error].push "//:fixtures/:rules/:#{name} is not a Symbol" unless name.is_a? Symbol
124
+ @status[:error].push "//:fixtures/:rules/:#{name} record is not a Hash" unless rule.is_a? Hash
125
+ @status[:error].push "//:fixtures/:rules/:#{name} not have key :proc defined" unless rule.include? :proc
126
+ @status[:error].push "//:fixtures/:rules/:#{name}/:proc is not a String" unless rule[:proc].is_a? String
127
+ if rule.include? :preserve then
128
+ @status[:error].push "//:fixtures/:rules/:#{name} have key :preserve but it's not a Boolean" unless [true, false].include? rule[:preserve]
129
+ end
130
+ end
131
+ @status[:status] = (@status[:error].size > count)? :ko : @status[:status]
132
+ end
133
+
134
+
135
+ def validate_datasets
136
+ return if @content.dig(:fixtures,:datasets).nil?
137
+ count = @status[:error].size
138
+ @content.dig(:fixtures,:datasets).each do |name, dataset|
139
+ @status[:error].push "//:fixtures/:datasets/:#{name} is not a Symbol" unless name.is_a? Symbol
140
+ @status[:error].push "//:fixtures/:datasets/:#{name} record is not a Hash" unless dataset.is_a? Hash
141
+ @status[:error].push "//:fixtures/:datasets/:#{name} not have key :definition defined" unless dataset.include? :definition
142
+ @status[:error].push "//:fixtures/:datasets/:#{name}/:definition is nil" if dataset[:definition].nil?
143
+ if dataset.include? :rules then
144
+ @status[:error].push "//:fixtures/:datasets/:#{name} have key :rules but it's not a Hash" unless dataset[:rules].is_a? Hash
145
+ dataset[:rules].each do |key,value|
146
+ @status[:error].push "//:fixtures/:datasets/:#{name}/:rules/#{key} is not a Symbol" unless key.is_a? Symbol
147
+ unless value.is_a? Symbol
148
+ @status[:error].push "//:fixtures/:datasets/:#{name}/:rules/#{key} value (#{value}) is not a Symbol"
149
+ else
150
+ @status[:warn].push "//:fixtures/:datasets/:#{name}/:rules/#{key} is useless" if deep_find_key(key,dataset[:definition]).empty?
151
+ end
152
+ @status[:error].push "//:fixtures/:datasets/:#{name}/:rules/#{key} set but no rules exist" unless @content.dig(:fixtures,:rules).include? value
153
+
154
+ end
155
+
156
+ end
157
+ end
158
+ @status[:status] = (@status[:error].size > count)? :ko : @status[:status]
159
+ end
160
+
161
+
162
+ def validate_models
163
+ return if @content.dig(:fixtures,:models).nil?
164
+ count = @status[:error].size
165
+ model_config = @content.dig(:fixtures,:models)
166
+ unless model_config.is_a? Hash
167
+ @status[:error].push "//:fixtures/:models record is not a Hash"
168
+ else
169
+ @status[:error].push "//:fixtures/:models/ not have key :type defined" unless model_config.include? :type
170
+ @status[:error].push "//:fixtures/:models/:type is not a symbol" unless model_config[:type].is_a? Symbol
171
+ @status[:error].push "//:fixtures/:models/ not have key :definitions defined" unless model_config.include? :definitions
172
+ unless model_config[:definitions].is_a? Hash then
173
+ @status[:error].push "//:fixtures/:models/:definitions is not a Hash"
174
+ else
175
+ @content.dig(:fixtures,:models,:definitions).each do |name, definitions|
176
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name} is not a Symbol" unless name.is_a? Symbol
177
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name} not have key :class defined" unless definitions.include? :class
178
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:class is not a String" unless definitions[:class].is_a? String
179
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name} not have key :unicity defined" unless definitions.include? :unicity
180
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:unicity is not a Symbol" unless definitions[:unicity].is_a? Symbol
181
+ if definitions.include? :rules then
182
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name} have key :rules but it's not a Hash" unless definitions[:rules].is_a? Hash
183
+ definitions[:rules].each do |key,value|
184
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:rules/#{key} is not a Symbol" unless key.is_a? Symbol
185
+ unless value.is_a? Symbol
186
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:rules/#{key} value (#{value}) is not a Symbol"
187
+ else
188
+ @status[:warn].push "//:fixtures/:models/:definitions/:#{name}/:rules/#{key} is useless" if deep_find_key(key,definitions[:collection]).empty?
189
+ end
190
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:rules/#{key} set but no rules exist" unless @content.dig(:fixtures,:rules).include? value
191
+ end
192
+ end
193
+
194
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name} not have key :collection defined" unless definitions.include? :collection
195
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:collection is not a Array" unless definitions[:collection].is_a? Array
196
+ if definitions.include? :collection then
197
+ definitions.dig(:collection).each_with_index do |record,i|
198
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:collection[#{i}] not have key :attributes defined" unless record.include? :attributes
199
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:collection[#{i}]/:attributes is not a Hash" unless record[:attributes].is_a? Hash
200
+ if record.include? :link
201
+ unless record[:link].is_a? Hash then
202
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:collection[#{i}]/:link is set but is not a Hash"
203
+ else
204
+ {to: Symbol, by: Symbol, search_by: Hash}.each do |verb,type|
205
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:collection[#{i}]/:link not have key :#{verb} defined" unless record[:link].include? verb
206
+ @status[:error].push "//:fixtures/:models/:definitions/:#{name}/:collection[#{i}]/:link/:#{verb} is not a #{type}" unless record[:link][verb].is_a? type
207
+ end
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
215
+ @status[:status] = (@status[:error].size > count)? :ko : @status[:status]
216
+ end
217
+
218
+ def deep_find_key(key, object)
219
+ found = []
220
+ if object.respond_to?(:key?) && object.key?(key)
221
+ found << key
222
+ end
223
+ if object.is_a? Enumerable
224
+ found << object.collect { |*a| deep_find_key(key, a.last) }
225
+ end
226
+ found.flatten.compact
227
+ end
228
+
229
+
230
+
231
+
232
+ end
233
+
22
234
  end
235
+
236
+
@@ -5,11 +5,10 @@ module Fixturizer
5
5
  class Models
6
6
  ADAPTERS = { mongoid: ::Fixturizer::Adapters::Mongoid }.freeze
7
7
 
8
- attr_reader :generated
8
+ attr_reader :generated, :order, :models
9
9
 
10
10
  def initialize
11
11
  @configuration = Fixturizer::Services.get.configuration
12
- @rules = @configuration.rules
13
12
  @models = @configuration.models
14
13
  @order = @configuration.models_order
15
14
  @type = @configuration.models_type
@@ -18,12 +17,12 @@ module Fixturizer
18
17
  end
19
18
 
20
19
  def populate
21
- generate_data
22
- inject_data
20
+ generate
21
+ inject
23
22
  true
24
23
  end
25
24
 
26
- def generate_data
25
+ def generate
27
26
  @generated.clear
28
27
  raise 'Order field format missmatch, not an array' unless @order.nil? || @order.is_a?(Array)
29
28
 
@@ -32,41 +31,35 @@ module Fixturizer
32
31
 
33
32
  @order.each do |item|
34
33
  raise "Definition #{item} not found in models definitions" unless @models.include?(item)
35
-
36
- @generated[item] = generate_collection(name: item)
34
+ @generated[item] = generate_collection(name: item)
37
35
  end
38
36
  else
39
- @models.each_key do |key|
40
- @generated[key] = generate_collection(name: key)
37
+ @models.each_key do |item|
38
+ @generated[item] = generate_collection(name: item)
41
39
  end
40
+
42
41
  end
43
42
  end
44
43
 
45
- def inject_data
44
+ def inject
46
45
  raise 'Data not generated' if @generated.empty?
47
-
48
- @generated.each do |key, value|
49
- model_infos = @models[key].dup
50
- model_infos.delete(:collection)
51
- value.each do |item|
52
- item[:attributes].merge!(binder(item: item[:links])) if item.include?(:links)
53
- injector model_infos:, item: item[:attributes]
54
- end
55
- end
46
+ inject_data
56
47
  end
57
48
 
58
49
  private
59
50
 
60
51
  def generate_collection(name:)
61
- data = @models[name][:collection].dup
62
- data.each do |item|
63
- dataset = {}
64
- dataset[:definition] = item[:attributes]
65
- dataset[:rules] = @models[name][:rules]
66
- item[:attributes] =
67
- Fixturizer::Services.get.engine(name: :dataset, parameters: { dataset: }).generate
52
+ data = []
53
+ @models[name][:collection].each do |item|
54
+ res = {data: Fixturizer::Services.get.engine(name: :dataset, parameters: { dataset:
55
+ { definition: item[:attributes], rules: @models[name][:rules]}
56
+ }).generate }
57
+ link = item.dig(:link)
58
+ res[:link] = link if link
59
+ data.push res
60
+
68
61
  end
69
- data
62
+ return data
70
63
  end
71
64
  end
72
65
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :fixturizer do
4
+ namespace :configuration do
5
+ desc 'Check the configuration file'
6
+ task :lint do
7
+ filename = (ENV['FIXTURIZER_CONFIGURATION_FILE'].is_a? String)?
8
+ ENV['FIXTURIZER_CONFIGURATION_FILE'] :
9
+ Fixturizer::Services.settings.configuration_filename
10
+ Fixturizer::Services.get.linter(filename: filename).display
11
+ end
12
+
13
+
14
+ end
15
+ end
@@ -15,6 +15,7 @@ module Fixturizer
15
15
  attr_reader :configuration, :log
16
16
 
17
17
  def initialize
18
+ linter(filename: @@settings.configuration_filename).validate! unless detect_rake_lint
18
19
  @configuration = Fixturizer::Configuration.new filename: @@settings.configuration_filename
19
20
  @log = Logger.new(Fixturizer::Services.settings.log_target)
20
21
  log.info 'Starting new Fixturing'
@@ -43,9 +44,24 @@ module Fixturizer
43
44
  service(type: :serializers, name:, parameters:)
44
45
  end
45
46
 
47
+ def linter(filename: @@settings.configuration_filename)
48
+ Fixturizer::ConfigurationLinter.new filename: filename
49
+ end
50
+
46
51
  class << self
47
52
  alias get instance
48
53
  alias init instance
49
54
  end
55
+
56
+ private
57
+
58
+ def detect_rake_lint
59
+ res = false
60
+ if defined?(Rake)
61
+ res = true if Rake.application.top_level_tasks.first == "fixturizer:configuration:lint"
62
+ end
63
+ return res
64
+ end
65
+
50
66
  end
51
67
  end
data/samples/Gemfile CHANGED
@@ -7,7 +7,7 @@ gem 'rackup', '~> 2.1'
7
7
  gem 'sinatra', '~> 4.0.0'
8
8
 
9
9
  group :test, :development do
10
- gem 'fixturizer', '~> 0.4.0'
10
+ gem 'fixturizer'
11
11
  gem 'rack-rest-rspec', '~> 0.0.3'
12
12
  gem 'rack-test', '~> 2.0.2'
13
13
  gem 'rake', '~> 13.0'
@@ -13,6 +13,7 @@
13
13
  - "two"
14
14
  :rules:
15
15
  :body: :body
16
+
16
17
  :rules:
17
18
  :body:
18
19
  :proc: "SecureRandom.urlsafe_base64(64)"
@@ -29,7 +30,7 @@
29
30
  :rules:
30
31
  :body: :preserve_body
31
32
  :class: Post
32
- :unicity: :id
33
+ :unicity: :title
33
34
  :collection:
34
35
  - :attributes:
35
36
  :title: First post Title
@@ -44,9 +45,8 @@
44
45
  - :attributes:
45
46
  :name: test name
46
47
  :message: test message
47
- :links:
48
- - :collection: :posts
49
- :index:
50
- :at: 1
51
- :fkey: :post
52
- :pkey: :id
48
+ :link:
49
+ :to: :posts
50
+ :by: :comments
51
+ :search_by:
52
+ :title: First post Title
@@ -81,17 +81,17 @@ RSpec.describe 'Test Fixturizer' do
81
81
  end
82
82
  end
83
83
 
84
- # context 'Database : drop and populate' do
85
- # it 'must be possible to drop database with helper ' do
86
- # expect(database.drop).to be true
87
- # end
88
- # it 'must be possible to populate database with helper ' do
89
- # expect(database.populate).to be true
90
- # end
91
-
92
- # it { expect(database).to be_correctly_dropped }
93
- # it { expect(database).to be_correctly_populated }
94
- # end
84
+ context 'Database : drop and populate' do
85
+ it 'must be possible to drop database with helper ' do
86
+ expect(database.drop).to be true
87
+ end
88
+ it 'must be possible to populate database with helper ' do
89
+ expect(database.populate).to be true
90
+ end
91
+
92
+ it { expect(database).to be_correctly_dropped }
93
+ it { expect(database).to be_correctly_populated }
94
+ end
95
95
 
96
96
  context 'Serializers : test ' do
97
97
  it 'must be possible to serialize data to JSON' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixturizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre ALPHONSE
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-05-16 00:00:00.000000000 Z
12
+ date: 2024-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -214,6 +214,7 @@ files:
214
214
  - lib/fixturizer/getters/json.rb
215
215
  - lib/fixturizer/getters/yaml.rb
216
216
  - lib/fixturizer/rake/manage.rb
217
+ - lib/fixturizer/rake/rules/configuration.rake
217
218
  - lib/fixturizer/rake/rules/database.rake
218
219
  - lib/fixturizer/rspec/helpers/database.rb
219
220
  - lib/fixturizer/rspec/helpers/datasets.rb