cassandra_object_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +3 -0
  3. data/.travis.yml +7 -0
  4. data/CHANGELOG +5 -0
  5. data/Gemfile +8 -0
  6. data/LICENSE +13 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.rdoc +97 -0
  9. data/Rakefile +12 -0
  10. data/cassandra_object_rails.gemspec +26 -0
  11. data/lib/cassandra_object/attribute_methods.rb +87 -0
  12. data/lib/cassandra_object/attribute_methods/definition.rb +19 -0
  13. data/lib/cassandra_object/attribute_methods/dirty.rb +44 -0
  14. data/lib/cassandra_object/attribute_methods/primary_key.rb +25 -0
  15. data/lib/cassandra_object/attribute_methods/typecasting.rb +59 -0
  16. data/lib/cassandra_object/base.rb +69 -0
  17. data/lib/cassandra_object/belongs_to.rb +63 -0
  18. data/lib/cassandra_object/belongs_to/association.rb +48 -0
  19. data/lib/cassandra_object/belongs_to/builder.rb +40 -0
  20. data/lib/cassandra_object/belongs_to/reflection.rb +30 -0
  21. data/lib/cassandra_object/callbacks.rb +29 -0
  22. data/lib/cassandra_object/config.rb +15 -0
  23. data/lib/cassandra_object/connection.rb +36 -0
  24. data/lib/cassandra_object/consistency.rb +18 -0
  25. data/lib/cassandra_object/core.rb +59 -0
  26. data/lib/cassandra_object/errors.rb +6 -0
  27. data/lib/cassandra_object/identity.rb +24 -0
  28. data/lib/cassandra_object/inspect.rb +25 -0
  29. data/lib/cassandra_object/log_subscriber.rb +29 -0
  30. data/lib/cassandra_object/persistence.rb +169 -0
  31. data/lib/cassandra_object/rails_initializer.rb +19 -0
  32. data/lib/cassandra_object/railtie.rb +11 -0
  33. data/lib/cassandra_object/savepoints.rb +79 -0
  34. data/lib/cassandra_object/schema.rb +78 -0
  35. data/lib/cassandra_object/schema/tasks.rb +48 -0
  36. data/lib/cassandra_object/scope.rb +48 -0
  37. data/lib/cassandra_object/scope/batches.rb +32 -0
  38. data/lib/cassandra_object/scope/finder_methods.rb +47 -0
  39. data/lib/cassandra_object/scope/query_methods.rb +111 -0
  40. data/lib/cassandra_object/scoping.rb +19 -0
  41. data/lib/cassandra_object/serialization.rb +6 -0
  42. data/lib/cassandra_object/tasks/cassandra.rake +53 -0
  43. data/lib/cassandra_object/timestamps.rb +19 -0
  44. data/lib/cassandra_object/type.rb +16 -0
  45. data/lib/cassandra_object/types.rb +8 -0
  46. data/lib/cassandra_object/types/array_type.rb +76 -0
  47. data/lib/cassandra_object/types/base_type.rb +26 -0
  48. data/lib/cassandra_object/types/boolean_type.rb +20 -0
  49. data/lib/cassandra_object/types/date_type.rb +17 -0
  50. data/lib/cassandra_object/types/float_type.rb +16 -0
  51. data/lib/cassandra_object/types/integer_type.rb +16 -0
  52. data/lib/cassandra_object/types/json_type.rb +52 -0
  53. data/lib/cassandra_object/types/string_type.rb +15 -0
  54. data/lib/cassandra_object/types/time_type.rb +16 -0
  55. data/lib/cassandra_object/validations.rb +44 -0
  56. data/lib/cassandra_object_rails.rb +64 -0
  57. data/test/support/connect.rb +17 -0
  58. data/test/support/issue.rb +5 -0
  59. data/test/support/teardown.rb +24 -0
  60. data/test/test_helper.rb +34 -0
  61. data/test/unit/active_model_test.rb +18 -0
  62. data/test/unit/attribute_methods/definition_test.rb +13 -0
  63. data/test/unit/attribute_methods/dirty_test.rb +71 -0
  64. data/test/unit/attribute_methods/primary_key_test.rb +26 -0
  65. data/test/unit/attribute_methods/typecasting_test.rb +112 -0
  66. data/test/unit/attribute_methods_test.rb +39 -0
  67. data/test/unit/base_test.rb +20 -0
  68. data/test/unit/belongs_to/reflection_test.rb +12 -0
  69. data/test/unit/belongs_to_test.rb +62 -0
  70. data/test/unit/callbacks_test.rb +46 -0
  71. data/test/unit/config_test.rb +23 -0
  72. data/test/unit/connection_test.rb +10 -0
  73. data/test/unit/consistency_test.rb +13 -0
  74. data/test/unit/core_test.rb +55 -0
  75. data/test/unit/identity_test.rb +26 -0
  76. data/test/unit/inspect_test.rb +26 -0
  77. data/test/unit/log_subscriber_test.rb +22 -0
  78. data/test/unit/persistence_test.rb +187 -0
  79. data/test/unit/savepoints_test.rb +35 -0
  80. data/test/unit/schema/tasks_test.rb +29 -0
  81. data/test/unit/schema_test.rb +47 -0
  82. data/test/unit/scope/batches_test.rb +30 -0
  83. data/test/unit/scope/finder_methods_test.rb +51 -0
  84. data/test/unit/scope/query_methods_test.rb +26 -0
  85. data/test/unit/scoping_test.rb +7 -0
  86. data/test/unit/timestamps_test.rb +27 -0
  87. data/test/unit/types/array_type_test.rb +71 -0
  88. data/test/unit/types/base_type_test.rb +24 -0
  89. data/test/unit/types/boolean_type_test.rb +24 -0
  90. data/test/unit/types/date_type_test.rb +11 -0
  91. data/test/unit/types/float_type_test.rb +17 -0
  92. data/test/unit/types/integer_type_test.rb +19 -0
  93. data/test/unit/types/json_type_test.rb +77 -0
  94. data/test/unit/types/string_type_test.rb +32 -0
  95. data/test/unit/types/time_type_test.rb +14 -0
  96. data/test/unit/validations_test.rb +27 -0
  97. metadata +208 -0
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::InspectTest < CassandraObject::TestCase
4
+ test 'attribute_for_inspect' do
5
+ object = temp_object do
6
+ string :long_string
7
+ time :the_time
8
+ integer :other
9
+ end
10
+
11
+ assert_equal "#{'x' * 51}...".inspect, object.new.attribute_for_inspect('x' * 100)
12
+ assert_equal "2012-02-14 12:01:02".inspect, object.new.attribute_for_inspect(Time.new(2012, 02, 14, 12, 01, 02))
13
+ assert_equal "\"foo\"", object.new.attribute_for_inspect('foo')
14
+ end
15
+
16
+ test 'inspect' do
17
+ object = temp_object do
18
+ string :description
19
+ integer :price
20
+ end.new(description: "yeah buddy", price: nil)
21
+
22
+ assert_match /id/, object.inspect
23
+ assert_match /description/, object.inspect
24
+ assert_no_match /price/, object.inspect
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ require "test_helper"
2
+ require "active_support/log_subscriber/test_helper"
3
+ require "cassandra_object/log_subscriber"
4
+
5
+ class CassandraObject::LogSubscriberTest < CassandraObject::TestCase
6
+ include ActiveSupport::LogSubscriber::TestHelper
7
+
8
+ def setup
9
+ super
10
+
11
+ CassandraObject::LogSubscriber.attach_to :cassandra_object
12
+ end
13
+
14
+ def test_cql_notification
15
+ Issue.execute_cql "SELECT * FROM Issues"
16
+
17
+ wait
18
+
19
+ assert_equal 1, @logger.logged(:debug).size
20
+ assert_match "SELECT * FROM Issues", @logger.logged(:debug)[0]
21
+ end
22
+ end
@@ -0,0 +1,187 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'test_helper'
5
+
6
+ class CassandraObject::PersistenceTest < CassandraObject::TestCase
7
+ test 'encode_attributes' do
8
+ klass = temp_object do
9
+ string :description
10
+ end
11
+
12
+ assert_equal(
13
+ {},
14
+ klass.encode_attributes({})
15
+ )
16
+
17
+ assert_equal(
18
+ {},
19
+ klass.encode_attributes({description: nil})
20
+ )
21
+
22
+ assert_equal(
23
+ {'description' => 'lol'},
24
+ klass.encode_attributes({description: 'lol'})
25
+ )
26
+ end
27
+
28
+ test "batch" do
29
+ first_issue = second_issue = nil
30
+
31
+ Issue.batch do
32
+ assert Issue.batching?
33
+
34
+ first_issue = Issue.create
35
+ second_issue = Issue.create
36
+
37
+ assert_raise(CassandraObject::RecordNotFound) { Issue.find(first_issue.id) }
38
+ assert_raise(CassandraObject::RecordNotFound) { Issue.find(second_issue.id) }
39
+ end
40
+
41
+ assert !Issue.batching?
42
+ assert_nothing_raised(CassandraObject::RecordNotFound) { Issue.find(first_issue.id) }
43
+ assert_nothing_raised(CassandraObject::RecordNotFound) { Issue.find(second_issue.id) }
44
+ end
45
+
46
+ test 'persistance inquiries' do
47
+ issue = Issue.new
48
+ assert issue.new_record?
49
+ assert !issue.persisted?
50
+
51
+ issue.save
52
+ assert issue.persisted?
53
+ assert !issue.new_record?
54
+ end
55
+
56
+ test 'create' do
57
+ issue = Issue.create { |i| i.description = 'foo' }
58
+ assert_equal 'foo', issue.description
59
+ assert_equal 'foo', Issue.find(issue.id).description
60
+ end
61
+
62
+ test 'read and write UTF' do
63
+ utf = "\ucba1\ucba2\ucba3 ƒ´∑ƒ©√åµ≈√ˆअनुच्छेद´µøµø¬≤ 汉语漢語".force_encoding(Encoding::UTF_8)
64
+
65
+ issue = Issue.create { |i| i.description = utf }
66
+ assert_equal utf, issue.description
67
+ reloaded = Issue.find(issue.id).description
68
+ assert_equal utf, reloaded
69
+ end
70
+
71
+ test 'save' do
72
+ issue = Issue.new
73
+ issue.save
74
+
75
+ assert_equal issue, Issue.find(issue.id)
76
+ end
77
+
78
+ test 'save!' do
79
+ klass = temp_object do
80
+ string :description
81
+ validates :description, presence: true
82
+ end
83
+
84
+ record = klass.new(description: 'bad')
85
+ record.save!
86
+
87
+ assert_raise CassandraObject::RecordInvalid do
88
+ record = klass.new
89
+ record.save!
90
+ end
91
+ end
92
+
93
+ test 'destroy' do
94
+ issue = Issue.create
95
+ issue.destroy
96
+
97
+ assert issue.destroyed?
98
+ assert !issue.persisted?
99
+ assert !issue.new_record?
100
+ end
101
+
102
+ test 'update_attribute' do
103
+ issue = Issue.create
104
+ issue.update_attribute(:description, 'lol')
105
+
106
+ assert !issue.changed?
107
+ assert_equal 'lol', issue.description
108
+ end
109
+
110
+ test 'update_attributes' do
111
+ issue = Issue.create
112
+ issue.update_attributes(description: 'lol')
113
+
114
+ assert !issue.changed?
115
+ assert_equal 'lol', issue.description
116
+ end
117
+
118
+ test 'update_attributes!' do
119
+ begin
120
+ Issue.validates(:description, presence: true)
121
+
122
+ issue = Issue.new(description: 'bad')
123
+ issue.save!
124
+
125
+ assert_raise CassandraObject::RecordInvalid do
126
+ issue.update_attributes! description: ''
127
+ end
128
+ ensure
129
+ Issue.reset_callbacks(:validate)
130
+ end
131
+ end
132
+
133
+ test 'update nil attributes' do
134
+ issue = Issue.create(title: 'I rule', description: 'lololol')
135
+
136
+ issue.update_attributes title: nil
137
+
138
+ issue = Issue.find issue.id
139
+ assert_nil issue.title
140
+ end
141
+
142
+ test 'becomes' do
143
+ klass = temp_object do
144
+ end
145
+
146
+ assert_kind_of klass, Issue.new.becomes(klass)
147
+ end
148
+
149
+ test 'reload' do
150
+ persisted_issue = Issue.create
151
+ fresh_issue = Issue.find(persisted_issue.id)
152
+ fresh_issue.update_attribute(:description, 'say what')
153
+
154
+ reloaded_issue = persisted_issue.reload
155
+ assert_equal 'say what', persisted_issue.description
156
+ assert_equal persisted_issue, reloaded_issue
157
+ end
158
+
159
+ test 'delete with consistency' do
160
+ issue = Issue.create
161
+ Issue.with_consistency 'QUORUM' do
162
+ issue.destroy
163
+ end
164
+ end
165
+
166
+ test 'insert with consistency' do
167
+ Issue.with_consistency 'QUORUM' do
168
+ Issue.create
169
+ end
170
+ end
171
+
172
+ test 'allow CQL keyword in column name' do
173
+ assert_nothing_raised do
174
+ Issue.string :text
175
+ issue = Issue.create :text => 'hello'
176
+ issue.text = 'world'
177
+ issue.save!
178
+ issue.text = nil
179
+ issue.save!
180
+ end
181
+ end
182
+
183
+ test 'quote_columns' do
184
+ klass = Class.new { include CassandraObject::Persistence }
185
+ assert_equal %w{'a' 'b'}, klass.__send__(:quote_columns, %w{a b})
186
+ end
187
+ end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::SavepointsTest < CassandraObject::TestCase
4
+ test 'rollback create' do
5
+ Issue.savepoint do
6
+ issue = Issue.create description: 'foo'
7
+ raise 'lol'
8
+ end
9
+
10
+ assert_nil Issue.first
11
+ end
12
+
13
+ test 'rollback update' do
14
+ issue = Issue.create description: 'foo'
15
+
16
+ Issue.savepoint do
17
+ issue.update_attributes description: 'bar'
18
+ raise 'lol'
19
+ end
20
+
21
+ issue = Issue.find issue.id
22
+ assert_equal 'foo', issue.description
23
+ end
24
+
25
+ test 'rollback destroy' do
26
+ issue = Issue.create description: 'foo'
27
+
28
+ Issue.savepoint do
29
+ issue.destroy
30
+ raise 'lol'
31
+ end
32
+
33
+ assert_nothing_raised { Issue.find issue.id }
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::Schema::TasksTest < CassandraObject::TestCase
4
+ test "column_families" do
5
+ assert_equal ['Issues'], CassandraObject::Schema.column_families
6
+ end
7
+
8
+ test "dump" do
9
+ io = StringIO.new
10
+
11
+ CassandraObject::Schema.dump(io)
12
+ io.rewind
13
+
14
+ assert_match /Issues/, io.read
15
+ end
16
+
17
+ test "load" do
18
+ CassandraObject::Base.expects(:execute_cql).with("DO STUFF;")
19
+ CassandraObject::Base.expects(:execute_cql).with("AND MORE;")
20
+
21
+ CassandraObject::Schema.load StringIO.new(
22
+ "DO\n" +
23
+ " STUFF;\n" +
24
+ "\n" +
25
+ "AND\n" +
26
+ " MORE;\n"
27
+ )
28
+ end
29
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::SchemaTest < CassandraObject::TestCase
4
+ test "create_column_family" do
5
+ CassandraObject::Schema.create_column_family 'TestRecords', 'compression_parameters:sstable_compression' => 'SnappyCompressor'
6
+
7
+ begin
8
+ CassandraObject::Schema.create_column_family 'TestRecords'
9
+ assert false, 'TestRecords should already exist'
10
+ rescue Exception => e
11
+ end
12
+ end
13
+
14
+ test "drop_column_family" do
15
+ CassandraObject::Schema.create_column_family 'TestCFToDrop'
16
+
17
+ CassandraObject::Schema.drop_column_family 'TestCFToDrop'
18
+
19
+ begin
20
+ CassandraObject::Schema.drop_column_family 'TestCFToDrop'
21
+ assert false, 'TestCFToDrop should not exist'
22
+ rescue Exception => e
23
+ end
24
+ end
25
+
26
+ test "create_index" do
27
+ CassandraObject::Schema.create_column_family 'TestIndexed'
28
+
29
+ CassandraObject::Schema.alter_column_family 'TestIndexed', "ADD id_value varchar"
30
+
31
+ CassandraObject::Schema.add_index 'TestIndexed', 'id_value'
32
+ end
33
+
34
+ test "drop_index" do
35
+ CassandraObject::Schema.create_column_family 'TestDropIndexes'
36
+
37
+ CassandraObject::Schema.alter_column_family 'TestDropIndexes', "ADD id_value1 varchar"
38
+ CassandraObject::Schema.alter_column_family 'TestDropIndexes', "ADD id_value2 varchar"
39
+
40
+ CassandraObject::Schema.add_index 'TestDropIndexes', 'id_value1'
41
+ CassandraObject::Schema.add_index 'TestDropIndexes', 'id_value2', 'special_name'
42
+
43
+ CassandraObject::Schema.drop_index 'TestDropIndexes_id_value1_idx'
44
+ CassandraObject::Schema.drop_index 'special_name'
45
+ end
46
+
47
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::BatchesTest < CassandraObject::TestCase
4
+ test 'find_each' do
5
+ Issue.create
6
+ Issue.create
7
+
8
+ issues = []
9
+ Issue.find_each do |issue|
10
+ issues << issue
11
+ end
12
+
13
+ assert_equal Issue.all.to_set, issues.to_set
14
+ end
15
+
16
+ test 'find_in_batches' do
17
+ Issue.create
18
+ Issue.create
19
+ Issue.create
20
+
21
+ issue_batches = []
22
+ Issue.find_in_batches(batch_size: 2) do |issues|
23
+ issue_batches << issues
24
+ end
25
+
26
+ assert_equal 2, issue_batches.size
27
+ assert issue_batches.any? { |issues| issues.size == 2 }
28
+ assert issue_batches.any? { |issues| issues.size == 1 }
29
+ end
30
+ end
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::FinderMethodsTest < CassandraObject::TestCase
4
+ test 'find' do
5
+ Issue.create.tap do |issue|
6
+ assert_equal issue, Issue.find(issue.id)
7
+ end
8
+
9
+ begin
10
+ Issue.find(nil)
11
+ assert false
12
+ rescue => e
13
+ assert_equal "Couldn't find Issue with key nil", e.message
14
+ end
15
+
16
+ assert_raise CassandraObject::RecordNotFound do
17
+ Issue.find('what')
18
+ end
19
+ end
20
+
21
+ test 'find with ids' do
22
+ first_issue = Issue.create
23
+ second_issue = Issue.create
24
+ third_issue = Issue.create
25
+
26
+ assert_equal [], Issue.find([])
27
+ assert_equal [first_issue, second_issue], Issue.find([first_issue.id, second_issue.id])
28
+ end
29
+
30
+ test 'find_by_id' do
31
+ Issue.create.tap do |issue|
32
+ assert_equal issue, Issue.find_by_id(issue.id)
33
+ end
34
+
35
+ assert_nil Issue.find_by_id('what')
36
+ end
37
+
38
+ test 'all' do
39
+ first_issue = Issue.create
40
+ second_issue = Issue.create
41
+
42
+ assert_equal [first_issue, second_issue].to_set, Issue.all.to_set
43
+ end
44
+
45
+ test 'first' do
46
+ first_issue = Issue.create
47
+ second_issue = Issue.create
48
+
49
+ assert [first_issue, second_issue].include?(Issue.first)
50
+ end
51
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::Scope::QueryMethodsTest < CassandraObject::TestCase
4
+ test "select" do
5
+ original_issue = Issue.create title: 'foo', description: 'bar'
6
+
7
+ found_issue = Issue.select(:title).find(original_issue.id)
8
+
9
+ assert_equal 'foo', found_issue.title
10
+ assert_equal original_issue.id, found_issue.id
11
+ assert_nil found_issue.description
12
+ end
13
+
14
+ test "select with block" do
15
+ foo_issue = Issue.create title: 'foo'
16
+ bar_issue = Issue.create title: 'bar'
17
+
18
+ assert_equal [foo_issue], Issue.select { |issue| issue.title == 'foo' }
19
+ end
20
+
21
+ test "select with consistency" do
22
+ Issue.with_consistency 'ONE' do
23
+ Issue.all
24
+ end
25
+ end
26
+ end