fixturizer 0.1.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.debride_whitelist +7 -0
  3. data/.rubocop.yml +80 -0
  4. data/Gemfile +4 -4
  5. data/Rakefile +58 -3
  6. data/VERSION +1 -0
  7. data/bin/console +4 -3
  8. data/bom.xml +802 -0
  9. data/fixturizer.gemspec +34 -16
  10. data/lib/dependencies.rb +10 -0
  11. data/lib/fixturizer/adapters/init.rb +3 -0
  12. data/lib/fixturizer/adapters/mongoid.rb +52 -0
  13. data/lib/fixturizer/configuration.rb +22 -0
  14. data/lib/fixturizer/engines/datasets.rb +46 -0
  15. data/lib/fixturizer/engines/init.rb +3 -0
  16. data/lib/fixturizer/engines/models.rb +73 -0
  17. data/lib/fixturizer/engines/records.rb +23 -0
  18. data/lib/fixturizer/getters/init.rb +28 -0
  19. data/lib/fixturizer/getters/json.rb +16 -0
  20. data/lib/fixturizer/getters/yaml.rb +16 -0
  21. data/lib/fixturizer/rake/manage.rb +3 -1
  22. data/lib/fixturizer/rake/rules/database.rake +13 -9
  23. data/lib/fixturizer/rspec/helpers/database.rb +27 -0
  24. data/lib/fixturizer/rspec/helpers/datasets.rb +19 -0
  25. data/lib/fixturizer/rspec/helpers/getter.rb +24 -0
  26. data/lib/fixturizer/rspec/helpers/records.rb +19 -0
  27. data/lib/fixturizer/rspec/helpers/serializer.rb +25 -0
  28. data/lib/fixturizer/rspec/matchers/database_be_correctly_dropped.rb +16 -0
  29. data/lib/fixturizer/rspec/matchers/database_be_correctly_populated.rb +16 -0
  30. data/lib/fixturizer/rspec/prepare.rb +6 -0
  31. data/lib/fixturizer/serializers/init.rb +30 -0
  32. data/lib/fixturizer/serializers/json.rb +18 -0
  33. data/lib/fixturizer/serializers/yaml.rb +15 -0
  34. data/lib/fixturizer/services.rb +51 -0
  35. data/lib/fixturizer/settings.rb +13 -0
  36. data/lib/fixturizer/version.rb +5 -1
  37. data/lib/fixturizer.rb +10 -109
  38. data/samples/.rspec +3 -0
  39. data/samples/Gemfile +17 -0
  40. data/samples/Rakefile +28 -0
  41. data/samples/app.rb +43 -0
  42. data/samples/config/rules.yml +52 -0
  43. data/samples/config.ru +10 -0
  44. data/samples/spec/app_spec.rb +168 -0
  45. data/samples/spec/fixtures/data.json +10 -0
  46. data/samples/spec/fixtures/data.yml +6 -0
  47. data/samples/spec/spec_helper.rb +23 -0
  48. metadata +218 -8
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fixturizer
4
+ class Services
5
+ include Singleton
6
+ @@settings = Fixturizer::Settings.new
7
+ def self.settings
8
+ @@settings
9
+ end
10
+
11
+ def self.configure
12
+ yield(@@settings)
13
+ end
14
+
15
+ attr_reader :configuration, :log
16
+
17
+ def initialize
18
+ @configuration = Fixturizer::Configuration.new filename: @@settings.configuration_filename
19
+ @log = Logger.new(Fixturizer::Services.settings.log_target)
20
+ log.info 'Starting new Fixturing'
21
+ end
22
+
23
+ def service(type:, name:, parameters: nil)
24
+ service = "Fixturizer::#{type.to_s.capitalize}::#{name.to_s.capitalize}"
25
+ log.info "running Service : #{service}"
26
+ if parameters.nil?
27
+ Object.const_get(service).new
28
+ else
29
+ log.info " => params : #{parameters}"
30
+ Object.const_get(service).new(**parameters)
31
+ end
32
+ end
33
+
34
+ def engine(name:, parameters: nil)
35
+ service(type: :engines, name:, parameters:)
36
+ end
37
+
38
+ def getter(name:, parameters: nil)
39
+ service(type: :getters, name:, parameters:)
40
+ end
41
+
42
+ def serializer(name:, parameters: nil)
43
+ service(type: :serializers, name:, parameters:)
44
+ end
45
+
46
+ class << self
47
+ alias get instance
48
+ alias init instance
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fixturizer
4
+ class Settings
5
+ attr_accessor :configuration_filename, :log_target, :verbose
6
+
7
+ def initialize
8
+ @configuration_filename = './config/rules.yml'
9
+ @log_target = '/tmp/fixturizer.log'
10
+ @verbose = false
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'version'
4
+
1
5
  module Fixturizer
