superstore 2.4.3 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +40 -0
- data/.gitignore +1 -0
- data/.simplecov +3 -0
- data/Gemfile +2 -3
- data/LICENSE +1 -1
- data/MIT-LICENSE +1 -1
- data/README.md +4 -34
- data/lib/superstore/adapters/abstract_adapter.rb +1 -27
- data/lib/superstore/adapters/jsonb_adapter.rb +4 -132
- data/lib/superstore/associations/association.rb +6 -0
- data/lib/superstore/associations/association_scope.rb +20 -0
- data/lib/superstore/associations/belongs_to.rb +3 -1
- data/lib/superstore/associations/builder/association.rb +1 -1
- data/lib/superstore/associations/has_many.rb +15 -2
- data/lib/superstore/associations/reflection.rb +8 -2
- data/lib/superstore/associations.rb +9 -4
- data/lib/superstore/attribute_assignment.rb +7 -0
- data/lib/superstore/attribute_methods/primary_key.rb +20 -11
- data/lib/superstore/attribute_methods.rb +1 -109
- data/lib/superstore/attributes.rb +13 -0
- data/lib/superstore/base.rb +6 -34
- data/lib/superstore/core.rb +7 -65
- data/lib/superstore/model_schema.rb +35 -0
- data/lib/superstore/persistence.rb +26 -115
- data/lib/superstore/railtie.rb +3 -11
- data/lib/superstore/relation/scrolling.rb +48 -0
- data/lib/superstore/types/array_type.rb +3 -7
- data/lib/superstore/types/base.rb +9 -0
- data/lib/superstore/types/boolean_type.rb +7 -12
- data/lib/superstore/types/date_range_type.rb +7 -0
- data/lib/superstore/types/date_type.rb +7 -10
- data/lib/superstore/types/float_type.rb +3 -11
- data/lib/superstore/types/geo_point_type.rb +30 -0
- data/lib/superstore/types/integer_range_type.rb +15 -0
- data/lib/superstore/types/integer_type.rb +8 -14
- data/lib/superstore/types/json_type.rb +1 -1
- data/lib/superstore/types/range_type.rb +58 -0
- data/lib/superstore/types/string_type.rb +4 -4
- data/lib/superstore/types/time_type.rb +10 -8
- data/lib/superstore/types.rb +11 -9
- data/lib/superstore.rb +17 -19
- data/superstore.gemspec +8 -10
- data/test/support/jsonb.rb +3 -1
- data/test/support/models.rb +10 -4
- data/test/test_helper.rb +7 -3
- data/test/unit/adapters/adapter_test.rb +1 -3
- data/test/unit/associations/belongs_to_test.rb +1 -1
- data/test/unit/associations/has_many_test.rb +10 -2
- data/test/unit/attribute_methods/dirty_test.rb +8 -19
- data/test/unit/attribute_methods/primary_key_test.rb +1 -1
- data/test/unit/attribute_methods_test.rb +10 -22
- data/test/unit/{attribute_methods/typecasting_test.rb → attributes_test.rb} +13 -39
- data/test/unit/base_test.rb +4 -0
- data/test/unit/caching_test.rb +1 -1
- data/test/unit/callbacks_test.rb +4 -4
- data/test/unit/core_test.rb +15 -20
- data/test/unit/persistence_test.rb +36 -44
- data/test/unit/{scope/batches_test.rb → relation/scrolling_test.rb} +9 -5
- data/test/unit/serialization_test.rb +10 -2
- data/test/unit/{timestamps_test.rb → timestamp_test.rb} +5 -5
- data/test/unit/types/array_type_test.rb +3 -18
- data/test/unit/types/boolean_type_test.rb +7 -21
- data/test/unit/types/date_range_type_test.rb +29 -0
- data/test/unit/types/date_type_test.rb +15 -6
- data/test/unit/types/float_type_test.rb +4 -19
- data/test/unit/types/geo_point_type_test.rb +28 -0
- data/test/unit/types/integer_range_type_test.rb +28 -0
- data/test/unit/types/integer_type_test.rb +7 -16
- data/test/unit/types/string_type_test.rb +9 -13
- data/test/unit/types/time_type_test.rb +17 -11
- data/test/unit/validations_test.rb +2 -2
- metadata +39 -53
- data/.travis.yml +0 -13
- data/Gemfile-rails4.2 +0 -11
- data/lib/superstore/attribute_methods/definition.rb +0 -17
- data/lib/superstore/attribute_methods/dirty.rb +0 -52
- data/lib/superstore/attribute_methods/typecasting.rb +0 -53
- data/lib/superstore/caching.rb +0 -13
- data/lib/superstore/callbacks.rb +0 -29
- data/lib/superstore/connection.rb +0 -24
- data/lib/superstore/errors.rb +0 -10
- data/lib/superstore/inspect.rb +0 -25
- data/lib/superstore/model.rb +0 -38
- data/lib/superstore/schema.rb +0 -20
- data/lib/superstore/scope/batches.rb +0 -27
- data/lib/superstore/scope/finder_methods.rb +0 -51
- data/lib/superstore/scope/query_methods.rb +0 -52
- data/lib/superstore/scope.rb +0 -73
- data/lib/superstore/scoping.rb +0 -30
- data/lib/superstore/timestamps.rb +0 -19
- data/lib/superstore/type.rb +0 -16
- data/lib/superstore/types/base_type.rb +0 -23
- data/lib/superstore/validations.rb +0 -44
- data/test/unit/attribute_methods/definition_test.rb +0 -16
- data/test/unit/inspect_test.rb +0 -26
- data/test/unit/schema_test.rb +0 -15
- data/test/unit/scope/finder_methods_test.rb +0 -62
- data/test/unit/scope/query_methods_test.rb +0 -36
- data/test/unit/scoping_test.rb +0 -7
- data/test/unit/types/base_type_test.rb +0 -11
data/test/unit/callbacks_test.rb
CHANGED
@@ -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
|
-
|
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
|
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
|
-
|
65
|
+
attribute :description, type: :string
|
66
66
|
|
67
67
|
before_create :expect_new_record
|
68
68
|
before_save :expect_new_record
|
data/test/unit/core_test.rb
CHANGED
@@ -1,30 +1,13 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Superstore::CoreTest < Superstore::TestCase
|
4
|
-
test '
|
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,11 +28,23 @@ class Superstore::CoreTest < Superstore::TestCase
|
|
45
28
|
|
46
29
|
test 'hash' do
|
47
30
|
issue = Issue.create
|
48
|
-
|
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
|
52
42
|
issue = Issue.create
|
53
|
-
assert issue.inspect =~ /^#<Issue id: \"\w+\",
|
43
|
+
assert issue.inspect =~ /^#<Issue id: \"\w+\", description: \".+\", created_at: \".+\", updated_at: \".+\">$/
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'inspect class' do
|
47
|
+
expected = "Issue(id: string, description: string, title: string, parent_issue_id: string, comments: json, created_at: time, updated_at: time)"
|
48
|
+
assert_equal expected, Issue.inspect
|
54
49
|
end
|
55
50
|
end
|
@@ -4,47 +4,37 @@
|
|
4
4
|
require 'test_helper'
|
5
5
|
|
6
6
|
class Superstore::PersistenceTest < Superstore::TestCase
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
class Parent < Superstore::Base
|
8
|
+
self.inheritance_column = 'document_type'
|
9
|
+
attribute :document_type, type: :string
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
string :description
|
11
|
+
def self.find_sti_class(type)
|
12
|
+
Child
|
14
13
|
end
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
)
|
16
|
+
class Child < Parent
|
17
|
+
attribute :eye_color, type: :string
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
klass.encode_attributes({'description' => nil})
|
24
|
-
)
|
20
|
+
test 'instantiate with unknowns' do
|
21
|
+
issue = Issue.instantiate('id' => 'theid', 'document' => {'z' => 'nooo', 'title' => 'Krazy'}.to_json)
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
klass.encode_attributes({'description' => 'lol'})
|
29
|
-
)
|
23
|
+
refute issue.attributes.key?('z')
|
24
|
+
assert_equal 'Krazy', issue.attributes['title']
|
30
25
|
end
|
31
26
|
|
32
|
-
test
|
33
|
-
|
34
|
-
|
35
|
-
Issue.batch do
|
36
|
-
assert Issue.batching?
|
27
|
+
test 'instantiate with an inheritance column' do
|
28
|
+
child = Parent.instantiate('id' => 'theid', 'document' => {'document_type' => 'child', 'eye_color' => 'blue'}.to_json)
|
37
29
|
|
38
|
-
|
39
|
-
|
30
|
+
assert_kind_of Child, child
|
31
|
+
assert_equal 'blue', child.eye_color
|
32
|
+
end
|
40
33
|
|
41
|
-
|
42
|
-
|
43
|
-
end
|
34
|
+
test 'instantiate when an inheritance column is expected but is nil' do
|
35
|
+
child = Parent.instantiate('id' => 'theid', 'document' => { }.to_json)
|
44
36
|
|
45
|
-
|
46
|
-
assert_nothing_raised { Issue.find(first_issue.id) }
|
47
|
-
assert_nothing_raised { Issue.find(second_issue.id) }
|
37
|
+
assert_kind_of Child, child
|
48
38
|
end
|
49
39
|
|
50
40
|
test 'persistence inquiries' do
|
@@ -81,14 +71,14 @@ class Superstore::PersistenceTest < Superstore::TestCase
|
|
81
71
|
|
82
72
|
test 'save!' do
|
83
73
|
klass = temp_object do
|
84
|
-
|
74
|
+
attribute :description, type: :string
|
85
75
|
validates :description, presence: true
|
86
76
|
end
|
87
77
|
|
88
78
|
record = klass.new(description: 'bad')
|
89
79
|
record.save!
|
90
80
|
|
91
|
-
assert_raise
|
81
|
+
assert_raise ActiveRecord::RecordInvalid do
|
92
82
|
record = klass.new
|
93
83
|
record.save!
|
94
84
|
end
|
@@ -126,7 +116,7 @@ class Superstore::PersistenceTest < Superstore::TestCase
|
|
126
116
|
issue = Issue.new(description: 'bad')
|
127
117
|
issue.save!
|
128
118
|
|
129
|
-
assert_raise
|
119
|
+
assert_raise ActiveRecord::RecordInvalid do
|
130
120
|
issue.update! description: ''
|
131
121
|
end
|
132
122
|
ensure
|
@@ -151,7 +141,7 @@ class Superstore::PersistenceTest < Superstore::TestCase
|
|
151
141
|
|
152
142
|
test 'becomes includes changed_attributes' do
|
153
143
|
klass = temp_object do
|
154
|
-
|
144
|
+
attribute :title, type: :string
|
155
145
|
end
|
156
146
|
|
157
147
|
issue = Issue.new(title: 'Something is wrong')
|
@@ -171,14 +161,9 @@ class Superstore::PersistenceTest < Superstore::TestCase
|
|
171
161
|
assert_equal persisted_issue, reloaded_issue
|
172
162
|
end
|
173
163
|
|
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
164
|
test 'delete' do
|
180
165
|
klass = temp_object do
|
181
|
-
|
166
|
+
attribute :name, type: :string
|
182
167
|
end
|
183
168
|
|
184
169
|
record = klass.new(name: 'cool')
|
@@ -189,14 +174,14 @@ class Superstore::PersistenceTest < Superstore::TestCase
|
|
189
174
|
|
190
175
|
klass.delete(id)
|
191
176
|
|
192
|
-
assert_raise
|
177
|
+
assert_raise ActiveRecord::RecordNotFound do
|
193
178
|
klass.find(id)
|
194
179
|
end
|
195
180
|
end
|
196
181
|
|
197
182
|
test 'delete multiple' do
|
198
183
|
klass = temp_object do
|
199
|
-
|
184
|
+
attribute :name, type: :string
|
200
185
|
end
|
201
186
|
|
202
187
|
ids = []
|
@@ -207,7 +192,14 @@ class Superstore::PersistenceTest < Superstore::TestCase
|
|
207
192
|
|
208
193
|
klass.delete(ids)
|
209
194
|
|
210
|
-
assert_equal [], klass.
|
195
|
+
assert_equal [], klass.where(id: ids)
|
211
196
|
end
|
212
197
|
|
198
|
+
test 'find_by_id' do
|
199
|
+
Issue.create.tap do |issue|
|
200
|
+
assert_equal issue, Issue.find_by_id(issue.id)
|
201
|
+
end
|
202
|
+
|
203
|
+
assert_nil Issue.find_by_id('what')
|
204
|
+
end
|
213
205
|
end
|
@@ -1,25 +1,29 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class Superstore::
|
4
|
-
|
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.
|
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 '
|
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.
|
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 = {
|
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::
|
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
|
-
|
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
|
-
|
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
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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,29 @@
|
|
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_equal Date.new(2004, 4, 25)..Date.new(2004, 5, 15), type.cast_value([Date.new(2004, 4, 25), Date.new(2004, 5, 15)])
|
20
|
+
assert_equal Date.new(2004, 4, 25)..Date.new(2004, 5, 15), type.cast_value(["2004-04-25", "2004-05-15"])
|
21
|
+
assert_equal Date.new(2004, 4, 25)..Date.new(2004, 4, 25), type.cast_value(["2004-04-25", "2004-04-25"])
|
22
|
+
assert_equal Date.new(2004, 4, 25).., type.cast_value(["2004-04-25", nil])
|
23
|
+
|
24
|
+
assert_nil type.cast_value( Date.new(2004, 5, 15)..Date.new(2004, 4, 25))
|
25
|
+
assert_nil type.cast_value([Date.new(2004, 5, 15), Date.new(2004, 4, 25)])
|
26
|
+
assert_nil type.cast_value([nil, "2004-05-15"])
|
27
|
+
assert_nil type.cast_value(["xx", "2004-05-15"])
|
28
|
+
end
|
29
|
+
end
|
@@ -1,15 +1,24 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Superstore::Types::DateTypeTest < Superstore::Types::TestCase
|
4
|
-
test '
|
5
|
-
assert_equal '2004-04-25', type.
|
4
|
+
test 'serialize' do
|
5
|
+
assert_equal '2004-04-25', type.serialize(Date.new(2004, 4, 25))
|
6
6
|
end
|
7
7
|
|
8
|
-
test '
|
9
|
-
assert_equal Date.new(2004, 4, 25), type.
|
8
|
+
test 'deserialize' do
|
9
|
+
assert_equal Date.new(2004, 4, 25), type.deserialize('2004-04-25')
|
10
10
|
end
|
11
11
|
|
12
|
-
test '
|
13
|
-
assert_nil type.
|
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
|
-
|
5
|
-
|
6
|
-
|
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
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Superstore::Types::GeoPointTypeTest < Superstore::Types::TestCase
|
4
|
+
test 'deserialize' do
|
5
|
+
lat, lon = 47.604, -122.329
|
6
|
+
seattle = {lat: lat, lon: lon}
|
7
|
+
|
8
|
+
assert_equal seattle, type.deserialize('lat' => lat, 'lon' => lon)
|
9
|
+
end
|
10
|
+
|
11
|
+
test 'cast_value' do
|
12
|
+
lat, lon = 47.604, -122.329
|
13
|
+
seattle = {lat: lat, lon: lon}
|
14
|
+
|
15
|
+
assert_equal seattle, type.cast_value(lat: lat, lon: lon)
|
16
|
+
assert_equal seattle, type.cast_value({ "lat" => lat, "lon" => lon })
|
17
|
+
assert_equal seattle, type.cast_value([lat, lon])
|
18
|
+
|
19
|
+
assert_equal({lat: 0.0, lon: 0.0}, type.cast_value(lat: "cats", lon: "dogs"))
|
20
|
+
|
21
|
+
assert_nil type.cast_value([])
|
22
|
+
assert_nil type.cast_value('invalid')
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'type' do
|
26
|
+
assert_equal 'geo_point', type.type
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Superstore::Types::IntegerRangeTypeTest < Superstore::Types::TestCase
|
4
|
+
test 'serialize' do
|
5
|
+
assert_equal [4, 5], type.serialize(4..5)
|
6
|
+
assert_equal [4, nil], type.serialize(4..)
|
7
|
+
assert_equal [nil, 5], type.serialize(-Float::INFINITY..5)
|
8
|
+
assert_equal [nil, nil], type.serialize(-Float::INFINITY..)
|
9
|
+
end
|
10
|
+
|
11
|
+
test 'deserialize' do
|
12
|
+
assert_equal 4..5, type.deserialize([4, 5])
|
13
|
+
assert_nil type.deserialize([5, 4])
|
14
|
+
assert_equal 4.., type.deserialize([4, nil])
|
15
|
+
assert_equal (-Float::INFINITY..5), type.deserialize([nil, 5])
|
16
|
+
assert_equal (-Float::INFINITY..), type.deserialize([nil, nil])
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'cast_value' do
|
20
|
+
assert_equal 1..5, type.cast_value(1..5)
|
21
|
+
assert_nil type.cast_value(5..1)
|
22
|
+
assert_equal 1..5, type.cast_value([1, 5])
|
23
|
+
assert_nil type.cast_value([5, 1])
|
24
|
+
assert_equal 1.., type.cast_value([1, nil])
|
25
|
+
assert_equal (-Float::INFINITY..2), type.cast_value([nil, 2])
|
26
|
+
assert_equal (-Float::INFINITY..), type.cast_value([nil, nil])
|
27
|
+
end
|
28
|
+
end
|
@@ -1,21 +1,12 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Superstore::Types::IntegerTypeTest < Superstore::Types::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
test 'decode' do
|
16
|
-
assert_nil type.decode('')
|
17
|
-
assert_equal(0, type.decode('abc'))
|
18
|
-
assert_equal(3, type.decode('3'))
|
19
|
-
assert_equal(-3, type.decode('-3'))
|
4
|
+
test 'cast_value' do
|
5
|
+
assert_nil type.cast_value('')
|
6
|
+
assert_nil type.cast_value('abc')
|
7
|
+
assert_equal 3, type.cast_value(3)
|
8
|
+
assert_equal 3, type.cast_value('3')
|
9
|
+
assert_equal(-3, type.cast_value('-3'))
|
10
|
+
assert_equal 27, type.cast_value('027')
|
20
11
|
end
|
21
12
|
end
|
@@ -1,30 +1,26 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Superstore::Types::StringTypeTest < Superstore::Types::TestCase
|
4
|
-
test '
|
5
|
-
assert_equal 'abc', type.
|
6
|
-
|
7
|
-
assert_raise ArgumentError do
|
8
|
-
type.encode(123)
|
9
|
-
end
|
4
|
+
test 'serialize' do
|
5
|
+
assert_equal 'abc', type.serialize('abc')
|
10
6
|
end
|
11
7
|
|
12
|
-
test '
|
8
|
+
test 'serialize as utf' do
|
13
9
|
assert_equal(
|
14
10
|
'123'.force_encoding('UTF-8').encoding,
|
15
|
-
type.
|
11
|
+
type.serialize('123'.force_encoding('ASCII-8BIT')).encoding
|
16
12
|
)
|
17
13
|
end
|
18
14
|
|
19
|
-
test '
|
15
|
+
test 'serialize frozen as utf' do
|
20
16
|
assert_equal(
|
21
17
|
'123'.force_encoding('UTF-8').encoding,
|
22
|
-
type.
|
18
|
+
type.serialize('123'.force_encoding('ASCII-8BIT').freeze).encoding
|
23
19
|
)
|
24
20
|
end
|
25
21
|
|
26
|
-
test '
|
27
|
-
assert_equal '123', type.
|
28
|
-
assert_equal '123', type.
|
22
|
+
test 'cast_value' do
|
23
|
+
assert_equal '123', type.cast_value(123)
|
24
|
+
assert_equal '123', type.cast_value('123')
|
29
25
|
end
|
30
26
|
end
|
@@ -1,23 +1,29 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Superstore::Types::TimeTypeTest < Superstore::Types::TestCase
|
4
|
-
test '
|
5
|
-
assert_equal '2004-12-24T01:02:03.000000Z', type.
|
6
|
-
|
7
|
-
assert_raise ArgumentError do
|
8
|
-
type.encode 123
|
9
|
-
end
|
4
|
+
test 'serialize' do
|
5
|
+
assert_equal '2004-12-24T01:02:03.000000Z', type.serialize(Time.utc(2004, 12, 24, 1, 2, 3))
|
6
|
+
assert_nil type.serialize(nil)
|
10
7
|
end
|
11
8
|
|
12
|
-
test '
|
13
|
-
assert_nil type.
|
14
|
-
assert_nil type.
|
15
|
-
assert_equal Time.utc(2004, 12, 24, 1, 2, 3), type.
|
9
|
+
test 'deserialize' do
|
10
|
+
assert_nil type.deserialize(nil)
|
11
|
+
assert_nil type.deserialize('bad format')
|
12
|
+
assert_equal Time.utc(2004, 12, 24, 1, 2, 3), type.deserialize('2004-12-24T01:02:03.000000Z')
|
13
|
+
assert_equal Time.utc(2004, 12, 24, 12, 13, 45), type.deserialize('2004-12-24 12:13:45 -0000')
|
16
14
|
|
17
15
|
Time.use_zone 'Central Time (US & Canada)' do
|
18
|
-
with_zone = type.
|
16
|
+
with_zone = type.deserialize('2013-07-18T13:12:46-07:00')
|
19
17
|
assert_equal Time.utc(2013, 07, 18, 20, 12, 46), with_zone
|
20
18
|
assert_equal 'CDT', with_zone.zone
|
21
19
|
end
|
22
20
|
end
|
21
|
+
|
22
|
+
test 'cast_value' do
|
23
|
+
assert_nil type.cast_value(1000)
|
24
|
+
assert_nil type.cast_value(1000.0)
|
25
|
+
|
26
|
+
my_date = Date.new
|
27
|
+
assert_equal my_date.to_time, type.cast_value(my_date)
|
28
|
+
end
|
23
29
|
end
|