animehunter-mongo 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/README.rdoc +311 -0
  2. data/Rakefile +62 -0
  3. data/bin/bson_benchmark.rb +59 -0
  4. data/bin/mongo_console +21 -0
  5. data/bin/run_test_script +19 -0
  6. data/bin/standard_benchmark +109 -0
  7. data/examples/admin.rb +41 -0
  8. data/examples/benchmarks.rb +42 -0
  9. data/examples/blog.rb +76 -0
  10. data/examples/capped.rb +23 -0
  11. data/examples/cursor.rb +47 -0
  12. data/examples/gridfs.rb +87 -0
  13. data/examples/index_test.rb +125 -0
  14. data/examples/info.rb +30 -0
  15. data/examples/queries.rb +69 -0
  16. data/examples/simple.rb +23 -0
  17. data/examples/strict.rb +34 -0
  18. data/examples/types.rb +40 -0
  19. data/lib/mongo.rb +19 -0
  20. data/lib/mongo/admin.rb +87 -0
  21. data/lib/mongo/collection.rb +235 -0
  22. data/lib/mongo/cursor.rb +227 -0
  23. data/lib/mongo/db.rb +538 -0
  24. data/lib/mongo/gridfs.rb +16 -0
  25. data/lib/mongo/gridfs/chunk.rb +96 -0
  26. data/lib/mongo/gridfs/grid_store.rb +468 -0
  27. data/lib/mongo/message.rb +20 -0
  28. data/lib/mongo/message/get_more_message.rb +37 -0
  29. data/lib/mongo/message/insert_message.rb +35 -0
  30. data/lib/mongo/message/kill_cursors_message.rb +36 -0
  31. data/lib/mongo/message/message.rb +84 -0
  32. data/lib/mongo/message/message_header.rb +50 -0
  33. data/lib/mongo/message/msg_message.rb +33 -0
  34. data/lib/mongo/message/opcodes.rb +32 -0
  35. data/lib/mongo/message/query_message.rb +77 -0
  36. data/lib/mongo/message/remove_message.rb +36 -0
  37. data/lib/mongo/message/update_message.rb +37 -0
  38. data/lib/mongo/mongo.rb +164 -0
  39. data/lib/mongo/query.rb +119 -0
  40. data/lib/mongo/types/binary.rb +42 -0
  41. data/lib/mongo/types/code.rb +34 -0
  42. data/lib/mongo/types/dbref.rb +37 -0
  43. data/lib/mongo/types/objectid.rb +137 -0
  44. data/lib/mongo/types/regexp_of_holding.rb +44 -0
  45. data/lib/mongo/types/undefined.rb +31 -0
  46. data/lib/mongo/util/bson.rb +534 -0
  47. data/lib/mongo/util/byte_buffer.rb +167 -0
  48. data/lib/mongo/util/ordered_hash.rb +96 -0
  49. data/lib/mongo/util/xml_to_ruby.rb +107 -0
  50. data/mongo-ruby-driver.gemspec +99 -0
  51. data/tests/mongo-qa/_common.rb +8 -0
  52. data/tests/mongo-qa/admin +26 -0
  53. data/tests/mongo-qa/capped +22 -0
  54. data/tests/mongo-qa/count1 +18 -0
  55. data/tests/mongo-qa/dbs +22 -0
  56. data/tests/mongo-qa/find +10 -0
  57. data/tests/mongo-qa/find1 +15 -0
  58. data/tests/mongo-qa/gridfs_in +16 -0
  59. data/tests/mongo-qa/gridfs_out +17 -0
  60. data/tests/mongo-qa/indices +49 -0
  61. data/tests/mongo-qa/remove +25 -0
  62. data/tests/mongo-qa/stress1 +35 -0
  63. data/tests/mongo-qa/test1 +11 -0
  64. data/tests/mongo-qa/update +18 -0
  65. data/tests/test_admin.rb +69 -0
  66. data/tests/test_bson.rb +246 -0
  67. data/tests/test_byte_buffer.rb +69 -0
  68. data/tests/test_chunk.rb +84 -0
  69. data/tests/test_cursor.rb +121 -0
  70. data/tests/test_db.rb +160 -0
  71. data/tests/test_db_api.rb +701 -0
  72. data/tests/test_db_connection.rb +18 -0
  73. data/tests/test_grid_store.rb +284 -0
  74. data/tests/test_message.rb +35 -0
  75. data/tests/test_mongo.rb +78 -0
  76. data/tests/test_objectid.rb +98 -0
  77. data/tests/test_ordered_hash.rb +129 -0
  78. data/tests/test_round_trip.rb +116 -0
  79. metadata +133 -0
