mongo-fixture 0.0.1 → 0.0.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.
- data/lib/mongo-fixture.rb +37 -6
- data/lib/mongo-fixture/version.rb +1 -1
- data/spec/mongo/fixture_spec.rb +114 -1
- metadata +4 -4
data/lib/mongo-fixture.rb
CHANGED
@@ -25,6 +25,7 @@ module Mongo
|
|
25
25
|
load fixture if fixture
|
26
26
|
|
27
27
|
@connection = connection if connection
|
28
|
+
@inserted = []
|
28
29
|
push if fixture && connection && option_push
|
29
30
|
end
|
30
31
|
|
@@ -92,6 +93,8 @@ module Mongo
|
|
92
93
|
matrix.each do |element, values|
|
93
94
|
begin
|
94
95
|
@connection[collection].insert simplify values.to_hash
|
96
|
+
@inserted ||= Array.new
|
97
|
+
@inserted << collection
|
95
98
|
rescue MissingProcessedValueError => m
|
96
99
|
rollback
|
97
100
|
raise MissingProcessedValueError, "In record '#{element}' to be inserted into '#{collection}', the processed value of field '#{m.field}' is missing, aborting"
|
@@ -101,17 +104,32 @@ module Mongo
|
|
101
104
|
end
|
102
105
|
|
103
106
|
# Simplifies the hash in order to insert it into the database
|
104
|
-
#
|
105
|
-
# specially now that is repeated here and in Sequel::Fixture)
|
107
|
+
# Resolves external references and flattens the values that provide alternatives
|
106
108
|
# @param [Hash] the hash to be processed
|
107
109
|
def simplify the_hash
|
108
110
|
the_returned_hash = {}
|
109
111
|
the_hash.each do |key, value|
|
110
112
|
if value.is_a? Hash
|
111
|
-
|
112
|
-
|
113
|
+
|
114
|
+
# If no alternative matches the name of a collection, look for a :processed value
|
115
|
+
if (value.keys & @data.keys).empty?
|
116
|
+
unless value.has_key? :processed
|
117
|
+
raise MissingProcessedValueError.new "The processed value to insert into the db is missing from the field '#{key}', aborting", key
|
118
|
+
end
|
119
|
+
the_returned_hash[key] = value[:processed]
|
120
|
+
else
|
121
|
+
|
122
|
+
# Does any of the options hold a record named after the value of the option?
|
123
|
+
options = value.keys & @data.keys
|
124
|
+
actual_option = options.each do |option|
|
125
|
+
break option if @data[option].has_key? value[option].to_sym
|
126
|
+
end
|
127
|
+
|
128
|
+
unless data_was_inserted_in? actual_option
|
129
|
+
insert_data_for actual_option
|
130
|
+
end
|
131
|
+
the_returned_hash[key] = @connection[actual_option].find( @data[actual_option][value[actual_option].to_sym] ).first[:_id]
|
113
132
|
end
|
114
|
-
the_returned_hash[key] = value[:processed]
|
115
133
|
else
|
116
134
|
the_returned_hash[key] = value
|
117
135
|
end
|
@@ -119,6 +137,14 @@ module Mongo
|
|
119
137
|
return the_returned_hash
|
120
138
|
end
|
121
139
|
|
140
|
+
# Inserts the collection data into the database
|
141
|
+
def insert_data_for collection
|
142
|
+
@data[collection].each do |key, record|
|
143
|
+
@connection[collection].insert record
|
144
|
+
end
|
145
|
+
@inserted << collection
|
146
|
+
end
|
147
|
+
|
122
148
|
# Empties the collections, only if they were empty to begin with
|
123
149
|
def rollback
|
124
150
|
begin
|
@@ -144,6 +170,11 @@ module Mongo
|
|
144
170
|
@field = field
|
145
171
|
super message
|
146
172
|
end
|
147
|
-
end
|
173
|
+
end
|
174
|
+
|
175
|
+
private
|
176
|
+
def data_was_inserted_in? collection
|
177
|
+
@inserted.include? collection
|
178
|
+
end
|
148
179
|
end
|
149
180
|
end
|
data/spec/mongo/fixture_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "mongo-fixture"
|
2
|
+
require "pry"
|
2
3
|
|
3
4
|
describe Mongo::Fixture do
|
4
5
|
describe ".path" do
|
@@ -398,7 +399,7 @@ describe Mongo::Fixture do
|
|
398
399
|
end
|
399
400
|
end
|
400
401
|
|
401
|
-
context "a fixture with a field with alternatives
|
402
|
+
context "a fixture with a field with alternatives missing the <processed> and the option doesn't match a collection" do
|
402
403
|
before do
|
403
404
|
Fast.file.write "test/fixtures/test/users.yaml", "hey: { pass: { raw: There } }"
|
404
405
|
end
|
@@ -424,6 +425,110 @@ describe Mongo::Fixture do
|
|
424
425
|
Fast.dir.remove! :test
|
425
426
|
end
|
426
427
|
end
|
428
|
+
|
429
|
+
context "a fixture with a field with one alternative name matches a collection name" do
|
430
|
+
context "the alternative value matches a record and in the collection" do
|
431
|
+
before do
|
432
|
+
Fast.file.write "test/fixtures/test/users.yaml", "pepe: { name: Jonah }"
|
433
|
+
Fast.file.write "test/fixtures/test/comments.yaml", "flamewar: { user: { users: pepe }, text: 'FLAME' }"
|
434
|
+
end
|
435
|
+
|
436
|
+
it "should insert the comment so that the comment user value matches the '_id' of the user" do
|
437
|
+
database = double 'database'
|
438
|
+
comm = double 'comments', :count => 0, :drop => nil
|
439
|
+
comm.should_receive( :insert ).with( :user => "un id", :text => "FLAME" )
|
440
|
+
record = stub 'record'
|
441
|
+
record.should_receive( :[] ).with( :_id ).and_return "un id"
|
442
|
+
usrs = double 'users', :count => 0, :find => stub( :first => record ), :drop => nil, :insert => nil
|
443
|
+
database.stub :[] do |coll|
|
444
|
+
case coll
|
445
|
+
when :users
|
446
|
+
usrs
|
447
|
+
when :comments
|
448
|
+
comm
|
449
|
+
end
|
450
|
+
end
|
451
|
+
fix = Mongo::Fixture.new :test, database
|
452
|
+
end
|
453
|
+
|
454
|
+
context "the collection is ordered so that the comment collection comes before the users one" do
|
455
|
+
it "should stop and process the users first" do
|
456
|
+
database = double 'database'
|
457
|
+
usrs = double 'users', :count => 0, :insert => nil, :find => stub( :first => stub( :[] => "un id" ) )
|
458
|
+
database.stub :[] do |argument|
|
459
|
+
case argument
|
460
|
+
when :comments
|
461
|
+
double 'comments', :count => 0, :insert => nil
|
462
|
+
when :users
|
463
|
+
usrs
|
464
|
+
end
|
465
|
+
end
|
466
|
+
fix = Mongo::Fixture.new :test, database, false
|
467
|
+
def fix.stub_data
|
468
|
+
@data = {
|
469
|
+
:comments => SymbolMatrix.new("test/fixtures/test/comments.yaml"),
|
470
|
+
:users => SymbolMatrix.new("test/fixtures/test/users.yaml") }
|
471
|
+
end
|
472
|
+
fix.stub_data
|
473
|
+
|
474
|
+
fix.push
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
after do
|
479
|
+
Fast.dir.remove! :test
|
480
|
+
end
|
481
|
+
end
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
describe "#data_was_inserted_in?" do
|
486
|
+
it "should be private" do
|
487
|
+
fix = Mongo::Fixture.new
|
488
|
+
fix.private_methods(false).should include :data_was_inserted_in?
|
489
|
+
end
|
490
|
+
|
491
|
+
context "there is a simple fixture and a collection has been inserted by this fixture" do
|
492
|
+
before do
|
493
|
+
Fast.file.write "test/fixtures/test/users.yaml", "pepe: { user: pepe }"
|
494
|
+
end
|
495
|
+
|
496
|
+
it "should return true" do
|
497
|
+
database = double 'database'
|
498
|
+
coll = double 'collection', :count => 0, :insert => nil
|
499
|
+
database.stub :[] => coll
|
500
|
+
fix = Mongo::Fixture.new :test, database
|
501
|
+
def fix.loot
|
502
|
+
data_was_inserted_in?(:users).should === true
|
503
|
+
end
|
504
|
+
fix.loot
|
505
|
+
end
|
506
|
+
|
507
|
+
after do
|
508
|
+
Fast.dir.remove! :test
|
509
|
+
end
|
510
|
+
end
|
511
|
+
|
512
|
+
context "there is a simple fixture and a collection was inserted but not this" do
|
513
|
+
before do
|
514
|
+
Fast.file.write "test/fixtures/test/users.yaml", "pepe: { user: pepe }"
|
515
|
+
end
|
516
|
+
|
517
|
+
it "should return false" do
|
518
|
+
database = double 'database'
|
519
|
+
coll = double 'collection', :count => 0, :insert => nil
|
520
|
+
database.stub :[] => coll
|
521
|
+
fix = Mongo::Fixture.new :test, database
|
522
|
+
def fix.loot
|
523
|
+
data_was_inserted_in?(:comment).should === false
|
524
|
+
end
|
525
|
+
fix.loot
|
526
|
+
end
|
527
|
+
|
528
|
+
after do
|
529
|
+
Fast.dir.remove! :test
|
530
|
+
end
|
531
|
+
end
|
427
532
|
end
|
428
533
|
|
429
534
|
# This should go in a dependency, pending refactoring TODO
|
@@ -444,6 +549,10 @@ describe Mongo::Fixture do
|
|
444
549
|
}
|
445
550
|
|
446
551
|
fix = Mongo::Fixture.new
|
552
|
+
def fix.stub_data
|
553
|
+
@data = {}
|
554
|
+
end
|
555
|
+
fix.stub_data
|
447
556
|
simplified = fix.simplify base_hash
|
448
557
|
simplified.should == {
|
449
558
|
:name => "Jane",
|
@@ -469,6 +578,10 @@ describe Mongo::Fixture do
|
|
469
578
|
}
|
470
579
|
|
471
580
|
fix = Mongo::Fixture.new
|
581
|
+
def fix.stub_data
|
582
|
+
@data = {}
|
583
|
+
end
|
584
|
+
fix.stub_data
|
472
585
|
expect { fix.simplify base_hash
|
473
586
|
}.to raise_error Mongo::Fixture::MissingProcessedValueError,
|
474
587
|
"The processed value to insert into the db is missing from the field 'pass', aborting"
|
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
|
+
version: 0.0.2
|
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-
|
12
|
+
date: 2012-08-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|
@@ -114,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
114
|
version: '0'
|
115
115
|
segments:
|
116
116
|
- 0
|
117
|
-
hash:
|
117
|
+
hash: 803460883
|
118
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
119
|
none: false
|
120
120
|
requirements:
|
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
123
|
version: '0'
|
124
124
|
segments:
|
125
125
|
- 0
|
126
|
-
hash:
|
126
|
+
hash: 803460883
|
127
127
|
requirements: []
|
128
128
|
rubyforge_project:
|
129
129
|
rubygems_version: 1.8.24
|