gotime-cassandra_object 4.5.1 → 4.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/.travis.yml +2 -0
  2. data/Gemfile +5 -1
  3. data/README.rdoc +0 -2
  4. data/gotime-cassandra_object.gemspec +1 -1
  5. data/lib/cassandra_object/attribute_methods.rb +4 -4
  6. data/lib/cassandra_object/attribute_methods/dirty.rb +1 -1
  7. data/lib/cassandra_object/attribute_methods/primary_key.rb +1 -1
  8. data/lib/cassandra_object/attribute_methods/typecasting.rb +9 -6
  9. data/lib/cassandra_object/base.rb +11 -43
  10. data/lib/cassandra_object/belongs_to.rb +1 -1
  11. data/lib/cassandra_object/belongs_to/association.rb +1 -1
  12. data/lib/cassandra_object/belongs_to/reflection.rb +2 -2
  13. data/lib/cassandra_object/config.rb +15 -0
  14. data/lib/cassandra_object/connection.rb +1 -19
  15. data/lib/cassandra_object/consistency.rb +1 -1
  16. data/lib/cassandra_object/core.rb +46 -0
  17. data/lib/cassandra_object/errors.rb +2 -2
  18. data/lib/cassandra_object/identity.rb +1 -1
  19. data/lib/cassandra_object/persistence.rb +21 -7
  20. data/lib/cassandra_object/railtie.rb +1 -1
  21. data/lib/cassandra_object/savepoints.rb +1 -1
  22. data/lib/cassandra_object/schema.rb +30 -15
  23. data/lib/cassandra_object/schema/tasks.rb +8 -4
  24. data/lib/cassandra_object/scope.rb +1 -1
  25. data/lib/cassandra_object/scope/batches.rb +1 -1
  26. data/lib/cassandra_object/scope/finder_methods.rb +1 -1
  27. data/lib/cassandra_object/scope/query_methods.rb +1 -1
  28. data/lib/cassandra_object/tasks/ks.rake +6 -6
  29. data/lib/cassandra_object/types/array_type.rb +1 -1
  30. data/lib/cassandra_object/types/base_type.rb +1 -1
  31. data/lib/cassandra_object/types/boolean_type.rb +2 -2
  32. data/lib/cassandra_object/types/float_type.rb +1 -1
  33. data/lib/cassandra_object/types/integer_type.rb +1 -1
  34. data/lib/cassandra_object/types/json_type.rb +1 -1
  35. data/lib/cassandra_object/types/string_type.rb +1 -1
  36. data/lib/cassandra_object/types/time_type.rb +1 -1
  37. data/lib/cassandra_object/validations.rb +3 -3
  38. data/lib/gotime-cassandra_object.rb +10 -8
  39. data/test/support/connect.rb +3 -3
  40. data/test/support/issue.rb +1 -1
  41. data/test/test_helper.rb +1 -1
  42. data/test/unit/active_model_test.rb +6 -1
  43. data/test/unit/attribute_methods/definition_test.rb +1 -1
  44. data/test/unit/attribute_methods/dirty_test.rb +2 -2
  45. data/test/unit/attribute_methods/primary_key_test.rb +1 -1
  46. data/test/unit/attribute_methods/typecasting_test.rb +28 -2
  47. data/test/unit/base_test.rb +1 -43
  48. data/test/unit/belongs_to/reflection_test.rb +12 -0
  49. data/test/unit/belongs_to_test.rb +1 -1
  50. data/test/unit/callbacks_test.rb +1 -1
  51. data/test/unit/config_test.rb +23 -0
  52. data/test/unit/connection_test.rb +0 -20
  53. data/test/unit/consistency_test.rb +1 -1
  54. data/test/unit/core_test.rb +50 -0
  55. data/test/unit/identity_test.rb +1 -1
  56. data/test/unit/inspect_test.rb +1 -1
  57. data/test/unit/persistence_test.rb +36 -7
  58. data/test/unit/savepoints_test.rb +7 -7
  59. data/test/unit/schema_test.rb +1 -1
  60. data/test/unit/scope/batches_test.rb +4 -4
  61. data/test/unit/scope/finder_methods_test.rb +3 -3
  62. data/test/unit/scope/query_methods_test.rb +1 -1
  63. data/test/unit/scoping_test.rb +1 -1
  64. data/test/unit/timestamps_test.rb +1 -1
  65. data/test/unit/types/array_type_test.rb +3 -3
  66. data/test/unit/types/base_type_test.rb +1 -1
  67. data/test/unit/types/float_type_test.rb +2 -2
  68. data/test/unit/types/integer_type_test.rb +1 -1
  69. data/test/unit/types/string_type_test.rb +1 -1
  70. data/test/unit/types/time_type_test.rb +1 -1
  71. data/test/unit/validations_test.rb +1 -1
  72. metadata +7 -2
