Skip to content

Instantly share code, notes, and snippets.

@semanticart
Created October 31, 2009 21:29
Show Gist options
  • Select an option

  • Save semanticart/223253 to your computer and use it in GitHub Desktop.

Select an option

Save semanticart/223253 to your computer and use it in GitHub Desktop.
# run the script and pass the path to your mongo/bin
# i.e.
# ruby mongodb_import_test.rb ~/Downloads/mongodb-osx-x86_64-1.0.1/bin
require 'rubygems'
require 'test/unit'
require 'mongo_mapper'
MongoMapper.database = "test-import"
BIN_PATH = ARGV[0] || ""
IMPORT_NAME = if File.exist?(BIN_PATH + "/mongoimport")
'mongoimport'
elsif File.exist?(BIN_PATH + "/mongoimportjson")
'mongoimportjson'
else
raise "please specify the path to your mongo/bin directory"
end
class Artist
include MongoMapper::Document
key :name, String
end
class MongoSanityTest < Test::Unit::TestCase
def setup
Artist.delete_all
@artist = Artist.create(:name => "SOME NAME")
end
def test_can_create_a_record
assert !Artist.find(@artist.id).blank?
end
def test_can_update_a_record
@artist.update_attributes(:name => "new name")
assert_equal Artist.find(@artist.id).name, "new name"
# we have just one record since we did an update instead of an insert
assert_equal Artist.count, 1
end
end
class MongoImportTest < Test::Unit::TestCase
def setup
# drop our json import file which contains a single record
File.open('out.json', 'w') do |f|
f.puts %({ "_id" : "4aecaaa7f62c1923628c36a8", "name" : "IMPORTED RECORD" })
end
# do the actual importing and pass --drop to ensure the collection is emptied first
`#{BIN_PATH}/#{IMPORT_NAME} -d test-import --drop -c artists out.json`
# ensure we imported our record properly
assert Artist.first(:conditions => {:name => "IMPORTED RECORD"})
# and that there is only one
assert_equal Artist.count, 1
end
def test_can_create_a_record_after_import
artist = Artist.create(:name => "SOME NAME")
assert !Artist.find(artist.id).blank?
assert_equal Artist.count, 2
end
def test_can_update_a_record_after_import
artist = Artist.first(:conditions => {:name => "IMPORTED RECORD"})
assert artist
# we rename our artist
artist.update_attributes(:name => "new name")
# we check that the name was updated
assert_equal Artist.find(artist.id).name, artist.name
# this should be 1 because we did an update and didn't add any new records
# but it fails on my machine miserably because update_attributes does an insert
# this occurs in both mongodb 1.1.0 and 1.0.1
assert_equal Artist.count, 1
end
def test_can_update_a_record_after_import_from_class_method
artist = Artist.first(:conditions => {:name => "IMPORTED RECORD"})
assert artist
# we rename our artist... only not because this can't find the artist and it explodes
Artist.update(artist.id, {:name => "new name"})
# we check that the name was updated
assert_equal Artist.find(artist.id).name, artist.name
assert_equal Artist.count, 1
end
def test_that_we_can_update_the_name_natively
# use the mongo binary to update the name
`#{BIN_PATH}/mongo test-import --eval "db.artists.update({name: 'IMPORTED RECORD'}, {name: 'new name'});"`
# we have a record matching 'new name'
assert_equal Artist.first.name, 'new name'
# we only have one record
assert_equal Artist.count, 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment