superstore 2.4.4 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +4 -6
  3. data/Gemfile +2 -3
  4. data/{Gemfile-rails4.2 → Gemfile.rails6} +2 -3
  5. data/README.md +4 -34
  6. data/lib/superstore.rb +17 -18
  7. data/lib/superstore/adapters/abstract_adapter.rb +1 -27
  8. data/lib/superstore/adapters/jsonb_adapter.rb +4 -132
  9. data/lib/superstore/associations.rb +6 -1
  10. data/lib/superstore/associations/association.rb +6 -0
  11. data/lib/superstore/associations/association_scope.rb +20 -0
  12. data/lib/superstore/associations/belongs_to.rb +3 -1
  13. data/lib/superstore/associations/has_many.rb +15 -2
  14. data/lib/superstore/associations/reflection.rb +8 -2
  15. data/lib/superstore/attribute_assignment.rb +7 -0
  16. data/lib/superstore/attribute_methods.rb +1 -109
  17. data/lib/superstore/attribute_methods/primary_key.rb +20 -11
  18. data/lib/superstore/attributes.rb +13 -0
  19. data/lib/superstore/base.rb +8 -33
  20. data/lib/superstore/core.rb +7 -65
  21. data/lib/superstore/model_schema.rb +35 -0
  22. data/lib/superstore/persistence.rb +31 -115
  23. data/lib/superstore/railtie.rb +3 -11
  24. data/lib/superstore/relation/scrolling.rb +48 -0
  25. data/lib/superstore/timestamp.rb +13 -0
  26. data/lib/superstore/types.rb +11 -9
  27. data/lib/superstore/types/array_type.rb +3 -7
  28. data/lib/superstore/types/boolean_type.rb +7 -12
  29. data/lib/superstore/types/date_range_type.rb +7 -0
  30. data/lib/superstore/types/date_type.rb +7 -10
  31. data/lib/superstore/types/float_type.rb +3 -11
  32. data/lib/superstore/types/geo_point_type.rb +30 -0
  33. data/lib/superstore/types/integer_range_type.rb +19 -0
  34. data/lib/superstore/types/integer_type.rb +8 -14
  35. data/lib/superstore/types/json_type.rb +1 -1
  36. data/lib/superstore/types/range_type.rb +51 -0
  37. data/lib/superstore/types/string_type.rb +4 -4
  38. data/lib/superstore/types/time_type.rb +10 -8
  39. data/superstore.gemspec +4 -3
  40. data/test/support/jsonb.rb +3 -1
  41. data/test/support/models.rb +8 -5
  42. data/test/test_helper.rb +6 -2
  43. data/test/unit/adapters/adapter_test.rb +1 -3
  44. data/test/unit/associations/belongs_to_test.rb +1 -1
  45. data/test/unit/associations/has_many_test.rb +10 -2
  46. data/test/unit/attribute_methods/dirty_test.rb +8 -19
  47. data/test/unit/attribute_methods/primary_key_test.rb +1 -1
  48. data/test/unit/attribute_methods_test.rb +10 -22
  49. data/test/unit/{attribute_methods/typecasting_test.rb → attributes_test.rb} +13 -39
  50. data/test/unit/base_test.rb +4 -0
  51. data/test/unit/caching_test.rb +1 -1
  52. data/test/unit/callbacks_test.rb +4 -4
  53. data/test/unit/core_test.rb +9 -19
  54. data/test/unit/persistence_test.rb +17 -54
  55. data/test/unit/{scope/batches_test.rb → relation/scrolling_test.rb} +9 -5
  56. data/test/unit/serialization_test.rb +10 -2
  57. data/test/unit/{timestamps_test.rb → timestamp_test.rb} +5 -5
  58. data/test/unit/types/array_type_test.rb +3 -18
  59. data/test/unit/types/boolean_type_test.rb +7 -21
  60. data/test/unit/types/date_range_type_test.rb +28 -0
  61. data/test/unit/types/date_type_test.rb +15 -6
  62. data/test/unit/types/float_type_test.rb +4 -19
  63. data/test/unit/types/geo_point_type_test.rb +24 -0
  64. data/test/unit/types/integer_range_type_test.rb +28 -0
  65. data/test/unit/types/integer_type_test.rb +7 -16
  66. data/test/unit/types/string_type_test.rb +9 -13
  67. data/test/unit/types/time_type_test.rb +17 -11
  68. data/test/unit/validations_test.rb +2 -2
  69. metadata +39 -39
  70. data/lib/superstore/attribute_methods/definition.rb +0 -17
  71. data/lib/superstore/attribute_methods/dirty.rb +0 -52
  72. data/lib/superstore/attribute_methods/typecasting.rb +0 -53
  73. data/lib/superstore/caching.rb +0 -13
  74. data/lib/superstore/callbacks.rb +0 -29
  75. data/lib/superstore/connection.rb +0 -24
  76. data/lib/superstore/errors.rb +0 -10
  77. data/lib/superstore/inspect.rb +0 -25
  78. data/lib/superstore/model.rb +0 -38
  79. data/lib/superstore/schema.rb +0 -20
  80. data/lib/superstore/scope.rb +0 -73
  81. data/lib/superstore/scope/batches.rb +0 -27
  82. data/lib/superstore/scope/finder_methods.rb +0 -51
  83. data/lib/superstore/scope/query_methods.rb +0 -52
  84. data/lib/superstore/scoping.rb +0 -30
  85. data/lib/superstore/timestamps.rb +0 -19
  86. data/lib/superstore/type.rb +0 -16
  87. data/lib/superstore/types/base_type.rb +0 -23
  88. data/lib/superstore/validations.rb +0 -44
  89. data/test/unit/attribute_methods/definition_test.rb +0 -16
  90. data/test/unit/inspect_test.rb +0 -26
  91. data/test/unit/schema_test.rb +0 -15
  92. data/test/unit/scope/finder_methods_test.rb +0 -62
  93. data/test/unit/scope/query_methods_test.rb +0 -37
  94. data/test/unit/scoping_test.rb +0 -7
  95. data/test/unit/types/base_type_test.rb +0 -11
