datastax_rails 1.2.3 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.rdoc +20 -8
  4. data/config/schema.xml.erb +22 -19
  5. data/config/solrconfig.xml.erb +1 -1
  6. data/lib/cql-rb_extensions.rb +27 -0
  7. data/lib/datastax_rails.rb +13 -17
  8. data/lib/datastax_rails/associations/association.rb +1 -4
  9. data/lib/datastax_rails/associations/collection_proxy.rb +0 -13
  10. data/lib/datastax_rails/attribute_assignment.rb +28 -91
  11. data/lib/datastax_rails/attribute_methods.rb +109 -44
  12. data/lib/datastax_rails/attribute_methods/before_type_cast.rb +71 -0
  13. data/lib/datastax_rails/attribute_methods/dirty.rb +52 -11
  14. data/lib/datastax_rails/attribute_methods/primary_key.rb +87 -0
  15. data/lib/datastax_rails/attribute_methods/read.rb +120 -0
  16. data/lib/datastax_rails/attribute_methods/typecasting.rb +52 -21
  17. data/lib/datastax_rails/attribute_methods/write.rb +59 -0
  18. data/lib/datastax_rails/base.rb +227 -236
  19. data/lib/datastax_rails/cassandra_only_model.rb +25 -19
  20. data/lib/datastax_rails/column.rb +384 -0
  21. data/lib/datastax_rails/connection.rb +12 -13
  22. data/lib/datastax_rails/cql/alter_column_family.rb +0 -1
  23. data/lib/datastax_rails/cql/base.rb +15 -3
  24. data/lib/datastax_rails/cql/column_family.rb +2 -2
  25. data/lib/datastax_rails/cql/create_column_family.rb +7 -18
  26. data/lib/datastax_rails/cql/delete.rb +4 -9
  27. data/lib/datastax_rails/cql/insert.rb +2 -8
  28. data/lib/datastax_rails/cql/select.rb +4 -4
  29. data/lib/datastax_rails/cql/update.rb +8 -17
  30. data/lib/datastax_rails/dynamic_model.rb +98 -0
  31. data/lib/datastax_rails/payload_model.rb +19 -31
  32. data/lib/datastax_rails/persistence.rb +39 -54
  33. data/lib/datastax_rails/railtie.rb +1 -0
  34. data/lib/datastax_rails/reflection.rb +1 -1
  35. data/lib/datastax_rails/relation.rb +20 -20
  36. data/lib/datastax_rails/relation/batches.rb +18 -16
  37. data/lib/datastax_rails/relation/facet_methods.rb +1 -1
  38. data/lib/datastax_rails/relation/finder_methods.rb +6 -10
  39. data/lib/datastax_rails/relation/search_methods.rb +62 -48
  40. data/lib/datastax_rails/rsolr_client_wrapper.rb +1 -1
  41. data/lib/datastax_rails/schema/cassandra.rb +34 -62
  42. data/lib/datastax_rails/schema/migrator.rb +9 -24
  43. data/lib/datastax_rails/schema/solr.rb +13 -30
  44. data/lib/datastax_rails/schema_cache.rb +67 -0
  45. data/lib/datastax_rails/timestamps.rb +84 -11
  46. data/lib/datastax_rails/types/dirty_collection.rb +88 -0
  47. data/lib/datastax_rails/types/dynamic_list.rb +14 -0
  48. data/lib/datastax_rails/types/dynamic_map.rb +32 -0
  49. data/lib/datastax_rails/types/dynamic_set.rb +10 -0
  50. data/lib/datastax_rails/util/solr_repair.rb +4 -5
  51. data/lib/datastax_rails/validations.rb +6 -12
  52. data/lib/datastax_rails/validations/uniqueness.rb +0 -4
  53. data/lib/datastax_rails/version.rb +1 -1
  54. data/lib/datastax_rails/wide_storage_model.rb +13 -29
  55. data/lib/schema_migration.rb +4 -0
  56. data/spec/datastax_rails/associations_spec.rb +0 -1
  57. data/spec/datastax_rails/attribute_methods_spec.rb +9 -6
  58. data/spec/datastax_rails/base_spec.rb +26 -0
  59. data/spec/datastax_rails/column_spec.rb +238 -0
  60. data/spec/datastax_rails/cql/select_spec.rb +1 -1
  61. data/spec/datastax_rails/cql/update_spec.rb +2 -2
  62. data/spec/datastax_rails/persistence_spec.rb +29 -15
  63. data/spec/datastax_rails/relation/batches_spec.rb +5 -5
  64. data/spec/datastax_rails/relation/finder_methods_spec.rb +0 -20
  65. data/spec/datastax_rails/relation/search_methods_spec.rb +8 -0
  66. data/spec/datastax_rails/relation_spec.rb +7 -0
  67. data/spec/datastax_rails/schema/migrator_spec.rb +5 -10
  68. data/spec/datastax_rails/schema/solr_spec.rb +1 -1
  69. data/spec/datastax_rails/types/dynamic_list_spec.rb +20 -0
  70. data/spec/datastax_rails/types/dynamic_map_spec.rb +22 -0
  71. data/spec/datastax_rails/types/dynamic_set_spec.rb +16 -0
  72. data/spec/dummy/config/application.rb +2 -1
  73. data/spec/dummy/config/datastax.yml +6 -3
  74. data/spec/dummy/config/environments/development.rb +4 -5
  75. data/spec/dummy/config/environments/test.rb +0 -5
  76. data/spec/dummy/log/development.log +18 -0
  77. data/spec/dummy/log/test.log +36 -0
  78. data/spec/feature/dynamic_fields_spec.rb +9 -0
  79. data/spec/feature/overloaded_tables_spec.rb +24 -0
  80. data/spec/spec_helper.rb +1 -1
  81. data/spec/support/default_consistency_shared_examples.rb +2 -2
  82. data/spec/support/models.rb +28 -14
  83. metadata +212 -188
  84. data/lib/datastax_rails/identity.rb +0 -64
  85. data/lib/datastax_rails/identity/abstract_key_factory.rb +0 -29
  86. data/lib/datastax_rails/identity/custom_key_factory.rb +0 -37
  87. data/lib/datastax_rails/identity/hashed_natural_key_factory.rb +0 -10
  88. data/lib/datastax_rails/identity/natural_key_factory.rb +0 -39
  89. data/lib/datastax_rails/identity/uuid_key_factory.rb +0 -27
  90. data/lib/datastax_rails/type.rb +0 -16
  91. data/lib/datastax_rails/types.rb +0 -9
  92. data/lib/datastax_rails/types/array_type.rb +0 -86
  93. data/lib/datastax_rails/types/base_type.rb +0 -42
  94. data/lib/datastax_rails/types/binary_type.rb +0 -19
  95. data/lib/datastax_rails/types/boolean_type.rb +0 -22
  96. data/lib/datastax_rails/types/date_type.rb +0 -23
  97. data/lib/datastax_rails/types/float_type.rb +0 -18
  98. data/lib/datastax_rails/types/integer_type.rb +0 -18
  99. data/lib/datastax_rails/types/string_type.rb +0 -16
  100. data/lib/datastax_rails/types/text_type.rb +0 -15
  101. data/lib/datastax_rails/types/time_type.rb +0 -23
  102. data/spec/datastax_rails/types/float_type_spec.rb +0 -31
  103. data/spec/datastax_rails/types/integer_type_spec.rb +0 -31
  104. data/spec/datastax_rails/types/time_type_spec.rb +0 -28