@@ -29,16 +29,20 @@ module CassandraObject
29
29
 
30
30
  private
31
31
  def run_command(command)
32
- `echo "#{command};" | cqlsh -k #{keyspace} #{server}`.sub(/^(.*)$/, '').strip
32
+ `echo "#{command};" | #{cqlsh} -k #{keyspace} #{server}`.sub(/^(.*)$/, '').strip
33
+ end
34
+
35
+ def cqlsh
36
+ ENV['CQLSH'] || 'cqlsh'
33
37
  end
34
38
 
35
39
  def keyspace
36
- CassandraObject::Base.connection_config.keyspace
40
+ CassandraObject::Base.config.keyspace
37
41
  end
38
42
 
39
43
  def server
40
- CassandraObject::Base.connection_config.servers.first.gsub(/:.*/, '')
44
+ CassandraObject::Base.config.servers.first.gsub(/:.*/, '')
41
45
  end
42
46
  end
43
47
  end
44
- end
48
+ end
@@ -45,4 +45,4 @@ module CassandraObject
45
45
  end
46
46
  end
47
47
  end
48
- end
48
+ end
@@ -29,4 +29,4 @@ module CassandraObject
29
29
  end
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -44,4 +44,4 @@ module CassandraObject
44
44
  end
45
45
  end
46
46
  end
47
- end
47
+ end
@@ -106,4 +106,4 @@ module CassandraObject
106
106
  end
107
107
  end
108
108
  end
109
- end
109
+ end
@@ -2,10 +2,10 @@ ks_namespace = namespace :ks do
2
2
  desc 'Create the keyspace in cassandra_config/cassandra.yml for the current environment'
3
3
  task create: :environment do
4
4
  begin
5
- CassandraObject::Schema.create_keyspace cassandra_config['keyspace']
5
+ CassandraObject::Schema.create_keyspace cassandra_config.keyspace, cassandra_config.keyspace_options
6
6
  rescue Exception => e
7
7
  if e.message =~ /conflicts/
8
- p "Keyspace #{cassandra_config['keyspace']} already exists"
8
+ p "Keyspace #{cassandra_config.keyspace} already exists"
9
9
  else
10
10
  raise e
11
11
  end
@@ -14,10 +14,10 @@ ks_namespace = namespace :ks do
14
14
 
15
15
  task drop: :environment do
16
16
  begin
17
- CassandraObject::Schema.drop_keyspace cassandra_config['keyspace']
17
+ CassandraObject::Schema.drop_keyspace cassandra_config.keyspace
18
18
  rescue Exception => e
19
19
  if e.message =~ /non existing keyspace/
20
- p "Keyspace #{cassandra_config['keyspace']} does not exist"
20
+ p "Keyspace #{cassandra_config.keyspace} does not exist"
21
21
  else
22
22
  raise e
23
23
  end
@@ -55,8 +55,8 @@ ks_namespace = namespace :ks do
55
55
  private
56
56
  def cassandra_config
57
57
  @cassandra_config ||= begin
58
- cassandra_configs = YAML.load_file(Rails.root.join("config", "cassandra.yml"))
59
- cassandra_configs[Rails.env || 'development']
58
+ cassandra_configs = YAML.load_file(Rails.root.join('config', 'cassandra.yml'))
59
+ CassandraObject::Config.new cassandra_configs[Rails.env || 'development']
60
60
  end
61
61
  end
62
62
  end
@@ -73,4 +73,4 @@ module CassandraObject
73
73
  end
74
74
  end
75
75
  end
76
- end
76
+ end
@@ -23,4 +23,4 @@ module CassandraObject
23
23
  end
24
24
  end
25
25
  end
26
- end
26
+ end
@@ -4,7 +4,7 @@ module CassandraObject
4
4
  TRUE_VALS = [true, 'true', '1']
5
5
  FALSE_VALS = [false, 'false', '0', '', nil]
6
6
  VALID_VALS = TRUE_VALS + FALSE_VALS