@@ -17,4 +17,8 @@ class Superstore::BaseTest < Superstore::TestCase
17
17
  assert_equal 'sons', Son.table_name
18
18
  assert_equal 'sons', Grandson.table_name
19
19
  end
20
+
21
+ test 'translations' do
22
+ assert_equal :activerecord, Son.i18n_scope
23
+ end
20
24
  end
@@ -16,6 +16,6 @@ class Superstore::CachingTest < Superstore::TestCase
16
16
  updated_at = Time.now
17
17
  issue = Issue.create!(id: 1, updated_at: updated_at)
18
18
 
19
- assert_equal "issues/1-#{updated_at.utc.to_s(:nsec)}", issue.cache_key
19
+ assert_equal "issues/1-#{updated_at.utc.to_s(:usec)}", issue.cache_key
20
20
  end
21
21
  end
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  class Superstore::CallbacksTest < Superstore::TestCase
4
4
  class TestIssue < Superstore::Base
5
5
  self.table_name = 'issues'
6
- string :description
6
+ attribute :description, type: :string
7
7
 
8
8
  %w(
9
9
  before_validation
@@ -35,8 +35,8 @@ class Superstore::CallbacksTest < Superstore::TestCase
35
35
  before_validation
36
36
  after_validation
37
37
  before_save
38
- after_save
39
38
  after_create
39
+ after_save
40
40
  )
41
41
  assert_equal expected, issue.callback_history
42
42
  end
@@ -47,7 +47,7 @@ class Superstore::CallbacksTest < Superstore::TestCase
47
47
 
48
48
  issue.update_attribute :description, 'foo'
49
49
 
50
- assert_equal %w(before_save after_save after_update), issue.callback_history
50
+ assert_equal %w(before_save after_update after_save), issue.callback_history
51
51
  end
52
52
 