@@ -7,10 +7,6 @@ module DatastaxRails
7
7
  super
8
8
  end
9
9
 
10
- def setup(klass)
11
- @klass = klass
12
- end
13
-
14
10
  def validate_each(record, attribute, value)
15
11
  return true if options[:allow_blank] && value.blank?
16
12
  # XXX: The following will break if/when abstract base classes
@@ -1,4 +1,4 @@
1
1
  module DatastaxRails
2
2
  # The current version of the gem
3
- VERSION = "1.2.3"
3
+ VERSION = "2.0.3"
4
4
  end
@@ -1,44 +1,28 @@
1
1
  module DatastaxRails
2
2
  # A special model that is designed to efficiently store very wide data.
3
- # This model type assumes that you have a unique ID and want to either
4
- # search or sort on a second piece of data. The store the data as a
5
- # single, very wide row; however you can safely treat them as if
6
- # there were multiple rows.
3
+ # This model type assumes that you have a unique ID that you want to
4
+ # store a lot of data about. The data is stored as a single, very
5
+ # wide row; however you can safely treat it as if there were multiple rows.
6
+ # A common example of this is a logs table
7
+ #
8
+ # You can also apply secondary indexes onto the other columns.
7
9
  #
8
10
  # CAVEATS:
9
- # * Wide Storage Models cannot be indexed into Solr.
10
- # * Once the cluster is set, it cannot be changed as it becomes the column header in Cassandra
11
+ # * Once the cluster attribute is set, it cannot be changed as it becomes the column header in Cassandra
11
12
  #