7
-
7
+
8
8
  def encode(bool)
9
9
  unless VALID_VALS.include?(bool)
10
10
  raise ArgumentError.new("#{bool.inspect} is not a Boolean")
@@ -18,4 +18,4 @@ module CassandraObject
18
18
  end
19
19
  end
20
20
  end
21
- end
21
+ end
@@ -14,4 +14,4 @@ module CassandraObject
14
14
  end
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -14,4 +14,4 @@ module CassandraObject
14
14
  end
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -10,4 +10,4 @@ module CassandraObject
10
10
  end
11
11
  end
12
12
  end
13
- end
13
+ end
@@ -12,4 +12,4 @@ module CassandraObject
12
12
  end
13
13
  end
14
14
  end
15
- end
15
+ end
@@ -21,4 +21,4 @@ module CassandraObject
21
21
  end
22
22
  end
23
23
  end
24
- end
24
+ end
@@ -10,11 +10,11 @@ module CassandraObject
10
10
  module Validations
11
11
  extend ActiveSupport::Concern
12
12
  include ActiveModel::Validations
13
-
13
+
14
14
  included do
15
15
  define_callbacks :validate, scope: :name
16
16
  end
17
-
17
+
18
18
  module ClassMethods
19
19
  def create!(attributes = {})
20
20
  new(attributes).tap do |object|
@@ -31,7 +31,7 @@ module CassandraObject
31
31
  false
32
32
  end
33
33
  end
34
-
34
+
35
35
  def save!
36
36
  save || raise(RecordInvalid.new(self))
37
37
  end
@@ -5,23 +5,25 @@ require 'cassandra-cql'
5
5
  module CassandraObject
6
6
  extend ActiveSupport::Autoload
7
7
 
8
- autoload :Base
9
- autoload :Connection
10
- autoload :Consistency
11
8
  autoload :AttributeMethods
9
+ autoload :Base
12
10
  autoload :BelongsTo
13
- autoload :Persistence
14
11
  autoload :Callbacks
15
- autoload :Validations
12
+ autoload :Config
13
+ autoload :Connection
14
+ autoload :Consistency
15
+ autoload :Core
16
16
  autoload :Identity
17
17
  autoload :Inspect
18
- autoload :Serialization
18
+ autoload :Persistence
19
19
  autoload :Savepoints
20
20
  autoload :Schema
21
21
  autoload :Scope
22
22
  autoload :Scoping
23
+ autoload :Serialization
23
24
  autoload :Timestamps
24
25
  autoload :Type
26
+ autoload :Validations
25
27
 
26
28
  module BelongsTo
27
29
  extend ActiveSupport::Autoload
@@ -44,7 +46,7 @@ module CassandraObject
44
46
 
45
47
  module Types
46
48
  extend ActiveSupport::Autoload
47
-
49
+
48
50
  autoload :BaseType
49
51
  autoload :ArrayType
50
52
  autoload :BooleanType
@@ -57,4 +59,4 @@ module CassandraObject
57
59
  end
58
60
  end
59
61
 
60
- require 'cassandra_object/railtie' if defined?(Rails)
62
+ require 'cassandra_object/railtie' if defined?(Rails)
@@ -1,8 +1,8 @@
1
- CassandraObject::Base.establish_connection(
1
+ CassandraObject::Base.config = {
2
2
  keyspace: 'cassandra_object_test',
3
3
  servers: '127.0.0.1:9160',
4
4
  thrift: {timeout: 10000}
5
- )
5
+ }
6
6
 
7
7
  begin
8
8
  CassandraObject::Schema.drop_keyspace 'cassandra_object_test'
@@ -12,4 +12,4 @@ end
12
12
  sleep 1
13
13
  CassandraObject::Schema.create_keyspace 'cassandra_object_test'
14
14
  CassandraObject::Schema.create_column_family 'Issues'
15
- CassandraObject::Base.default_consistency = 'QUORUM'
15
+ CassandraObject::Base.default_consistency = 'QUORUM'
@@ -2,4 +2,4 @@ class Issue < CassandraObject::Base
2
2
  string :description
3
3
  string :title
4
4
  before_create { self.description ||= 'funny' }
5
- end
5
+ end
data/test/test_helper.rb CHANGED
@@ -18,7 +18,7 @@ module CassandraObject
18
18
  'Issue'
19
19
  end
20
20
 