53
53
  test 'destroy' do
@@ -62,7 +62,7 @@ class Superstore::CallbacksTest < Superstore::TestCase
62
62
  test 'new_record during callbacks' do
63
63
  class NewRecordTestClass < Superstore::Base
64
64
  self.table_name = 'issues'
65
- string :description
65
+ attribute :description, type: :string
66
66
 
67
67
  before_create :expect_new_record
68
68
  before_save :expect_new_record
@@ -1,30 +1,13 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::CoreTest < Superstore::TestCase
4
- test 'initialiaze' do
4
+ test 'initialize' do
5
5
  issue = Issue.new
6
6
 
7
7
  assert issue.new_record?
8
8
  assert !issue.destroyed?
9
9
  end
10
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
11
  test 'equality of new records' do
29
12
  assert_not_equal Issue.new, Issue.new
30
13
  end
@@ -45,7 +28,14 @@ class Superstore::CoreTest < Superstore::TestCase
45
28
 
46
29
  test 'hash' do
47
30
  issue = Issue.create
48
- assert_equal issue.id.hash, issue.hash
31
+ issue2 = Issue.create
32
+ refute_equal issue.hash, issue2.hash
33
+
34
+ issue3 = Issue.new(id: issue.id)
35
+ assert_equal issue.hash, issue3.hash
36
+
37
+ user = User.new(id: issue.id)
38
+ refute_equal issue.hash, user.hash
49
39
  end
50
40
 
51
41
  test 'inspect' do
@@ -4,47 +4,8 @@
4
4
  require 'test_helper'
5
5
 
6
6
  class Superstore::PersistenceTest < Superstore::TestCase
7
- test 'instantiate removes unknowns' do
8
- assert_nil Issue.instantiate('theid', 'z' => 'nooo').attributes['z']
9
- end
10
-
11
- test 'encode_attributes' do
12
- klass = temp_object do
13
- string :description
14
- end
15
-
16
- assert_equal(
17
- {},
18
- klass.encode_attributes({})
19
- )
20
-
21
- assert_equal(
22
- {'description' => nil},
23
- klass.encode_attributes({'description' => nil})
24
- )
25
-
26
- assert_equal(
27
- {'description' => 'lol'},
28
- klass.encode_attributes({'description' => 'lol'})
29
- )
30
- end
31
-
32
- test "batch" do
33
- first_issue = second_issue = nil
34
-
35
- Issue.batch do
36
- assert Issue.batching?
37
-
38
- first_issue = Issue.create
39
- second_issue = Issue.create
40
-
41
- assert_raise(Superstore::RecordNotFound) { Issue.find(first_issue.id) }
42
- assert_raise(Superstore::RecordNotFound) { Issue.find(second_issue.id) }
43
- end
44
-
45
- assert !Issue.batching?
46
- assert_nothing_raised { Issue.find(first_issue.id) }
47
- assert_nothing_raised { Issue.find(second_issue.id) }
7
+ test 'instantiate with unknowns' do
8
+ assert_equal 'nooo', Issue.instantiate('id' => 'theid', 'document' => {'z' => 'nooo'}.to_json).attributes['z']
48
9
  end
49
10
 
50
11
  test 'persistence inquiries' do
@@ -81,14 +42,14 @@ class Superstore::PersistenceTest < Superstore::TestCase
81
42
 
82
43
  test 'save!' do
83
44
  klass = temp_object do
84
- string :description
45
+ attribute :description, type: :string
85
46
  validates :description, presence: true
86
47
  end
87
48
 
88
49
  record = klass.new(description: 'bad')
89
50
  record.save!
90
51
 
91
- assert_raise Superstore::RecordInvalid do
52
+ assert_raise ActiveRecord::RecordInvalid do
92
53
  record = klass.new
93
54
  record.save!
94
55
  end
@@ -126,7 +87,7 @@ class Superstore::PersistenceTest < Superstore::TestCase
126
87
  issue = Issue.new(description: 'bad')
127
88
  issue.save!
128
89
 