12
13
  # class AuditLog < DatastaxRails::WideStorageModel
13
14
  # self.column_family = 'audit_logs'
14
- #
15
- # key :natural, :attributes => [:uuid]
16
- # cluster_by :created_at => :desc
15
+ # self.primary_key = :uuid
16
+ # self.cluster_by = :created_at
17
+ # # If you don't want the default ascending sort order
18
+ # self.create_options = 'CLUSTERING ORDER BY (created_at DESC)'
17
19
  #
18
20
  # string :uuid
19
21
  # string :message
22
+ # string :user_id, :cql_index => true
20
23
  # timestamps
21
24
  # end
22
- class WideStorageModel < CassandraOnlyModel
25
+ class WideStorageModel < DatastaxRails::Base
23
26
  self.abstract_class = true
24
-
25
- def self.cluster_by(attr = nil)
26
- @cluster_by ||= attr.is_a?(Hash) ? attr : {attr => :asc}
27
- end
28
-
29
- def self.write(key, attributes, options = {})
30
- attributes = encode_attributes(attributes)
31
- level = (options[:consistency] || self.default_consistency).to_s.upcase
32
- if(valid_consistency?(level))
33
- options[:consistency] = level
34
- else
35
- raise ArgumentError, "'#{level}' is not a valid Cassandra consistency level"
36
- end
37
- key.tap do |key|
38
- ActiveSupport::Notifications.instrument("insert.datastax_rails", :column_family => column_family, :key => key, :attributes => attributes) do
39
- cql.insert.using(level).columns(attributes).execute
40
- end
41
- end
42
- end
43
27
  end
44
28
  end
@@ -9,4 +9,8 @@ class SchemaMigration
9
9
  def self.column_family
10
10
  'schema_migrations'
11
11
  end
12
+
13
+ def self.primary_key
14
+ 'cf'
15
+ end
12
16
  end
@@ -17,7 +17,6 @@ describe DatastaxRails::Base do
17
17
  person = Person.create!(:name => "John")
18
18
  job = Job.create!(:title => "Developer", :person_id => person.id)
19
19
  Person.commit_solr
20
- Person.commit_solr
21
20
  Job.commit_solr
22
21
  Job.first.person.should == person
23
22
  end
@@ -2,12 +2,13 @@ require 'spec_helper'
2
2
 
3
3
  class AttributeMethodsTester < DatastaxRails::Base
4
4
  string :test_string
5
- string :non_search_string, :searchable => false
5
+ string :non_search_string, :solr_index => false
6
6
  end
7
7
 
8
- class CassandraOnlyTester < DatastaxRails::CassandraOnlyModel
8
+ class CassandraOnlyTester < DatastaxRails::Base
9
+ include DatastaxRails::CassandraOnlyModel
9
10
  string :test_string
10
- string :test_string2, :indexed => :both
11
+ string :test_string2, :cql_index => true
11
12
  end
12
13
 
13
14
  describe DatastaxRails::Base do
@@ -28,17 +29,19 @@ describe DatastaxRails::Base do
28
29
  describe "#attribute" do
29
30
  context "Cassandra-only models" do
30
31
  it "does not index columns by default" do
31
- expect(CassandraOnlyTester.attribute_definitions[:test_string].coder.options[:indexed]).to be_false
32
+ expect(CassandraOnlyTester.attribute_definitions[:test_string].options[:solr_index]).to be_false
33
+ expect(CassandraOnlyTester.attribute_definitions[:test_string].options[:cql_index]).to be_false
32
34
  end
33
35
 
34
36
  it "does not index into solr" do
