mongodb-mongo-activerecord-ruby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,176 @@
1
+ # Copyright (C) 2008 10gen Inc.
2
+ #
3
+ # This program is free software: you can redistribute it and/or modify it
4
+ # under the terms of the GNU Affero General Public License, version 3, as
5
+ # published by the Free Software Foundation.
6
+ #
7
+ # This program is distributed in the hope that it will be useful, but WITHOUT
8
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
10
+ # for more details.
11
+ #
12
+ # You should have received a copy of the GNU Affero General Public License
13
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
+
15
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '../lib')
16
+ require 'rubygems'
17
+ require 'test/unit'
18
+ require 'mongo_record/sql'
19
+
20
+ class SQLTest < Test::Unit::TestCase
21
+
22
+ include MongoRecord::SQL
23
+
24
+ def assert_done(t)
25
+ assert !t.more?
26
+ assert_nil t.next_token
27
+ end
28
+
29
+ def test_tokenizer
30
+ t = Tokenizer.new('clicked = 1')
31
+ assert_equal 'clicked', t.next_token
32
+ assert_equal '=', t.next_token
33
+ assert_equal 1, t.next_token
34
+ assert_done t
35
+
36
+ t = Tokenizer.new('clicked=1 ')
37
+ assert_equal 'clicked', t.next_token
38
+ assert_equal '=', t.next_token
39
+ assert_equal 1, t.next_token
40
+ assert !t.more?
41
+ assert_done t
42
+
43
+ t = Tokenizer.new('clicked2=1 ')
44
+ assert_equal 'clicked2', t.next_token
45
+ assert_equal '=', t.next_token
46
+ assert_equal 1, t.next_token
47
+ assert_done t
48
+
49
+ t = Tokenizer.new('clicked=1 and foo = 5')
50
+ assert_equal 'clicked', t.next_token
51
+ assert_equal '=', t.next_token
52
+ assert_equal 1, t.next_token
53
+ assert_equal 'and', t.next_token
54
+ assert_equal 'foo', t.next_token
55
+ assert_equal '=', t.next_token
56
+ assert_equal 5, t.next_token
57
+ assert_done t
58
+
59
+ t = Tokenizer.new("name = 'foo'")
60
+ assert_equal 'name', t.next_token
61
+ assert_equal '=', t.next_token
62
+ assert_equal 'foo', t.next_token
63
+ assert_done t
64
+
65
+ t = Tokenizer.new("name = \"bar\"")
66
+ assert_equal 'name', t.next_token
67
+ assert_equal '=', t.next_token
68
+ assert_equal 'bar', t.next_token
69
+ assert_done t
70
+
71
+ t = Tokenizer.new("name = 'foo''bar'")
72
+ assert_equal 'name', t.next_token
73
+ assert_equal '=', t.next_token
74
+ assert_equal "foo'bar", t.next_token
75
+ assert_done t
76
+
77
+ t = Tokenizer.new("age <= 42")
78
+ assert_equal 'age', t.next_token
79
+ assert_equal '<=', t.next_token
80
+ assert_equal 42, t.next_token
81
+ assert_done t
82
+
83
+ t = Tokenizer.new("age <> 42")
84
+ assert_equal 'age', t.next_token
85
+ assert_equal '<>', t.next_token
86
+ assert_equal 42, t.next_token
87
+ assert_done t
88
+ end
89
+
90
+ def test_strip_table_name
91
+ w = Parser.parse_where("user.name = 'foo'", true)
92
+ assert_equal 'foo', w['name']
93
+ w = Parser.parse_where("schema.table.column = 'foo'", true)
94
+ assert_equal 'foo', w['column']
95
+
96
+ w = Parser.parse_where("user.name = 'foo'")
97
+ assert_equal 'foo', w['user.name']
98
+ w = Parser.parse_where("schema.table.column = 'foo'")
99
+ assert_equal 'foo', w['schema.table.column']
100
+ end
101
+
102
+ def test_arrays
103
+ w = Parser.parse_where("name in (1, 2, 42)")
104
+ a = w['name'][:$in]
105
+ assert_equal Array, a.class
106
+ assert_equal 3, a.length
107
+ assert_equal 1, a[0]
108
+ assert_equal 2, a[1]
109
+ assert_equal 42, a[2]
110
+ end
111
+
112
+ def test_regex
113
+ p = Parser.new('')
114
+ assert_equal /foo/i, p.regexp_from_string('%foo%')
115
+ assert_equal /^foo/i, p.regexp_from_string('foo%')
116
+ assert_equal /foo$/i, p.regexp_from_string('%foo')
117
+ assert_equal /^foo$/i, p.regexp_from_string('foo')
118
+ end
119
+
120
+ def test_parser
121
+ w = Parser.parse_where('clicked = 1 ')
122
+ assert_equal 1, w['clicked']
123
+
124
+ w = Parser.parse_where('clicked = 1 and z = 3')
125
+ assert_equal 1, w['clicked']
126
+ assert_equal 3, w['z']
127
+
128
+ w = Parser.parse_where("name = 'foo'")
129
+ assert_equal 'foo', w['name']
130
+
131
+ w = Parser.parse_where("name like '%foo%'")
132
+ assert_equal /foo/i, w['name']
133
+ w = Parser.parse_where("name like 'foo%'")
134
+ assert_equal /^foo/i, w['name']
135
+
136
+ w = Parser.parse_where("foo <> 'bar'")
137
+ assert_equal 'bar', w['foo'][:$ne]
138
+ w = Parser.parse_where("foo != 'bar'")
139
+ assert_equal 'bar', w['foo'][:$ne]
140
+
141
+ w = Parser.parse_where("foo in (1, 2, 'a')")
142
+ assert_equal "1, 2, a", w['foo'][:$in].join(', ')
143
+
144
+ w = Parser.parse_where("foo in ('a', 'b', 'c')")
145
+ assert_equal "a, b, c", w['foo'][:$in].join(', ')
146
+
147
+ w = Parser.parse_where("name = 'the word '' or '' anywhere (surrounded by spaces) used to throw an error'")
148
+ assert_equal "the word ' or ' anywhere (surrounded by spaces) used to throw an error", w['name']
149
+
150
+ w = Parser.parse_where("foo between 1 and 3")
151
+ assert_equal 1, w['foo'][:$gte]
152
+ assert_equal 3, w['foo'][:$lte]
153
+
154
+ w = Parser.parse_where("foo between 3 and 1")
155
+ assert_equal 1, w['foo'][:$gte]
156
+ assert_equal 3, w['foo'][:$lte]
157
+
158
+ w = Parser.parse_where("foo between 'a' and 'Z'")
159
+ assert_equal 'Z', w['foo'][:$gte] # 'Z' is < 'a'
160
+ assert_equal 'a', w['foo'][:$lte]
161
+
162
+ w = Parser.parse_where("foo between 'Z' and 'a'")
163
+ assert_equal 'Z', w['foo'][:$gte]
164
+ assert_equal 'a', w['foo'][:$lte]
165
+
166
+ sql = "name = 'foo' or name = 'bar'"
167
+ err = "sql parser can't handle ors yet: #{sql}"
168
+ begin
169
+ w = Parser.parse_where(sql)
170
+ fail("expected to see \"#{err}\" error")
171
+ rescue => ex
172
+ assert_equal err, ex.to_s
173
+ end
174
+ end
175
+
176
+ end
@@ -0,0 +1,9 @@
1
+ require 'mongo_record/base'
2
+
3
+ class Track2 < MongoRecord::Base
4
+ collection_name :tracks
5
+ fields :artist, :album, :song, :track
6
+ def to_s
7
+ "artist: #{artist}, album: #{album}, song: #{song}, track: #{track ? track.to_i : nil}"
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'mongo_record/base'
2
+
3
+ class Track3 < MongoRecord::Base
4
+ collection_name :tracks
5
+ fields :artist, :album, :song, :track
6
+ def to_s
7
+ "artist: #{artist}, album: #{album}, song: #{song}, track: #{track ? track.to_i : nil}"
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongodb-mongo-activerecord-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jim Menard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mongodb-mongo-ruby-driver
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.1.1
23
+ version:
24
+ description: MongoRecord is an ActiveRecord-like framework for the 10gen Monog database. For more information about Mongo, see http://www.mongodb.org.
25
+ email: jim@10gen.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.rdoc
32
+ files:
33
+ - examples/tracks.rb
34
+ - lib/mongo_record.rb
35
+ - lib/mongo_record/base.rb
36
+ - lib/mongo_record/convert.rb
37
+ - lib/mongo_record/log_device.rb
38
+ - lib/mongo_record/sql.rb
39
+ - lib/mongo_record/subobject.rb
40
+ - README.rdoc
41
+ - Rakefile
42
+ - mongo-activerecord-ruby.gemspec
43
+ has_rdoc: true
44
+ homepage: http://www.mongodb.org
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --main
48
+ - README.rdoc
49
+ - --inline-source
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: ActiveRecord-like models for the 10gen Mongo DB
71
+ test_files:
72
+ - tests/address.rb
73
+ - tests/course.rb
74
+ - tests/student.rb
75
+ - tests/test_log_device.rb
76
+ - tests/test_mongo.rb
77
+ - tests/test_sql.rb
78
+ - tests/track2.rb
79
+ - tests/track3.rb