gotime-cassandra_object 2.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/Gemfile.lock +1 -1
  2. data/gotime-cassandra_object.gemspec +1 -1
  3. data/lib/cassandra_object.rb +1 -2
  4. data/lib/cassandra_object/associations.rb +3 -4
  5. data/lib/cassandra_object/associations/one_to_many.rb +2 -10
  6. data/lib/cassandra_object/associations/one_to_one.rb +1 -1
  7. data/lib/cassandra_object/base.rb +20 -4
  8. data/lib/cassandra_object/consistency.rb +21 -20
  9. data/lib/cassandra_object/cursor.rb +6 -7
  10. data/lib/cassandra_object/finder_methods.rb +4 -10
  11. data/lib/cassandra_object/identity.rb +8 -26
  12. data/lib/cassandra_object/persistence.rb +5 -30
  13. data/lib/cassandra_object/{validation.rb → validations.rb} +6 -8
  14. data/{test-old → test}/active_model_test.rb +2 -2
  15. data/test/base_test.rb +10 -0
  16. data/test/consistency_test.rb +20 -0
  17. data/test/identity_test.rb +6 -1
  18. data/test/validations_test.rb +15 -0
  19. metadata +15 -34
  20. data/lib/cassandra_object/primary_key.rb +0 -12
  21. data/test-old/base_test.rb +0 -4
  22. data/test-old/basic_scenarios_test.rb +0 -243
  23. data/test-old/callbacks_test.rb +0 -19
  24. data/test-old/config/cassandra.in.sh +0 -53
  25. data/test-old/config/log4j.properties +0 -38
  26. data/test-old/config/storage-conf.xml +0 -221
  27. data/test-old/connection.rb +0 -25
  28. data/test-old/cursor_test.rb +0 -66
  29. data/test-old/dirty_test.rb +0 -34
  30. data/test-old/fixture_models.rb +0 -90
  31. data/test-old/identity/natural_key_factory_test.rb +0 -94
  32. data/test-old/index_test.rb +0 -69
  33. data/test-old/legacy/test_helper.rb +0 -18
  34. data/test-old/migration_test.rb +0 -21
  35. data/test-old/one_to_many_associations_test.rb +0 -163
  36. data/test-old/test_case.rb +0 -28
  37. data/test-old/test_helper.rb +0 -16
  38. data/test-old/time_test.rb +0 -32
  39. data/test-old/types_test.rb +0 -252
  40. data/test-old/validation_test.rb +0 -25
  41. data/test-old/z_mock_test.rb +0 -36
  42. data/test/primary_key_test.rb +0 -9
