semanticart (owner)

Revisions

gist: 223253 Download_button fork
public
Public Clone URL: git://gist.github.com/223253.git
Embed All Files: show embed
mongodb_import_test.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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
Text only #