129
- assert_raise Superstore::RecordInvalid do
90
+ assert_raise ActiveRecord::RecordInvalid do
130
91
  issue.update! description: ''
131
92
  end
132
93
  ensure
@@ -151,7 +112,7 @@ class Superstore::PersistenceTest < Superstore::TestCase
151
112
 
152
113
  test 'becomes includes changed_attributes' do
153
114
  klass = temp_object do
154
- string :title
115
+ attribute :title, type: :string
155
116
  end
156
117
 
157
118
  issue = Issue.new(title: 'Something is wrong')
@@ -171,14 +132,9 @@ class Superstore::PersistenceTest < Superstore::TestCase
171
132
  assert_equal persisted_issue, reloaded_issue
172
133
  end
173
134
 
174
- test 'quote_columns' do
175
- klass = Class.new { include Superstore::Persistence }
176
- assert_equal %w{'a' 'b'}, klass.__send__(:quote_columns, %w{a b})
177
- end
178
-
179
135
  test 'delete' do
180
136
  klass = temp_object do
181
- string :name
137
+ attribute :name, type: :string
182
138
  end
183
139
 
184
140
  record = klass.new(name: 'cool')
@@ -189,14 +145,14 @@ class Superstore::PersistenceTest < Superstore::TestCase
189
145
 
190
146
  klass.delete(id)
191
147
 
192
- assert_raise Superstore::RecordNotFound do
148
+ assert_raise ActiveRecord::RecordNotFound do
193
149
  klass.find(id)
194
150
  end
195
151
  end
196
152
 
197
153
  test 'delete multiple' do
198
154
  klass = temp_object do
199
- string :name
155
+ attribute :name, type: :string
200
156
  end
201
157
 
202
158
  ids = []
@@ -207,7 +163,14 @@ class Superstore::PersistenceTest < Superstore::TestCase
207
163
 
208
164
  klass.delete(ids)
209
165
 
210
- assert_equal [], klass.find(ids)
166
+ assert_equal [], klass.where(id: ids)
211
167
  end
212
168
 
169
+ test 'find_by_id' do
170
+ Issue.create.tap do |issue|
171
+ assert_equal issue, Issue.find_by_id(issue.id)
172
+ end
173
+
174
+ assert_nil Issue.find_by_id('what')
175
+ end
213
176
  end
@@ -1,25 +1,29 @@
1
1
  require 'test_helper'
2
2
 
3
- class Superstore::BatchesTest < Superstore::TestCase
4
- test 'find_each' do
3
+ class Superstore::ScrollingTest < Superstore::TestCase
4
+ ActiveRecord::Relation.class_eval do
5
+ include Superstore::Relation::Scrolling
6
+ end
7
+
8
+ test 'scroll_each' do
5
9
  Issue.create
6
10
  Issue.create
7
11
 
8
12
  issues = []
9
- Issue.find_each do |issue|
13
+ Issue.all.scroll_each do |issue|
10
14
  issues << issue
11
15
  end
12
16
 
13
17
  assert_equal Issue.all.to_set, issues.to_set
14
18
  end
15
19
 
16
- test 'find_in_batches' do
20
+ test 'scroll_in_batches' do
17
21
  Issue.create
18
22
  Issue.create
19
23
  Issue.create
20
24
 
21
25
  issue_batches = []
22
- Issue.find_in_batches(batch_size: 2) do |issues|
26
+ Issue.all.scroll_in_batches(batch_size: 2) do |issues|
23
27
  issue_batches << issues
24
28
  end
25
29
 
@@ -3,8 +3,16 @@ require 'test_helper'
3
3
  class Superstore::SerializationTest < Superstore::TestCase
4
4
  test 'as_json' do
5
5
  issue = Issue.new
6
- expected = {"id" => issue.id}
6
+ expected = {
7
+ "id" => issue.id,
8
+ "created_at" => nil,
9
+ "updated_at" => nil,
10
+ "description" => nil,
11
+ "title" => nil,
12
+ "parent_issue_id" => nil,
13
+ "comments" => nil
14
+ }
7
15
 
