mongo-fixture 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +82 -8
- data/features/stepdefs/configurable_fixtures_folder/the_folder_is_now_just_fixtures.rb +3 -0
- data/features/support/hooks.rb +2 -1
- data/lib/mongo-fixture.rb +6 -1
- data/lib/mongo-fixture/version.rb +1 -1
- data/spec/mongo/fixture/inserter_spec.rb +2 -4
- data/spec/mongo/fixture_spec.rb +11 -0
- metadata +6 -9
data/README.md
CHANGED
@@ -2,7 +2,7 @@ Mongo::Fixture
|
|
2
2
|
===============
|
3
3
|
[![Build Status](https://secure.travis-ci.org/Fetcher/mongo-fixture.png)](http://travis-ci.org/Fetcher/mongo-fixture) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/Fetcher/mongo-fixture)
|
4
4
|
|
5
|
-
|
5
|
+
Similar to Rails 2 fixtures, but for [MongoDB][mongo-db] (using the standard [mongo connector Gem][mongo-gem] ).
|
6
6
|
|
7
7
|
[mongo-db]: http://www.mongodb.org/
|
8
8
|
[mongo-gem]: http://rubygems.org/gems/mongo
|
@@ -10,6 +10,39 @@ Just like Rails 2 fixtures, but for [MongoDB][mongo-db] (using the standard [mon
|
|
10
10
|
Show off
|
11
11
|
========
|
12
12
|
|
13
|
+
### Single collection
|
14
|
+
|
15
|
+
Assuming you have a fixture for the collection `messages` with:
|
16
|
+
```yaml
|
17
|
+
# test/fixtures/some_data/messages.yaml
|
18
|
+
yesterday_afternoon:
|
19
|
+
text: Honey, pizza tonight?
|
20
|
+
sent: 2012-09-11
|
21
|
+
today_morning:
|
22
|
+
text: Nice date yesterday, how about next week?
|
23
|
+
sent: 2012-09-12
|
24
|
+
```
|
25
|
+
|
26
|
+
You can write a brief ruby script and insert the data into your mongo db:
|
27
|
+
```ruby
|
28
|
+
require 'mongo'
|
29
|
+
require 'mongo-fixture'
|
30
|
+
|
31
|
+
DB = Mongo::Connection.new.db 'messages-db' # An example connetion setup
|
32
|
+
|
33
|
+
# Insert the fixture into the database
|
34
|
+
fixture_some_data = Mongo::Fixture.new :some_data, DB
|
35
|
+
|
36
|
+
# You can now query the fixture for the data that was sent into the DB
|
37
|
+
fixture_some_data.messages.yesterday_afternoon.text # => "Honey, pizza tonight?"
|
38
|
+
```
|
39
|
+
|
40
|
+
The fixture is identified by the name of the folder containing the fixture YAML files. The default folder is `test/fixtures`.
|
41
|
+
|
42
|
+
As you can see, each record is preceded by a name, in this case `yesterday_afternoon` and `today_morning`. This names never get inserted into the database, they are just references for making it easier to access the fixture's information.
|
43
|
+
|
44
|
+
### Associations
|
45
|
+
|
13
46
|
Assuming you have a fixture for the collection users with:
|
14
47
|
```yaml
|
15
48
|
# test/fixtures/simple/users.yaml
|
@@ -27,16 +60,31 @@ and for messages:
|
|
27
60
|
```yaml
|
28
61
|
# test/fixtures/simple/messages.yaml
|
29
62
|
greeting:
|
30
|
-
|
31
|
-
|
63
|
+
sender:
|
64
|
+
users: john
|
65
|
+
receiver:
|
66
|
+
users: jane
|
32
67
|
text: Hi Jane! Long time no see.
|
33
68
|
long_time:
|
34
|
-
|
35
|
-
|
69
|
+
sender:
|
70
|
+
users: jane
|
71
|
+
receiver:
|
72
|
+
users: john
|
36
73
|
text: John! Long time indeed. How are you doing?
|
37
74
|
```
|
38
75
|
|
39
|
-
|
76
|
+
Mongo Fixture will automatically insert the object id (`_id` field in the database) for the referenced users. The Mongo Fixture syntax specifies that:
|
77
|
+
|
78
|
+
```yaml
|
79
|
+
sender:
|
80
|
+
users: john
|
81
|
+
```
|
82
|
+
|
83
|
+
is a reference to the record named `john` in the collection `users`.
|
84
|
+
|
85
|
+
> Currently Mongo Fixture does not support references from one collection to another _and back_ from that collection to the first one.
|
86
|
+
|
87
|
+
For example given the ruby script:
|
40
88
|
|
41
89
|
```ruby
|
42
90
|
# script.rb
|
@@ -54,7 +102,7 @@ fixture.users.john.name # => "John"
|
|
54
102
|
fixture.rollback # returns users and messages to pristine status (#drop)
|
55
103
|
|
56
104
|
|
57
|
-
fixture = Mongo::Fixture.new :simple, DB, false # The `false` flag prevent the constructor
|
105
|
+
fixture = Mongo::Fixture.new :simple, DB, false # The `false` flag prevent the constructor from automatically pushing
|
58
106
|
# the fixture into the database
|
59
107
|
|
60
108
|
fixture.check # Will fail if the user or messages collection
|
@@ -68,7 +116,29 @@ fixture.rollback # Don't forget to rollback
|
|
68
116
|
|
69
117
|
...naturally, `mongo-fixture` makes a lot more sense within some testing framework.
|
70
118
|
|
71
|
-
|
119
|
+
### Many to many associations
|
120
|
+
|
121
|
+
As is custom in Mongo, a many-to-many association is implemented by passing an array of ids to a field in any of the records to associate. Mongo Fixture supports this. For example:
|
122
|
+
|
123
|
+
```yaml
|
124
|
+
message:
|
125
|
+
sender:
|
126
|
+
users: john
|
127
|
+
receivers:
|
128
|
+
users: [mary, sue, harry, jack]
|
129
|
+
text: Meeting tonight guys?
|
130
|
+
```
|
131
|
+
|
132
|
+
will insert into the `receivers` field an array with the `_id`s of the referenced users.
|
133
|
+
|
134
|
+
Changing fixtures directory
|
135
|
+
---------------------------
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
require 'mongo-fixture'
|
139
|
+
|
140
|
+
Mongo::Fixture.path = 'fixtures' # Now fixtures will be required from `fixtures/`
|
141
|
+
```
|
72
142
|
|
73
143
|
Installation
|
74
144
|
------------
|
@@ -83,6 +153,10 @@ And then execute:
|
|
83
153
|
|
84
154
|
bundle
|
85
155
|
|
156
|
+
Future
|
157
|
+
------
|
158
|
+
|
159
|
+
- Dump from a database to fixtures! This would be awesome.
|
86
160
|
|
87
161
|
## License
|
88
162
|
|
data/features/support/hooks.rb
CHANGED
data/lib/mongo-fixture.rb
CHANGED
@@ -12,11 +12,16 @@ module Mongo
|
|
12
12
|
class Fixture
|
13
13
|
## Class methods
|
14
14
|
|
15
|
-
# Returns the current path to the fixtures folder
|
15
|
+
# @return [String] Returns the current path to the fixtures folder
|
16
16
|
def self.path
|
17
17
|
@@path ||= "test/fixtures"
|
18
18
|
end
|
19
19
|
|
20
|
+
# Sets the current path to the fixtures folder
|
21
|
+
def self.path= the_path
|
22
|
+
@@path = the_path
|
23
|
+
end
|
24
|
+
|
20
25
|
## Instance methods
|
21
26
|
|
22
27
|
# Initializes the fixture handler
|
@@ -54,8 +54,7 @@ describe Mongo::Fixture::Inserter do
|
|
54
54
|
fixture = double 'fixture', :data => {}, :rollback => nil
|
55
55
|
ins = Mongo::Fixture::Inserter.new fixture
|
56
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]"
|
57
|
+
}.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError
|
59
58
|
end
|
60
59
|
|
61
60
|
it "should call #resolve_field_hash with the data hash" do
|
@@ -332,8 +331,7 @@ describe Mongo::Fixture::Inserter do
|
|
332
331
|
fixture = stub 'fixture', :data => { :collection => "", :another_collection => ""}
|
333
332
|
inserter = Mongo::Fixture::Inserter.new fixture
|
334
333
|
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]"
|
334
|
+
}.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError
|
337
335
|
end
|
338
336
|
end
|
339
337
|
end
|
data/spec/mongo/fixture_spec.rb
CHANGED
@@ -7,6 +7,17 @@ describe Mongo::Fixture do
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
describe ".path=" do
|
11
|
+
it 'should set the path with the passed string' do
|
12
|
+
Mongo::Fixture.path = "fixtures"
|
13
|
+
Mongo::Fixture.path.should == "fixtures"
|
14
|
+
end
|
15
|
+
|
16
|
+
after do
|
17
|
+
Mongo::Fixture.path = 'test/fixtures'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
10
21
|
describe ".new" do
|
11
22
|
context "a symbol is sent representing a fixture" do
|
12
23
|
it "should call load" do
|
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.1.0
|
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-
|
12
|
+
date: 2012-09-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- Rakefile
|
92
92
|
- features/configurable_fixtures_folder.feature
|
93
93
|
- features/play_around_with_fixtures.feature
|
94
|
+
- features/stepdefs/configurable_fixtures_folder/the_folder_is_now_just_fixtures.rb
|
94
95
|
- features/stepdefs/play_around_with_fixtures/background.rb
|
95
96
|
- features/stepdefs/play_around_with_fixtures/create_a_simple_fixture_push_it_and_rollback.rb
|
96
97
|
- features/stepdefs/play_around_with_fixtures/misconfigured_password_field.rb
|
@@ -115,27 +116,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
116
|
- - ! '>='
|
116
117
|
- !ruby/object:Gem::Version
|
117
118
|
version: '0'
|
118
|
-
segments:
|
119
|
-
- 0
|
120
|
-
hash: -421222579
|
121
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
120
|
none: false
|
123
121
|
requirements:
|
124
122
|
- - ! '>='
|
125
123
|
- !ruby/object:Gem::Version
|
126
124
|
version: '0'
|
127
|
-
segments:
|
128
|
-
- 0
|
129
|
-
hash: -421222579
|
130
125
|
requirements: []
|
131
126
|
rubyforge_project:
|
132
|
-
rubygems_version: 1.8.
|
127
|
+
rubygems_version: 1.8.21
|
133
128
|
signing_key:
|
134
129
|
specification_version: 3
|
135
130
|
summary: Flexible fixtures for the MongoDB Gem inspired in Rails 2 fixtures
|
136
131
|
test_files:
|
137
132
|
- features/configurable_fixtures_folder.feature
|
138
133
|
- features/play_around_with_fixtures.feature
|
134
|
+
- features/stepdefs/configurable_fixtures_folder/the_folder_is_now_just_fixtures.rb
|
139
135
|
- features/stepdefs/play_around_with_fixtures/background.rb
|
140
136
|
- features/stepdefs/play_around_with_fixtures/create_a_simple_fixture_push_it_and_rollback.rb
|
141
137
|
- features/stepdefs/play_around_with_fixtures/misconfigured_password_field.rb
|
@@ -144,3 +140,4 @@ test_files:
|
|
144
140
|
- spec/mongo/fixture/inserter_spec.rb
|
145
141
|
- spec/mongo/fixture_spec.rb
|
146
142
|
- spec/spec_helper.rb
|
143
|
+
has_rdoc:
|