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::AttributeMethods::PrimaryKeyTest < CassandraObject::TestCase
4
+ test 'get id' do
5
+ model = temp_object do
6
+ key do
7
+ "foo"
8
+ end
9
+ end
10
+ record = model.new
11
+
12
+ assert_equal 'foo', record.id
13
+ end
14
+
15
+ test 'set id' do
16
+ issue = Issue.new id: 'foo'
17
+
18
+ assert_equal 'foo', issue.id
19
+ end
20
+
21
+ test 'attributes' do
22
+ issue = Issue.new
23
+
24
+ assert_not_nil issue.attributes['id']
25
+ end
26
+ end
@@ -0,0 +1,112 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::AttributeMethods::TypecastingTest < CassandraObject::TestCase
4
+ class CustomType
5
+ end
6
+
7
+ class CustomCoder < CassandraObject::Types::BaseType
8
+ end
9
+
10
+ class TestIssue < CassandraObject::Base
11
+ self.column_family = 'Issues'
12
+
13
+ attribute :custom_column, type: CustomType, coder: CustomCoder
14
+ boolean :enabled
15
+ float :rating
16
+ integer :price
17
+ json :orders
18
+ string :name
19
+ end
20
+
21
+ class TestChildIssue < TestIssue
22
+ string :description
23
+ end
24
+
25
+ test 'attributes not shared' do
26
+ assert_nothing_raised { Issue.new.description }
27
+ assert_raise(NoMethodError) { TestIssue.new.description }
28
+ assert_nothing_raised { TestChildIssue.new.description }
29
+ end
30
+
31
+ test 'custom attribute definer' do
32
+ model_attribute = TestIssue.attribute_definitions[:custom_column]
33
+
34
+ assert_kind_of CustomCoder, model_attribute.coder
35
+ assert_equal 'custom_column', model_attribute.name
36
+ end
37
+
38
+ test 'typecast_attribute' do
39
+ assert_equal 1, TestIssue.typecast_attribute(TestIssue.new, 'price', 1)
40
+ assert_equal 1, TestIssue.typecast_attribute(TestIssue.new, :price, 1)
41
+
42
+ assert_raise NoMethodError do
43
+ TestIssue.typecast_attribute(TestIssue.new, 'wtf', 1)
44
+ end
45
+ end
46
+
47
+ test 'boolean attribute' do
48
+ issue = TestIssue.create! enabled: '1'
49
+ assert_equal true, issue.enabled
50
+
51
+ issue = TestIssue.find issue.id
52
+ assert_equal true, issue.enabled
53
+ end
54
+
55
+ test 'float attribute' do
56
+ issue = TestIssue.create! rating: '4.5'
57
+ assert_equal 4.5, issue.rating
58
+
59
+ issue = TestIssue.find issue.id
60
+ assert_equal(4.5, issue.rating)
61
+ end
62
+
63
+ test 'integer attribute' do
64
+ issue = TestIssue.create! price: '101'
65
+ assert_equal 101, issue.price
66
+
67
+ issue = TestIssue.find issue.id
68
+ assert_equal(101, issue.price)
69
+ end
70
+
71
+ test 'json attribute' do
72
+ issue = TestIssue.create! orders: {'a' => 'b'}
73
+ assert_equal({'a' => 'b'}, issue.orders)
74
+
75
+ issue = TestIssue.find issue.id
76
+ assert_equal({'a' => 'b'}, issue.orders)
77
+ end
78
+
79
+ test 'string attribute' do
80
+ issue = TestIssue.create! name: 'hola'
81
+ assert_equal('hola', issue.name)
82
+
83
+ issue = TestIssue.find issue.id
84
+ assert_equal('hola', issue.name)
85
+ end
86
+
87
+ test 'multiple attributes definition' do
88
+ class MultipleAttributesIssue < CassandraObject::Base
89
+ self.column_family = 'Issues'
90
+ end
91
+
92
+ assert_nothing_raised {
93
+ MultipleAttributesIssue.string :hello, :greetings, :bye
94
+ }
95
+ issue = MultipleAttributesIssue.new :hello => 'hey', :greetings => 'how r u', :bye => 'see ya'
96
+
97
+ assert_equal 'how r u', issue.greetings
98
+ end
99
+
100
+ test 'multiple attributes with options' do
101
+ class MultipleAttributesIssue < CassandraObject::Base
102
+ self.column_family = 'Issues'
103
+ end
104
+
105
+ MultipleAttributesIssue.expects(:attribute).with(:hello, { :unique => :true, :type => :string })
106
+ MultipleAttributesIssue.expects(:attribute).with(:world, { :unique => :true, :type => :string })
107
+
108
+ class MultipleAttributesIssue < CassandraObject::Base
109
+ string :hello, :world, :unique => :true
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::AttributeMethodsTest < CassandraObject::TestCase
4
+ test 'read and write attributes' do
5
+ issue = Issue.new
6
+ assert_nil issue.read_attribute(:description)
7
+
8
+ issue.write_attribute(:description, nil)
9
+ assert_nil issue.read_attribute(:description)
10
+
11
+ issue.write_attribute(:description, 'foo')
12
+ assert_equal 'foo', issue.read_attribute(:description)
13
+ end
14
+
15
+ test 'hash accessor aliases' do
16
+ issue = Issue.new
17
+
18
+ issue[:description] = 'bar'
19
+
20
+ assert_equal 'bar', issue[:description]
21
+ end
22
+
23
+ # test 'attribute_exists' do
24
+ # assert !Issue.new.attribute_exists?(:description)
25
+ # assert Issue.new(description: nil).attribute_exists?(:description)
26
+ # assert Issue.new(description: false).attribute_exists?(:description)
27
+ # assert Issue.new(description: 'hey').attribute_exists?(:description)
28
+ # end
29
+
30
+ test 'attributes setter' do
31
+ issue = Issue.new
32
+
33
+ issue.attributes = {
34
+ description: 'foo'
35
+ }
36
+
37
+ assert_equal 'foo', issue.description
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::BaseTest < CassandraObject::TestCase
4
+ class Son < CassandraObject::Base
5
+ end
6
+
7
+ class Grandson < Son
8
+ end
9
+
10
+ test 'base_class' do
11
+ assert_equal CassandraObject::Base, CassandraObject::Base
12
+ assert_equal Son, Son.base_class
13
+ assert_equal Son, Grandson.base_class
14
+ end
15
+
16
+ test 'column family' do
17
+ assert_equal 'CassandraObject::BaseTest::Sons', Son.column_family
18
+ assert_equal 'CassandraObject::BaseTest::Sons', Grandson.column_family
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::BelongsTo::ReflectionTest < CassandraObject::TestCase
4
+ class ::Status < CassandraObject::Base; end
5
+ class ::User < CassandraObject::Base
6
+ belongs_to :status
7
+ end
8
+
9
+ test 'class_name' do
10
+ assert_equal 'Status', User.new.belongs_to_reflections[:status].class_name
11
+ end
12
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::BelongsToTest < CassandraObject::TestCase
4
+ class TestObject < Issue
5
+ string :issue_id
6
+ belongs_to :issue
7
+
8
+ string :widget_id
9
+ belongs_to :widget, class_name: 'Issue'
10
+
11
+ string :target_id
12
+ string :target_type
13
+ belongs_to :target, polymorphic: true
14
+ end
15
+
16
+ test 'belongs_to' do
17
+ issue = Issue.create
18
+
19
+ record = TestObject.create(issue: issue)
20
+
21
+ assert_equal issue, record.issue
22
+ assert_equal issue.id, record.issue_id
23
+
24
+ record = TestObject.find(record.id)
25
+ assert_equal issue, record.issue
26
+ end
27
+
28
+ test 'belongs_to with class_name' do
29
+ issue = Issue.create
30
+
31
+ record = TestObject.create(widget: issue)
32
+
33
+ assert_equal issue, record.widget
34
+ assert_equal issue.id, record.widget_id
35
+
36
+ record = TestObject.find(record.id)
37
+ assert_equal issue, record.widget
38
+ end
39
+
40
+ test 'belongs_to with polymorphic' do
41
+ issue = Issue.create
42
+
43
+ record = TestObject.create(target: issue)
44
+
45
+ assert_equal issue, record.target
46
+ assert_equal issue.id, record.target_id
47
+ assert_equal 'Issue', record.target_type
48
+
49
+ record = TestObject.find(record.id)
50
+ assert_equal issue, record.target
51
+ end
52
+
53
+ test 'belongs_to clear cache after reload' do
54
+ issue = Issue.create
55
+ record = TestObject.create(issue: issue)
56
+ issue.destroy
57
+
58
+ assert_not_nil record.issue
59
+ assert_nil TestObject.find(record.id).issue
60
+ assert_nil record.reload.issue
61
+ end
62
+ end
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::CallbacksTest < CassandraObject::TestCase
4
+ class TestIssue < CassandraObject::Base
5
+ self.column_family = 'Issues'
6
+ string :description
7
+
8
+ %w(before_validation after_validation after_save after_create after_update after_destroy).each do |method|
9
+ send(method) do
10
+ callback_history << method
11
+ end
12
+ end
13
+
14
+ def reset_callback_history
15
+ @callback_history = []
16
+ end
17
+
18
+ def callback_history
19
+ @callback_history ||= []
20
+ end
21
+ end
22
+
23
+ test 'create' do
24
+ issue = TestIssue.create
25
+
26
+ assert_equal ['before_validation', 'after_validation', 'after_save', 'after_create'], issue.callback_history
27
+ end
28
+
29
+ test 'update' do
30
+ issue = TestIssue.create
31
+ issue.reset_callback_history
32
+
33
+ issue.update_attribute :description, 'foo'
34
+
35
+ assert_equal ['after_save', 'after_update'], issue.callback_history
36
+ end
37
+
38
+ test 'destroy' do
39
+ issue = TestIssue.create
40
+ issue.reset_callback_history
41
+
42
+ issue.destroy
43
+
44
+ assert_equal ['after_destroy'], issue.callback_history
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::ConfigTest < CassandraObject::TestCase
4
+ test 'config writer' do
5
+ config = CassandraObject::Config.new(
6
+ keyspace: 'place_directory_development',
7
+ servers: '192.168.0.100:9160',
8
+ thrift: {'timeout' => 10},
9
+ keyspace_options: {'placement_strategy' => 'NetworkTopologyStrategy'}
10
+ )
11
+
12
+ assert_equal ['192.168.0.100:9160'], config.servers
13
+ assert_equal 'place_directory_development', config.keyspace
14
+ assert_equal 10, config.thrift_options[:timeout]
15
+ assert_equal 'NetworkTopologyStrategy', config.keyspace_options[:placement_strategy]
16
+ end
17
+
18
+ test 'defaults' do
19
+ config = CassandraObject::Config.new(keyspace: 'widget_factory')
20
+
21
+ assert_equal ["127.0.0.1:9160"], config.servers
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::ConnectionTest < CassandraObject::TestCase
4
+ class TestObject < CassandraObject::Base
5
+ end
6
+
7
+ test "sanitize supports question marks" do
8
+ assert_equal 'hello ?', CassandraCQL::Statement.sanitize('hello ?')
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ require "test_helper"
2
+
3
+ class CassandraObject::ConsistencyTest < CassandraObject::TestCase
4
+ test "with_consistency" do
5
+ original = CassandraObject::Base.default_consistency
6
+
7
+ CassandraObject::Base.with_consistency 'LOL' do
8
+ assert_equal 'LOL', CassandraObject::Base.default_consistency
9
+ end
10
+
11
+ assert_equal original, CassandraObject::Base.default_consistency
12
+ end
13
+ end
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::CoreTest < CassandraObject::TestCase
4
+ test 'initialiaze' do
5
+ issue = Issue.new
6
+
7
+ assert issue.new_record?
8
+ assert !issue.destroyed?
9
+ end
10
+
11
+ test 'initialize yields self' do
12
+ issue = Issue.new { |i| i.description = 'bar' }
13
+ assert_equal 'bar', issue.description
14
+ end
15
+
16
+ test 'dup' do
17
+ issue = Issue.create description: 'foo'
18
+
19
+ dup_issue = issue.dup
20
+
21
+ assert dup_issue.new_record?
22
+ assert_not_equal issue.id, dup_issue.id
23
+ assert_nil dup_issue.created_at
24
+ assert_nil dup_issue.updated_at
25
+ assert_equal 'foo', issue.description
26
+ end
27
+
28
+ test 'equality of new records' do
29
+ assert_not_equal Issue.new, Issue.new
30
+ end
31
+
32
+ test 'equality' do
33
+ first_issue = Issue.create
34
+ second_issue = Issue.create
35
+
36
+ assert_equal first_issue, first_issue
37
+ assert_equal first_issue, Issue.find(first_issue.id)
38
+ assert_not_equal first_issue, second_issue
39
+ end
40
+
41
+ test 'to_param' do
42
+ issue = Issue.new
43
+ assert_equal issue.id, issue.to_param
44
+ end
45
+
46
+ test 'hash' do
47
+ issue = Issue.create
48
+ assert_equal issue.id.hash, issue.hash
49
+ end
50
+
51
+ test 'inspect' do
52
+ issue = Issue.create
53
+ assert issue.inspect =~ /^#<Issue id: \"\w+\", created_at: \".+\", updated_at: \".+\", description: \".+\">$/
54
+ end
55
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::IdentityTest < CassandraObject::TestCase
4
+ test 'primary_key' do
5
+ assert_equal 'id', Issue.primary_key
6
+ end
7
+
8
+ test 'default _generate_key' do
9
+ issue = Issue.new
10
+
11
+ assert_not_nil Issue._generate_key(issue)
12
+ end
13
+
14
+ test 'custom key' do
15
+ model = temp_object do
16
+ key do
17
+ "name:#{name}"
18
+ end
19
+ attr_accessor :name
20
+ end
21
+ record = model.new
22
+ record.name = 'bar'
23
+
24
+ assert_equal 'name:bar', model._generate_key(record)
25
+ end
26
+ end