diff options
author | Antonin Kral <a.kral@bobek.cz> | 2011-09-14 17:08:06 +0200 |
---|---|---|
committer | Antonin Kral <a.kral@bobek.cz> | 2011-09-14 17:08:06 +0200 |
commit | 5d342a758c6095b4d30aba0750b54f13b8916f51 (patch) | |
tree | 762e9aa84781f5e3b96db2c02d356c29cf0217c0 /jstests/tool | |
parent | cbe2d992e9cd1ea66af9fa91df006106775d3073 (diff) | |
download | mongodb-5d342a758c6095b4d30aba0750b54f13b8916f51.tar.gz |
Imported Upstream version 2.0.0
Diffstat (limited to 'jstests/tool')
-rw-r--r-- | jstests/tool/csv1.js | 8 | ||||
-rw-r--r-- | jstests/tool/csvexport1.js | 45 | ||||
-rw-r--r-- | jstests/tool/csvexport2.js | 31 | ||||
-rw-r--r-- | jstests/tool/csvimport1.js | 40 | ||||
-rw-r--r-- | jstests/tool/data/a.tsv | 2 | ||||
-rw-r--r-- | jstests/tool/data/csvimport1.csv | 8 | ||||
-rw-r--r-- | jstests/tool/data/dumprestore6/foo.bson | bin | 0 -> 44 bytes | |||
-rw-r--r-- | jstests/tool/data/dumprestore6/system.indexes.bson | bin | 0 -> 144 bytes | |||
-rw-r--r-- | jstests/tool/dumprestore5.js | 36 | ||||
-rw-r--r-- | jstests/tool/dumprestore6.js | 27 | ||||
-rw-r--r-- | jstests/tool/exportimport1.js | 29 | ||||
-rw-r--r-- | jstests/tool/tsv1.js | 32 |
12 files changed, 251 insertions, 7 deletions
diff --git a/jstests/tool/csv1.js b/jstests/tool/csv1.js index ccf1d09..5eb7ab0 100644 --- a/jstests/tool/csv1.js +++ b/jstests/tool/csv1.js @@ -4,25 +4,25 @@ t = new ToolTest( "csv1" ) c = t.startDB( "foo" ); -base = { a : 1 , b : "foo,bar\"baz,qux" , c: 5, 'd d': -6 , e: '-'}; +base = { a : 1 , b : "foo,bar\"baz,qux" , c: 5, 'd d': -6 , e: '-', f : "."}; assert.eq( 0 , c.count() , "setup1" ); c.insert( base ); delete base._id assert.eq( 1 , c.count() , "setup2" ); -t.runTool( "export" , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" , "--csv" , "-f" , "a,b,c,d d,e" ) +t.runTool( "export" , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" , "--csv" , "-f" , "a,b,c,d d,e,f" ) c.drop() assert.eq( 0 , c.count() , "after drop" ) -t.runTool( "import" , "--file" , t.extFile , "-d" , t.baseName , "-c" , "foo" , "--type" , "csv" , "-f" , "a,b,c,d d,e" ); +t.runTool( "import" , "--file" , t.extFile , "-d" , t.baseName , "-c" , "foo" , "--type" , "csv" , "-f" , "a,b,c,d d,e,f" ); assert.soon( "2 == c.count()" , "restore 2" ); a = c.find().sort( { a : 1 } ).toArray(); delete a[0]._id delete a[1]._id -assert.eq( tojson( { a : "a" , b : "b" , c : "c" , 'd d': "d d", e: 'e'} ) , tojson( a[1] ) , "csv parse 1" ); +assert.eq( tojson( { a : "a" , b : "b" , c : "c" , 'd d': "d d", e: 'e', f : "f"} ) , tojson( a[1] ) , "csv parse 1" ); assert.eq( tojson( base ) , tojson(a[0]) , "csv parse 0" ) c.drop() diff --git a/jstests/tool/csvexport1.js b/jstests/tool/csvexport1.js new file mode 100644 index 0000000..eb4e6e3 --- /dev/null +++ b/jstests/tool/csvexport1.js @@ -0,0 +1,45 @@ +// csvexport1.js + +t = new ToolTest( "csvexport1" ) + +c = t.startDB( "foo" ); + +assert.eq( 0 , c.count() , "setup1" ); + +objId = ObjectId() + +c.insert({ a : new NumberInt(1) , b : objId , c: [1, 2, 3], d : {a : "hello", b : "world"} , e: '-'}) +c.insert({ a : -2.0, c : MinKey, d : "Then he said, \"Hello World!\"", e : new NumberLong(3)}) +c.insert({ a : new BinData(0, "1234"), b : ISODate("2009-08-27"), c : new Timestamp(1234, 9876), d : /foo*\"bar\"/i, e : function foo() { print("Hello World!"); }}) + +assert.eq( 3 , c.count() , "setup2" ); + +t.runTool( "export" , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" , "--csv", "-f", "a,b,c,d,e") + + +c.drop() + +assert.eq( 0 , c.count() , "after drop" ) + +t.runTool("import", "--file", t.extFile, "-d", t.baseName, "-c", "foo", "--type", "csv", "--headerline"); + +assert.soon ( 3 + " == c.count()", "after import"); + +// Note: Exporting and Importing to/from CSV is not designed to be round-trippable +expected = [] +expected.push({ a : 1, b : "ObjectID(" + objId.toString() + ")", c : "[ 1, 2, 3 ]", d : "{ \"a\" : \"hello\", \"b\" : \"world\" }", e : "-"}) +expected.push({ a : -2.0, b : "", c : "$MinKey", d : "Then he said, \"Hello World!\"", e : 3}) +expected.push({ a : "D76DF8", b : "2009-08-27T00:00:00Z", c : "{ \"t\" : 1000 , \"i\" : 9876 }", d : "/foo*\\\"bar\\\"/i", e : tojson(function foo() { print("Hello World!"); })}) + +actual = [] +actual.push(c.find({a : 1}).toArray()[0]); +actual.push(c.find({a : -2.0}).toArray()[0]); +actual.push(c.find({a : "D76DF8"}).toArray()[0]); + +for (i = 0; i < expected.length; i++) { + delete actual[i]._id + assert.eq( expected[i], actual[i], "CSV export " + i); +} + + +t.stop()
\ No newline at end of file diff --git a/jstests/tool/csvexport2.js b/jstests/tool/csvexport2.js new file mode 100644 index 0000000..3e0dd2c --- /dev/null +++ b/jstests/tool/csvexport2.js @@ -0,0 +1,31 @@ +// csvexport2.js + +t = new ToolTest( "csvexport2" ) + +c = t.startDB( "foo" ); + +// This test is designed to test exporting of a CodeWithScope object. +// However, due to SERVER-3391, it is not possible to create a CodeWithScope object in the mongo shell, +// therefore this test does not work. Once SERVER-3391 is resolved, this test should be un-commented out + +//assert.eq( 0 , c.count() , "setup1" ); + +//c.insert({ a : 1 , b : Code("print(\"Hello \" + x);", {"x" : "World!"})}) +//assert.eq( 1 , c.count() , "setup2" ); +//t.runTool( "export" , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" , "--csv", "-f", "a,b") + + +//c.drop() + +//assert.eq( 0 , c.count() , "after drop" ) +//t.runTool("import", "--file", t.extFile, "-d", t.baseName, "-c", "foo", "--type", "csv", "--headerline"); +//assert.soon ( 1 + " == c.count()", "after import"); + +//expected = { a : 1, b : "\"{ \"$code\" : print(\"Hello \" + x); , \"$scope\" : { \"x\" : \"World!\" } }"}; +//actual = c.findOne() + +//delete actual._id; +//assert.eq( expected, actual ); + + +t.stop()
\ No newline at end of file diff --git a/jstests/tool/csvimport1.js b/jstests/tool/csvimport1.js new file mode 100644 index 0000000..3bff111 --- /dev/null +++ b/jstests/tool/csvimport1.js @@ -0,0 +1,40 @@ +// csvimport1.js + +t = new ToolTest( "csvimport1" ) + +c = t.startDB( "foo" ); + +base = [] +base.push({ a : 1, b : "this is some text.\nThis text spans multiple lines, and just for fun\ncontains a comma", "c" : "This has leading and trailing whitespace!" }) +base.push({a : 2, b : "When someone says something you \"put it in quotes\"", "c" : "I like embedded quotes/slashes\\backslashes" }) +base.push({a : 3, b : " This line contains the empty string and has leading and trailing whitespace inside the quotes! ", "c" : "" }) +base.push({a : 4, b : "", "c" : "How are empty entries handled?" }) +base.push({a : 5, b : "\"\"", c : "\"This string is in quotes and contains empty quotes (\"\")\""}) +base.push({ a : "a" , b : "b" , c : "c"}) + +assert.eq( 0 , c.count() , "setup" ); + +t.runTool( "import" , "--file" , "jstests/tool/data/csvimport1.csv" , "-d" , t.baseName , "-c" , "foo" , "--type" , "csv" , "-f" , "a,b,c" ); +assert.soon( base.length + " == c.count()" , "after import 1 " ); + +a = c.find().sort( { a : 1 } ).toArray(); +for (i = 0; i < base.length; i++ ) { + delete a[i]._id + assert.eq( tojson(base[i]), tojson(a[i]), "csv parse " + i) +} + +c.drop() +assert.eq( 0 , c.count() , "after drop" ) + +t.runTool( "import" , "--file" , "jstests/tool/data/csvimport1.csv" , "-d" , t.baseName , "-c" , "foo" , "--type" , "csv" , "--headerline" ) +assert.soon( "c.findOne()" , "no data after sleep" ); +assert.eq( base.length - 1 , c.count() , "after import 2" ); + +x = c.find().sort( { a : 1 } ).toArray(); +for (i = 0; i < base.length - 1; i++ ) { + delete x[i]._id + assert.eq( tojson(base[i]), tojson(x[i]), "csv parse with headerline " + i) +} + + +t.stop() diff --git a/jstests/tool/data/a.tsv b/jstests/tool/data/a.tsv new file mode 100644 index 0000000..1e09417 --- /dev/null +++ b/jstests/tool/data/a.tsv @@ -0,0 +1,2 @@ +a b c d e + 1 foobar 5 -6 diff --git a/jstests/tool/data/csvimport1.csv b/jstests/tool/data/csvimport1.csv new file mode 100644 index 0000000..256d40a --- /dev/null +++ b/jstests/tool/data/csvimport1.csv @@ -0,0 +1,8 @@ +a,b,c +1,"this is some text. +This text spans multiple lines, and just for fun +contains a comma", "This has leading and trailing whitespace!" +2, "When someone says something you ""put it in quotes""", I like embedded quotes/slashes\backslashes + 3 , " This line contains the empty string and has leading and trailing whitespace inside the quotes! ", "" + "4" ,, How are empty entries handled? +"5","""""", """This string is in quotes and contains empty quotes ("""")""" diff --git a/jstests/tool/data/dumprestore6/foo.bson b/jstests/tool/data/dumprestore6/foo.bson Binary files differnew file mode 100644 index 0000000..b8f8f99 --- /dev/null +++ b/jstests/tool/data/dumprestore6/foo.bson diff --git a/jstests/tool/data/dumprestore6/system.indexes.bson b/jstests/tool/data/dumprestore6/system.indexes.bson Binary files differnew file mode 100644 index 0000000..dde25da --- /dev/null +++ b/jstests/tool/data/dumprestore6/system.indexes.bson diff --git a/jstests/tool/dumprestore5.js b/jstests/tool/dumprestore5.js new file mode 100644 index 0000000..ce28fea --- /dev/null +++ b/jstests/tool/dumprestore5.js @@ -0,0 +1,36 @@ +// dumprestore5.js + +t = new ToolTest( "dumprestore5" ); + +t.startDB( "foo" ); + +db = t.db + +db.addUser('user','password') + +assert.eq(1, db.system.users.count(), "setup") +assert.eq(1, db.system.indexes.count(), "setup2") + +t.runTool( "dump" , "--out" , t.ext ); + +db.dropDatabase() + +assert.eq(0, db.system.users.count(), "didn't drop users") +assert.eq(0, db.system.indexes.count(), "didn't drop indexes") + +t.runTool("restore", "--dir", t.ext) + +assert.soon("db.system.users.findOne()", "no data after restore"); +assert.eq(1, db.system.users.find({user:'user'}).count(), "didn't restore users") +assert.eq(1, db.system.indexes.count(), "didn't restore indexes") + +db.removeUser('user') +db.addUser('user2', 'password2') + +t.runTool("restore", "--dir", t.ext, "--drop") + +assert.soon("1 == db.system.users.find({user:'user'}).count()", "didn't restore users 2") +assert.eq(0, db.system.users.find({user:'user2'}).count(), "didn't drop users") +assert.eq(1, db.system.indexes.count(), "didn't maintain indexes") + +t.stop(); diff --git a/jstests/tool/dumprestore6.js b/jstests/tool/dumprestore6.js new file mode 100644 index 0000000..d8b349e --- /dev/null +++ b/jstests/tool/dumprestore6.js @@ -0,0 +1,27 @@ +// dumprestore6.js +// Test restoring from a dump with an old index version + +t = new ToolTest( "dumprestore6" ); + +c = t.startDB( "foo" ); +db = t.db +assert.eq( 0 , c.count() , "setup1" ); + +t.runTool("restore", "--dir", "jstests/tool/data/dumprestore6", "--db", "jstests_tool_dumprestore6") + +assert.soon( "c.findOne()" , "no data after sleep" ); +assert.eq( 1 , c.count() , "after restore" ); +assert.eq( 1 , db.system.indexes.findOne({name:'a_1'}).v, "index version wasn't updated") +assert.eq( 1, c.count({v:0}), "dropped the 'v' field from a non-index collection") + +db.dropDatabase() +assert.eq( 0 , c.count() , "after drop" ); + +t.runTool("restore", "--dir", "jstests/tool/data/dumprestore6", "--db", "jstests_tool_dumprestore6", "--keepIndexVersion") + +assert.soon( "c.findOne()" , "no data after sleep2" ); +assert.eq( 1 , c.count() , "after restore2" ); +assert.eq( 0 , db.system.indexes.findOne({name:'a_1'}).v, "index version wasn't maintained") +assert.eq( 1, c.count({v:0}), "dropped the 'v' field from a non-index collection") + +t.stop(); diff --git a/jstests/tool/exportimport1.js b/jstests/tool/exportimport1.js index 915adcd..451078e 100644 --- a/jstests/tool/exportimport1.js +++ b/jstests/tool/exportimport1.js @@ -4,7 +4,8 @@ t = new ToolTest( "exportimport1" ); c = t.startDB( "foo" ); assert.eq( 0 , c.count() , "setup1" ); -c.save( { a : 22 } ); +var arr = ["x", undefined, "y", undefined]; +c.save( { a : 22 , b : arr} ); assert.eq( 1 , c.count() , "setup2" ); t.runTool( "export" , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" ); @@ -15,8 +16,11 @@ assert.eq( 0 , c.count() , "after drop" , "-d" , t.baseName , "-c" , "foo" );; t.runTool( "import" , "--file" , t.extFile , "-d" , t.baseName , "-c" , "foo" ); assert.soon( "c.findOne()" , "no data after sleep" ); assert.eq( 1 , c.count() , "after restore 2" ); -assert.eq( 22 , c.findOne().a , "after restore 2" ); - +var doc = c.findOne(); +assert.eq( 22 , doc.a , "after restore 2" ); +for (var i=0; i<arr.length; i++) { + assert.eq( arr[i], doc.b[i] , "after restore array: "+i ); +} // now with --jsonArray @@ -30,4 +34,23 @@ assert.soon( "c.findOne()" , "no data after sleep" ); assert.eq( 1 , c.count() , "after restore 2" ); assert.eq( 22 , c.findOne().a , "after restore 2" ); +c.drop(); +assert.eq( 0 , c.count() , "after drop" , "-d" , t.baseName , "-c" , "foo" ); + +arr = ["a", undefined, "c"]; +c.save({a : arr}); +assert.eq( 1 , c.count() , "setup2" ); +t.runTool( "export" , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" ); +c.drop(); +assert.eq( 0 , c.count() , "after drop" , "-d" , t.baseName , "-c" , "foo" );; + +t.runTool( "import" , "--file" , t.extFile , "-d" , t.baseName , "-c" , "foo" ); +assert.soon( "c.findOne()" , "no data after sleep" ); +assert.eq( 1 , c.count() , "after restore 2" ); +var doc = c.findOne(); +for (var i=0; i<arr.length; i++) { + assert.eq( arr[i], doc.a[i] , "after restore array: "+i ); +} + + t.stop(); diff --git a/jstests/tool/tsv1.js b/jstests/tool/tsv1.js new file mode 100644 index 0000000..1b0ddbb --- /dev/null +++ b/jstests/tool/tsv1.js @@ -0,0 +1,32 @@ +// tsv1.js + +t = new ToolTest( "tsv1" ) + +c = t.startDB( "foo" ); + +base = { a : "", b : 1 , c : "foobar" , d: 5, e: -6 }; + +t.runTool( "import" , "--file" , "jstests/tool/data/a.tsv" , "-d" , t.baseName , "-c" , "foo" , "--type" , "tsv" , "-f" , "a,b,c,d,e" ); +assert.soon( "2 == c.count()" , "restore 2" ); + +a = c.find().sort( { a : 1 } ).toArray(); +delete a[0]._id +delete a[1]._id + +assert.eq( { a : "a" , b : "b" , c : "c" , d: "d", e: "e"} , a[1] , "tsv parse 1" ); +assert.eq( base , a[0] , "tsv parse 0" ) + +c.drop() +assert.eq( 0 , c.count() , "after drop 2" ) + +t.runTool( "import" , "--file" , "jstests/tool/data/a.tsv" , "-d" , t.baseName , "-c" , "foo" , "--type" , "tsv" , "--headerline" ) +assert.soon( "c.findOne()" , "no data after sleep" ); +assert.eq( 1 , c.count() , "after restore 2" ); + +x = c.findOne() +delete x._id; +assert.eq( base , x , "tsv parse 2" ) + + + +t.stop() |