Skip to content
Browse files

compare Ruby hashes instead of JSON strings so that sort order is irr…

…elevant
  • Loading branch information...
1 parent 0b4e40f commit 440db7c2729d64430d9d05f4d4c689c49692300f @jweiss committed
Showing with 46 additions and 42 deletions.
  1. +24 −24 test/database_test.rb
  2. +4 −0 test/test_helper.rb
  3. +18 −18 test/view_test.rb
View
48 test/database_test.rb
@@ -26,13 +26,13 @@ class DatabaseTest < Test::Unit::TestCase
should "store an element by id" do
@db.stubs(:rev).returns('rev')
@db['abc'] = {:a => :b}.to_json
- assert_equal({'a' => 'b', '_rev' => 'rev', '_id' => 'abc'}, JSON.parse(@db['abc']))
+ assert_equal({'a' => 'b', '_rev' => 'rev', '_id' => 'abc'}, parse_json(@db['abc']))
end
should "assing an ID is none given" do
@db.stubs(:rev).returns('rev')
RockingChair::Database.expects(:uuid).returns('uuid')
- assert_equal( {"rev" => "rev", "id" => "uuid", "ok" => true}, JSON.parse(@db.store(nil, {'a' => 'b'}.to_json)))
+ assert_equal( {"rev" => "rev", "id" => "uuid", "ok" => true}, parse_json(@db.store(nil, {'a' => 'b'}.to_json)))
end
should "make sure the content is valid JSON" do
@@ -47,18 +47,18 @@ class DatabaseTest < Test::Unit::TestCase
end
should "set the id if none given" do
- _id = JSON.parse(@db.store(nil, {:a => :b}.to_json))['_id']
- assert_not_nil JSON.parse(@db[_id])['_id']
+ _id = parse_json(@db.store(nil, {:a => :b}.to_json))['_id']
+ assert_not_nil parse_json(@db[_id])['_id']
end
should "populate the id" do
@db['abc'] = {:a => :b}.to_json
- assert_equal 'abc', JSON.parse(@db['abc'])['_id']
+ assert_equal 'abc', parse_json(@db['abc'])['_id']
end
should "populate the revision" do
@db['abc'] = {:a => :b}.to_json
- assert_not_nil JSON.parse(@db['abc'])['_rev']
+ assert_not_nil parse_json(@db['abc'])['_rev']
end
should "get the document count" do
@@ -73,9 +73,9 @@ class DatabaseTest < Test::Unit::TestCase
context "when updating" do
should "update the content" do
- state = JSON.parse( @db.store('abc', {:a => :b}.to_json ))
+ state = parse_json( @db.store('abc', {:a => :b}.to_json ))
@db['abc'] = {:a => :c, :_rev => state['rev']}.to_json
- assert_equal 'c', JSON.parse(@db['abc'])['a']
+ assert_equal 'c', parse_json(@db['abc'])['a']
end
should "raise an error if the revs aren't matching" do
@@ -83,14 +83,14 @@ class DatabaseTest < Test::Unit::TestCase
assert_error_code(409) do
@db['abc'] = {:a => :c, :_rev => 'REV'}.to_json
end
- assert_equal 'b', JSON.parse(@db['abc'])['a']
+ assert_equal 'b', parse_json(@db['abc'])['a']
end
end
end
context "when deleting" do
setup do
- @state = JSON.parse(@db.store('abc',{:a => :b}.to_json))
+ @state = parse_json(@db.store('abc',{:a => :b}.to_json))
end
should "delete only if the revision matches" do
@@ -108,14 +108,14 @@ class DatabaseTest < Test::Unit::TestCase
context "when copying" do
setup do
- @state = JSON.parse(@db.store('abc',{:a => :b}.to_json))
+ @state = parse_json(@db.store('abc',{:a => :b}.to_json))
end
should "copy" do
@db.expects(:rev).returns('355068078')
- @state = JSON.parse(@db.copy('abc', 'def'))
+ @state = parse_json(@db.copy('abc', 'def'))
assert_equal({"ok" => true, "id" => "def", "rev" => "355068078"}, @state)
- assert_equal({'a' => 'b', '_id' => 'def', '_rev' => "355068078"}, JSON.parse(@db['def']))
+ assert_equal({'a' => 'b', '_id' => 'def', '_rev' => "355068078"}, parse_json(@db['def']))
end
should "raise 404 if the original if not found" do
@@ -132,7 +132,7 @@ class DatabaseTest < Test::Unit::TestCase
end
should "raise 409 if the rev does not match" do
- @state = JSON.parse(@db.store('def',{'1' => '2'}.to_json))
+ @state = parse_json(@db.store('def',{'1' => '2'}.to_json))
@db.expects(:rev).returns('355068078')
assert_error_code 409 do
@@ -141,7 +141,7 @@ class DatabaseTest < Test::Unit::TestCase
assert_nothing_raised do
@db.copy('abc', 'def', @state['rev'])
end
- assert_equal({'a' => 'b', '_id' => 'def', '_rev' => "355068078"}, JSON.parse(@db['def']))
+ assert_equal({'a' => 'b', '_id' => 'def', '_rev' => "355068078"}, parse_json(@db['def']))
end
end
@@ -150,13 +150,13 @@ class DatabaseTest < Test::Unit::TestCase
@db.stubs(:rev).returns('rev')
@db['a'] = {:a => :b}.to_json
@db['b'] = {1 => 2}.to_json
- assert_equal({'a' => 'b', '_rev' => 'rev', '_id' => 'a'}, JSON.parse(@db['a']))
+ assert_equal({'a' => 'b', '_rev' => 'rev', '_id' => 'a'}, parse_json(@db['a']))
end
should "return a matching document by revision" do
@db.stubs(:rev).returns('rev')
@db['a'] = {:a => :b}.to_json
- assert_equal({'a' => 'b', '_rev' => 'rev', '_id' => 'a'}, JSON.parse(@db.load('a', 'rev' => 'rev')))
+ assert_equal({'a' => 'b', '_rev' => 'rev', '_id' => 'a'}, parse_json(@db.load('a', 'rev' => 'rev')))
end
should "raise a 404 if there is no matching document" do
@@ -176,7 +176,7 @@ class DatabaseTest < Test::Unit::TestCase
should "load the revision history" do
@db.stubs(:rev).returns('rev')
@db['a'] = {:a => :b}.to_json
- assert_equal({'a' => 'b', '_rev' => 'rev', '_id' => 'a', '_revisions' => {'start' => 1, 'ids' => ['rev']}}, JSON.parse(@db.load('a', 'revs' => 'true')))
+ assert_equal({'a' => 'b', '_rev' => 'rev', '_id' => 'a', '_revisions' => {'start' => 1, 'ids' => ['rev']}}, parse_json(@db.load('a', 'revs' => 'true')))
end
end
@@ -339,7 +339,7 @@ class DatabaseTest < Test::Unit::TestCase
{"id" => "B", "key" => "B", "value" => {"rev" => "rev", '_rev' => 'rev', 'data' => 'Z', '_id' => 'B'}},
{"id" => "C", "key" => "C", "value" => {"rev" => "rev", '_rev' => 'rev', 'data' => 'Z', '_id' => 'C'}}
]
- }.to_json, @db.all_documents('startkey' => 'B', 'endkey' => 'C', 'include_docs' => 'true'))
+ }, parse_json(@db.all_documents('startkey' => 'B', 'endkey' => 'C', 'include_docs' => 'true')))
end
end
@@ -370,7 +370,7 @@ class DatabaseTest < Test::Unit::TestCase
'_id' => 'A',
'_rev' => 'the-revision',
'data' => 1
- }, JSON.parse(@db['A']))
+ }, parse_json(@db['A']))
end
should "handle conflics gracefully" do
@@ -382,7 +382,7 @@ class DatabaseTest < Test::Unit::TestCase
assert_equal([
{'id' => 'A', "rev" => 'the-revision'},
{'id' => 'B', "error" => 'conflict', 'reason' => 'Document update conflict.'}
- ].to_json, @db.bulk(docs))
+ ], parse_json(@db.bulk(docs)))
end
end
@@ -395,7 +395,7 @@ class DatabaseTest < Test::Unit::TestCase
assert_equal([
{'id' => 'A', "rev" => 'the-revision'},
{'id' => 'B', "rev" => 'the-revision'}
- ].to_json, @db.bulk(docs))
+ ], parse_json(@db.bulk(docs)))
end
end
end
@@ -406,7 +406,7 @@ class DatabaseTest < Test::Unit::TestCase
@db.stubs(:rev).returns('rev')
@db['_design/user'] = {'language' => 'javascript', 'views' => {}}.to_json
- assert_equal({'language' => 'javascript', 'views' => {}, '_rev' => 'rev', '_id' => '_design/user'}, JSON.parse(@db['_design/user']))
+ assert_equal({'language' => 'javascript', 'views' => {}, '_rev' => 'rev', '_id' => '_design/user'}, parse_json(@db['_design/user']))
end
should "not allow to store invalid design documents" do
@@ -431,7 +431,7 @@ class DatabaseTest < Test::Unit::TestCase
}},
'_id' => '_design/user',
'_rev' => 'rev'
- }, JSON.parse(@db['_design/user']))
+ }, parse_json(@db['_design/user']))
end
should "raise a 404 if there is no such design document" do
View
4 test/test_helper.rb
@@ -43,4 +43,8 @@ def with_debug(&blk)
HttpAbstraction.instance_variable_set("@_rocking_chair_debug", true)
blk.call
HttpAbstraction.instance_variable_set("@_rocking_chair_debug", false)
+end
+
+def parse_json(string)
+ JSON.parse(string, :create_additions => false)
end
View
36 test/view_test.rb
@@ -35,7 +35,7 @@ class ViewTest < Test::Unit::TestCase
}}
assert_nothing_raised do
- JSON.parse(@db.view('user', 'by_firstname', {}))
+ parse_json(@db.view('user', 'by_firstname', {}))
end
end
@@ -102,7 +102,7 @@ class ViewTest < Test::Unit::TestCase
"key" => nil,
"value" => nil
}
- ]}, JSON.parse(@db.view('user', 'by_firstname')))
+ ]}, parse_json(@db.view('user', 'by_firstname')))
end
should "return all docs if no key is given and we asked to include the docs" do
@@ -144,7 +144,7 @@ class ViewTest < Test::Unit::TestCase
'_rev' => 'the-rev',
'_id' => 'user_2' }
}
- ]}.to_json, @db.view('user', 'by_firstname', 'include_docs' => 'true'))
+ ]}, parse_json(@db.view('user', 'by_firstname', 'include_docs' => 'true')))
end
should "return matching elements" do
@@ -176,7 +176,7 @@ class ViewTest < Test::Unit::TestCase
'_rev' => 'the-rev',
'_id' => 'user_3' }
}
- ]}.to_json, @db.view('user', 'by_firstname', 'key' => "Alf".to_json, 'include_docs' => 'true'))
+ ]}, parse_json(@db.view('user', 'by_firstname', 'key' => "Alf".to_json, 'include_docs' => 'true')))
end
should "only return items with the correct klass matcher" do
@@ -198,7 +198,7 @@ class ViewTest < Test::Unit::TestCase
'_rev' => 'the-rev',
'_id' => 'user_3' }
}
- ]}.to_json, @db.view('user', 'by_firstname', 'key' => "Alf".to_json, 'include_docs' => 'true'))
+ ]}, parse_json(@db.view('user', 'by_firstname', 'key' => "Alf".to_json, 'include_docs' => 'true')))
end
should "support multiple attributes" do
@@ -220,7 +220,7 @@ class ViewTest < Test::Unit::TestCase
'_rev' => 'the-rev',
'_id' => 'user_1' }
}
- ]}.to_json, @db.view('user', 'by_firstname_and_lastname', 'key' => ["Alf", "Bert"].to_json, 'include_docs' => 'true'))
+ ]}, parse_json(@db.view('user', 'by_firstname_and_lastname', 'key' => ["Alf", "Bert"].to_json, 'include_docs' => 'true')))
end
should "support startkey and endkey parameters" do
@@ -228,7 +228,7 @@ class ViewTest < Test::Unit::TestCase
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
- assert_equal(JSON.parse({
+ assert_equal({
"total_rows" => 2,
"offset" => 0,
"rows" => [{
@@ -254,7 +254,7 @@ class ViewTest < Test::Unit::TestCase
'_rev' => 'the-rev',
'_id' => 'user_3' }
}
- ]}.to_json), JSON.parse(@db.view('user', 'by_firstname', 'startkey' => "Alf".to_json, 'endkey' => "Alf".to_json, 'include_docs' => 'true')))
+ ]}, parse_json(@db.view('user', 'by_firstname', 'startkey' => "Alf".to_json, 'endkey' => "Alf".to_json, 'include_docs' => 'true')))
end
should "support startkey/endkey combined with startkey_docid/endkey_docid parameters" do
@@ -262,7 +262,7 @@ class ViewTest < Test::Unit::TestCase
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
- assert_equal(JSON.parse({
+ assert_equal({
"total_rows" => 2,
"offset" => 0,
"rows" => [{
@@ -279,7 +279,7 @@ class ViewTest < Test::Unit::TestCase
'_rev' => 'the-rev',
'_id' => 'user_3' }
}
- ]}.to_json), JSON.parse(@db.view('user', 'by_firstname', 'startkey' => "Alf".to_json, 'endkey' => "Alf".to_json, 'startkey_docid' => "user_3".to_json, "endkey_docid" => 'user_3'.to_json, 'include_docs' => 'true', 'limit' => '1')))
+ ]}, parse_json(@db.view('user', 'by_firstname', 'startkey' => "Alf".to_json, 'endkey' => "Alf".to_json, 'startkey_docid' => "user_3".to_json, "endkey_docid" => 'user_3'.to_json, 'include_docs' => 'true', 'limit' => '1')))
end
end
@@ -303,7 +303,7 @@ class ViewTest < Test::Unit::TestCase
'_rev' => 'the-rev',
'_id' => 'user_1' }
}
- ]}.to_json, @db.view('user', 'association_user_belongs_to_project', 'key' => "project_1".to_json, 'include_docs' => 'true'))
+ ]}, parse_json(@db.view('user', 'association_user_belongs_to_project', 'key' => "project_1".to_json, 'include_docs' => 'true')))
end
end
@@ -337,7 +337,7 @@ class ViewTest < Test::Unit::TestCase
'_rev' => 'the-rev',
'_id' => 'user_2' }
}
- ]}.to_json, @db.view('user', 'all_documents', 'include_docs' => 'true'))
+ ]}, parse_json(@db.view('user', 'all_documents', 'include_docs' => 'true')))
end
should "limit the results if asked to" do
@@ -358,7 +358,7 @@ class ViewTest < Test::Unit::TestCase
'_rev' => 'the-rev',
'_id' => 'user_1' }
}
- ]}.to_json, @db.view('user', 'all_documents', 'include_docs' => 'true', 'limit' => '1'))
+ ]}, parse_json(@db.view('user', 'all_documents', 'include_docs' => 'true', 'limit' => '1')))
end
should "count the objects with reduce" do
@@ -367,7 +367,7 @@ class ViewTest < Test::Unit::TestCase
assert_equal({
"rows" => [{ "key" => nil, "value" => 2}]
- }.to_json, @db.view('user', 'all_documents', 'include_docs' => 'false', 'reduce' => 'true'))
+ }, parse_json(@db.view('user', 'all_documents', 'include_docs' => 'false', 'reduce' => 'true')))
end
end
@@ -390,7 +390,7 @@ class ViewTest < Test::Unit::TestCase
end
should "return all item not storing keys" do
- assert_equal(JSON.parse({
+ assert_equal(parse_json({
"total_rows" => 2,
"rows" => [
{"doc" => {
@@ -415,11 +415,11 @@ class ViewTest < Test::Unit::TestCase
"value" => nil,
"key" => "group_1"
}],
- "offset" => 0}.to_json), JSON.parse(@db.view('user', 'association_user_has_and_belongs_to_many_groups', 'key' => "group_1".to_json, 'include_docs' => 'true')))
+ "offset" => 0}.to_json), parse_json(@db.view('user', 'association_user_has_and_belongs_to_many_groups', 'key' => "group_1".to_json, 'include_docs' => 'true')))
end
should "return all item storing keys" do
- assert_equal(JSON.parse({
+ assert_equal(parse_json({
"total_rows" => 2,
"rows" => [
{"doc" => {
@@ -442,7 +442,7 @@ class ViewTest < Test::Unit::TestCase
"value" => nil,
"key" => "user_1"
}],
- "offset" => 0}.to_json), JSON.parse(@db.view('group', 'association_group_has_and_belongs_to_many_users', 'key' => "user_1".to_json, 'include_docs' => 'true')))
+ "offset" => 0}.to_json), parse_json(@db.view('group', 'association_group_has_and_belongs_to_many_users', 'key' => "user_1".to_json, 'include_docs' => 'true')))
end
end
end

0 comments on commit 440db7c

Please sign in to comment.
Something went wrong with that request. Please try again.