mongo-fixture 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -11,4 +11,5 @@ end
11
11
 
12
12
  group :test do
13
13
  gem "rake"
14
+ gem "pry"
14
15
  end
@@ -64,9 +64,6 @@ Scenario: Misconfigured password field
64
64
  missing: The field
65
65
  """
66
66
  Then the loading of misconfigured fixture should fail
67
- And I should see that the collection was "users"
68
- And I should see that the field was "password"
69
- And I should see that the entry was "wrong_entry"
70
67
  And I should see 0 records in users
71
68
 
72
69
  Scenario: I save the done fixtures so to perform the rollbacks later
@@ -113,3 +110,37 @@ Scenario: References across collections
113
110
  And I load the references fixture
114
111
  Then I should see 1 record in users with username "pepe" and name "Pepe"
115
112
  And I should see 3 records in sessions
113
+
114
+ Scenario: Many-to-many associations
115
+ Given a collection users
116
+ And a collection documents
117
+ And a file "test/fixtures/associations/users.yaml" with:
118
+ """
119
+ johnny:
120
+ name: John
121
+ documents:
122
+ documents: [brief, docs, extra_data]
123
+ susan:
124
+ name: Susan
125
+ documents:
126
+ documents: [brief, resume, docs]
127
+ """
128
+ And a file "test/fixtures/associations/documents.yaml" with:
129
+ """
130
+ brief:
131
+ title: Data
132
+ text: Resumee
133
+ docs:
134
+ title: Doc
135
+ text: Documentation
136
+ extra_data:
137
+ title: Xtra
138
+ text: More and more data
139
+ resume:
140
+ title: CV
141
+ text: Curriculum Vitae
142
+ """
143
+ When I load the associations fixture
144
+ Then I should see 2 records in users
145
+ And I should see 4 records in documents
146
+ And the user named "John" should have in documents the id of the one titled "Doc"
@@ -66,3 +66,10 @@ end
66
66
  Then /^I should see (\d+) records in (\w+)$/ do |amount, collection|
67
67
  @DB[collection].count.should == amount.to_i
68
68
  end
69
+
70
+ And /^the user named "(\w+)" should have in documents the id of the one titled "(\w+)"$/ do
71
+ |name, title|
72
+ user = @DB[:users].find_one :name => name
73
+ document = @DB[:documents].find_one :title => title
74
+ user["documents"].should include document["_id"]
75
+ end
@@ -22,7 +22,7 @@
22
22
  Then /^the loading of (\w+) fixture should fail$/ do |fixture|
23
23
  begin
24
24
  Mongo::Fixture.new fixture.to_sym, @DB
25
- rescue Mongo::Fixture::MissingProcessedValueError => e
25
+ rescue Mongo::Fixture::ReferencedRecordNotFoundError => e
26
26
  @exception = e
27
27
  end
28
28
  end
@@ -1,5 +1,5 @@
1
1
  After do
2
- [:visitors, :aliens, :visits, :users, :sessions].each do |collection|
2
+ [:visitors, :aliens, :visits, :users, :sessions, :documents].each do |collection|
3
3
  @DB[collection].drop
4
4
  end
5
5
  Fast.dir.remove! :test, :fixtures
@@ -0,0 +1,131 @@
1
+ module Mongo
2
+ class Fixture
3
+ # Handles the actual insertion into the database.
4
+ class Inserter
5
+
6
+ # Receives a fixture as argument
7
+ def initialize fixture
8
+ @fixture = fixture
9
+ @inserted = []
10
+ end
11
+
12
+ # Simplifies the hash in order to insert it into the database
13
+ # Resolves external references and flattens the values that provide alternatives
14
+ # @param [Hash] the hash to be processed
15
+ def simplify the_record
16
+ the_returned_hash = {}
17
+ the_record.each do |field, value|
18
+ begin
19
+ value = resolve_field_hash value if value.is_a? Hash
20
+ rescue Mongo::Fixture::ReferencedRecordNotFoundError => e
21
+ @fixture.rollback
22
+ raise e
23
+ end
24
+ the_returned_hash[field] = value
25
+ end
26
+ return the_returned_hash
27
+ end
28
+
29
+ # Returns the correct data for this field resolving the hash
30
+ def resolve_field_hash value
31
+ raise ArgumentError, "Hash expected" unless value.is_a? Hash
32
+ return value[:processed] if value.has_key? :processed
33
+
34
+ intersection = value.keys & @fixture.data.keys
35
+ if intersection.empty?
36
+ raise Mongo::Fixture::ReferencedRecordNotFoundError,
37
+ "This fixture does not include data for the collections [#{value.keys.join(',')}]"
38
+ else
39
+ intersection.each do |collection|
40
+ insert_data_for collection unless data_was_inserted_in? collection
41
+ if value[collection].is_a? Array
42
+ ids = []
43
+ value[collection].each do |element|
44
+ ids.push do_the_resolving collection, element.to_sym
45
+ end
46
+ return ids
47
+ else
48
+ return do_the_resolving collection, value[collection].to_sym
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ # options = value.keys & @fixture.data.keys
55
+ # # If no alternative matches the name of a collection, look for a :processed value
56
+ # if options.empty?
57
+ # unless value.has_key? :processed
58
+ # raise MissingProcessedValueError.new "The processed value to insert into the db is missing from the field '#{field}', aborting", field
59
+ # end
60
+ # the_returned_hash[field] = value[:processed]
61
+ # else
62
+ #
63
+ # # Does any of the options hold a record named after the value of the option?
64
+ # actual_option = options.each do |option|
65
+ # break option if @fixture.data[option].has_key? value[option].to_sym
66
+ # end
67
+ #
68
+ # unless data_was_inserted_in? actual_option
69
+ # insert_data_for actual_option
70
+ # end
71
+ # current_collection = @connection[actual_option]
72
+ # current_data = simplify @fixture.data[actual_option][value[actual_option].to_sym]
73
+ # the_returned_hash[field] = current_collection.find(current_data).first["_id"]
74
+ # end
75
+
76
+
77
+ # Inserts the collection data into the database
78
+ def insert_data_for collection
79
+ # @data.each do |collection, matrix|
80
+ # unless @inserter.data_was_inserted_in? collection
81
+ # matrix.each do |element, values|
82
+ # begin
83
+ # @connection[collection].insert @inserter.simplify values.to_hash
84
+ # rescue MissingProcessedValueError => m
85
+ # rollback
86
+ # raise MissingProcessedValueError, "In record '#{element}' to be inserted into '#{collection}', the processed value of field '#{m.field}' is missing, aborting"
87
+ # end
88
+ # end
89
+ # @inserted << collection
90
+ # end
91
+ return if data_was_inserted_in? collection
92
+ @fixture.data[collection].each do |key, record|
93
+ begin
94
+ @fixture.connection[collection].insert simplify record
95
+ rescue MissingProcessedValueError => m
96
+ @fixture.rollback
97
+ raise MissingProcessedValueError, "In record '#{key}' to be inserted into '#{collection}', the processed value of field '#{m.field}' is missing, aborting"
98
+ end
99
+ end
100
+ @inserted << collection
101
+ end
102
+
103
+ # Returns true if the collection was already inserted
104
+ def data_was_inserted_in? collection
105
+ @inserted.include? collection
106
+ end
107
+
108
+ def inserted
109
+ @inserted
110
+ end
111
+
112
+ # Returns the fixture
113
+ attr_reader :fixture
114
+
115
+ private
116
+ def validate_record_existence collection, record_name
117
+ unless @fixture.data[collection].has_key? record_name
118
+ raise Mongo::Fixture::ReferencedRecordNotFoundError,
119
+ "The collection '#{collection}' doesn't include the record '#{record_name}'"
120
+ end
121
+ end
122
+
123
+ def do_the_resolving collection, record_name
124
+ validate_record_existence collection, record_name
125
+
126
+ record_data = simplify @fixture.data[collection][record_name]
127
+ return @fixture.connection[collection].find(record_data).first["_id"]
128
+ end
129
+ end
130
+ end
131
+ end
@@ -1,5 +1,5 @@
1
1
  module Mongo
2
2
  class Fixture
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
data/lib/mongo-fixture.rb CHANGED
@@ -4,6 +4,7 @@ require "symbolmatrix"
4
4
  require "mongo"
5
5
 
6
6
  require "mongo-fixture/version"
7
+ require "mongo-fixture/inserter"
7
8
 
8
9
  module Mongo
9
10
 
@@ -25,7 +26,7 @@ module Mongo
25
26
  load fixture if fixture
26
27
 
27
28
  @connection = connection if connection
28
- @inserted = []
29
+ @inserter = Inserter.new self
29
30
  push if fixture && connection && option_push
30
31
  end
31
32
 
@@ -62,6 +63,9 @@ module Mongo
62
63
 
63
64
  # Returns the current database connection
64
65
  attr_reader :connection
66
+
67
+ # Returns the inserter for this fixture
68
+ attr_reader :inserter
65
69
 
66
70
  # Sets the connection. Raises an ChangingConnectionIllegal exception if this fixture has already been checked
67
71
  def connection= the_connection
@@ -89,65 +93,24 @@ module Mongo
89
93
  def push
90
94
  check
91
95
 
92
- @data.each do |collection, matrix|
93
- unless data_was_inserted_in? collection
94
- matrix.each do |element, values|
95
- begin
96
- @connection[collection].insert simplify values.to_hash
97
- rescue MissingProcessedValueError => m
98
- rollback
99
- raise MissingProcessedValueError, "In record '#{element}' to be inserted into '#{collection}', the processed value of field '#{m.field}' is missing, aborting"
100
- end
101
- end
102
- @inserted << collection
103
- end
96
+ data.each_key do |collection|
97
+ @inserter.insert_data_for collection
104
98
  end
99
+ # @data.each do |collection, matrix|
100
+ # unless @inserter.data_was_inserted_in? collection
101
+ # matrix.each do |element, values|
102
+ # begin
103
+ # @connection[collection].insert @inserter.simplify values.to_hash
104
+ # rescue MissingProcessedValueError => m
105
+ # rollback
106
+ # raise MissingProcessedValueError, "In record '#{element}' to be inserted into '#{collection}', the processed value of field '#{m.field}' is missing, aborting"
107
+ # end
108
+ # end
109
+ # @inserted << collection
110
+ # end
111
+ # end
105
112
  end
106
-
107
- # Simplifies the hash in order to insert it into the database
108
- # Resolves external references and flattens the values that provide alternatives
109
- # @param [Hash] the hash to be processed
110
- def simplify the_hash
111
- the_returned_hash = {}
112
- the_hash.each do |key, value|
113
- if value.is_a? Hash
114
-
115
- # If no alternative matches the name of a collection, look for a :processed value
116
- if (value.keys & @data.keys).empty?
117
- unless value.has_key? :processed
118
- raise MissingProcessedValueError.new "The processed value to insert into the db is missing from the field '#{key}', aborting", key
119
- end
120
- the_returned_hash[key] = value[:processed]
121
- else
122
-
123
- # Does any of the options hold a record named after the value of the option?
124
- options = value.keys & @data.keys
125
- actual_option = options.each do |option|
126
- break option if @data[option].has_key? value[option].to_sym
127
- end
128
-
129
- unless data_was_inserted_in? actual_option
130
- insert_data_for actual_option
131
- end
132
- current_collection = @connection[actual_option]
133
- current_data = simplify @data[actual_option][value[actual_option].to_sym]
134
- the_returned_hash[key] = current_collection.find(current_data).first["_id"]
135
- end
136
- else
137
- the_returned_hash[key] = value
138
- end
139
- end
140
- return the_returned_hash
141
- end
142
-
143
- # Inserts the collection data into the database
144
- def insert_data_for collection
145
- @data[collection].each do |key, record|
146
- @connection[collection].insert simplify record
147
- end
148
- @inserted << collection
149
- end
150
-
113
+
151
114
  # Empties the collections, only if they were empty to begin with
152
115
  def rollback
153
116
  begin
@@ -167,6 +130,7 @@ module Mongo
167
130
  class MissingConnectionError < StandardError; end
168
131
  class ChangingConnectionIllegal < StandardError; end
169
132
  class RollbackIllegalError < StandardError; end
133
+ class ReferencedRecordNotFoundError < StandardError; end
170
134
  class MissingProcessedValueError < StandardError
171
135
  attr_accessor :field
172
136
  def initialize message, field = nil
@@ -175,9 +139,6 @@ module Mongo
175
139
  end
176
140
  end
177
141
 
178
- private
179
- def data_was_inserted_in? collection
180
- @inserted.include? collection
181
- end
142
+
182
143
  end
183
144
  end
@@ -0,0 +1,429 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongo::Fixture::Inserter do
4
+ describe ".new" do
5
+ it "should accept an argument" do
6
+ Mongo::Fixture::Inserter.new double "fixture"
7
+ end
8
+ end
9
+
10
+ describe "#simplify" do
11
+ context "when receiving a multidimensional hash containing a field with raw and processed" do
12
+ it "should convert it in a simple hash using the processed value as replacement" do
13
+ base_hash = {
14
+ :name => "Jane",
15
+ :band => "Witherspoons",
16
+ :pass => {
17
+ :raw => "secret",
18
+ :processed => "53oih7fhjdgj3f8="
19
+ },
20
+ :email => {
21
+ :raw => "Jane@gmail.com ",
22
+ :processed => "jane@gmail.com"
23
+ }
24
+ }
25
+ fixture = double 'fixture', :data => {}
26
+
27
+ ins = Mongo::Fixture::Inserter.new fixture
28
+ simplified = ins.simplify base_hash
29
+ simplified.should == {
30
+ :name => "Jane",
31
+ :band => "Witherspoons",
32
+ :pass => "53oih7fhjdgj3f8=",
33
+ :email => "jane@gmail.com"
34
+ }
35
+ end
36
+ end
37
+
38
+ context "the multidimensional array is missing the processed part of the field" do
39
+ before do
40
+ @base_hash = {
41
+ :name => "Jane",
42
+ :pass => {
43
+ :raw => "secret",
44
+ :not_processed => "53oih7fhjdgj3f8="
45
+ },
46
+ :email => {
47
+ :raw => "Jane@gmail.com ",
48
+ :processed => "jane@gmail.com"
49
+ }
50
+ }
51
+ end
52
+
53
+ it "should raise an exception" do
54
+ fixture = double 'fixture', :data => {}, :rollback => nil
55
+ ins = Mongo::Fixture::Inserter.new fixture
56
+ expect { ins.simplify @base_hash
57
+ }.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError,
58
+ "This fixture does not include data for the collections [raw,not_processed]"
59
+ end
60
+
61
+ it "should call #resolve_field_hash with the data hash" do
62
+ fixture = double 'fixture', :data => {}
63
+ ins = Mongo::Fixture::Inserter.new fixture
64
+ ins.should_receive(:resolve_field_hash).with :raw => "secret", :not_processed => "53oih7fhjdgj3f8="
65
+ ins.should_receive(:resolve_field_hash).with :raw => "Jane@gmail.com ", :processed => "jane@gmail.com"
66
+ ins.simplify @base_hash
67
+ end
68
+ end
69
+ end
70
+
71
+ describe "#resolve_field_hash" do
72
+ context "the data is not a hash" do
73
+ it "should raise a wrong argument exception" do
74
+ inserter = Mongo::Fixture::Inserter.new double 'fixture'
75
+ expect { inserter.resolve_field_hash "not a hash"
76
+ }.to raise_error ArgumentError, "Hash expected"
77
+ end
78
+ end
79
+
80
+ context "at leasts one key matches a collection name" do
81
+ context "no record name matches an existing record in target collection" do
82
+ before do
83
+ @data = {
84
+ :users => { :pepe => { :name => "Jose" } },
85
+ :comments => { :demo => { :text => "Hola", :user => { :users => "lula" } } }
86
+ }
87
+ end
88
+
89
+ it "should raise an exception" do
90
+ coll = double "collection", :insert => nil
91
+ connection = double "connection", :[] => coll
92
+ fixture = double 'fixture', :data => @data, :connection => connection
93
+ inserter = Mongo::Fixture::Inserter.new fixture
94
+ expect { inserter.resolve_field_hash :users => "lula"
95
+ }.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError,
96
+ "The collection 'users' doesn't include the record 'lula'"
97
+ end
98
+ end
99
+ end
100
+
101
+ context "many to many associations" do
102
+ before do
103
+ @data = {
104
+ :users => {
105
+ :pepe => {
106
+ :username => "jose",
107
+ :pass => "secret"
108
+ },
109
+ :lula => {
110
+ :username => "lula",
111
+ :pass => {
112
+ :raw => "moresecret",
113
+ :processed => "asdfhjlueiwywhetkjtret66666666"
114
+ }
115
+ }
116
+ },
117
+ :admins => {
118
+ :superadmin => {
119
+ :login => "super"
120
+ }
121
+ },
122
+ :comments => {
123
+ :demo => {
124
+ :text => "Simple text",
125
+ :user => {
126
+ :users => [ "pepe", "lula" ],
127
+ :admins => "superadmin"
128
+ }
129
+ },
130
+ :another => {
131
+ :text => "Die another day",
132
+ :user => {
133
+ :users => "pepe"
134
+ }
135
+ }
136
+ }
137
+ }
138
+ end
139
+
140
+ it "should send to each comment the respective ids" do
141
+ pepe_id = "ladjsfljasf"
142
+ lula_id = "gsuyhkasrhte"
143
+ superadmin_id = "323454"
144
+
145
+ lula_record_data = double "lula record data"
146
+ lula_record_data.should_receive( :[] ).with("_id").and_return lula_id
147
+ lula_record = double "lula record", :first => lula_record_data
148
+ pepe_record_data = double "pepe record data"
149
+ pepe_record_data.should_receive(:[]).twice.with("_id").and_return(pepe_id)
150
+ pepe_record = double "pepe record"
151
+ pepe_record.should_receive(:first).twice.and_return pepe_record_data
152
+ admin_record_data = double "admin record data"
153
+ admin_record_data.should_receive(:[]).with("_id").and_return superadmin_id
154
+ admin_record = double "admin record", :first => admin_record_data
155
+ admins = double "admins", :insert => nil
156
+ admins.should_receive(:find).with( :login => "super" ).and_return admin_record
157
+ users = double 'users', :insert => nil
158
+ users.should_receive(:find).twice.with( :username => "jose", :pass => "secret" ).and_return pepe_record
159
+ users.should_receive(:find).with( :username => "lula", :pass => "asdfhjlueiwywhetkjtret66666666" ).and_return lula_record
160
+ connection = double 'connection'
161
+ connection.should_receive(:[]).exactly(5).times.with(:users).and_return users
162
+ connection.should_receive(:[]).twice.with(:admins).and_return admins
163
+ fixture = double 'fixture', :data => @data, :connection => connection
164
+ inserter = Mongo::Fixture::Inserter.new fixture
165
+ inserter.resolve_field_hash( :users => [ "pepe", "lula" ] ).should == [ pepe_id, lula_id ]
166
+ inserter.resolve_field_hash( :admins => "superadmin" ).should == superadmin_id
167
+ inserter.resolve_field_hash( :users => "pepe" ).should == pepe_id
168
+ end
169
+
170
+ context "some user is missing" do
171
+ before do
172
+ @data = {
173
+ :users => {
174
+ :pepe => {
175
+ :username => "jose",
176
+ :pass => "secret"
177
+ },
178
+ :lula => {
179
+ :username => "lula",
180
+ :pass => {
181
+ :raw => "moresecret",
182
+ :processed => "asdfhjlueiwywhetkjtret66666666"
183
+ }
184
+ }
185
+ },
186
+ :comments => {
187
+ :demo => {
188
+ :text => "Simple text",
189
+ :user => {
190
+ :users => [ "pepe", "lula", "noexiste" ],
191
+ }
192
+ },
193
+ }
194
+ }
195
+ end
196
+
197
+ it "should raise an exception" do
198
+ pepe_id = "ladjsfljasf"
199
+ lula_id = "gsuyhkasrhte"
200
+
201
+ lula_data = double "lula_data"
202
+ lula_data.should_receive(:[]).with("_id").and_return lula_id
203
+ lula = double "lula", :first => lula_data
204
+ pepe_data = double "pepe_data"
205
+ pepe_data.should_receive(:[]).with("_id").and_return pepe_id
206
+ pepe = double "pepe", :first => pepe_data
207
+ users = double "users", :insert => nil
208
+ users.should_receive(:find).with( :username => "jose", :pass => "secret" ).and_return pepe
209
+ users.should_receive(:find).with( :username => "lula", :pass => "asdfhjlueiwywhetkjtret66666666" ).and_return lula
210
+ connection = double "connection"
211
+ connection.should_receive(:[]).exactly(4).times.with(:users).and_return users
212
+ fixture = double "fixture", :data => @data, :connection => connection
213
+ inserter = Mongo::Fixture::Inserter.new fixture
214
+ expect { inserter.resolve_field_hash( :users => [ "pepe", "lula", "noexiste" ] )
215
+ }.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError,
216
+ "The collection 'users' doesn't include the record 'noexiste'"
217
+ end
218
+ end
219
+ end
220
+
221
+ context "reference from one record to another" do
222
+ context "the key matches a collection name" do
223
+ context "the referenced record exists" do
224
+ it "should call #data_was_inserted_in? with the collections name" do
225
+ @data = {
226
+ :users => { :pepe => { :name => "Jose" } },
227
+ :comments => { :demo => { :text => "hola", :user => {:users => "pepe" } } }
228
+ }
229
+
230
+ pepe_data = double "pepe_data", :[] => nil
231
+ pepe = double "pepe", :insert => nil
232
+ pepe.should_receive(:find).with(:name => "Jose").and_return double"pepeee", :first => pepe_data
233
+ connection = double "connection"
234
+ connection.should_receive(:[]).twice.with(:users).and_return pepe
235
+ fixture = double "fixture", :data => @data, :connection => connection
236
+ inserter = Mongo::Fixture::Inserter.new fixture
237
+ inserter.should_receive(:data_was_inserted_in?).twice.with(:users)
238
+ inserter.resolve_field_hash(:users => "pepe")
239
+ end
240
+
241
+ context "the data wasn't inserted into users" do
242
+ it "should call #insert_data_for with the collections name" do
243
+ @data = {
244
+ :users => { :pepe => { :name => "Jose" } },
245
+ :comments => { :demo => { :text => "hola", :user => {:users => "pepe" } } }
246
+ }
247
+
248
+ pepe_data = double "pepe_data", :[] => nil
249
+ pepe = double "pepe"
250
+ pepe.should_receive(:find).with(:name => "Jose").and_return double"pepeee", :first => pepe_data
251
+ connection = double "connection"
252
+ connection.should_receive(:[]).with(:users).and_return pepe
253
+ fixture = double "fixture", :data => @data, :connection => connection
254
+ inserter = Mongo::Fixture::Inserter.new fixture
255
+ inserter.should_receive(:insert_data_for).with(:users)
256
+ inserter.resolve_field_hash(:users => "pepe")
257
+ end
258
+ end
259
+
260
+ context "the data was inserted into users" do
261
+ it "should NOT call #insert_data_for with the collections name" do
262
+ @data = {
263
+ :users => { :pepe => { :name => "Jose" } },
264
+ :comments => { :demo => { :text => "hola", :user => {:users => "pepe" } } }
265
+ }
266
+
267
+ pepe_data = double "pepe_data", :[] => nil
268
+ pepe = double "pepe"
269
+ pepe.should_receive(:find).with(:name => "Jose").and_return double"pepeee", :first => pepe_data
270
+ connection = double "connection"
271
+ connection.should_receive(:[]).with(:users).and_return pepe
272
+ fixture = double "fixture", :data => @data, :connection => connection
273
+ inserter = Mongo::Fixture::Inserter.new fixture
274
+ inserter.should_receive(:insert_data_for).with(:users)
275
+ inserter.resolve_field_hash(:users => "pepe")
276
+ end
277
+ end
278
+
279
+
280
+ it "should return the object id of the referenced record" do
281
+ @data = {
282
+ :users => { :pepe => { :name => "Jose" } },
283
+ :comments => { :demo => { :text => "hola", :user => {:users => "pepe" } } }
284
+ }
285
+
286
+ actual_record = double "actual_record"
287
+ actual_record.should_receive(:[]).with("_id").and_return "un id"
288
+ record_from_db = double 'record from db', :first => actual_record
289
+
290
+ collection = double 'collection', :insert => nil
291
+ collection.should_receive(:find).with(:name => "Jose").and_return record_from_db
292
+ fixture = double 'fixture', :data => @data, :connection => double( :[] => collection)
293
+ inserter = Mongo::Fixture::Inserter.new fixture
294
+ inserter.resolve_field_hash( :users => "pepe" ).should == "un id"
295
+ end
296
+ end
297
+
298
+ context "the referenced record exists, but have complex field values" do
299
+ it "should return the object id of the referenced record" do
300
+ @data = {
301
+ :users => { :pepe => { :name => "Jose", :pass => { :raw => "a", :processed => "234" } } },
302
+ :comments => { :demo => { :text => "hola", :user => {:users => "pepe" } } }
303
+ }
304
+
305
+ actual_record = double "actual_record"
306
+ actual_record.should_receive(:[]).with("_id").and_return "un id"
307
+ record_from_db = double 'record from db', :first => actual_record
308
+
309
+ collection = double 'collection', :insert => nil
310
+ collection.should_receive(:find).with(:name => "Jose", :pass => "234").and_return record_from_db
311
+ fixture = double 'fixture', :data => @data, :connection => double( :[] => collection)
312
+ inserter = Mongo::Fixture::Inserter.new fixture
313
+ inserter.resolve_field_hash( :users => "pepe" ).should == "un id"
314
+ end
315
+ end
316
+
317
+
318
+ context "the referenced record does not exist" do
319
+ it "should raise an error" do
320
+ data = { :users => "user" }
321
+ inserter = Mongo::Fixture::Inserter.new double 'fixture', :data => {}
322
+ expect { inserter.resolve_field_hash data
323
+ }.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError,
324
+ "This fixture does not include data for the collections [users]"
325
+ end
326
+ end
327
+ end
328
+
329
+ context "no key matches a collection name" do
330
+ it "should raise an error" do
331
+ data = { :users => "user", :comments => "comment"}
332
+ fixture = stub 'fixture', :data => { :collection => "", :another_collection => ""}
333
+ inserter = Mongo::Fixture::Inserter.new fixture
334
+ expect { inserter.resolve_field_hash data
335
+ }.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError,
336
+ "This fixture does not include data for the collections [users,comments]"
337
+ end
338
+ end
339
+ end
340
+
341
+ context "there is a :processed key" do
342
+ it "should return the value of the :processed field" do
343
+ data = { :raw => "hello", :processed => "hash" }
344
+ inserter = Mongo::Fixture::Inserter.new double "fixture"
345
+ inserter.resolve_field_hash(data).should == "hash"
346
+ end
347
+ end
348
+ end
349
+
350
+ describe "#data_was_inserted_in?" do
351
+ context "there is a simple fixture and a collection has been inserted by this fixture" do
352
+ before do
353
+ Fast.file.write "test/fixtures/test/users.yaml", "pepe: { user: pepe }"
354
+ end
355
+
356
+ it "should return true" do
357
+ database = double 'database'
358
+ coll = double 'collection', :count => 0, :insert => nil
359
+ database.stub :[] => coll
360
+ fix = Mongo::Fixture.new :test, database
361
+ ins = fix.inserter
362
+ ins.data_was_inserted_in?(:users).should === true
363
+ end
364
+
365
+ after do
366
+ Fast.dir.remove! :test
367
+ end
368
+ end
369
+
370
+ context "there is a simple fixture and a collection was inserted but not this" do
371
+ before do
372
+ Fast.file.write "test/fixtures/test/users.yaml", "pepe: { user: pepe }"
373
+ end
374
+
375
+ it "should return false" do
376
+ database = double 'database'
377
+ coll = double 'collection', :count => 0, :insert => nil
378
+ database.stub :[] => coll
379
+ fix = Mongo::Fixture.new :test, database
380
+ ins = Mongo::Fixture::Inserter.new fix
381
+ def ins.loot
382
+ data_was_inserted_in?(:comment).should === false
383
+ end
384
+ ins.loot
385
+ end
386
+
387
+ after do
388
+ Fast.dir.remove! :test
389
+ end
390
+ end
391
+ end
392
+
393
+ describe "#insert_data_for" do
394
+ context "provided the collection has data in the fixture" do
395
+ it "should insert the data of the collection using the fixture's connection" do
396
+ @data = { :users => { :pepe => { :name => "Jose" } } }
397
+ collection = double "collection"
398
+ collection.should_receive(:insert).with( :name => "Jose" )
399
+ connection = double 'connection'
400
+ connection.should_receive(:[]).with(:users).and_return collection
401
+ fixture = double "fixture", :data => @data
402
+ fixture.should_receive(:connection).and_return connection
403
+ inserter = Mongo::Fixture::Inserter.new fixture
404
+ inserter.insert_data_for :users
405
+ end
406
+
407
+ it "should add the collection as inserted" do
408
+ @data = { :users => { :pepe => { :name => "Jose" } } }
409
+ collection = double "collection"
410
+ collection.should_receive(:insert).with( :name => "Jose" )
411
+ connection = double 'connection'
412
+ connection.should_receive(:[]).with(:users).and_return collection
413
+ fixture = double "fixture", :data => @data
414
+ fixture.should_receive(:connection).and_return connection
415
+ inserter = Mongo::Fixture::Inserter.new fixture
416
+ inserter.insert_data_for :users
417
+ inserter.inserted.should include :users
418
+ end
419
+ end
420
+ end
421
+
422
+ describe "#fixture" do
423
+ it "should return the fixture" do
424
+ fixture = double "fixture"
425
+ inserter = Mongo::Fixture::Inserter.new fixture
426
+ inserter.fixture.should === fixture
427
+ end
428
+ end
429
+ end
@@ -1,4 +1,4 @@
1
- require "mongo-fixture"
1
+ require "spec_helper"
2
2
 
3
3
  describe Mongo::Fixture do
4
4
  describe ".path" do
@@ -84,7 +84,7 @@ describe Mongo::Fixture do
84
84
  end
85
85
  end
86
86
  end
87
-
87
+
88
88
  describe "#force_checked!" do
89
89
  it "check should return true and should not call [] in the passed database" do
90
90
  database = stub 'database'
@@ -96,7 +96,7 @@ describe Mongo::Fixture do
96
96
  fix.check.should === true
97
97
  end
98
98
  end
99
-
99
+
100
100
  describe "#[]" do
101
101
  context "a valid fixture has been loaded" do
102
102
  before do
@@ -122,7 +122,7 @@ describe Mongo::Fixture do
122
122
  end
123
123
  end
124
124
  end
125
-
125
+
126
126
  describe "#method_missing" do
127
127
  context "a valid fixture has been loaded" do
128
128
  context "a collection key is passed" do
@@ -153,7 +153,7 @@ describe Mongo::Fixture do
153
153
  }.to raise_error NoMethodError
154
154
  end
155
155
  end
156
-
156
+
157
157
  describe "#fixtures_path" do
158
158
  it "should call Mongo::Fixture.path" do
159
159
  Mongo::Fixture.should_receive :path
@@ -288,7 +288,7 @@ describe Mongo::Fixture do
288
288
  fix.connection.should === connection
289
289
  end
290
290
  end
291
-
291
+
292
292
  describe "#connection=" do
293
293
  it "sets the connection" do
294
294
  fix = Mongo::Fixture.new
@@ -318,7 +318,7 @@ describe Mongo::Fixture do
318
318
  end
319
319
  end
320
320
  end
321
-
321
+
322
322
  describe "#data" do
323
323
  context "a fixture has been loaded" do
324
324
  before do
@@ -343,7 +343,7 @@ describe Mongo::Fixture do
343
343
  end
344
344
  end
345
345
  end
346
-
346
+
347
347
  describe "#push" do
348
348
  it "should call #check" do
349
349
  fix = Mongo::Fixture.new
@@ -407,8 +407,8 @@ describe Mongo::Fixture do
407
407
  database = double 'database', :[] => stub( 'collection', :count => 0, :drop => nil )
408
408
  fix = Mongo::Fixture.new :test, database, false
409
409
  expect { fix.push
410
- }.to raise_error Mongo::Fixture::MissingProcessedValueError,
411
- "In record 'hey' to be inserted into 'users', the processed value of field 'pass' is missing, aborting"
410
+ }.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError,
411
+ "This fixture does not include data for the collections [raw]"
412
412
  end
413
413
 
414
414
 
@@ -417,14 +417,14 @@ describe Mongo::Fixture do
417
417
  fix = Mongo::Fixture.new :test, database, false
418
418
  fix.should_receive :rollback
419
419
  expect { fix.push
420
- }.to raise_error Mongo::Fixture::MissingProcessedValueError
420
+ }.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError
421
421
  end
422
422
 
423
423
  after do
424
424
  Fast.dir.remove! :test
425
425
  end
426
426
  end
427
-
427
+
428
428
  context "a fixture with a field with one alternative name matches a collection name" do
429
429
  context "the alternative value matches a record and in the collection" do
430
430
  before do
@@ -433,21 +433,26 @@ describe Mongo::Fixture do
433
433
  end
434
434
 
435
435
  it "should insert the comment so that the comment user value matches the '_id' of the user" do
436
- database = double 'database'
437
- comm = double 'comments', :count => 0, :drop => nil
438
- comm.should_receive( :insert ).with( :user => "un id", :text => "FLAME" )
436
+ comments = double 'comments', :count => 0, :drop => nil
437
+ comments.should_receive( :insert ).with( :user => "un id", :text => "FLAME" )
438
+
439
439
  record = stub 'record'
440
440
  record.should_receive( :[] ).with( "_id" ).and_return "un id"
441
+
441
442
  usrs = double 'users', :count => 0, :find => stub( :first => record ), :drop => nil, :insert => nil
443
+
444
+ database = double 'database'
442
445
  database.stub :[] do |coll|
443
446
  case coll
444
447
  when :users
445
448
  usrs
446
449
  when :comments
447
- comm
450
+ comments
448
451
  end
449
452
  end
450
- fix = Mongo::Fixture.new :test, database
453
+
454
+ fix = Mongo::Fixture.new :test, database, false
455
+ fix.push
451
456
  end
452
457
 
453
458
  context "the collection is ordered so that the comment collection comes before the users one" do
@@ -473,121 +478,25 @@ describe Mongo::Fixture do
473
478
  fix.push
474
479
  end
475
480
  end
476
-
481
+
477
482
  after do
478
483
  Fast.dir.remove! :test
479
484
  end
480
- end
481
- end
482
- end
483
-
484
- describe "#data_was_inserted_in?" do
485
- it "should be private" do
486
- fix = Mongo::Fixture.new
487
- fix.private_methods(false).should include :data_was_inserted_in?
488
- end
489
-
490
- context "there is a simple fixture and a collection has been inserted by this fixture" do
491
- before do
492
- Fast.file.write "test/fixtures/test/users.yaml", "pepe: { user: pepe }"
493
485
  end
494
-
495
- it "should return true" do
496
- database = double 'database'
497
- coll = double 'collection', :count => 0, :insert => nil
498
- database.stub :[] => coll
499
- fix = Mongo::Fixture.new :test, database
500
- def fix.loot
501
- data_was_inserted_in?(:users).should === true
502
- end
503
- fix.loot
504
- end
505
-
506
- after do
507
- Fast.dir.remove! :test
508
- end
509
- end
510
-
511
- context "there is a simple fixture and a collection was inserted but not this" do
512
- before do
513
- Fast.file.write "test/fixtures/test/users.yaml", "pepe: { user: pepe }"
514
- end
515
-
516
- it "should return false" do
517
- database = double 'database'
518
- coll = double 'collection', :count => 0, :insert => nil
519
- database.stub :[] => coll
520
- fix = Mongo::Fixture.new :test, database
521
- def fix.loot
522
- data_was_inserted_in?(:comment).should === false
523
- end
524
- fix.loot
525
- end
526
-
527
- after do
528
- Fast.dir.remove! :test
529
- end
530
- end
486
+ end
531
487
  end
532
-
533
- # This should go in a dependency, pending refactoring TODO
534
- describe "#simplify" do
535
- context "when receiving a multidimensional hash containing a field with raw and processed" do
536
- it "should convert it in a simple hash using the processed value as replacement" do
537
- base_hash = {
538
- :name => "Jane",
539
- :band => "Witherspoons",
540
- :pass => {
541
- :raw => "secret",
542
- :processed => "53oih7fhjdgj3f8="
543
- },
544
- :email => {
545
- :raw => "Jane@gmail.com ",
546
- :processed => "jane@gmail.com"
547
- }
548
- }
549
-
550
- fix = Mongo::Fixture.new
551
- def fix.stub_data
552
- @data = {}
553
- end
554
- fix.stub_data
555
- simplified = fix.simplify base_hash
556
- simplified.should == {
557
- :name => "Jane",
558
- :band => "Witherspoons",
559
- :pass => "53oih7fhjdgj3f8=",
560
- :email => "jane@gmail.com"
561
- }
562
- end
563
- end
564
-
565
- context "the multidimensional array is missing the processed part of the field" do
566
- it "should raise an exception" do
567
- base_hash = {
568
- :name => "Jane",
569
- :pass => {
570
- :raw => "secret",
571
- :not_processed => "53oih7fhjdgj3f8="
572
- },
573
- :email => {
574
- :raw => "Jane@gmail.com ",
575
- :processed => "jane@gmail.com"
576
- }
577
- }
578
-
579
- fix = Mongo::Fixture.new
580
- def fix.stub_data
581
- @data = {}
582
- end
583
- fix.stub_data
584
- expect { fix.simplify base_hash
585
- }.to raise_error Mongo::Fixture::MissingProcessedValueError,
586
- "The processed value to insert into the db is missing from the field 'pass', aborting"
587
- end
488
+
489
+ describe "#inserter" do
490
+ it "should return an inserter with a reference to this" do
491
+ database = double 'database'
492
+ Mongo::Fixture.any_instance.stub :load
493
+ fixture = Mongo::Fixture.new :test, database, false
494
+ inserter = fixture.inserter
495
+ inserter.should be_a Mongo::Fixture::Inserter
496
+ inserter.fixture.should === fixture
588
497
  end
589
498
  end
590
-
499
+
591
500
  describe "#rollback" do
592
501
  it "should check" do
593
502
  fix = Mongo::Fixture.new
@@ -632,40 +541,4 @@ describe Mongo::Fixture do
632
541
  end
633
542
  end
634
543
  end
635
-
636
- context "two collections are to be inserted with references" do
637
- before do
638
- Fast.file.write "test/fixtures/references/users.yaml", "pepe:
639
- username: pepe
640
- password:
641
- raw: secreto
642
- processed: 252db48960f032db4bb604bc26f97106fa85ff88dedef3a28671b6bcd9f9644bf90d7e444d587c9351dfa237a6fc8fe38641a8469d084a166c7807d9c6564860
643
- name: Pepe"
644
- Fast.file.write "test/fixtures/references/sessions.yaml", "14_horas:
645
- user:
646
- users: pepe
647
- time: 2012-07-30T14:02:40-03:00
648
- y_tres_minutos:
649
- user:
650
- users: pepe
651
- time: 2012-07-30T14:03:40-03:00
652
- y_cuatro_minutos:
653
- user:
654
- users: pepe
655
- time: 2012-07-30T14:04:40-03:00"
656
- end
657
-
658
- it "should not fail!" do
659
- collection = double 'coll', :count => 0, :insert => nil
660
- database = double 'database'
661
- database.stub :[] do |argument|
662
- collection
663
- end
664
- Mongo::Fixture.new :references, database
665
- end
666
-
667
- after do
668
- Fast.dir.remove! :test
669
- end
670
- end
671
544
  end
@@ -0,0 +1,2 @@
1
+ require "mongo-fixture"
2
+ require "pry"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo-fixture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-14 00:00:00.000000000 Z
12
+ date: 2012-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo
@@ -97,10 +97,12 @@ files:
97
97
  - features/support/env.rb
98
98
  - features/support/hooks.rb
99
99
  - lib/mongo-fixture.rb
100
+ - lib/mongo-fixture/inserter.rb
100
101
  - lib/mongo-fixture/version.rb
101
102
  - mongo-fixture.gemspec
102
- - spec/mongo/cross_references_spec.rb
103
+ - spec/mongo/fixture/inserter_spec.rb
103
104
  - spec/mongo/fixture_spec.rb
105
+ - spec/spec_helper.rb
104
106
  homepage: http://github.com/Fetcher/mongo-fixture
105
107
  licenses: []
106
108
  post_install_message:
@@ -115,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
117
  version: '0'
116
118
  segments:
117
119
  - 0
118
- hash: -277568515
120
+ hash: -421222579
119
121
  required_rubygems_version: !ruby/object:Gem::Requirement
120
122
  none: false
121
123
  requirements:
@@ -124,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
126
  version: '0'
125
127
  segments:
126
128
  - 0
127
- hash: -277568515
129
+ hash: -421222579
128
130
  requirements: []
129
131
  rubyforge_project:
130
132
  rubygems_version: 1.8.24
@@ -139,5 +141,6 @@ test_files:
139
141
  - features/stepdefs/play_around_with_fixtures/misconfigured_password_field.rb
140
142
  - features/support/env.rb
141
143
  - features/support/hooks.rb
142
- - spec/mongo/cross_references_spec.rb
144
+ - spec/mongo/fixture/inserter_spec.rb
143
145
  - spec/mongo/fixture_spec.rb
146
+ - spec/spec_helper.rb
@@ -1,39 +0,0 @@
1
- require "mongo-fixture"
2
-
3
- describe Mongo::Fixture do
4
- context "two collections are to be inserted with references" do
5
- before do
6
- Fast.file.write "test/fixtures/references/users.yaml", "pepe:
7
- username: pepe
8
- password:
9
- raw: secreto
10
- processed: 252db48960f032db4bb604bc26f97106fa85ff88dedef3a28671b6bcd9f9644bf90d7e444d587c9351dfa237a6fc8fe38641a8469d084a166c7807d9c6564860
11
- name: Pepe"
12
- Fast.file.write "test/fixtures/references/sessions.yaml", "14_horas:
13
- user:
14
- users: pepe
15
- time: 2012-07-30T14:02:40-03:00
16
- y_tres_minutos:
17
- user:
18
- users: pepe
19
- time: 2012-07-30T14:03:40-03:00
20
- y_cuatro_minutos:
21
- user:
22
- users: pepe
23
- time: 2012-07-30T14:04:40-03:00"
24
- end
25
-
26
- it "should not fail!" do
27
- collection = double 'coll', :count => 0, :insert => nil
28
- database = double 'database'
29
- database.stub :[] do |argument|
30
- collection
31
- end
32
- Mongo::Fixture.new :references, database
33
- end
34
-
35
- after do
36
- Fast.dir.remove! :test
37
- end
38
- end
39
- end