8
16
  assert_equal expected, issue.as_json
9
17
  end
10
- end
18
+ end
@@ -1,8 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
- class Superstore::TimestampsTest < Superstore::TestCase
3
+ class Superstore::TimestampTest < Superstore::TestCase
4
4
  test 'timestamps set on create' do
5
- issue = Issue.create
5
+ issue = Issue.create#issue.created_at.to_i = #{issue.created_at.to_i}
6
6
 
7
7
  assert_in_delta Time.now.to_i, issue.created_at.to_i, 3
8
8
  assert_in_delta Time.now.to_i, issue.updated_at.to_i, 3
@@ -30,13 +30,13 @@ class Superstore::TimestampsTest < Superstore::TestCase
30
30
  time = 5.days.ago
31
31
  issue = Issue.create created_at: time
32
32
 
33
- assert_equal time, issue.created_at
33
+ assert_in_delta time.to_i, issue.created_at.to_i, 3
34
34
  end
35
35
 
36
36
  test 'updated_at sets only if nil' do
37
- time = 5.days.ago
37
+ time = 5.days.ago.utc
38
38
  issue = Issue.create updated_at: time
39
39
 
40
- assert_equal time, issue.updated_at
40
+ assert_in_delta time.to_i, issue.updated_at.to_i, 3
41
41
  end
42
42
  end
@@ -1,23 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::Types::ArrayTypeTest < Superstore::Types::TestCase
4
- if Superstore::Base.adapter.class.name == 'Superstore::Adapters::CassandraAdapter'
5
- test 'encode' do
6
- assert_equal ['1', '2'].to_json, type.encode(['1', '2'])
7
-
8
- assert_raise ArgumentError do
9
- type.encode('wtf')
10
- end
11
- end
12
-
13
- test 'decode' do
14
- assert_equal ['1', '2'], type.decode(['1', '2'].to_json)
15
- assert_equal nil, type.decode(nil)
16
- assert_equal nil, type.decode('')
17
- end
18
- end
19
-
20
- test 'typecast' do
21
- assert_equal ['x', 'y'], type.typecast(['x', 'y'].to_set)
4
+ test 'cast_value' do
5
+ assert_equal ['x', 'y'], type.cast_value(['x', 'y'].to_set)
6
+ assert_equal ['x'], type.cast_value('x')
22
7
  end
23
8
  end
@@ -1,26 +1,12 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::Types::BooleanTypeTest < Superstore::Types::TestCase
4
- if Superstore::Base.adapter.class.name == 'Superstore::Adapters::CassandraAdapter'
5
- test 'encode' do
6
- assert_equal '1', type.encode(true)
7
- assert_equal '1', type.encode('true')
8
- assert_equal '1', type.encode('1')
9
-
10
- assert_equal '0', type.encode(false)
11
- assert_equal '0', type.encode('false')
12
- assert_equal '0', type.encode('0')
13
- assert_equal '0', type.encode('')
14
-
15
- assert_raise ArgumentError do
16
- type.encode('wtf')
17
- end
18
- end
19
- end
20
-
21
- test 'decode' do
22
- assert_equal true, type.decode('1')
23
- assert_equal false, type.decode('0')
24
- # assert_nil type.decode(nil)
4
+ test 'cast_value' do
5
+ assert_equal true, type.cast_value('1')
6
+ assert_equal true, type.cast_value('true')
7
+ assert_equal true, type.cast_value('true')
8
+ assert_equal false, type.cast_value('0')
9
+ assert_equal false, type.cast_value(false)
10
+ assert_nil type.cast_value('')
25
11
  end
26
12
  end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class Superstore::Types::DateRangeTypeTest < Superstore::Types::TestCase
