data_magic 0.22 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/ChangeLog +6 -1
- data/README.md +11 -0
- data/lib/data_magic.rb +30 -1
- data/lib/data_magic/version.rb +1 -1
- data/spec/lib/data_magic_spec.rb +53 -3
- data/spec/lib/translation_spec.rb +5 -1
- metadata +18 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8d3f6a94411e15c7259ab68c9025824694a6ba2
|
4
|
+
data.tar.gz: 43d27206b78632d49b42a96264eefccd31ddc6c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d1dcacb77607bf29d0e66d6a4ff95823e61995b610924a7e1e354fd597308a1252625817b00aa689a54a6f303245662d7bb9069e0a8c17671251a3f0467823e
|
7
|
+
data.tar.gz: fbf215b0e7d60ee954939d51a6b467542eb60fa0292331eb6b51c8d84f68b0744d6694408760592b0dbac182622d07fb25ff0cbbd8af1f2223aeecc271b8728d
|
data/.gitignore
CHANGED
data/ChangeLog
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
+
=== Version 1.0 / 2016-11-23
|
2
|
+
* Enhancements
|
3
|
+
* Allow merged to be a Hash of Hashes and find key that matches (thanks Donavan Stanley)
|
4
|
+
* Allow you to specify file to load with tag when using Cucumber (thanks Donavan Stanley)
|
5
|
+
|
1
6
|
=== Version 0.22 / 2015-12-2
|
2
7
|
* Enhancements
|
3
|
-
* Better handling for nexted hash translation
|
8
|
+
* Better handling for nexted hash translation (thanks vveliev)
|
4
9
|
|
5
10
|
=== Version 0.21 / 2015-4-12
|
6
11
|
* Enhancements
|
data/README.md
CHANGED
@@ -21,6 +21,17 @@ After setting the directory you must load a file. This can be accomplished by c
|
|
21
21
|
DataMagic.load 'filename.yml'
|
22
22
|
````
|
23
23
|
|
24
|
+
Another way to specify the file to load is to use a _tag_ in a cucumber scenario. You tag should take the
|
25
|
+
form of `@datamagic_FILENAME` where `FILENAME` is replaced with the file you wish to load. Then you can
|
26
|
+
simply have the following code in a hook:
|
27
|
+
|
28
|
+
````ruby
|
29
|
+
Before do |scenario|
|
30
|
+
DataMagic.load_for_scenario(scenario)
|
31
|
+
end
|
32
|
+
````
|
33
|
+
|
34
|
+
|
24
35
|
If you do not specify a filename the gem will attempt to use a file named _default.yml_. If you are using this for testing you will more than likely want to call load before each test to load the proper data for the specific test, or use the namespaced keys method, detailed below.
|
25
36
|
|
26
37
|
Another option is to set an environment variable DATA_MAGIC_FILE. When this is set it will be used instead of the _default.yml_ file.
|
data/lib/data_magic.rb
CHANGED
@@ -37,7 +37,18 @@ module DataMagic
|
|
37
37
|
end
|
38
38
|
data = DataMagic.yml[record]
|
39
39
|
raise ArgumentError, "Undefined key #{key}" unless data
|
40
|
-
prep_data data.merge(additional).clone
|
40
|
+
additional.key?(record) ? prep_data(data.merge(additional[record]).clone) : prep_data(data.merge(additional).clone)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Given a scenario, load any fixture it needs.
|
44
|
+
# Fixture tags should be in the form of @datamagic_FIXTUREFILE
|
45
|
+
def self.load_for_scenario(scenario, fixture_folder = DataMagic.yml_directory)
|
46
|
+
orig_yml_directory = DataMagic.yml_directory
|
47
|
+
DataMagic.yml_directory = fixture_folder
|
48
|
+
fixture_files = fixture_files_on(scenario)
|
49
|
+
|
50
|
+
DataMagic.load "#{fixture_files.last}.yml" if fixture_files.count > 0
|
51
|
+
DataMagic.yml_directory = orig_yml_directory
|
41
52
|
end
|
42
53
|
|
43
54
|
private
|
@@ -68,6 +79,24 @@ module DataMagic
|
|
68
79
|
@translation ||= Translation.new parent
|
69
80
|
end
|
70
81
|
|
82
|
+
# Load a fixture and merge it with an existing hash
|
83
|
+
def self.load_fixture_and_merge_with(fixture_name, base_hash, fixture_folder = DEFAULT_FIXTURE_FOLDER)
|
84
|
+
new_hash = load_fixture(fixture_name, fixture_folder)
|
85
|
+
base_hash.deep_merge new_hash
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.fixture_tags_on(scenario)
|
89
|
+
# tags for cuke 2, source_tags for cuke 1
|
90
|
+
tags = scenario.send(scenario.respond_to?(:tags) ? :tags : :source_tags)
|
91
|
+
tags.map(&:name).select { |t| t =~ /@datamagic_/ }
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.fixture_files_on(scenario)
|
95
|
+
# tags for cuke 2, source_tags for cuke 1
|
96
|
+
tags = scenario.send(scenario.respond_to?(:tags) ? :tags : :source_tags)
|
97
|
+
tags.map(&:name).select { |t| t =~ /@datamagic_/ }.map { |t| t.gsub('@datamagic_', '').to_sym }
|
98
|
+
end
|
99
|
+
|
71
100
|
class << self
|
72
101
|
attr_accessor :yml
|
73
102
|
|
data/lib/data_magic/version.rb
CHANGED
data/spec/lib/data_magic_spec.rb
CHANGED
@@ -4,6 +4,21 @@ class UserPage
|
|
4
4
|
include DataMagic
|
5
5
|
end
|
6
6
|
|
7
|
+
class MockScenario
|
8
|
+
attr_accessor :tags
|
9
|
+
def initialize(tags)
|
10
|
+
@tags = tags
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class MockTag
|
15
|
+
attr_reader :name, :line
|
16
|
+
def initialize(name, line)
|
17
|
+
@name = name
|
18
|
+
@line = line
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
7
22
|
describe DataMagic do
|
8
23
|
context "when configuring the yml directory" do
|
9
24
|
before(:each) do
|
@@ -28,9 +43,10 @@ describe DataMagic do
|
|
28
43
|
|
29
44
|
context "when reading yml files" do
|
30
45
|
it "should read files from the config directory" do
|
31
|
-
DataMagic.
|
32
|
-
|
33
|
-
|
46
|
+
DataMagic.yml = nil
|
47
|
+
DataMagic.load("user.yml")
|
48
|
+
data = UserPage.new.data_for "valid"
|
49
|
+
expect(data.keys.sort).to eq(['job','name'])
|
34
50
|
end
|
35
51
|
|
36
52
|
it "should default to reading a file named default.yml" do
|
@@ -48,6 +64,17 @@ describe DataMagic do
|
|
48
64
|
expect(data.keys.sort).to eq(['job','name'])
|
49
65
|
ENV['DATA_MAGIC_FILE'] = nil
|
50
66
|
end
|
67
|
+
|
68
|
+
it 'should merge additional data to the same key if not present in addtional' do
|
69
|
+
DataMagic.yml_directory = 'config/data'
|
70
|
+
data = UserPage.new.data_for 'user/valid', {'job' => 'Overlord'}
|
71
|
+
expect(data['job']).to eq('Overlord')
|
72
|
+
end
|
73
|
+
it 'should merge additional data to resulting hash if present in additional data' do
|
74
|
+
DataMagic.yml_directory = 'config/data'
|
75
|
+
data = UserPage.new.data_for 'user/valid', { 'valid' => {'job' => 'Overlord'} }
|
76
|
+
expect(data['job']).to eq('Overlord')
|
77
|
+
end
|
51
78
|
end
|
52
79
|
|
53
80
|
context "namespaced keys" do
|
@@ -57,4 +84,27 @@ describe DataMagic do
|
|
57
84
|
expect(data.keys.sort).to eq(['job','name'])
|
58
85
|
end
|
59
86
|
end
|
87
|
+
|
88
|
+
context "loading fixtures for cucumber scenarios" do
|
89
|
+
it "loads the fixture for a scenario" do
|
90
|
+
DataMagic.yml_directory = 'config/data'
|
91
|
+
scenario = MockScenario.new([MockTag.new('@tag', 1), MockTag.new('@datamagic_user', 1)])
|
92
|
+
expect(DataMagic).to receive(:load).with('user.yml')
|
93
|
+
DataMagic.load_for_scenario scenario
|
94
|
+
end
|
95
|
+
|
96
|
+
it "uses the last fixture listed for a scenario if multiple exist" do
|
97
|
+
scenario = MockScenario.new([MockTag.new('@fixture_default', 1), MockTag.new('@datamagic_user', 1)])
|
98
|
+
expect(DataMagic).to receive(:load).with('user.yml')
|
99
|
+
DataMagic.load_for_scenario scenario
|
100
|
+
end
|
101
|
+
|
102
|
+
it "allows you to force loading from a different folder without stepping on the global folder" do
|
103
|
+
DataMagic.yml_directory = 'features'
|
104
|
+
scenario = MockScenario.new([MockTag.new('@tag', 1), MockTag.new('@datamagic_user', 1)])
|
105
|
+
expect(DataMagic).to receive(:load).with('user.yml')
|
106
|
+
DataMagic.load_for_scenario scenario, 'config/data'
|
107
|
+
expect(DataMagic.yml_directory).to eq('features')
|
108
|
+
end
|
109
|
+
end
|
60
110
|
end
|
@@ -23,8 +23,12 @@ describe "DataMagic translations" do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should default to use a file named 'default.yml'" do
|
26
|
+
hsh = double('hash')
|
26
27
|
DataMagic.yml_directory = 'test'
|
27
|
-
expect(File).to receive(:read).with("test/default.yml")
|
28
|
+
expect(File).to receive(:read).with("test/default.yml")
|
29
|
+
expect(ERB).to receive(:new).and_return hsh
|
30
|
+
expect(hsh).to receive(:result)
|
31
|
+
expect(YAML).to receive(:load).and_return(test: 'test')
|
28
32
|
expect(DataMagic).to receive(:yml).and_return(nil)
|
29
33
|
expect(DataMagic).to receive(:yml).and_return({'key' => {'field' => 'value'}})
|
30
34
|
expect(example.data_for('key')).to have_field_value 'value'
|
metadata
CHANGED
@@ -1,69 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_magic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0
|
4
|
+
version: '1.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faker
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 1.1.2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 1.1.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: yml_reader
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0.6'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0.6'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 2.12.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.12.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: cucumber
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 1.2.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 1.2.0
|
69
69
|
description: Provides datasets to application stored in YAML files
|
@@ -73,11 +73,11 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
-
- .gitignore
|
77
|
-
- .rspec
|
78
|
-
- .ruby-gemset
|
79
|
-
- .ruby-version
|
80
|
-
- .travis.yml
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".ruby-gemset"
|
79
|
+
- ".ruby-version"
|
80
|
+
- ".travis.yml"
|
81
81
|
- ChangeLog
|
82
82
|
- Gemfile
|
83
83
|
- Guardfile
|
@@ -114,17 +114,17 @@ require_paths:
|
|
114
114
|
- lib
|
115
115
|
required_ruby_version: !ruby/object:Gem::Requirement
|
116
116
|
requirements:
|
117
|
-
- -
|
117
|
+
- - ">="
|
118
118
|
- !ruby/object:Gem::Version
|
119
119
|
version: '0'
|
120
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
requirements: []
|
126
126
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.
|
127
|
+
rubygems_version: 2.5.1
|
128
128
|
signing_key:
|
129
129
|
specification_version: 4
|
130
130
|
summary: Provides datasets to application via YAML files
|