gotime-cassandra_object 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/CHANGELOG +3 -0
  2. data/Gemfile +14 -0
  3. data/Gemfile.lock +42 -0
  4. data/LICENSE +13 -0
  5. data/README.markdown +79 -0
  6. data/Rakefile +74 -0
  7. data/TODO +2 -0
  8. data/VERSION +1 -0
  9. data/gotime-cassandra_object.gemspec +134 -0
  10. data/lib/cassandra_object.rb +13 -0
  11. data/lib/cassandra_object/associations.rb +35 -0
  12. data/lib/cassandra_object/associations/one_to_many.rb +136 -0
  13. data/lib/cassandra_object/associations/one_to_one.rb +77 -0
  14. data/lib/cassandra_object/attributes.rb +93 -0
  15. data/lib/cassandra_object/base.rb +97 -0
  16. data/lib/cassandra_object/callbacks.rb +10 -0
  17. data/lib/cassandra_object/collection.rb +8 -0
  18. data/lib/cassandra_object/cursor.rb +86 -0
  19. data/lib/cassandra_object/dirty.rb +27 -0
  20. data/lib/cassandra_object/identity.rb +61 -0
  21. data/lib/cassandra_object/identity/abstract_key_factory.rb +36 -0
  22. data/lib/cassandra_object/identity/key.rb +20 -0
  23. data/lib/cassandra_object/identity/natural_key_factory.rb +51 -0
  24. data/lib/cassandra_object/identity/uuid_key_factory.rb +37 -0
  25. data/lib/cassandra_object/indexes.rb +129 -0
  26. data/lib/cassandra_object/log_subscriber.rb +17 -0
  27. data/lib/cassandra_object/migrations.rb +72 -0
  28. data/lib/cassandra_object/mocking.rb +15 -0
  29. data/lib/cassandra_object/persistence.rb +195 -0
  30. data/lib/cassandra_object/serialization.rb +6 -0
  31. data/lib/cassandra_object/type_registration.rb +7 -0
  32. data/lib/cassandra_object/types.rb +128 -0
  33. data/lib/cassandra_object/validation.rb +49 -0
  34. data/test/basic_scenarios_test.rb +243 -0
  35. data/test/callbacks_test.rb +19 -0
  36. data/test/config/cassandra.in.sh +53 -0
  37. data/test/config/log4j.properties +38 -0
  38. data/test/config/storage-conf.xml +221 -0
  39. data/test/connection.rb +25 -0
  40. data/test/cursor_test.rb +66 -0
  41. data/test/dirty_test.rb +34 -0
  42. data/test/fixture_models.rb +90 -0
  43. data/test/identity/natural_key_factory_test.rb +94 -0
  44. data/test/index_test.rb +69 -0
  45. data/test/legacy/test_helper.rb +18 -0
  46. data/test/migration_test.rb +21 -0
  47. data/test/one_to_many_associations_test.rb +163 -0
  48. data/test/test_case.rb +28 -0
  49. data/test/test_helper.rb +16 -0
  50. data/test/time_test.rb +32 -0
  51. data/test/types_test.rb +252 -0
  52. data/test/validation_test.rb +25 -0
  53. data/test/z_mock_test.rb +36 -0
  54. metadata +243 -0