21
- instance_eval(&block)
21
+ instance_eval(&block) if block_given?
22
22
  end
23
23
  end
24
24
  end
@@ -1,7 +1,12 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class ActiveModelTest < CassandraObject::TestCase
4
- # include ActiveModel::Lint::Tests
4
+
5
+ include ActiveModel::Lint::Tests
6
+
7
+ # overrides ActiveModel::Lint::Tests#test_to_param
8
+ def test_to_param
9
+ end
5
10
 
6
11
  def setup
7
12
  @model = Issue.new
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  class CassandraObject::AttributeMethods::DefinitionTest < CassandraObject::TestCase
4
4
  class TestType < CassandraObject::Types::BaseType
5
5
  end
6
-
6
+
7
7
  test 'typecast' do
8
8
  definition = CassandraObject::AttributeMethods::Definition.new(:foo, TestType, {a: :b})
9
9
 
@@ -5,7 +5,7 @@ class CassandraObject::AttributeMethods::DirtyTest < CassandraObject::TestCase
5
5
  record = temp_object do
6
6
  string :name
7
7
  end.new name: 'foo'
8
-
8
+
9
9
  assert record.changed?
10
10
 
11
11
  record.save!
@@ -42,7 +42,7 @@ class CassandraObject::AttributeMethods::DirtyTest < CassandraObject::TestCase
42
42
  record = temp_object do
43
43
  boolean :awesome
44
44
  end.create(awesome: false)
45
-
45
+
46
46
  record.awesome = false
47
47
  assert !record.changed?
48
48
 
@@ -23,4 +23,4 @@ class CassandraObject::AttributeMethods::PrimaryKeyTest < CassandraObject::TestC
23
23
 
24
24
  assert_not_nil issue.attributes['id']
25
25
  end
26
- end
26
+ end
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  class CassandraObject::AttributeMethods::TypecastingTest < CassandraObject::TestCase
4
4
  class CustomType
5
5
  end
6
-
6
+
7
7
  class CustomCoder < CassandraObject::Types::BaseType
8
8
  end
9
9
 
@@ -83,4 +83,30 @@ class CassandraObject::AttributeMethods::TypecastingTest < CassandraObject::Test
83
83
  issue = TestIssue.find issue.id
84
84
  assert_equal('hola', issue.name)
85
85
  end
86
- 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
@@ -16,46 +16,4 @@ class CassandraObject::BaseTest < CassandraObject::TestCase
16
16
  assert_equal 'CassandraObject::BaseTest::Sons', Son.column_family
17
17
  assert_equal 'CassandraObject::BaseTest::Sons', Grandson.column_family
18
18
  end
19
-
20
- test 'initialiaze' do
21
- issue = Issue.new
22
-
23
- assert issue.new_record?
24
- assert !issue.destroyed?
25
- end
26
-
27
- test 'dup' do
28
- issue = Issue.create description: 'foo'
29
-
30
- dup_issue = issue.dup
31
-
32
- assert dup_issue.new_record?
33
- assert_not_equal issue.id, dup_issue.id
34
- assert_nil dup_issue.created_at
35
- assert_nil dup_issue.updated_at
36
- assert_equal 'foo', issue.description
37
- end
38
-
39
- test 'equality of new records' do
40
- assert_not_equal Issue.new, Issue.new
41
- end
42
-
43
- test 'equality' do
44
- first_issue = Issue.create description: 'poop'
45
- second_issue = Issue.create description: 'poop'
46
-
47
- assert_equal first_issue, first_issue
48
- assert_equal first_issue, Issue.find(first_issue.id)
49
- assert_not_equal first_issue, second_issue
50
- end
51
-
52
- test 'to_param' do
53
- issue = Issue.new
54
- assert_equal issue.id, issue.to_param
55
- end
56
-
57
- test 'hash' do
58
- issue = Issue.create
59
- assert_equal issue.id.hash, issue.hash
60
- end
61
- end
19
+ 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
@@ -49,4 +49,4 @@ class CassandraObject::BelongsToTest < CassandraObject::TestCase
49
49
  record = TestObject.find(record.id)
50
50
  assert_equal issue, record.target
51
51
  end
52
- end
52
+ end
@@ -43,4 +43,4 @@ class CassandraObject::CallbacksTest < CassandraObject::TestCase
43
43
 
44
44
  assert_equal ['after_destroy'], issue.callback_history
45
45
  end
46
- 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