2
- VERSION = "0.1.0"
6
+ VERSION = Version.current
3
7
  end
data/lib/fixturizer.rb CHANGED
@@ -1,109 +1,10 @@
1
- require 'mongoid'
2
- require "fixturizer/version"
3
- require 'yaml'
4
-
5
- module Fixturizer
6
- module Adapters
7
- module Mongoid
8
- def injector(model_infos:, item:)
9
- model = Object.const_get(model_infos[:class])
10
- model.create!(item) unless model.where(model_infos[:unicity] => item[model_infos[:unicity]]).exists?
11
- end
12
-
13
- def binder(item:)
14
- result = Hash::new
15
- item.each do |element|
16
- model_class = @models[element[:collection]][:class]
17
- model = Object.const_get(model_class)
18
- if element[:index].include?(:at)
19
- result[element[:fkey]] = model.all[element[:index][:at] - 1][element[:pkey]].to_s
20
- elsif element[:index].include?(:search_key)
21
- raise 'Links configuration failure' unless element[:index].include?(:for)
22
-
23
- result[element[:fkey]] = model.where({ element[:index][:search_key] => element[:index][:for] }).first[element[:pkey]]
24
- else
25
- raise 'Links configuration failure'
26
- end
27
- end
28
- return result
29
- end
30
-
31
- def drop_database
32
- ::Mongoid.purge!
33
- end
34
- end
35
- end
36
-
37
- class Engine
38
- ADAPTERS = {:mongoid => ::Fixturizer::Adapters::Mongoid}
39
-
40
- attr_reader :generated
41
-
42
- def initialize(filename:)
43
- @rules = read_rules(filename: filename)
44
- @generations = @rules[:fixtures][:rules][:generation]
45
- @models = @rules[:fixtures][:models][:definition]
46
- @order = @rules[:fixtures][:models].include?(:order) ? @rules[:fixtures][:models][:order] : false
47
- @generated = Hash::new
48
- self.extend ADAPTERS[@rules[:fixtures][:type]]
49
- end
50
-
51
- def generate_from(rule:)
52
- rule = @generations[rule]
53
- myproc = eval("lambda { #{rule[:proc]} } ")
54
- return myproc.call
55
- end
56
-
57
- def link_data
58
- end
59
-
60
- def generate_data
61
- @generated.clear
62
- raise 'Order field format missmatch, not an array' unless @order == false || @order.class == Array
63
-
64
- if @order then
65
- raise 'Order field size missmatch for configurate definitions' unless @order.size == @models.size
66
-
67
- @order.each do |item|
68
- raise "Definition #{item} not found in models definitions" unless @models.include?(item)
69
-
70
- @generated[item] = generate_collection(name: item)
71
- end
72
- else
73
- @models.each do |key, _value|
74
- @generated[key] = generate_collection(name: key)
75
- end
76
- end
77
- end
78
-
79
- def inject_data
80
- raise 'Data not generated' if @generated.empty?
81
-
82
- @generated.each do |key, value|
83
- model_infos = @models[key].dup
84
- model_infos.delete(:collection)
85
- value.each do |item|
86
- item[:attributes].merge!(self.binder(item: item[:links])) if item.include?(:links)
87
- self.injector model_infos: model_infos, item: item[:attributes]
88
- end
89
- end
90
- end
91
-
92
- private
93
- def read_rules(filename:)
94
- return YAML.load(File::readlines(filename).join)
95
- end
96
-
97
- def generate_collection(name:)
98
- data = @models[name][:collection].dup
99
- data.each do |item|
100
-
101
- item[:attributes].each do |key, _value|
102
- item[:attributes][key] = generate_from(rule: @models[name][:rules][key]) if @models[name].include?(:rules) && @models[name][:rules].include?(key)
103
- end
104
-
105
- end
106
- return data
107
- end
108
- end
109
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'dependencies'
4
+ require_relative 'fixturizer/settings'
5
+ require_relative 'fixturizer/configuration'
6
+ require_relative 'fixturizer/adapters/init'
7
+ require_relative 'fixturizer/engines/init'
8
+ require_relative 'fixturizer/serializers/init'
9
+ require_relative 'fixturizer/getters/init'
10
+ require_relative 'fixturizer/services'
data/samples/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/samples/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'puma', '~> 6.4'
6
+ gem 'rackup', '~> 2.1'
7
+ gem 'sinatra', '~> 4.0.0'
8
+
9
+ group :test, :development do
10
+ gem 'fixturizer', '~> 0.4.0'
11
+ gem 'rack-rest-rspec', '~> 0.0.3'
12
+ gem 'rack-test', '~> 2.0.2'
13
+ gem 'rake', '~> 13.0'
14
+ gem 'rspec', '~> 3.10.0'
15
+ end
16
+
17
+ gem 'mongoid', '~> 9.0'
data/samples/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/core/rake_task'
4
+ require_relative 'app'
5
+
6
+ RSpec::Core::RakeTask.new do |task|
7
+ task.pattern = 'spec/**/*_spec.rb'
8
+ end
9
+
10
+ desc 'List defined routes'
11
+ task :routes do
12
+ Application.routes.map do |method, routes|
13
+ routes.map { |r| r.first.to_s }.map do |route|
14
+ "#{method.rjust(7, ' ')} #{route}"
15
+ end
16
+ end.flatten.sort.each do |route|
17
+ puts route
18
+ end
19
+ end
20
+
21
+ require 'fixturizer/rake/manage'
22
+
23
+
24
+ Fixturizer::Services.configure do |settings|
25
+ settings.configuration_filename = './config/rules.yml'
26
+ settings.log_target = '/tmp/fixturizer.log'
27
+ settings.verbose = (ENV['VERBOSE'])? true : false
28
+ end
data/samples/app.rb ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sinatra'
4
+ require 'mongoid'
5
+
6
+ Mongoid.configure do |config|
7
+ config.clients.default = {
8
+ uri: 'mongodb://localhost:27017/testbase'
9
+
10
+ }
11
+ config.belongs_to_required_by_default = false
12
+ end
13
+
14
+ class Post
15
+ include Mongoid::Document
16
+ field :title, type: String
17
+ field :body, type: String
18
+ has_many :comments
19
+ end
20
+
21
+ class Comment
22
+ include Mongoid::Document
23
+ field :name, type: String
24
+ field :message, type: String
25
+ belongs_to :post
26
+ end
27
+
28
+ class Application < Sinatra::Base
29
+ before do
30
+ content_type 'application/json'
31
+ end
32
+
33
+ get '/posts' do
34
+ Post.all.to_json
35
+ end
36
+
37
+ get '/posts/:post_id' do |post_id|
38
+ post = Post.find(post_id)
39
+ post.attributes.merge(
40
+ comments: post.comments
41
+ ).to_json
42
+ end
43
+ end
@@ -0,0 +1,52 @@
1
+ ---
2
+ :fixtures:
3
+ :datasets:
4
+ :myset:
5
+ :definition:
6
+ - :name: test dataset
7
+ :list:
8
+ - 1
9
+ - :body:
10
+ - 3
11
+ :nested:
12
+ :body:
13
+ - "two"
14
+ :rules:
15
+ :body: :body
16
+ :rules:
17
+ :body:
18
+ :proc: "SecureRandom.urlsafe_base64(64)"
19
+ :preserve_body:
20
+ :preserve: true
21
+ :proc: "SecureRandom.urlsafe_base64(64)"
22
+ :models:
23
+ :type: :mongoid
24
+ :order:
25
+ - :posts
26
+ - :comments
27
+ :definitions:
28
+ :posts:
29
+ :rules:
30
+ :body: :preserve_body
31
+ :class: Post
32
+ :unicity: :id
33
+ :collection:
34
+ - :attributes:
35
+ :title: First post Title
36
+ :body: First Post Body
37
+ - :attributes:
38
+ :title: Second post Title
39
+ :body:
40
+ :comments:
41
+ :class: Comment
42
+ :unicity: :id
43
+ :collection:
44
+ - :attributes:
45
+ :name: test name
46
+ :message: test message
47
+ :links:
48
+ - :collection: :posts
49
+ :index:
50
+ :at: 1
51
+ :fkey: :post
52
+ :pkey: :id
data/samples/config.ru ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ Bundler.require
7
+
8
+ require './app'
9
+
10
+ run Application
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe 'Test Fixturizer' do
4
+ context 'Records : test fixturizing' do
5
+ it 'must be possible to apply non preserving rule by definition' do
6
+ value = 'placeholder'
7
+ rule = { proc: 'SecureRandom.urlsafe_base64(64)' }
8
+ expect(record.apply(value:, rule:).size).to be(86)
9
+ end
10
+
11
+ it 'must be possible to apply preserving rule by definition' do
12
+ value = 'present value'
13
+ rule = { proc: 'SecureRandom.urlsafe_base64(64)', preserve: true }
14
+ expect(record.apply(value:, rule:)).to be(value)
15
+ end
16
+
17
+ it 'must be possible to apply rule by name (in configuration)' do
18
+ value = 'present value'
19
+ rule = :body
20
+ expect(record.apply(value:, rule:).size).to be(86)
21
+ end
22
+ end
23
+
24
+ context 'Datasets : test fixturizing' do
25
+ it 'must be possible to generate result for dataset by definition with named rule' do
26
+ dataset = { definition: { name: 'test dataset', body: 'placeholder' }, rules: { body: :body } }
27
+ result = datasets.generate(dataset:)
28
+ expect(result[:body].size).to be(86)
29
+ expect(result[:name]).to eq('test dataset')
30
+ end
31
+
32
+ it 'must be possible to generate result for dataset by definition with defined rule' do
33
+ dataset = { definition: { name: 'test dataset', body: 'placeholder' },
34
+ rules: { body: { proc: 'SecureRandom.urlsafe_base64(64)' } } }
35
+ result = datasets.generate(dataset:)
36
+ expect(result[:body].size).to be(86)
37
+ expect(result[:name]).to eq('test dataset')
38
+ end
39
+
40
+ it 'must be possible to generate result for dataset by definition with named rule and nil field' do
41
+ dataset = { definition: { name: 'test dataset', body: nil },
42
+ rules: { body: :body } }
43
+ result = datasets.generate(dataset:)
44
+ expect(result[:body].size).to be(86)
45
+ expect(result[:name]).to eq('test dataset')
46
+ end
47
+
48
+ it 'must be possible to generate result for dataset by definition with named preserving rule' do
49
+ dataset = { definition: { name: 'test dataset', body: 'test preserved' },
50
+ rules: { body: :preserve_body } }
51
+ result = datasets.generate(dataset:)
52
+ expect(result[:body]).to eq('test preserved')
53
+ expect(result[:name]).to eq('test dataset')
54
+ end
55
+
56
+ it 'must be possible to generate result for nested dataset by definition with named rule' do
57
+ dataset = { definition: { name: 'test dataset', nested: { body: nil } },
58
+ rules: { body: :body } }
59
+ result = datasets.generate(dataset:)
60
+ expect(result[:nested][:body].size).to be(86)
61
+ expect(result[:name]).to eq('test dataset')
62
+ end
63
+
64
+ it 'must be possible to generate result for complex dataset by definition with named rule' do
65
+ dataset = { definition: { name: 'test dataset', list: [1, { body: nil }, 3], nested: { body: nil } },
66
+ rules: { body: :body } }
67
+ result = datasets.generate(dataset:)
68
+ expect(result[:nested][:body].size).to be(86)
69
+ expect(result[:list].size).to be(3)
70
+ expect(result[:list][1][:body].size).to be(86)
71
+ expect(result[:name]).to eq('test dataset')
72
+ end
73
+
74
+ it 'must be possible to generate result for complex dataset by name with named rule' do
75
+ result = datasets.generate dataset: :myset
76
+ expect(result[0][:nested][:body].size).to be(86)
77
+ expect(result[0][:list].size).to be(3)
78
+ expect(result[0][:list][1][:body].size).to be(86)
79
+ expect(result[0][:name]).to eq('test dataset')
80
+ expect(result.last).to eq('two')
81
+ end
82
+ end
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
95
+
96
+ context 'Serializers : test ' do
97
+ it 'must be possible to serialize data to JSON' do
98
+ data = { name: 'test data', body: 'placeholder' }
99
+ result = serialize.to(format: :json, data:)
100
+ expect(result).to be_an_instance_of(String)
101
+ expect(result.size).to eq(51)
102
+ end
103
+
104
+ it 'must be possible to serialize data to JSON in raw format' do
105
+ data = { name: 'test data', body: 'placeholder' }
106
+ result = serialize.to format: :json, data:, raw: true
107
+ expect(result).to be_an_instance_of(String)
108
+ expect(result.size).to eq(41)
109
+ end
110
+
111
+ it 'must be possible to serialize data to JSON and write in a file' do
112
+ data = { name: 'test data', body: 'placeholder' }
113
+ filename = '/tmp/test.json'
114
+ serialize.to format: :json, data:, to_file: filename
115
+ expect(File.exist?(filename)).to eq true
116
+ File.unlink(filename)
117
+ end
118
+
119
+ it 'must be possible to serialize data to YAML' do
120
+ data = { name: 'test data', body: 'placeholder' }
121
+ result = serialize.to(format: :yaml, data:)
122
+ expect(result).to be_an_instance_of(String)
123
+ expect(result.size).to eq(40)
124
+ end
125
+
126
+ it 'must be possible to serialize data to YAML and write in a file' do
127
+ data = { name: 'test data', body: 'placeholder' }
128
+ filename = '/tmp/test.yml'
129
+ serialize.to format: :yaml, data:, to_file: filename
130
+ expect(File.exist?(filename)).to eq true
131
+ File.unlink(filename)
132
+ end
133
+ end
134
+
135
+
136
+ context 'Getters : test ' do
137
+ it 'must be possible to get content data from YAML file' do
138
+ filename = './spec/fixtures/data.yml'
139
+ result = get_content.from file: filename, format: :yaml
140
+ expect(result[:body]).to eq "test Body"
141
+ expect(result[:list].size).to eq 3
142
+ end
143
+
144
+ it 'must be possible to get content data from YAML file without symbols keys' do
145
+ filename = './spec/fixtures/data.yml'
146
+ result = get_content.from file: filename, format: :yaml, symbolize: false
147
+ expect(result["body"]).to eq "test Body"
148
+ expect(result["list"].size).to eq 3
149
+ end
150
+
151
+ it 'must be possible to get content data from JSON file' do
152
+ filename = './spec/fixtures/data.json'
153
+ result = get_content.from file: filename, format: :json
154
+ expect(result[:body]).to eq "test Body"
155
+ expect(result[:list].size).to eq 3
156
+ end
157
+
158
+ it 'must be possible to get content data from JSON file without symbols keys' do
159
+ filename = './spec/fixtures/data.json'
160
+ result = get_content.from file: filename, format: :json, symbolize: false
161
+ expect(result["body"]).to eq "test Body"
162
+ expect(result["list"].size).to eq 3
163
+ end
164
+
165
+ end
166
+
167
+
168
+ end
@@ -0,0 +1,10 @@
1
+ {
2
+ "body": "test Body",
3
+ "list": [
4
+ "one",
5
+ 2,
6
+ {
7
+ "three": 3
8
+ }
9
+ ]
10
+ }
@@ -0,0 +1,6 @@
1
+ ---
2
+ body: test Body
3
+ list:
4
+ - "one"
5
+ - 2
6
+ - {three: 3}
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'fixturizer/rspec/prepare'
5
+
6
+ Fixturizer::Services.configure do |settings|
7
+ settings.configuration_filename = './config/rules.yml'
8
+ settings.log_target = '/tmp/fixturizer.log'
9
+ end
10
+
11
+ require_relative '../app'
12
+
13
+ RSpec.configure do |config|
14
+ # Enable flags like --only-failures and --next-failure
15
+ config.example_status_persistence_file_path = '.rspec_status'
16
+
17
+ # Disable RSpec exposing methods globally on `Module` and `main`
18
+ config.disable_monkey_patching!
19
+
20
+ config.expect_with :rspec do |c|
21
+ c.syntax = :expect
22
+ end
23
+ end