35
- expect(CassandraOnlyTester.attribute_definitions[:test_string2].coder.options[:indexed]).to eq(:cassandra)
37
+ expect(CassandraOnlyTester.attribute_definitions[:test_string2].options[:solr_index]).to be_false
38
+ expect(CassandraOnlyTester.attribute_definitions[:test_string2].options[:cql_index]).to be_true
36
39
  end
37
40
  end
38
41
 
39
42
  context "Normal models" do
40
43
  it "indexes data into solr by default" do
41
- expect(AttributeMethodsTester.attribute_definitions[:test_string].coder.options[:indexed]).to eq(:solr)
44
+ expect(AttributeMethodsTester.attribute_definitions[:test_string].options[:solr_index]).to be_true
42
45
  end
43
46
  end
44
47
  end
@@ -17,4 +17,30 @@ describe DatastaxRails::Base do
17
17
  it "should raise RecordNotFound when finding a bogus ID" do
18
18
  lambda { Person.find("xyzzy") }.should raise_exception(DatastaxRails::RecordNotFound)
19
19
  end
20
+
21
+ describe "equality" do
22
+ it "considers new objects to be unequal" do
23
+ p1=Person.new(:name => 'John')
24
+ p2=Person.new(:name => 'John')
25
+ expect(p1).not_to eq(p2)
26
+ end
27
+
28
+ it "considers a new object to be unequal to a saved object" do
29
+ p1=Person.create!(:name => 'John')
30
+ p2=Person.new(:name => 'John')
31
+ expect(p1).not_to eq(p2)
32
+ end
33
+
34
+ it "considers two persisted objects to be equal if their primary keys are equal" do
35
+ p1=Person.create!(:name => 'John')
36
+ p2=Person.find(p1.id)
37
+ expect(p1).to eq(p2)
38
+ end
39
+
40
+ it "considers two persisted objects to be unequal if they have different primary keys" do
41
+ p1=Person.create!(:name => 'John')
42
+ p2=Person.create!(:name => 'James')
43
+ expect(p1).not_to eq(p2)
44
+ end
45
+ end
20
46
  end
