mongo-import 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MongoImport
2
2
 
3
- TODO: Write a gem description
3
+ A wrapper to run mongo's `mongoimport` from specs.
4
4
 
5
5
  ## Installation
6
6
 
@@ -17,8 +17,26 @@ Or install it yourself as:
17
17
  $ gem install mongo-import
18
18
 
19
19
  ## Usage
20
+ 1. Create a directory `spec/snapshots`
21
+ 2. Run `mongoexport` and redirect to a file called `db_name.collection_name.json` to the snapshots directory
22
+ 3. In your `spec_helper.rb` add:
23
+
24
+ ```ruby
25
+ RSpec.configure do |c|
26
+ c.include MongoImport
27
+ end
28
+ ```
29
+
30
+ 4. Use in your spec like a boss:
31
+
32
+ ```ruby
33
+ snapshot 'db_name.collection_name'
34
+ ```
35
+ or
36
+ ```ruby
37
+ snapshot 'file_name', :db => 'db_name', :collection => 'collection_name'
38
+ ```
20
39
 
21
- TODO: Write usage instructions here
22
40
 
23
41
  ## Contributing
24
42
 
data/lib/mongo-import.rb CHANGED
@@ -1 +1,3 @@
1
- require "mongo-import/version"
1
+ require 'mongo-import/version'
2
+ require 'ostruct'
3
+ require 'mongo-import/snapshot'
@@ -0,0 +1,29 @@
1
+ module MongoImport
2
+ def snapshot(expr, opts={})
3
+ db, collection = expr.split('.')
4
+ Snapshot.new(expr, :db => opts[:db] || db, :collection => opts[:collection] || collection).import
5
+ end
6
+
7
+ class Snapshot
8
+ attr_reader :opts, :db, :collection
9
+
10
+ def initialize(name, opts={})
11
+ raise ArgumentError, ':db is missing' unless opts[:db]
12
+ raise ArgumentError, ':collection is missing' unless opts[:collection]
13
+ defaults = {:host => 'localhost', :port => 27017, :exec => 'mongoimport'}
14
+ @opts = OpenStruct.new(defaults.merge(opts))
15
+ p @opts
16
+ @path = 'spec/snapshots'
17
+ @name = name
18
+ end
19
+
20
+ # returns true if the import succeeded
21
+ def import
22
+ raise "Could not find #{@opts.exec} in your PATH" unless system("which #{opts.exec} > /dev/null")
23
+
24
+ file = File.join(@path, "#{@name}.json")
25
+ cmd = "#{opts.exec} --host #{opts.host} --port #{opts.port} --drop --db #{opts.db} --collection #{opts.collection} #{file}"
26
+ system cmd
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module MongoImport
2
- VERSION = "0.0.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,32 +1,27 @@
1
1
  require 'spec_helper'
2
- require 'json'
3
2
 
4
- module MongoImport
5
- class Snapshot
6
- def initialize(db, collection)
7
- @collection, @db = collection, db
8
- @dir = File.expand_path('../../snapshots', __FILE__)
9
- @host = 'localhost'
10
- @port = 27017
11
- end
3
+ describe 'Integration' do
4
+ it "should import a snapshot to mongo" do
5
+ s = 'mongo_import_test.test'
6
+ snapshot(s).should be_true
12
7
 
13
- def import
14
- file = File.join(@dir, "#{@collection}.json")
15
- cmd = "mongoimport --host #{@host} --port #{@port} --drop --db #{@db} --collection #{@collection} #{file}"
16
- system cmd
17
- end
8
+ db_name, collection_name = s.split '.'
9
+ c = Mongo::Connection.new[db_name][collection_name]
10
+ c.count.should == 1
11
+ c.find_one.should == {"_id"=>BSON::ObjectId('4deca6e4b496b2fb83000003'), "name"=>"anixe",
12
+ "created_at"=> Time.utc(2011, 6, 6, 10, 7, 33)}
18
13
  end
19
14
  end
20
15
 
21
16
  describe 'Integration' do
22
- it "works" do
23
- snapshot = MongoImport::Snapshot.new 'mongo_import_test', 'test'
24
- snapshot.import
25
- # c = db['test']
26
- # json = JSON.parse(s)
27
- # p json
28
- # c.insert(json)
17
+ it "should import a snapshot to mongo with providing name and opts separately" do
18
+ s = 'mongo_import_test.test'
19
+ db_name, collection_name = 'test_mongo_import', 'test_test'
20
+ snapshot(s, :db => db_name, :collection => collection_name).should be_true
29
21
 
30
- # db['test'].count.should == 1
22
+ c = Mongo::Connection.new[db_name][collection_name]
23
+ c.count.should == 1
24
+ c.find_one.should == {"_id"=>BSON::ObjectId('4deca6e4b496b2fb83000003'), "name"=>"anixe",
25
+ "created_at"=> Time.utc(2011, 6, 6, 10, 7, 33)}
31
26
  end
32
27
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'mongo'
2
+ require 'mongo-import'
2
3
 
3
- ENV['TZ'] = "CET" # make sure all tests are in the same timezone
4
- $LOAD_PATH << File.expand_path('../../lib', __FILE__) # the gem's lib dir
4
+ RSpec.configure do |c|
5
+ c.include MongoImport
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo-import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.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-11-19 00:00:00.000000000 Z
12
+ date: 2012-12-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bson_ext
@@ -202,10 +202,11 @@ files:
202
202
  - README.md
203
203
  - Rakefile
204
204
  - lib/mongo-import.rb
205
+ - lib/mongo-import/snapshot.rb
205
206
  - lib/mongo-import/version.rb
206
207
  - mongo-import.gemspec
207
208
  - spec/acceptance/integration_spec.rb
208
- - spec/snapshots/test.json
209
+ - spec/snapshots/mongo_import_test.test.json
209
210
  - spec/spec.rake
210
211
  - spec/spec_helper.rb
211
212
  homepage: ''
@@ -220,26 +221,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
220
221
  - - ! '>='
221
222
  - !ruby/object:Gem::Version
222
223
  version: '0'
223
- segments:
224
- - 0
225
- hash: 2688478119102984263
226
224
  required_rubygems_version: !ruby/object:Gem::Requirement
227
225
  none: false
228
226
  requirements:
229
227
  - - ! '>='
230
228
  - !ruby/object:Gem::Version
231
229
  version: '0'
232
- segments:
233
- - 0
234
- hash: 2688478119102984263
235
230
  requirements: []
236
231
  rubyforge_project:
237
- rubygems_version: 1.8.24
232
+ rubygems_version: 1.8.23
238
233
  signing_key:
239
234
  specification_version: 3
240
235
  summary: see description
241
236
  test_files:
242
237
  - spec/acceptance/integration_spec.rb
243
- - spec/snapshots/test.json
238
+ - spec/snapshots/mongo_import_test.test.json
244
239
  - spec/spec.rake
245
240
  - spec/spec_helper.rb