4
+ test 'serialize' do
5
+ assert_equal ["2004-04-25", "2004-05-15"], type.serialize(Date.new(2004, 4, 25) .. Date.new(2004, 5, 15))
6
+ end
7
+
8
+ test 'deserialize' do
9
+ assert_equal Date.new(2004, 4, 25)..Date.new(2004, 5, 15), type.deserialize(["2004-04-25", "2004-05-15"])
10
+ assert_nil type.deserialize(["2004-05-15", "2004-04-25"])
11
+
12
+ # decode returns argument if already a Range
13
+ range = Date.new(2019, 1, 1)..Date.new(2019, 2, 1)
14
+ assert_equal range, type.deserialize(range)
15
+ end
16
+
17
+ test 'cast_value' do
18
+ assert_equal Date.new(2004, 4, 25)..Date.new(2004, 5, 15), type.cast_value(Date.new(2004, 4, 25)..Date.new(2004, 5, 15))
19
+ assert_nil type.cast_value(Date.new(2004, 5, 15)..Date.new(2004, 4, 25))
20
+ assert_equal Date.new(2004, 4, 25)..Date.new(2004, 5, 15), type.cast_value([Date.new(2004, 4, 25), Date.new(2004, 5, 15)])
21
+ assert_nil type.cast_value([Date.new(2004, 5, 15), Date.new(2004, 4, 25)])
22
+ assert_equal Date.new(2004, 4, 25)..Date.new(2004, 5, 15), type.cast_value(["2004-04-25", "2004-05-15"])
23
+
24
+ assert_nil type.cast_value(["2004-04-25", nil])
25
+ assert_nil type.cast_value([nil, "2004-05-15"])
26
+ assert_nil type.cast_value(["xx", "2004-05-15"])
27
+ end
28
+ end
@@ -1,15 +1,24 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::Types::DateTypeTest < Superstore::Types::TestCase
4
- test 'encode' do
5
- assert_equal '2004-04-25', type.encode(Date.new(2004, 4, 25))
4
+ test 'serialize' do
5
+ assert_equal '2004-04-25', type.serialize(Date.new(2004, 4, 25))
6
6
  end
7
7
 
8
- test 'decode' do
9
- assert_equal Date.new(2004, 4, 25), type.decode('2004-04-25')
8
+ test 'deserialize' do
9
+ assert_equal Date.new(2004, 4, 25), type.deserialize('2004-04-25')
10
10
  end
11
11
 
12
- test 'decoding a blank dates' do
13
- assert_nil type.decode('')
12
+ test 'cast_value' do
13
+ assert_nil type.cast_value(1000)
14
+ assert_nil type.cast_value(1000.0)
15
+ assert_nil type.cast_value('')
16
+ assert_nil type.cast_value('nil')
17
+ assert_nil type.cast_value('bad format')
18
+ assert_equal Date.new(2004, 4, 25), type.cast_value('2004-04-25')
19
+ assert_equal Date.new(2017, 5, 1), type.cast_value('2017-05-01T21:39:06.796897Z')
20
+
21
+ my_time = Time.current
22
+ assert_equal my_time.to_date, type.cast_value(my_time)
14
23
  end
15
24
  end
@@ -1,24 +1,9 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::Types::FloatTypeTest < Superstore::Types::TestCase
4
- if Superstore::Base.adapter.class.name == 'Superstore::Adapters::CassandraAdapter'
5
- test 'encode' do
6
- assert_equal '5.01', type.encode(5.01)
7
-
8
- assert_raise ArgumentError do
9
- type.encode('x')
10
- end
11
- end
12
- end
13
-
14
- test 'decode' do
15
- assert_equal 0.0, type.decode('xyz')
16
- assert_equal 3.14, type.decode('3.14')
17
- assert_equal 5, type.decode('5')
18
- end
19
-
20
- test 'typecast' do
21
- assert_equal 1.1, type.typecast('1.1')
22
- assert_equal 42.0, type.typecast(42)
4
+ test 'cast_value' do
5
+ assert_nil type.cast_value('xyz')
6
+ assert_equal 1.1, type.cast_value('1.1')
7
+ assert_equal 42.0, type.cast_value(42)
23
8
  end
24
9
  end