@@ -0,0 +1,129 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo/util/ordered_hash'
3
+ require 'test/unit'
4
+
5
+ class OrderedHashTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @oh = OrderedHash.new
9
+ @oh['c'] = 1
10
+ @oh['a'] = 2
11
+ @oh['z'] = 3
12
+ @ordered_keys = %w(c a z)
13
+ end
14
+
15
+ def test_empty
16
+ assert_equal [], OrderedHash.new.keys
17
+ end
18
+
19
+ def test_equality
20
+ a = OrderedHash.new
21
+ a['x'] = 1
22
+ a['y'] = 2
23
+
24
+ b = OrderedHash.new
25
+ b['y'] = 2
26
+ b['x'] = 1
27
+
28
+ c = OrderedHash.new
29
+ c['x'] = 1
30
+ c['y'] = 2
31
+
32
+ d = OrderedHash.new
33
+ d['x'] = 2
34
+ d['y'] = 3
35
+
36
+ e = OrderedHash.new
37
+ e['z'] = 1
38
+ e['y'] = 2
39
+
40
+ assert_equal a, c
41
+ assert_not_equal a, b
42
+ assert_not_equal a, d
43
+ assert_not_equal a, e
44
+ end
45
+
46
+ def test_order_preserved
47
+ assert_equal @ordered_keys, @oh.keys
48
+ end
49
+
50
+ def test_order_preserved_after_replace
51
+ @oh['a'] = 42
52
+ assert_equal @ordered_keys, @oh.keys
53
+ @oh['c'] = 'foobar'
54
+ assert_equal @ordered_keys, @oh.keys
55
+ @oh['z'] = /huh?/
56
+ assert_equal @ordered_keys, @oh.keys
57
+ end
58
+
59
+ def test_each
60
+ keys = []
61
+ @oh.each { |k, v| keys << k }
62
+ assert_equal keys, @oh.keys
63
+
64
+ @oh['z'] = 42
65
+ assert_equal keys, @oh.keys
66
+ end
67
+
68
+ def test_values
69
+ assert_equal [1, 2, 3], @oh.values
70
+ end
71
+
72
+ def test_merge
73
+ other = OrderedHash.new
74
+ other['f'] = 'foo'
75
+ noob = @oh.merge(other)
76
+ assert_equal @ordered_keys + ['f'], noob.keys
77
+ assert_equal [1, 2, 3, 'foo'], noob.values
78
+ end
79
+
80
+ def test_merge_bang
81
+ other = OrderedHash.new
82
+ other['f'] = 'foo'
83
+ @oh.merge!(other)
84
+ assert_equal @ordered_keys + ['f'], @oh.keys
85
+ assert_equal [1, 2, 3, 'foo'], @oh.values
86
+ end
87
+
88
+ def test_merge_bang_with_overlap
89
+ other = OrderedHash.new
90
+ other['a'] = 'apple'
91
+ other['c'] = 'crab'
92
+ other['f'] = 'foo'
93
+ @oh.merge!(other)
94
+ assert_equal @ordered_keys + ['f'], @oh.keys
95
+ assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
96
+ end
97
+
98
+ def test_merge_bang_with_hash_with_overlap
99
+ other = Hash.new
100
+ other['a'] = 'apple'
101
+ other['c'] = 'crab'
102
+ other['f'] = 'foo'
103
+ @oh.merge!(other)
104
+ assert_equal @ordered_keys + ['f'], @oh.keys
105
+ assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
106
+ end
107
+
108
+ def test_inspect_retains_order
109
+ assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
110
+ end
111
+
112
+ def test_clear
113
+ @oh.clear
114
+ assert @oh.keys.empty?
115
+ end
116
+
117
+ def test_delete
118
+ assert @oh.keys.include?('z')
119
+ @oh.delete('z')
120
+ assert !@oh.keys.include?('z')
121
+ end
122
+
123
+ def test_delete_if
124
+ assert @oh.keys.include?('z')
125
+ @oh.delete_if { |k,v| k == 'z' }
126
+ assert !@oh.keys.include?('z')
127
+ end
128
+
129
+ end
@@ -0,0 +1,116 @@
1
+ HERE = File.dirname(__FILE__)
2
+ $LOAD_PATH[0,0] = File.join(HERE, '..', 'lib')
3
+ require 'mongo'
4
+ require 'mongo/util/xml_to_ruby'
5
+ require 'test/unit'
6
+
7
+ # For each xml/bson file in the data subdirectory, we turn the XML into an
8
+ # OrderedHash and then test both Ruby-to-BSON and BSON-to-Ruby translations.
9
+ #
10
+ # There is a whole other project that includes similar tests
11
+ # (http://github.com/mongodb/mongo-qa). If the directory ../../mongo-qa
12
+ # exists, (that is, the top-level dir of mongo-qa is next to the top-level dir
13
+ # of this project), then we find the BSON test files there and use those, too.
14
+ class RoundTripTest < Test::Unit::TestCase
15
+
16
+ include XGen::Mongo::Driver
17
+
18
+ @@ruby = nil
19
+
20
+ def setup
21
+ unless @@ruby
22
+ names = Dir[File.join(HERE, 'data', '*.xml')].collect {|f| File.basename(f).sub(/\.xml$/, '') }
23
+ @@ruby = {}
24
+ names.each { |name|
25
+ File.open(File.join(HERE, 'data', "#{name}.xml")) { |f|
26
+ @@ruby[name] = XMLToRuby.new.xml_to_ruby(f)
27
+ }
28
+ }
29
+ end
30
+ end
31
+
32
+ def test_dummy
33
+ assert true
34
+ end
35
+
36
+ def self.create_test_for_round_trip_files_in_dir(dir)
37
+ names = Dir[File.join(dir, '*.xson')].collect {|f| File.basename(f).sub(/\.xson$/, '') }
38
+ names.each { |name|
39
+ eval <<EOS
40
+ def test_#{name}_#{dir.gsub(/[^a-zA-Z0-9_]/, '_')}
41
+ one_round_trip("#{dir}", "#{name}")
42
+ end
43
+ EOS
44
+ }
45
+ end
46
+
47
+ # Dynamically generate one test for each test file. This way, if one test
48
+ # fails the others will still run.
49
+ create_test_for_round_trip_files_in_dir(File.join(HERE, 'data'))
50
+ mongo_qa_dir = File.join(HERE, '../..', 'mongo-qa/modules/bson_tests/tests')
51
+ if File.exist?(mongo_qa_dir)
52
+ %w(basic_types complex single_types).each { |subdir_name|
53
+ create_test_for_round_trip_files_in_dir(File.join(mongo_qa_dir, subdir_name))
54
+ }
55
+ end
56
+
57
+ # Round-trip comparisons of Ruby-to-BSON and back.
58
+ # * Take the objects that were read from XML
59
+ # * Turn them into BSON bytes
60
+ # * Compare that with the BSON files we have
61
+ # * Turn those BSON bytes back in to Ruby objects
62
+ # * Turn them back into BSON bytes
63
+ # * Compare that with the BSON files we have (or the bytes that were already
64
+ # generated)
65
+ def one_round_trip(dir, name)
66
+ obj = File.open(File.join(dir, "#{name}.xson")) { |f|
67
+ XMLToRuby.new.xml_to_ruby(f)
68
+ }
69
+
70
+ File.open(File.join(dir, "#{name}.bson"), 'rb') { |f|
71
+ # Read the BSON from the file
72
+ bson = f.read
73
+ bson = if RUBY_VERSION >= '1.9'
74
+ bson.bytes.to_a
75
+ else
76
+ bson.split(//).collect { |c| c[0] }
77
+ end
78
+
79
+ # Turn the Ruby object into BSON bytes and compare with the BSON bytes
80
+ # from the file.
81
+ bson_from_ruby = BSON.new.serialize(obj).to_a
82
+
83
+ begin
84
+ assert_equal bson.length, bson_from_ruby.length
85
+ assert_equal bson, bson_from_ruby
86
+ rescue => ex
87
+ # File.open(File.join(dir, "#{name}_out_a.bson"), 'wb') { |f| # DEBUG
88
+ # bson_from_ruby.each { |b| f.putc(b) }
89
+ # }
90
+ raise ex
91
+ end
92
+
93
+ # Turn those BSON bytes back into a Ruby object.
94
+ #
95
+ # We're passing a nil db to the contructor here, but that's OK because
96
+ # the BSON DBRef bytes don't contain the db object in any case, and we
97
+ # don't care what the database is.
98
+ obj_from_bson = BSON.new.deserialize(ByteBuffer.new(bson_from_ruby))
99
+ assert_kind_of OrderedHash, obj_from_bson
100
+
101
+ # Turn that Ruby object into BSON and compare it to the original BSON
102
+ # bytes.
103
+ bson_from_ruby = BSON.new.serialize(obj_from_bson).to_a
104
+ begin
105
+ assert_equal bson.length, bson_from_ruby.length
106
+ assert_equal bson, bson_from_ruby
107
+ rescue => ex
108
+ # File.open(File.join(dir, "#{name}_out_b.bson"), 'wb') { |f| # DEBUG
109
+ # bson_from_ruby.each { |b| f.putc(b) }
110
+ # }
111
+ raise ex
112
+ end
113
+ }
114
+ end
115
+
116
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: animehunter-mongo
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.9"
5
+ platform: ruby
6
+ authors:
7
+ - Jim Menard
8
+ - Mike Dirolf
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-05-16 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: A Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.
18
+ email: mongodb-dev@googlegroups.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.rdoc
25
+ files:
26
+ - README.rdoc
27
+ - Rakefile
28
+ - mongo-ruby-driver.gemspec
29
+ - bin/bson_benchmark.rb
30
+ - bin/mongo_console
31
+ - bin/run_test_script
32
+ - bin/standard_benchmark
33
+ - examples/admin.rb
34
+ - examples/benchmarks.rb
35
+ - examples/blog.rb
36
+ - examples/capped.rb
37
+ - examples/cursor.rb
38
+ - examples/gridfs.rb
39
+ - examples/index_test.rb
40
+ - examples/info.rb
41
+ - examples/queries.rb
42
+ - examples/simple.rb
43
+ - examples/strict.rb
44
+ - examples/types.rb
45
+ - lib/mongo/admin.rb
46
+ - lib/mongo/collection.rb
47
+ - lib/mongo/cursor.rb
48
+ - lib/mongo/db.rb
49
+ - lib/mongo/gridfs/chunk.rb
50
+ - lib/mongo/gridfs/grid_store.rb
51
+ - lib/mongo/gridfs.rb
52
+ - lib/mongo/message/get_more_message.rb
53
+ - lib/mongo/message/insert_message.rb
54
+ - lib/mongo/message/kill_cursors_message.rb
55
+ - lib/mongo/message/message.rb
56
+ - lib/mongo/message/message_header.rb
57
+ - lib/mongo/message/msg_message.rb
58
+ - lib/mongo/message/opcodes.rb
59
+ - lib/mongo/message/query_message.rb
60
+ - lib/mongo/message/remove_message.rb
61
+ - lib/mongo/message/update_message.rb
62
+ - lib/mongo/message.rb
63
+ - lib/mongo/mongo.rb
64
+ - lib/mongo/query.rb
65
+ - lib/mongo/types/binary.rb
66
+ - lib/mongo/types/code.rb
67
+ - lib/mongo/types/dbref.rb
68
+ - lib/mongo/types/objectid.rb
69
+ - lib/mongo/types/regexp_of_holding.rb
70
+ - lib/mongo/types/undefined.rb
71
+ - lib/mongo/util/bson.rb
72
+ - lib/mongo/util/byte_buffer.rb
73
+ - lib/mongo/util/ordered_hash.rb
74
+ - lib/mongo/util/xml_to_ruby.rb
75
+ - lib/mongo.rb
76
+ has_rdoc: true
77
+ homepage: http://www.mongodb.org
78
+ licenses:
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --main
82
+ - README.rdoc
83
+ - --inline-source
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ version:
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.5
102
+ signing_key:
103
+ specification_version: 2
104
+ summary: Ruby driver for the 10gen Mongo DB
105
+ test_files:
106
+ - tests/mongo-qa/_common.rb
107
+ - tests/mongo-qa/admin
108
+ - tests/mongo-qa/capped
109
+ - tests/mongo-qa/count1
110
+ - tests/mongo-qa/dbs
111
+ - tests/mongo-qa/find
112
+ - tests/mongo-qa/find1
113
+ - tests/mongo-qa/gridfs_in
114
+ - tests/mongo-qa/gridfs_out
115
+ - tests/mongo-qa/indices
116
+ - tests/mongo-qa/remove
117
+ - tests/mongo-qa/stress1
118
+ - tests/mongo-qa/test1
119
+ - tests/mongo-qa/update
120
+ - tests/test_admin.rb
121
+ - tests/test_bson.rb
122
+ - tests/test_byte_buffer.rb
123
+ - tests/test_chunk.rb
124
+ - tests/test_cursor.rb
125
+ - tests/test_db.rb
126
+ - tests/test_db_api.rb
127
+ - tests/test_db_connection.rb
128
+ - tests/test_grid_store.rb
129
+ - tests/test_message.rb
130
+ - tests/test_mongo.rb
131
+ - tests/test_objectid.rb
132
+ - tests/test_ordered_hash.rb
133
+ - tests/test_round_trip.rb