@@ -1,90 +0,0 @@
1
- module ReverseStorage
2
- def encode(str)
3
- str.reverse
4
- end
5
- module_function :encode
6
-
7
- def decode(str)
8
- str.reverse
9
- end
10
- module_function :decode
11
- end
12
-
13
-
14
- class Customer < CassandraObject::Base
15
- attribute :first_name, :type => :string
16
- attribute :last_name, :type => :string
17
- attribute :date_of_birth, :type => :date
18
- attribute :preferences, :type => :hash
19
- attribute :custom_storage, :type => String, :converter=>ReverseStorage
20
-
21
- validate :should_be_cool
22
- validates_presence_of :last_name
23
-
24
- after_create :set_after_create_called
25
-
26
- key :uuid
27
-
28
- index :last_name, :reversed=>true
29
-
30
- association :invoices, :unique=>false, :inverse_of=>:customer, :reversed=>true
31
- association :paid_invoices, :unique=>false, :class_name=>'Invoice'
32
-
33
- def after_create_called?
34
- @after_create_called
35
- end
36
-
37
- def set_after_create_called
38
- @after_create_called = true
39
- end
40
-
41
- private
42
-
43
- def should_be_cool
44
- unless ["Michael", "Anika", "Evan", "Tom"].include?(first_name)
45
- errors.add(:first_name, "must be that of a cool person")
46
- end
47
- end
48
- end
49
-
50
- class Invoice < CassandraObject::Base
51
- attribute :number, :type=>:integer
52
- attribute :total, :type=>:float
53
- attribute :gst_number, :type=>:string
54
-
55
- index :number, :unique=>true
56
-
57
- association :customer, :unique=>true, :inverse_of=>:invoices
58
-
59
- migrate 1 do |attrs|
60
- attrs["total"] ||= (rand(2000) / 100.0).to_s
61
- end
62
-
63
- migrate 2 do |attrs|
64
- attrs["gst_number"] = "66-666-666"
65
- end
66
-
67
- key :uuid
68
- end
69
-
70
- class Payment < CassandraObject::Base
71
- attribute :reference_number, :type => :string
72
- attribute :amount, :type => :integer
73
-
74
- key :natural, :attributes => :reference_number
75
- end
76
-
77
- MockRecord = Struct.new(:key)
78
-
79
- class Person < CassandraObject::Base
80
- attribute :name, :type => :string
81
- attribute :age, :type => :integer
82
- end
83
-
84
- class Appointment < CassandraObject::Base
85
- attribute :title, :type => :string
86
- attribute :start_time, :type => :time
87
- attribute :end_time, :type => :time_with_zone, :allow_nil => true
88
-
89
- key :natural, :attributes => :title
90
- end
@@ -1,94 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Identity
4
- class NaturalKeyFactoryTest < CassandraObjectTestCase
5
- context "With one attribute" do
6
- setup do
7
- @key_factory = CassandraObject::Identity::NaturalKeyFactory.new :attributes => :name
8
- @james = Person.new("name" => "james")
9
- end
10
-
11
- should "have a key whose string and param value is the value of that attribute" do
12
- @key = @key_factory.next_key(@james)
13
-
14
- assert_equal "james", @key.to_s
15
- assert_equal "james", @key.to_param
16
- end
17
-
18
- should "parse to a key from a param value" do
19
- @key = @key_factory.parse("james")
20
-
21
- assert_equal "james", @key.to_s
22
- assert_equal "james", @key.to_param
23
- end
24
-
25
- should "create a key from a value" do
26
- @key = @key_factory.create("james")
27
-
28
- assert_equal "james", @key.to_s
29
- assert_equal "james", @key.to_param
30
- end
31
- end
32
-
33
- context "With multiple attributes" do
34
- setup do
35
- @key_factory = CassandraObject::Identity::NaturalKeyFactory.new :attributes => [:name, :age]
36
- @james = Person.new("name" => "james", "age" => 23)
37
- end
38
-
39
- should "create a key whose string value is the two values, joined with a separator" do
40
- key = @key_factory.next_key(@james)
41
-
42
- assert_equal "james-23", key.to_s
43
- assert_equal "james-23", key.to_param
44
- end
45
-
46
- should "parse the key" do
47
- key = @key_factory.parse("james-23")
48
-
49
- assert_equal "james-23", key.to_s
50
- assert_equal "james-23", key.to_param
51
- end
52
-
53
- should "create the key" do
54
- key = @key_factory.create("james-23")
55
-
56
- assert_equal "james-23", key.to_s
57
- assert_equal "james-23", key.to_param
58
- end
59
- end
60
-
61
- context "With a custom separator" do
62
- setup do
63
- @key_factory = CassandraObject::Identity::NaturalKeyFactory.new :attributes => [:name, :age],
64
- :separator => "#"
65
- @james = Person.new("name" => "james", "age" => 23)
66
- end
67
-
68
- should "join the attributes with the custom separator" do
69
- key = @key_factory.next_key(@james)
70
-
71
- assert_equal "james#23", key.to_s
72
- end
73
- end
74
-
75
- context "Natural keys" do
76
- setup do
77
- @james = CassandraObject::Identity::NaturalKeyFactory::NaturalKey.new("james")
78
- @another_james = CassandraObject::Identity::NaturalKeyFactory::NaturalKey.new("james")
79
- @joe = CassandraObject::Identity::NaturalKeyFactory::NaturalKey.new("joe")
80
- end
81
-
82
- should "be equal, if their values are equal" do
83
- assert @james == @another_james
84
- assert @james.eql?(@another_james)
85
- end
86
-
87
- should "not be equal if their values are different" do
88
- assert @james != @joe
89
- assert !@james.eql?(@joe)
90
- end
91
- end
92
- end
93
- end
94
-
@@ -1,69 +0,0 @@
1
- require 'test_helper'
2
-
3
- class IndexTest < CassandraObjectTestCase
4
- context "A non-unique index" do
5
- setup do
6
- @last_name = ActiveSupport::SecureRandom.hex(5)
7
- @koz = Customer.create :first_name=>"Michael", :last_name=>@last_name, :date_of_birth=>28.years.ago.to_date
8
- @wife = Customer.create :first_name=>"Anika", :last_name=>@last_name, :date_of_birth=>30.years.ago.to_date
9
- end
10
-
11
- should "Return both values" do
12
- assert_ordered [@wife.key, @koz.key], Customer.find_all_by_last_name(@last_name).map(&:key)
13
- end
14
-
15
- should "return the older when the newer is destroyed" do
16
- @wife.destroy
17
- assert_equal [@koz.key], Customer.find_all_by_last_name(@last_name).map(&:key)
18
- end
19
-
20
- should "return a single value when the original one is changed" do
21
- @wife.last_name = "WTF"
22
- @wife.save
23
- assert_equal [@koz.key], Customer.find_all_by_last_name(@last_name).map(&:key)
24
- end
25
- end
26
-
27
- context "A corrupt non-unique index" do
28
- setup do
29
- @last_name = ActiveSupport::SecureRandom.hex(5)
30
- @koz = Customer.create :first_name=>"Michael", :last_name=>@last_name, :date_of_birth=>28.years.ago.to_date
31
- connection.insert("CustomersByLastName", @last_name, {"last_name"=>{SimpleUUID::UUID.new=>"ROFLSKATES"}})
32
- @wife = Customer.create :first_name=>"Anika", :last_name=>@last_name, :date_of_birth=>30.years.ago.to_date
33
- end
34
-
35
- should "Return both values and clean up" do
36
- assert_ordered [@wife.key, @koz.key], Customer.find_all_by_last_name(@last_name).map(&:key)
37
- assert_ordered [@wife.key, @koz.key], connection.get("CustomersByLastName", @last_name, "last_name", :reversed=>true).values
38
- end
39
-
40
- end
41
-
42
- context "A unique index" do
43
- setup do
44
- @invoice = mock_invoice
45
- @number = @invoice.number
46
- end
47
-
48
- should "return the right record" do
49
- assert_equal @invoice, Invoice.find_by_number(@number)
50
- end
51
-
52
- should "return nil after destroy" do
53
- @invoice.destroy
54
- assert_nil Invoice.find_by_number(@number)
55
- end
56
- end
57
-
58
- context " A corrupt unique index" do
59
- setup do
60
- connection.insert("InvoicesByNumber", '15' , {"key"=>"HAHAHAHA"})
61
- end
62
-
63
- should "return nil on fetch and cleanup" do
64
- assert_nil Invoice.find_by_number(15)
65
- assert connection.get("InvoicesByNumber", "15", "number").blank?
66
- end
67
-
68
- end
69
- end
@@ -1,18 +0,0 @@
1
- require 'rubygems'
2
-
3
- gem 'activesupport', "~> 2.3.5"
4
-
5
- puts "LEGACY TEST"
6
-
7
- require 'cassandra_object'
8
- require 'connection'
9
-
10
- require 'test/unit'
11
- require 'active_support/test_case'
12
- require 'shoulda'
13
-
14
- require 'fixture_models'
15
- require 'pp'
16
-
17
- require 'test_case'
18
-
@@ -1,21 +0,0 @@
1
- require 'test_helper'
2
-
3
- class MigrationTest < CassandraObjectTestCase
4
- test "a new invoice should have the right schema version" do
5
- i = mock_invoice
6
- assert_equal 2, i.schema_version
7
- end
8
-
9
- test " a new invoice should have an empty gst_number" do
10
- assert_equal nil, mock_invoice.gst_number
11
- end
12
-
13
- test "an old invoice should get fetched and updated" do
14
- key = Invoice.next_key.to_s
15
- connection.insert(Invoice.column_family, key, {"schema_version"=>"1", "number"=>"200", "total"=>"150.35"})
16
-
17
- invoice = Invoice.get(key)
18
- assert_equal 2, invoice.schema_version
19
- assert_equal 150.35, invoice.total
20
- end
21
- end
@@ -1,163 +0,0 @@
1
- require 'test_helper'
2
-
3
- class OneToManyAssociationsTest < CassandraObjectTestCase
4
-
5
- context "A customer with no invoices added to its invoice association" do
6
- setup do
7
- @customer = Customer.create :first_name => "Michael",
8
- :last_name => "Koziarski",
9
- :date_of_birth => Date.parse("1980/08/15")
10
-
11
- assert @customer.valid?, @customer.errors
12
- end
13
-
14
- should "return an empty array for the association" do
15
- assert_equal [], @customer.invoices.to_a
16
- end
17
- end
18
-
19
- context "A customer with an invoice added to its invoice association" do
20
- setup do
21
- @customer = Customer.create :first_name => "Michael",
22
- :last_name => "Koziarski",
23
- :date_of_birth => Date.parse("1980/08/15")
24
-
25
- assert @customer.valid?, @customer.errors
26
-
27
- @invoice = mock_invoice
28
- assert @invoice.valid?, @invoice.errors
29
-
30
- @customer.invoices << @invoice
31
- end
32
-
33
- should "have set the inverse" do
34
- assert_equal @customer, @invoice.customer
35
- end
36
-
37
- should "have also written to cassandra" do
38
- assert_equal @invoice, @customer.invoices.to_a.first
39
- end
40
-
41
- context "Simple Read-Repair" do
42
- setup do
43
- add_junk_key
44
- assert_ordered ["SomethingStupid", @invoice.key], association_keys_in_cassandra
45
- end
46
-
47
- should "tidy up when fetching" do
48
- assert_equal [@invoice], @customer.invoices.all
49
- assert_equal [@invoice.key.to_s], association_keys_in_cassandra
50
- end
51
- end
52
-
53
- context "More complicated Read-Repair" do
54
- setup do
55
- # Now add a second legit invoice
56
- @second_invoice = mock_invoice
57
- @customer.invoices << @second_invoice
58
-
59
- add_junk_key
60
-
61
- @third_invoice = mock_invoice
62
- @customer.invoices << @third_invoice
63
-
64
- #
65
-
66
- assert_ordered [@third_invoice.key,"SomethingStupid", @second_invoice.key, @invoice.key],
67
- association_keys_in_cassandra
68
- end
69
-
70
- should "return the last one when passed a limit of one, and not touch the keys" do
71
- assert_equal [@third_invoice], @customer.invoices.all(:limit=>1)
72
- assert_ordered [@third_invoice.key,"SomethingStupid", @second_invoice.key, @invoice.key],
73
- association_keys_in_cassandra
74
- end
75
-
76
- should "return them all when passed a limit of 3, and clean up the keys" do
77
- assert_ordered [@third_invoice, @second_invoice, @invoice], @customer.invoices.all(:limit=>3), false
78
- assert_ordered [@third_invoice.key, @second_invoice.key, @invoice.key],
79
- association_keys_in_cassandra
80
-
81
- end
82
-
83
- should "return the first invoice when told to start after the second" do
84
- assert_ordered [@invoice.key], @customer.invoices.all(:limit=>1, :start_after=>index_key_for(@second_invoice)).map(&:key)
85
- assert_ordered [@third_invoice.key,"SomethingStupid", @second_invoice.key, @invoice.key],
86
- association_keys_in_cassandra
87
- end
88
- end
89
- end
90
-
91
- context "A customer with an invoice added to its paid invoices association (which has no explicit reversed option)" do
92
- setup do
93
- @customer = Customer.create :first_name => "Michael",
94
- :last_name => "Koziarski",
95
- :date_of_birth => Date.parse("1980/08/15")
96
-
97
- assert @customer.valid?, @customer.errors
98
-
99
- @invoice = mock_invoice
100
- assert @invoice.valid?, @invoice.errors
101
-
102
- @customer.paid_invoices << @invoice
103
- end
104
-
105
- should "return invoice from association" do
106
- assert_equal @invoice, @customer.paid_invoices.to_a.first
107
- end
108
- end
109
-
110
- context "Association proxy create" do
111
- setup do
112
- @customer = Customer.create! :first_name => "Michael",
113
- :last_name => "Koziarski",
114
- :date_of_birth => Date.parse("1980/08/15")
115
- @invoice = @customer.invoices.create :number=>50, :total=>25.0
116
- end
117
-
118
- should "return the invoice" do
119
- assert_kind_of Invoice, @invoice
120
- end
121
-
122
- should "have set the attributes" do
123
- assert_equal 50, @invoice.number
124
- assert_equal 25.0, @invoice.total
125
- end
126
-
127
- should "have set the inverse" do
128
- assert_equal @customer, @invoice.customer
129
- end
130
- end
131
-
132
- context "Association proxy all" do
133
- setup do
134
- @customer = Customer.create! :first_name => "Michael",
135
- :last_name => "Koziarski",
136
- :date_of_birth => Date.parse("1980/08/15")
137
- @first = @customer.invoices.create :number => 50, :total => 25.0
138
- @second = @customer.invoices.create :number => 50, :total => 25.0
139
- end
140
-
141
- should "suport overriding :reversed value" do
142
- assert_ordered [@first.key, @second.key], @customer.invoices.all(:reversed => false).map(&:key)
143
- end
144
- end
145
-
146
- def add_junk_key
147
- Customer.associations[:invoices].add(@customer, MockRecord.new("SomethingStupid"))
148
- end
149
-
150
- def association_keys_in_cassandra
151
- Customer.connection.get(Customer.associations[:invoices].column_family, @customer.key.to_s, "invoices", :reversed=>true).values
152
- end
153
-
154
- def index_key_for(object)
155
- Customer.connection.get(Customer.associations[:invoices].column_family, @customer.key.to_s, "invoices").each do |(key, value)|
156
- if value == object.key.to_s
157
- return key
158
- end
159
- end
160
- raise "Not found"
161
- end
162
-
163
- end