@@ -0,0 +1,238 @@
1
+ require 'spec_helper'
2
+
3
+ describe DatastaxRails::Column do
4
+ describe "type casting" do
5
+ let(:record) {double(Person, :changed_attributes => {}, :attributes => {})}
6
+
7
+ describe "boolean" do
8
+ let(:c) {DatastaxRails::Column.new("field", nil, "boolean")}
9
+
10
+ describe "to ruby" do
11
+ it "casts '' to nil" do
12
+ expect(c.type_cast('')).to be_nil
13
+ end
14
+
15
+ it "casts nil to nil" do
16
+ expect(c.type_cast(nil)).to be_nil
17
+ end
18
+
19
+ [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].each do |val|
20
+ it "casts #{val.inspect} to true" do
21
+ expect(c.type_cast(val)).to eq(true)
22
+ end
23
+ end
24
+
25
+ [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', ' ', "\u3000\r\n", "\u0000", 'SOMETHING RANDOM'].each do |val|
26
+ it "casts #{val.inspect} to false" do
27
+ expect(c.type_cast(val)).to eq(false)
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "to cql3" do
33
+ it "casts false to false" do
34
+ expect(c.type_cast_for_cql3(false)).to be_false
35
+ end
36
+
37
+ it "casts true to true" do
38
+ expect(c.type_cast_for_cql3(true)).to be_true
39
+ end
40
+ end
41
+
42
+ describe "to solr" do
43
+ it "casts false to 'false'" do
44
+ expect(c.type_cast_for_solr(false)).to eq('false')
45
+ end
46
+
47
+ it "casts true to 'true'" do
48
+ expect(c.type_cast_for_solr(true)).to eq('true')
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "integer" do
54
+ let(:c) {DatastaxRails::Column.new("field", nil, "integer")}
55
+
56
+ describe "to ruby" do
57
+ [1,'1','1ignore','1.7',true].each do |val|
58
+ it "casts #{val.inspect} to true" do
59
+ expect(c.type_cast(val)).to eq(1)
60
+ end
61
+ end
62
+
63
+ ['bad1','bad',false].each do |val|
64
+ it "casts #{val.inspect} to 0" do
65
+ expect(c.type_cast(val)).to eq(0)
66
+ end
67
+ end
68
+
69
+ [nil,[1,2],{1 => 2},(1..2),Object.new,Float::NAN,(1.0/0.0)].each do |val|
70
+ it "casts #{val.inspect} to nil" do
71
+ expect(c.type_cast(val)).to be_nil
72
+ end
73
+ end
74
+
75
+ it "casts a duration to an integer" do
76
+ expect(c.type_cast(30.minutes)).to be(1800)
77
+ end
78
+ end
79
+
80
+ describe "to cql3" do
81
+ it "casts 1 to 1" do
82
+ expect(c.type_cast_for_cql3(1)).to be(1)
83
+ end
84
+ end
85
+
86
+ describe "to solr" do
87
+ it "casts 1 to 1" do
88
+ expect(c.type_cast_for_solr(1)).to be(1)
89
+ end
90
+ end
91
+ end
92
+
93
+ describe "time" do
94
+ let(:c) {DatastaxRails::Column.new("field", nil, "time")}
95
+
96
+ describe "to ruby" do
97
+ [nil,'ABC',''].each do |val|
98
+ it "casts #{val.inspect} to nil" do
99
+ expect(c.type_cast(val)).to be_nil
100
+ end
101
+ end
102
+
103
+ it "casts a time string to Time" do
104
+ time_string = Time.now.utc.strftime("%T")
105
+ expect(c.type_cast(time_string).strftime("%T")).to eq(time_string)
106
+ end
107
+ end
108
+
109
+ describe "to cql3" do
110
+ it "casts a Time object to a Time object" do
111
+ time = Time.parse('1980-10-19 17:55:00')
112
+ expect(c.type_cast_for_cql3(time)).to eq(time)
113
+ end
114
+ end
115
+
116
+ describe "to solr" do
117
+ it "casts a Time object to a solr formatted time string" do
118
+ time = Time.parse('1980-10-19 17:55:00')
119
+ expect(c.type_cast_for_solr(time)).to eq('1980-10-19T17:55:00Z')
120
+ end
121
+ end
122
+ end
123
+
124
+ describe "timestamp" do
125
+ let(:c) {DatastaxRails::Column.new("field", nil, "timestamp")}
126
+
127
+ describe "to ruby" do
128
+ [nil,'',' ', 'ABC'].each do |val|
129
+ it "casts #{val.inspect} to nil" do
130
+ expect(c.type_cast(val)).to be_nil
131
+ end
132
+ end
133
+
134
+ it "casts a datetime string to Time" do
135
+ datetime_string = Time.now.utc.strftime("%FT%T")
136
+ expect(c.type_cast(datetime_string).strftime("%FT%T")).to eq(datetime_string)
137
+ end
138
+
139
+ it "casts a datetime string with timezone to Time" do
140
+ begin
141
+ old = DatastaxRails::Base.default_timezone
142
+ [:utc, :local].each do |zone|
143
+ DatastaxRails::Base.default_timezone = zone
144
+ datetime_string = "Wed, 04 Sep 2013 03:00:00 EAT"
145
+ expect(c.type_cast(val)).to eq(Time.utc(2013, 9, 4, 0, 0, 0))
146
+ end
147
+ rescue
148
+ DatastaxRails::Base.default_timezone = old
149
+ end
150
+ end
151
+ end
152
+
153
+ describe "to cql3" do
154
+ it "casts a Time object to a Time object" do
155
+ time = Time.parse('1980-10-19 17:55:00')
156
+ expect(c.type_cast_for_cql3(time)).to eq(time)
157
+ end
158
+ end
159
+
160
+ describe "to solr" do
161
+ it "casts a Time object to a solr formatted time string" do
162
+ time = Time.parse('1980-10-19 17:55:00')
163
+ expect(c.type_cast_for_solr(time)).to eq('1980-10-19T17:55:00Z')
164
+ end
165
+ end
166
+ end
167
+
168
+ describe "date" do
169
+ let(:c) {DatastaxRails::Column.new("field", nil, "date")}
170
+
171
+ [nil,'',' ', 'ABC'].each do |val|
172
+ it "casts #{val.inspect} to nil" do
173
+ expect(c.type_cast(val)).to be_nil
174
+ end
175
+ end
176
+
177
+ it "casts a date string to Date" do
178
+ date_string = Time.now.utc.strftime("%F")
179
+ expect(c.type_cast(date_string).strftime("%F")).to eq(date_string)
180
+ end
181
+
182
+ describe "to cql3" do
183
+ it "casts a Date object to a Time object" do
184
+ time = Time.parse('1980-10-19 00:00:00')
185
+ date = Date.parse('1980-10-19')
186
+ expect(c.type_cast_for_cql3(date)).to eq(time)
187
+ end
188
+ end
189
+
190
+ describe "to solr" do
191
+ it "casts a Date object to a solr formatted time string" do
192
+ date = Date.parse('1980-10-19')
193
+ expect(c.type_cast_for_solr(date)).to eq('1980-10-19T00:00:00Z')
194
+ end
195
+ end
196
+ end
197
+
198
+ describe "map" do
199
+ let(:c) {DatastaxRails::Column.new("field_", nil, "map", :holds => :integer)}
200
+
201
+ it "casts map keys to strings" do
202
+ expect(c.type_cast({:field_key => 7}, record)).to eq({"field_key" => 7})
203
+ end
204
+
205
+ it "casts map values to the type specified in the options" do
206
+ expect(c.type_cast({'field_key' => '7'}, record)).to eq({"field_key" => 7})
207
+ end
208
+
209
+ it "wraps map values in a DynamicMap" do
210
+ expect(c.type_cast({'field_key' => '7'}, record)).to be_a(DatastaxRails::Types::DynamicMap)
211
+ end
212
+ end
213
+
214
+ describe "list" do
215
+ let(:c) {DatastaxRails::Column.new("field", nil, "list", :holds => :integer)}
216
+
217
+ it "casts list values to the type specified in the options" do
218
+ expect(c.type_cast([1,"2",6.minutes], record)).to eq([1,2,360])
219
+ end
220
+
221
+ it "wraps list values in a DynamicList" do
222
+ expect(c.type_cast([1,"2",6.minutes], record)).to be_a(DatastaxRails::Types::DynamicList)
223
+ end
224
+ end
225
+
226
+ describe "set" do
227
+ let(:c) {DatastaxRails::Column.new("field", nil, "set", :holds => :integer)}
228
+
229
+ it "casts list values to the type specified in the options" do
230
+ expect(c.type_cast([1,"2",6.minutes, 2], record)).to eq(Set.new([1,2,360]))
231
+ end
232
+
233
+ it "wraps list values in a DynamicSet" do
234
+ expect(c.type_cast([1,"2",6.minutes, 2], record)).to be_a(DatastaxRails::Types::DynamicSet)
235
+ end
236
+ end
237
+ end
238
+ end
@@ -8,7 +8,7 @@ describe DatastaxRails::Cql::Select do
8
8
  it "should generate valid CQL" do
9
9
  cql = DatastaxRails::Cql::Select.new(@model_class, ["*"])
10
10
  cql.using(DatastaxRails::Cql::Consistency::QUORUM).conditions(:key => '12345').limit(1)
11
- cql.to_cql.should == "SELECT * FROM users WHERE \"key\" = '12345' LIMIT 1 "
11
+ cql.to_cql.should == "SELECT * FROM users WHERE \"key\" = ? LIMIT 1 "
12
12
  end
13
13
 
14
14
  it_has_behavior "default_consistency"
@@ -2,13 +2,13 @@ require 'spec_helper'
2
2
 
3
3
  describe DatastaxRails::Cql::Update do
4
4
  before(:each) do
5
- @model_class = double("Model Class", :column_family => 'users', :default_consistency => DatastaxRails::Cql::Consistency::QUORUM)
5
+ @model_class = double("Model Class", :column_family => 'users', :default_consistency => DatastaxRails::Cql::Consistency::QUORUM, :primary_key => 'id')
6
6
  end
7
7
 
8
8
  it "should generate valid CQL" do
9
9
  cql = DatastaxRails::Cql::Update.new(@model_class, "12345")
10
10
  cql.using(DatastaxRails::Cql::Consistency::QUORUM).columns(:name => 'John', :age => '23')
11
- cql.to_cql.should match(/update users SET ("name" = 'John', "age" = '23'|"age" = '23', "name" = 'John') WHERE key IN \('12345'\)/)
11
+ cql.to_cql.should match(/update users SET ("name" = \?, "age" = \?|"age" = \?, "name" = \?) WHERE id IN \(\?\)/)
12
12
  end
13
13
 
14
14
  it_has_behavior "default_consistency"
@@ -13,29 +13,53 @@ describe "DatastaxRails::Base" do
13
13
  end
14
14
 
15
15
  describe "with cql" do
16
+ before(:each) do
17
+ Person.storage_method = :cql
18
+ @statement = double("prepared statement")
19
+ DatastaxRails::Base.connection.stub(:prepare).and_return(@statement)
20
+ end
21
+
16
22
  describe "#create" do
17
23
  it "should persist at the given consistency level" do
18
- Person.storage_method = :cql
19
- DatastaxRails::Base.connection.should_receive(:execute_cql_query).with(an_instance_of(String), :consistency => CassandraCQL::Thrift::ConsistencyLevel::LOCAL_QUORUM)
24
+ @statement.should_receive(:execute) do |*args|
25
+ expect(args.last).to include(:consistency => :local_quorum)
26
+ end
20
27
  Person.create({:name => 'Steven'},{:consistency => 'LOCAL_QUORUM'})
21
28
  end
22
29
  end
23
30
 
24
31
  describe "#save" do
25
32
  it "should persist at the given consistency level" do
26
- Person.storage_method = :cql
27
- DatastaxRails::Base.connection.should_receive(:execute_cql_query).with(an_instance_of(String), :consistency => CassandraCQL::Thrift::ConsistencyLevel::LOCAL_QUORUM)
33
+ @statement.should_receive(:execute) do |*args|
34
+ expect(args.last).to include(:consistency => :local_quorum)
35
+ end
28
36
  p=Person.new(:name => 'Steven')
29
37
  p.save(:consistency => 'LOCAL_QUORUM')
30
38
  end
31
39
  end
40
+
41
+ describe "#remove" do
42
+ it "should remove at the given consistency level" do
43
+ @statement.stub(:execute)
44
+ p=Person.create(:name => 'Steven')
45
+ @statement.should_receive(:execute) do |*args|
46
+ expect(args.last).to include(:consistency => :local_quorum)
47
+ end
48
+ p.destroy(:consistency => :local_quorum)
49
+ end
50
+ end
32
51
  end
33
52
 
34
53
  describe "with solr" do
54
+ around(:each) do |example|
55
+ Person.storage_method = :solr
56
+ example.run
57
+ Person.storage_method = :cql
58
+ end
59
+
35
60
  describe "#create" do
36
61
  it "should persist at the given consistency level" do
37
62
  Person.solr_connection.should_receive(:update).with(hash_including(:params => hash_including({:cl => 'LOCAL_QUORUM'}))).and_return(true)
38
- Person.storage_method = :solr
39
63
  Person.create({:name => 'Steven'},{:consistency => 'LOCAL_QUORUM'})
40
64
  end
41
65
  end
@@ -43,14 +67,12 @@ describe "DatastaxRails::Base" do
43
67
  describe "#save" do
44
68
  it "should persist at the given consistency level" do
45
69
  Person.solr_connection.should_receive(:update).with(hash_including(:params => hash_including({:cl => 'LOCAL_QUORUM'}))).and_return(true)
46
- Person.storage_method = :solr
47
70
  p=Person.new(:name => 'Steven')
48
71
  p.save(:consistency => 'LOCAL_QUORUM')
49
72
  end
50
73
 
51
74
  it "should successfully remove columns that are set to nil" do
52
75
  pending do
53
- Person.storage_method = :solr
54
76
  p = Person.create!(:name => 'Steven', :birthdate => Date.today)
55
77
  Person.commit_solr
56
78
  p = Person.find_by_name('Steven')
@@ -63,14 +85,6 @@ describe "DatastaxRails::Base" do
63
85
  end
64
86
  end
65
87
 
66
- describe "#remove" do
67
- it "should remove at the given consistency level" do
68
- p=Person.create(:name => 'Steven')
69
- DatastaxRails::Base.connection.should_receive(:execute_cql_query).with(an_instance_of(String), :consistency => CassandraCQL::Thrift::ConsistencyLevel::LOCAL_QUORUM)
70
- p.destroy(:consistency => :local_quorum)
71
- end
72
- end
73
-
74
88
  describe "#store_file" do
75
89
  it "should store a file", :slow => true do
76
90
  file = "abcd"*1.megabyte