@@ -0,0 +1,252 @@
1
+ require 'test_helper'
2
+
3
+ class TypesTest < CassandraObjectTestCase
4
+ context "IntegerType" do
5
+ context "encode" do
6
+
7
+ should "return an int with no errors" do
8
+ assert_nothing_raised {
9
+ i = 12
10
+ assert_equal i.to_s, CassandraObject::IntegerType.encode(i)
11
+ }
12
+ end
13
+
14
+ should "raises an error on non-Integer input" do
15
+ assert_raises(ArgumentError) { CassandraObject::IntegerType::encode('')}
16
+ end
17
+
18
+ should "work with Bignums" do
19
+ assert_nothing_raised {
20
+ i = 2**32
21
+ assert_equal i.to_s, CassandraObject::IntegerType.encode(i)
22
+ }
23
+ end
24
+
25
+ should "work with Fixnums" do
26
+ assert_nothing_raised {
27
+ i = 2**16
28
+ assert_equal Fixnum, i.class
29
+ assert_equal i.to_s, CassandraObject::IntegerType.encode(i)
30
+ }
31
+ end
32
+ end
33
+
34
+ context "decode" do
35
+ should "work with an int" do
36
+ assert_nothing_raised {
37
+ assert_equal 12, CassandraObject::IntegerType.decode('12')
38
+ }
39
+ end
40
+
41
+ should "work with Bignums" do
42
+ assert_nothing_raised {
43
+ assert_equal 2**32, CassandraObject::IntegerType.decode((2**32).to_s)
44
+ }
45
+ end
46
+
47
+ should "reject trash" do
48
+ assert_raises(ArgumentError) {
49
+ CassandraObject::IntegerType.decode('asdf')
50
+ }
51
+ end
52
+
53
+ should "work on a negative number" do
54
+ assert_equal -1, CassandraObject::IntegerType.decode('-1')
55
+ end
56
+
57
+ should "work with a leading +" do
58
+ assert_equal 1, CassandraObject::IntegerType.decode('+1')
59
+ end
60
+ end
61
+ end
62
+
63
+ context "FloatType" do
64
+ context "encode" do
65
+ should "should reject an Integer" do
66
+ assert_raises(ArgumentError) {
67
+ CassandraObject::FloatType.encode(1)
68
+ }
69
+ end
70
+
71
+ should "reject a string" do
72
+ assert_raises(ArgumentError) {
73
+ CassandraObject::FloatType.encode('asdf')
74
+ }
75
+ end
76
+
77
+ should "return a string" do
78
+ assert_equal '1.0', CassandraObject::FloatType.encode(1.0)
79
+ end
80
+
81
+ should "accept a negative number" do
82
+ assert_equal '-1.0', CassandraObject::FloatType.encode(-1.0)
83
+ end
84
+ end
85
+
86
+ context "decode" do
87
+ should "reject a non-float" do
88
+ assert_raises(ArgumentError) {
89
+ CassandraObject::FloatType.decode('asdf')
90
+ }
91
+ end
92
+
93
+ should "work on a float" do
94
+ assert_equal 1.2, CassandraObject::FloatType.decode('1.2')
95
+ end
96
+
97
+ should "work on a negative number" do
98
+ assert_equal -1.2, CassandraObject::FloatType.decode('-1.2')
99
+ end
100
+
101
+ should "work with a leading +" do
102
+ assert_equal 1.2, CassandraObject::FloatType.decode('+1.2')
103
+ end
104
+ end
105
+ end
106
+
107
+ context "DateType" do
108
+ context "encode" do
109
+ should "accept a valid Date object" do
110
+ assert_nothing_raised {
111
+ CassandraObject::DateType.encode(Date.today)
112
+ }
113
+ end
114
+
115
+ should "reject a String, even if it looks like a date" do
116
+ assert_raises(ArgumentError) { CassandraObject::DateType.encode('2009-10-31') }
117
+ end
118
+
119
+ should "reject an Integer" do
120
+ assert_raises(ArgumentError) { CassandraObject::DateType.encode(12) }
121
+ end
122
+
123
+ should "return a properly formatted string" do
124
+ str = '2009-10-31'
125
+ date = Date.strptime(str)
126
+ assert_equal str, CassandraObject::DateType.encode(date)
127
+ end
128
+ end
129
+
130
+ context "decode" do
131
+ should "work on a properly formatted string" do
132
+ s = '2009-10-31'
133
+ assert_equal Date.strptime(s), CassandraObject::DateType.decode(s)
134
+ end
135
+
136
+ should "reject a badly formatted string" do
137
+ assert_raises(ArgumentError) {
138
+ CassandraObject::DateType.decode('asdf')
139
+ }
140
+ end
141
+ end
142
+ end
143
+
144
+ context "TimeType" do
145
+ context "#encode" do
146
+ should "accept a Time" do
147
+ assert_nothing_raised {CassandraObject::TimeType.encode(Time.now)}
148
+ end
149
+
150
+ should "return a properly formated string for a legit time" do
151
+ t = Time.now
152
+ formatted = t.xmlschema(6)
153
+ assert_equal(formatted, CassandraObject::TimeType.encode(t))
154
+ end
155
+
156
+ should "reject things other than Time" do
157
+ assert_raises(ArgumentError) {
158
+ CassandraObject::TimeType.encode(1)
159
+ }
160
+ assert_raises(ArgumentError) {
161
+ CassandraObject::TimeType.encode('asdf')
162
+ }
163
+ end
164
+ end
165
+
166
+ context "#decode" do
167
+ should "parse a valid string and return a Time (with timezone)" do
168
+ time = Time.xmlschema(str = "2009-11-05T13:24:07-08:00")
169
+ assert_nothing_raised {
170
+ assert_equal time, CassandraObject::TimeType.decode(str)
171
+ }
172
+ end
173
+
174
+ should "parse a valid string and return a Time (with UTC)" do
175
+ time = Time.xmlschema(str = "2009-11-05T13:24:07Z")
176
+ assert_nothing_raised {
177
+ assert_equal time, CassandraObject::TimeType.decode(str)
178
+ }
179
+ end
180
+ end
181
+ end
182
+
183
+ context "HashType" do
184
+ context "encode" do
185
+ should "handle an empty Hash" do
186
+ assert_nothing_raised {
187
+ assert_equal({}.to_json, CassandraObject::HashType.encode({}))
188
+ }
189
+ end
190
+
191
+ should "handle string keys" do
192
+ assert_nothing_raised {
193
+ h = {'foo' => 'bar'}
194
+ assert_equal(h.to_json, CassandraObject::HashType.encode(h))
195
+ }
196
+ end
197
+
198
+ should "handle symbol keys" do
199
+ assert_nothing_raised {
200
+ h = {:foo => 'bar'}
201
+ assert_equal(h.to_json, CassandraObject::HashType.encode(h))
202
+ }
203
+ end
204
+ end
205
+ end
206
+
207
+ context "BooleanType" do
208
+ context "encode" do
209
+ should "handle true" do
210
+ assert_nothing_raised {
211
+ assert_equal '1', CassandraObject::BooleanType.encode(true)
212
+ }
213
+ end
214
+
215
+ should "handle false" do
216
+ assert_nothing_raised {
217
+ assert_equal '0', CassandraObject::BooleanType.encode(false)
218
+ }
219
+ end
220
+
221
+ should "handle nil" do
222
+ assert_nothing_raised {
223
+ assert_equal '0', CassandraObject::BooleanType.encode(nil)
224
+ }
225
+ end
226
+
227
+ should "not handle normal objects" do
228
+ assert_raises(ArgumentError) {
229
+ assert_equal '1', CassandraObject::BooleanType.encode(Object.new)
230
+ }
231
+ end
232
+ end
233
+
234
+ context "decode" do
235
+ should "handle true" do
236
+ assert_nothing_raised {
237
+ assert_equal true, CassandraObject::BooleanType.decode('1')
238
+ }
239
+ end
240
+
241
+ should "handle false" do
242
+ assert_nothing_raised {
243
+ assert_equal false, CassandraObject::BooleanType.decode('0')
244
+ }
245
+ end
246
+
247
+ should "handle bad data as false" do
248
+ assert_equal false, CassandraObject::BooleanType.decode('')
249
+ end
250
+ end
251
+ end
252
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class ValidationTest < CassandraObjectTestCase
4
+
5
+ test "save! raises an error" do
6
+ begin
7
+ customer = Customer.new :first_name=>"steve", :date_of_birth=>Date.parse("1979/12/25")
8
+ customer.save!
9
+ flunk "Should have failed to save"
10
+ rescue CassandraObject::Validation::RecordInvalidError => e
11
+ assert_equal customer, e.record
12
+ end
13
+ end
14
+
15
+ test "create! raises an error" do
16
+ begin
17
+ customer = Customer.create! :first_name=>"steve", :date_of_birth=>Date.parse("1979/12/25")
18
+ flunk "Should have failed to create!"
19
+ rescue CassandraObject::Validation::RecordInvalidError => e
20
+ assert_kind_of Customer, e.record
21
+ end
22
+ end
23
+
24
+
25
+ end
@@ -0,0 +1,36 @@
1
+ # The filename is so that this test gets loaded last, since it depends on all the other tests.
2
+ # Suggestions of less janky ways to do this would be appreciated. -rk
3
+
4
+ require 'test_helper'
5
+
6
+ class MockTest < CassandraObjectTestCase
7
+ context CassandraObject::Base do
8
+ should "respond to use_mock!" do
9
+ assert CassandraObject::Base.respond_to?(:use_mock!)
10
+ end
11
+ context "use_mock!" do
12
+ should "set the connection_class" do
13
+ CassandraObject::Base.use_mock!
14
+ assert_equal Cassandra::Mock, CassandraObject::Base.connection_class
15
+ CassandraObject::Base.use_mock!(false)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ module MockTestMixin
22
+ def setup
23
+ CassandraObject::Base.use_mock!
24
+ super
25
+ end
26
+
27
+ def teardown
28
+ CassandraObject::Base.use_mock!(false)
29
+ end
30
+ end
31
+
32
+ %w[BasicScenarios Cursor Dirty Index Migration OneToManyAssociations Time Types Validation].each do |test|
33
+ klass = Class.new(Kernel.const_get(test + 'Test'))
34
+ klass.send(:include, MockTestMixin)
35
+ Kernel.const_set(test + 'WithMockTest', klass)
36
+ end
metadata ADDED
@@ -0,0 +1,243 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gotime-cassandra_object
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 6
8
+ - 1
9
+ version: 0.6.1
10
+ platform: ruby
11
+ authors:
12
+ - Michael Koziarski
13
+ - grantr
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-10 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ version: "3"
31
+ type: :runtime
32
+ prerelease: false
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activemodel
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 3
43
+ version: "3"
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: cassandra
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: *id003
60
+ - !ruby/object:Gem::Dependency
61
+ name: nokogiri
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: *id004
73
+ - !ruby/object:Gem::Dependency
74
+ name: shoulda
75
+ requirement: &id005 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: *id005
86
+ - !ruby/object:Gem::Dependency
87
+ name: bundler
88
+ requirement: &id006 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ segments:
94
+ - 1
95
+ - 0
96
+ - 0
97
+ version: 1.0.0
98
+ type: :development
99
+ prerelease: false
100
+ version_requirements: *id006
101
+ - !ruby/object:Gem::Dependency
102
+ name: jeweler
103
+ requirement: &id007 !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ~>
107
+ - !ruby/object:Gem::Version
108
+ segments:
109
+ - 1
110
+ - 5
111
+ - 1
112
+ version: 1.5.1
113
+ type: :development
114
+ prerelease: false
115
+ version_requirements: *id007
116
+ - !ruby/object:Gem::Dependency
117
+ name: rcov
118
+ requirement: &id008 !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ type: :development
127
+ prerelease: false
128
+ version_requirements: *id008
129
+ description: Cassandra ActiveModel
130
+ email: grantr@gmail.com
131
+ executables: []
132
+
133
+ extensions: []
134
+
135
+ extra_rdoc_files:
136
+ - LICENSE
137
+ - README.markdown
138
+ - TODO
139
+ files:
140
+ - CHANGELOG
141
+ - Gemfile
142
+ - Gemfile.lock
143
+ - LICENSE
144
+ - README.markdown
145
+ - Rakefile
146
+ - TODO
147
+ - VERSION
148
+ - gotime-cassandra_object.gemspec
149
+ - lib/cassandra_object.rb
150
+ - lib/cassandra_object/associations.rb
151
+ - lib/cassandra_object/associations/one_to_many.rb
152
+ - lib/cassandra_object/associations/one_to_one.rb
153
+ - lib/cassandra_object/attributes.rb
154
+ - lib/cassandra_object/base.rb
155
+ - lib/cassandra_object/callbacks.rb
156
+ - lib/cassandra_object/collection.rb
157
+ - lib/cassandra_object/cursor.rb
158
+ - lib/cassandra_object/dirty.rb
159
+ - lib/cassandra_object/identity.rb
160
+ - lib/cassandra_object/identity/abstract_key_factory.rb
161
+ - lib/cassandra_object/identity/key.rb
162
+ - lib/cassandra_object/identity/natural_key_factory.rb
163
+ - lib/cassandra_object/identity/uuid_key_factory.rb
164
+ - lib/cassandra_object/indexes.rb
165
+ - lib/cassandra_object/log_subscriber.rb
166
+ - lib/cassandra_object/migrations.rb
167
+ - lib/cassandra_object/mocking.rb
168
+ - lib/cassandra_object/persistence.rb
169
+ - lib/cassandra_object/serialization.rb
170
+ - lib/cassandra_object/type_registration.rb
171
+ - lib/cassandra_object/types.rb
172
+ - lib/cassandra_object/validation.rb
173
+ - test/basic_scenarios_test.rb
174
+ - test/callbacks_test.rb
175
+ - test/config/cassandra.in.sh
176
+ - test/config/log4j.properties
177
+ - test/config/storage-conf.xml
178
+ - test/connection.rb
179
+ - test/cursor_test.rb
180
+ - test/dirty_test.rb
181
+ - test/fixture_models.rb
182
+ - test/identity/natural_key_factory_test.rb
183
+ - test/index_test.rb
184
+ - test/legacy/test_helper.rb
185
+ - test/migration_test.rb
186
+ - test/one_to_many_associations_test.rb
187
+ - test/test_case.rb
188
+ - test/test_helper.rb
189
+ - test/time_test.rb
190
+ - test/types_test.rb
191
+ - test/validation_test.rb
192
+ - test/z_mock_test.rb
193
+ has_rdoc: true
194
+ homepage: http://github.com/gotime/cassandra_object
195
+ licenses:
196
+ - MIT
197
+ post_install_message:
198
+ rdoc_options: []
199
+
200
+ require_paths:
201
+ - lib
202
+ required_ruby_version: !ruby/object:Gem::Requirement
203
+ none: false
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ hash: 2344157015665262405
208
+ segments:
209
+ - 0
210
+ version: "0"
211
+ required_rubygems_version: !ruby/object:Gem::Requirement
212
+ none: false
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ segments:
217
+ - 0
218
+ version: "0"
219
+ requirements: []
220
+
221
+ rubyforge_project:
222
+ rubygems_version: 1.3.7
223
+ signing_key:
224
+ specification_version: 3
225
+ summary: Cassandra ActiveModel
226
+ test_files:
227
+ - test/basic_scenarios_test.rb
228
+ - test/callbacks_test.rb
229
+ - test/connection.rb
230
+ - test/cursor_test.rb
231
+ - test/dirty_test.rb
232
+ - test/fixture_models.rb
233
+ - test/identity/natural_key_factory_test.rb
234
+ - test/index_test.rb
235
+ - test/legacy/test_helper.rb
236
+ - test/migration_test.rb
237
+ - test/one_to_many_associations_test.rb
238
+ - test/test_case.rb
239
+ - test/test_helper.rb
240
+ - test/time_test.rb
241
+ - test/types_test.rb
242
+ - test/validation_test.rb
243
+ - test/z_mock_test.rb