record-cache 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/lib/record_cache/base.rb +4 -4
- data/lib/record_cache/datastore/active_record_30.rb +1 -1
- data/lib/record_cache/datastore/active_record_31.rb +1 -1
- data/lib/record_cache/datastore/active_record_32.rb +2 -2
- data/lib/record_cache/datastore/active_record_40.rb +445 -0
- data/lib/record_cache/datastore/active_record_41.rb +446 -0
- data/lib/record_cache/strategy/full_table_cache.rb +1 -1
- data/lib/record_cache/strategy/util.rb +20 -3
- data/lib/record_cache/version.rb +1 -1
- data/spec/db/create-record-cache-db_and_user.sql +5 -0
- data/spec/db/database.yml +7 -0
- data/spec/db/schema.rb +9 -15
- data/spec/initializers/backward_compatibility.rb +32 -0
- data/spec/lib/active_record/visitor_spec.rb +1 -1
- data/spec/lib/base_spec.rb +2 -2
- data/spec/lib/dispatcher_spec.rb +1 -1
- data/spec/lib/multi_read_spec.rb +1 -1
- data/spec/lib/query_spec.rb +1 -1
- data/spec/lib/statistics_spec.rb +1 -1
- data/spec/lib/strategy/base_spec.rb +39 -39
- data/spec/lib/strategy/full_table_cache_spec.rb +18 -18
- data/spec/lib/strategy/index_cache_spec.rb +58 -52
- data/spec/lib/strategy/query_cache_spec.rb +1 -1
- data/spec/lib/strategy/unique_index_on_id_cache_spec.rb +57 -45
- data/spec/lib/strategy/unique_index_on_string_cache_spec.rb +47 -45
- data/spec/lib/strategy/util_spec.rb +49 -43
- data/spec/lib/version_store_spec.rb +1 -1
- data/spec/models/apple.rb +1 -2
- data/spec/spec_helper.rb +16 -7
- data/spec/support/matchers/hit_cache_matcher.rb +1 -1
- data/spec/support/matchers/miss_cache_matcher.rb +1 -1
- data/spec/support/matchers/use_cache_matcher.rb +1 -1
- metadata +63 -17
- data/spec/support/after_commit.rb +0 -73
@@ -1,168 +1,170 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe RecordCache::Strategy::UniqueIndexCache do
|
3
|
+
RSpec.describe RecordCache::Strategy::UniqueIndexCache do
|
4
4
|
|
5
5
|
it "should retrieve an Person from the cache" do
|
6
|
-
expect{ Person.
|
7
|
-
expect{ Person.
|
6
|
+
expect{ Person.find_by(name: "Fry") }.to miss_cache(Person).on(:name).times(1)
|
7
|
+
expect{ Person.find_by(name: "Fry") }.to hit_cache(Person).on(:name).times(1)
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should retrieve cloned records" do
|
11
|
-
@fry_a = Person.
|
12
|
-
@fry_b = Person.
|
11
|
+
@fry_a = Person.find_by(name: "Fry")
|
12
|
+
@fry_b = Person.find_by(name: "Fry")
|
13
13
|
expect(@fry_a).to eq(@fry_b)
|
14
14
|
expect(@fry_a.object_id).to_not eq(@fry_b.object_id)
|
15
15
|
end
|
16
16
|
|
17
17
|
context "logging" do
|
18
18
|
before(:each) do
|
19
|
-
Person.
|
19
|
+
Person.find_by(name: "Fry")
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should write full hits to the debug log" do
|
23
|
-
expect{ Person.
|
23
|
+
expect{ Person.find_by(name: "Fry") }.to log(:debug, %(UniqueIndexCache on 'Person.name' hit for ids "Fry"))
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should write full miss to the debug log" do
|
27
|
-
expect{ Person.
|
27
|
+
expect{ Person.find_by(name: "Chase") }.to log(:debug, %(UniqueIndexCache on 'Person.name' miss for ids "Chase"))
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should write partial hits to the debug log" do
|
31
|
-
expect{ Person.where(:name => ["Fry", "Chase"]).
|
31
|
+
expect{ Person.where(:name => ["Fry", "Chase"]).load }.to log(:debug, %(UniqueIndexCache on 'Person.name' partial hit for ids ["Fry", "Chase"]: missing ["Chase"]))
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
35
|
context "cacheable?" do
|
36
36
|
before(:each) do
|
37
37
|
# fill cache
|
38
|
-
@fry = Person.
|
39
|
-
@chase = Person.
|
38
|
+
@fry = Person.find_by(name: "Fry")
|
39
|
+
@chase = Person.find_by(name: "Chase")
|
40
40
|
end
|
41
41
|
|
42
42
|
# @see https://github.com/orslumen/record-cache/issues/2
|
43
43
|
it "should not use the cache when a lock is used" do
|
44
|
-
|
44
|
+
pending("Any_lock is sqlite specific and I'm not aware of a mysql alternative") unless ActiveRecord::Base.connection.adapter_name == "SQLite"
|
45
|
+
|
46
|
+
expect{ Person.lock("any_lock").where(:name => "Fry").load }.to_not hit_cache(Person)
|
45
47
|
end
|
46
48
|
|
47
49
|
it "should use the cache when a single id is requested" do
|
48
|
-
expect{ Person.where(:name => "Fry").
|
50
|
+
expect{ Person.where(:name => "Fry").load }.to hit_cache(Person).on(:name).times(1)
|
49
51
|
end
|
50
52
|
|
51
53
|
it "should use the cache when a multiple ids are requested" do
|
52
|
-
expect{ Person.where(:name => ["Fry", "Chase"]).
|
54
|
+
expect{ Person.where(:name => ["Fry", "Chase"]).load }.to hit_cache(Person).on(:name).times(2)
|
53
55
|
end
|
54
56
|
|
55
57
|
it "should use the cache when a single id is requested and the limit is 1" do
|
56
|
-
expect{ Person.where(:name => "Fry").limit(1).
|
58
|
+
expect{ Person.where(:name => "Fry").limit(1).load }.to hit_cache(Person).on(:name).times(1)
|
57
59
|
end
|
58
60
|
|
59
61
|
it "should not use the cache when a single id is requested and the limit is > 1" do
|
60
|
-
expect{ Person.where(:name => "Fry").limit(2).
|
62
|
+
expect{ Person.where(:name => "Fry").limit(2).load }.to_not use_cache(Person).on(:name)
|
61
63
|
end
|
62
64
|
|
63
65
|
it "should not use the cache when multiple ids are requested and the limit is 1" do
|
64
|
-
expect{ Person.where(:name => ["Fry", "Chase"]).limit(1).
|
66
|
+
expect{ Person.where(:name => ["Fry", "Chase"]).limit(1).load }.to_not use_cache(Person).on(:name)
|
65
67
|
end
|
66
68
|
|
67
69
|
it "should use the cache when a single id is requested together with other where clauses" do
|
68
|
-
expect{ Person.where(:name => "Fry").where(:height => 1.67).
|
70
|
+
expect{ Person.where(:name => "Fry").where(:height => 1.67).load }.to hit_cache(Person).on(:name).times(1)
|
69
71
|
end
|
70
72
|
|
71
73
|
it "should use the cache when a multiple ids are requested together with other where clauses" do
|
72
|
-
expect{ Person.where(:name => ["Fry", "Chase"]).where(:height => 1.67).
|
74
|
+
expect{ Person.where(:name => ["Fry", "Chase"]).where(:height => 1.67).load }.to hit_cache(Person).on(:name).times(2)
|
73
75
|
end
|
74
76
|
|
75
77
|
it "should use the cache when a single id is requested together with (simple) sort clauses" do
|
76
|
-
expect{ Person.where(:name => "Fry").order("name ASC").
|
78
|
+
expect{ Person.where(:name => "Fry").order("name ASC").load }.to hit_cache(Person).on(:name).times(1)
|
77
79
|
end
|
78
80
|
|
79
81
|
it "should use the cache when a single id is requested together with (simple) case insensitive sort clauses" do
|
80
|
-
expect{ Person.where(:name => "Fry").order("name desc").
|
82
|
+
expect{ Person.where(:name => "Fry").order("name desc").load }.to hit_cache(Person).on(:name).times(1)
|
81
83
|
end
|
82
84
|
|
83
85
|
it "should use the cache when a single id is requested together with (simple) sort clauses with table prefix" do
|
84
|
-
expect{ Person.where(:name => "Fry").order("people.name desc").
|
86
|
+
expect{ Person.where(:name => "Fry").order("people.name desc").load }.to hit_cache(Person).on(:name).times(1)
|
85
87
|
end
|
86
88
|
|
87
89
|
it "should not use the cache when a single id is requested together with an unknown sort clause" do
|
88
|
-
expect{ Person.where(:name => "Fry").order("lower(people.name) desc").
|
90
|
+
expect{ Person.where(:name => "Fry").order("lower(people.name) desc").load }.to_not hit_cache(Person).on(:name).times(1)
|
89
91
|
end
|
90
92
|
|
91
93
|
it "should use the cache when a multiple ids are requested together with (simple) sort clauses" do
|
92
|
-
expect{ Person.where(:name => ["Fry", "Chase"]).order("name ASC").
|
94
|
+
expect{ Person.where(:name => ["Fry", "Chase"]).order("name ASC").load }.to hit_cache(Person).on(:name).times(2)
|
93
95
|
end
|
94
96
|
end
|
95
|
-
|
97
|
+
|
96
98
|
context "record_change" do
|
97
99
|
before(:each) do
|
98
100
|
# fill cache
|
99
|
-
@fry = Person.
|
100
|
-
@chase = Person.
|
101
|
+
@fry = Person.find_by(name: "Fry")
|
102
|
+
@chase = Person.find_by(name: "Chase")
|
101
103
|
end
|
102
104
|
|
103
105
|
it "should invalidate destroyed records" do
|
104
|
-
expect{ Person.where(:name => "Fry").
|
106
|
+
expect{ Person.where(:name => "Fry").load }.to hit_cache(Person).on(:name).times(1)
|
105
107
|
@fry.destroy
|
106
|
-
expect{ @people = Person.where(:name => "Fry").
|
108
|
+
expect{ @people = Person.where(:name => "Fry").load }.to miss_cache(Person).on(:name).times(1)
|
107
109
|
expect(@people).to be_empty
|
108
110
|
# try again, to make sure the "missing record" is not cached
|
109
|
-
expect{ Person.where(:name => "Fry").
|
111
|
+
expect{ Person.where(:name => "Fry").load }.to miss_cache(Person).on(:name).times(1)
|
110
112
|
end
|
111
113
|
|
112
114
|
it "should add updated records directly to the cache" do
|
113
115
|
@fry.height = 1.71
|
114
116
|
@fry.save!
|
115
|
-
expect{ @person = Person.
|
117
|
+
expect{ @person = Person.find_by(name: "Fry") }.to hit_cache(Person).on(:name).times(1)
|
116
118
|
expect(@person.height).to eq(1.71)
|
117
119
|
end
|
118
120
|
|
119
121
|
it "should add created records directly to the cache" do
|
120
122
|
Person.create!(:name => "Flower", :birthday => Date.civil(1990,07,29), :height => 1.80)
|
121
|
-
expect{ @person = Person.
|
123
|
+
expect{ @person = Person.find_by(name: "Flower") }.to hit_cache(Person).on(:name).times(1)
|
122
124
|
expect(@person.height).to eq(1.80)
|
123
125
|
end
|
124
126
|
|
125
127
|
it "should add updated records to the cache, also when multiple ids are queried" do
|
126
128
|
@fry.height = 1.71
|
127
129
|
@fry.save!
|
128
|
-
expect{ @people = Person.where(:name => ["Fry", "Chase"]).order("id ASC").
|
130
|
+
expect{ @people = Person.where(:name => ["Fry", "Chase"]).order("id ASC").load }.to hit_cache(Person).on(:name).times(2)
|
129
131
|
expect(@people.map(&:height)).to eq([1.71, 1.91])
|
130
132
|
end
|
131
|
-
|
133
|
+
|
132
134
|
end
|
133
|
-
|
135
|
+
|
134
136
|
context "invalidate" do
|
135
137
|
before(:each) do
|
136
|
-
@fry = Person.
|
137
|
-
@chase = Person.
|
138
|
+
@fry = Person.find_by(name: "Fry")
|
139
|
+
@chase = Person.find_by(name: "Chase")
|
138
140
|
end
|
139
141
|
|
140
142
|
it "should invalidate single records" do
|
141
143
|
Person.record_cache[:name].invalidate("Fry")
|
142
|
-
expect{ Person.
|
144
|
+
expect{ Person.find_by(name: "Fry") }.to miss_cache(Person).on(:name).times(1)
|
143
145
|
end
|
144
146
|
|
145
147
|
it "should only miss the cache for the invalidated record when multiple ids are queried" do
|
146
148
|
# miss on 1
|
147
149
|
Person.record_cache[:name].invalidate("Fry")
|
148
|
-
expect{ Person.where(:name => ["Fry", "Chase"]).
|
150
|
+
expect{ Person.where(:name => ["Fry", "Chase"]).load }.to miss_cache(Person).on(:name).times(1)
|
149
151
|
# hit on 2
|
150
152
|
Person.record_cache[:name].invalidate("Fry")
|
151
|
-
expect{ Person.where(:name => ["Fry", "Chase"]).
|
153
|
+
expect{ Person.where(:name => ["Fry", "Chase"]).load }.to hit_cache(Person).on(:name).times(1)
|
152
154
|
# nothing invalidated, both hit
|
153
|
-
expect{ Person.where(:name => ["Fry", "Chase"]).
|
155
|
+
expect{ Person.where(:name => ["Fry", "Chase"]).load }.to hit_cache(Person).on(:name).times(2)
|
154
156
|
end
|
155
157
|
|
156
158
|
it "should invalidate records when using update_all" do
|
157
|
-
Person.where(:id => ["Fry", "Chase", "Penny"]).
|
158
|
-
expect{ @people = Person.where(:name => ["Fry", "Chase", "Penny"]).order("name ASC").
|
159
|
+
Person.where(:id => ["Fry", "Chase", "Penny"]).load # fill id cache on all Adam Store apples
|
160
|
+
expect{ @people = Person.where(:name => ["Fry", "Chase", "Penny"]).order("name ASC").load }.to hit_cache(Person).on(:name).times(2)
|
159
161
|
expect(@people.map(&:name)).to eq(["Chase", "Fry", "Penny"])
|
160
162
|
# update 2 of the 3 People
|
161
163
|
Person.where(:name => ["Fry", "Penny"]).update_all(:height => 1.21)
|
162
|
-
expect{ @people = Person.where(:name => ["Fry", "Chase", "Penny"]).order("height ASC").
|
164
|
+
expect{ @people = Person.where(:name => ["Fry", "Chase", "Penny"]).order("height ASC").load }.to hit_cache(Person).on(:name).times(1)
|
163
165
|
expect(@people.map(&:height)).to eq([1.21, 1.21, 1.91])
|
164
166
|
end
|
165
167
|
|
166
168
|
end
|
167
|
-
|
169
|
+
|
168
170
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
describe RecordCache::Strategy::Util do
|
4
|
+
RSpec.describe RecordCache::Strategy::Util do
|
5
5
|
|
6
6
|
it "should serialize a record (currently Active Record only)" do
|
7
7
|
expect(subject.serialize(Banana.find(1))).to eq({:a=>{"name"=>"Blue Banana 1", "id"=>1, "store_id"=>2, "person_id"=>4}, :c=>"Banana"})
|
@@ -24,47 +24,53 @@ describe RecordCache::Strategy::Util do
|
|
24
24
|
|
25
25
|
context "filter" do
|
26
26
|
it "should apply filter" do
|
27
|
-
apples = Apple.where(:
|
28
|
-
subject.filter!(apples, :
|
29
|
-
expect(apples).to eq([Apple.
|
27
|
+
apples = Apple.where(id: [1, 2]).to_a
|
28
|
+
subject.filter!(apples, name: "Adams Apple 1")
|
29
|
+
expect(apples).to eq([Apple.find_by(name: "Adams Apple 1")])
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should return empty array when filter does not match any record" do
|
33
|
-
apples = Apple.where(:
|
34
|
-
subject.filter!(apples, :
|
33
|
+
apples = Apple.where(id: [1, 2])
|
34
|
+
subject.filter!(apples, name: "Adams Apple Pie")
|
35
35
|
expect(apples).to be_empty
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should filter on text" do
|
39
|
-
apples = Apple.where(:
|
40
|
-
subject.filter!(apples, :
|
41
|
-
expect(apples).to eq([Apple.
|
39
|
+
apples = Apple.where(id: [1, 2])
|
40
|
+
subject.filter!(apples, name: "Adams Apple 1")
|
41
|
+
expect(apples).to eq([Apple.find_by(name: "Adams Apple 1")])
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should filter on text case insensitive" do
|
45
|
+
apples = Apple.where(id: [1, 2])
|
46
|
+
subject.filter!(apples, name: "adams aPPle 1")
|
47
|
+
expect(apples).to eq([Apple.find_by(name: "Adams Apple 1")])
|
42
48
|
end
|
43
49
|
|
44
50
|
it "should filter on integers" do
|
45
|
-
apples = Apple.where(:
|
46
|
-
subject.filter!(apples, :
|
47
|
-
expect(apples.map(&:id).sort).to eq([8,9])
|
51
|
+
apples = Apple.where(id: [1, 2, 8, 9])
|
52
|
+
subject.filter!(apples, store_id: 2)
|
53
|
+
expect(apples.map(&:id).sort).to eq([8, 9])
|
48
54
|
end
|
49
55
|
|
50
56
|
it "should filter on dates" do
|
51
|
-
people = Person.where(:
|
52
|
-
subject.filter!(people, :
|
57
|
+
people = Person.where(id: [1, 2, 3])
|
58
|
+
subject.filter!(people, birthday: Date.civil(1953, 11, 11))
|
53
59
|
expect(people.size).to eq(1)
|
54
60
|
expect(people.first.name).to eq("Blue")
|
55
61
|
end
|
56
62
|
|
57
63
|
it "should filter on floats" do
|
58
|
-
people = Person.where(:
|
59
|
-
subject.filter!(people, :
|
64
|
+
people = Person.where(id: [1, 2, 3])
|
65
|
+
subject.filter!(people, height: 1.75)
|
60
66
|
expect(people.size).to eq(2)
|
61
67
|
expect(people.map(&:name).sort).to eq(["Blue", "Cris"])
|
62
68
|
end
|
63
69
|
|
64
70
|
it "should filter on arrays" do
|
65
|
-
apples = Apple.where(:id => [1,2,8,9])
|
71
|
+
apples = Apple.where(:id => [1, 2, 8, 9])
|
66
72
|
subject.filter!(apples, :store_id => [2, 4])
|
67
|
-
expect(apples.map(&:id).sort).to eq([8,9])
|
73
|
+
expect(apples.map(&:id).sort).to eq([8, 9])
|
68
74
|
end
|
69
75
|
|
70
76
|
it "should filter on multiple fields" do
|
@@ -73,8 +79,8 @@ describe RecordCache::Strategy::Util do
|
|
73
79
|
apple.name = Apple.find(9).name
|
74
80
|
apple.save!
|
75
81
|
|
76
|
-
apples = Apple.where(:
|
77
|
-
subject.filter!(apples, :
|
82
|
+
apples = Apple.where(id: [1, 2, 3, 8, 9, 10])
|
83
|
+
subject.filter!(apples, store_id: [2, 4], name: apple.name)
|
78
84
|
expect(apples.size).to eq(2)
|
79
85
|
expect(apples.map(&:name)).to eq([apple.name, apple.name])
|
80
86
|
expect(apples.map(&:id).sort).to eq([8,9])
|
@@ -82,137 +88,137 @@ describe RecordCache::Strategy::Util do
|
|
82
88
|
|
83
89
|
it "should filter with more than 2 and conditions" do
|
84
90
|
# this construction leads to a arel object with 3 Equality Nodes within a single And Node
|
85
|
-
apples = Apple.where(:
|
91
|
+
apples = Apple.where(store_id: [1,2]).where(store_id: 1, person_id: nil)
|
86
92
|
expect(apples.size).to eq(2)
|
87
|
-
expect(apples.map(&:id).sort).to eq([1,2])
|
93
|
+
expect(apples.map(&:id).sort).to eq([1, 2])
|
88
94
|
end
|
89
95
|
|
90
96
|
end
|
91
97
|
|
92
98
|
context "sort" do
|
93
99
|
it "should accept a Symbol as a sort order" do
|
94
|
-
people = Person.where(:id => [1,2,3]).
|
100
|
+
people = Person.where(:id => [1,2,3]).to_a
|
95
101
|
subject.sort!(people, :name)
|
96
102
|
expect(people.map(&:name)).to eq(["Adam", "Blue", "Cris"])
|
97
103
|
end
|
98
104
|
|
99
105
|
it "should accept a single Array as a sort order" do
|
100
|
-
people = Person.where(:id => [1,2,3]).
|
106
|
+
people = Person.where(:id => [1,2,3]).to_a
|
101
107
|
subject.sort!(people, [:name, false])
|
102
108
|
expect(people.map(&:name)).to eq(["Cris", "Blue", "Adam"])
|
103
109
|
end
|
104
110
|
|
105
111
|
it "should accept multiple Symbols as a sort order" do
|
106
|
-
people = Person.where(:id => [2,3,4,5]).
|
112
|
+
people = Person.where(:id => [2,3,4,5]).to_a
|
107
113
|
subject.sort!(people, :height, :id)
|
108
114
|
expect(people.map(&:height)).to eq([1.69, 1.75, 1.75, 1.91])
|
109
115
|
expect(people.map(&:id)).to eq([4, 2, 3, 5])
|
110
116
|
end
|
111
117
|
|
112
118
|
it "should accept a mix of Symbols and Arrays as a sort order" do
|
113
|
-
people = Person.where(:id => [2,3,4,5]).
|
119
|
+
people = Person.where(:id => [2,3,4,5]).to_a
|
114
120
|
subject.sort!(people, [:height, false], :id)
|
115
121
|
expect(people.map(&:height)).to eq([1.91, 1.75, 1.75, 1.69])
|
116
122
|
expect(people.map(&:id)).to eq([5, 2, 3, 4])
|
117
123
|
end
|
118
124
|
|
119
125
|
it "should accept multiple Arrays as a sort order" do
|
120
|
-
people = Person.where(:id => [2,3,4,5]).
|
126
|
+
people = Person.where(:id => [2,3,4,5]).to_a
|
121
127
|
subject.sort!(people, [:height, false], [:id, false])
|
122
128
|
expect(people.map(&:height)).to eq([1.91, 1.75, 1.75, 1.69])
|
123
129
|
expect(people.map(&:id)).to eq([5, 3, 2, 4])
|
124
130
|
end
|
125
131
|
|
126
132
|
it "should accept an Array with Arrays as a sort order (default used by record cache)" do
|
127
|
-
people = Person.where(:id => [2,3,4,5]).
|
133
|
+
people = Person.where(:id => [2,3,4,5]).to_a
|
128
134
|
subject.sort!(people, [[:height, false], [:id, false]])
|
129
135
|
expect(people.map(&:height)).to eq([1.91, 1.75, 1.75, 1.69])
|
130
136
|
expect(people.map(&:id)).to eq([5, 3, 2, 4])
|
131
137
|
end
|
132
138
|
|
133
139
|
it "should order nil first for ASC" do
|
134
|
-
apples = Apple.where(:store_id => 1).
|
140
|
+
apples = Apple.where(:store_id => 1).to_a
|
135
141
|
subject.sort!(apples, [:person_id, true])
|
136
142
|
expect(apples.map(&:person_id)).to eq([nil, nil, 4, 4, 5])
|
137
143
|
end
|
138
144
|
|
139
145
|
it "should order nil last for DESC" do
|
140
|
-
apples = Apple.where(:store_id => 1).
|
146
|
+
apples = Apple.where(:store_id => 1).to_a
|
141
147
|
subject.sort!(apples, [:person_id, false])
|
142
148
|
expect(apples.map(&:person_id)).to eq([5, 4, 4, nil, nil])
|
143
149
|
end
|
144
150
|
|
145
151
|
it "should order ascending on text" do
|
146
|
-
people = Person.where(:id => [1,2,3,4]).
|
152
|
+
people = Person.where(:id => [1,2,3,4]).to_a
|
147
153
|
subject.sort!(people, [:name, true])
|
148
154
|
expect(people.map(&:name)).to eq(["Adam", "Blue", "Cris", "Fry"])
|
149
155
|
end
|
150
156
|
|
151
157
|
it "should order descending on text" do
|
152
|
-
people = Person.where(:id => [1,2,3,4]).
|
158
|
+
people = Person.where(:id => [1,2,3,4]).to_a
|
153
159
|
subject.sort!(people, [:name, false])
|
154
160
|
expect(people.map(&:name)).to eq(["Fry", "Cris", "Blue", "Adam"])
|
155
161
|
end
|
156
162
|
|
157
163
|
it "should order ascending on integers" do
|
158
|
-
people = Person.where(:id => [4,2,1,3]).
|
164
|
+
people = Person.where(:id => [4,2,1,3]).to_a
|
159
165
|
subject.sort!(people, [:id, true])
|
160
166
|
expect(people.map(&:id)).to eq([1,2,3,4])
|
161
167
|
end
|
162
168
|
|
163
169
|
it "should order descending on integers" do
|
164
|
-
people = Person.where(:id => [4,2,1,3]).
|
170
|
+
people = Person.where(:id => [4,2,1,3]).to_a
|
165
171
|
subject.sort!(people, [:id, false])
|
166
172
|
expect(people.map(&:id)).to eq([4,3,2,1])
|
167
173
|
end
|
168
174
|
|
169
175
|
it "should order ascending on dates" do
|
170
|
-
people = Person.where(:id => [1,2,3,4]).
|
176
|
+
people = Person.where(:id => [1,2,3,4]).to_a
|
171
177
|
subject.sort!(people, [:birthday, true])
|
172
178
|
expect(people.map(&:birthday)).to eq([Date.civil(1953,11,11), Date.civil(1975,03,20), Date.civil(1975,03,20), Date.civil(1985,01,20)])
|
173
179
|
end
|
174
180
|
|
175
181
|
it "should order descending on dates" do
|
176
|
-
people = Person.where(:id => [1,2,3,4]).
|
182
|
+
people = Person.where(:id => [1,2,3,4]).to_a
|
177
183
|
subject.sort!(people, [:birthday, false])
|
178
184
|
expect(people.map(&:birthday)).to eq([Date.civil(1985,01,20), Date.civil(1975,03,20), Date.civil(1975,03,20), Date.civil(1953,11,11)])
|
179
185
|
end
|
180
186
|
|
181
187
|
it "should order ascending on float" do
|
182
|
-
people = Person.where(:id => [1,2,3,4]).
|
188
|
+
people = Person.where(:id => [1,2,3,4]).to_a
|
183
189
|
subject.sort!(people, [:height, true])
|
184
190
|
expect(people.map(&:height)).to eq([1.69, 1.75, 1.75, 1.83])
|
185
191
|
end
|
186
192
|
|
187
193
|
it "should order descending on float" do
|
188
|
-
people = Person.where(:id => [1,2,3,4]).
|
194
|
+
people = Person.where(:id => [1,2,3,4]).to_a
|
189
195
|
subject.sort!(people, [:height, false])
|
190
196
|
expect(people.map(&:height)).to eq([1.83, 1.75, 1.75, 1.69])
|
191
197
|
end
|
192
198
|
|
193
199
|
it "should order on multiple fields (ASC + ASC)" do
|
194
|
-
people = Person.where(:id => [2,3,4,5]).
|
200
|
+
people = Person.where(:id => [2,3,4,5]).to_a
|
195
201
|
subject.sort!(people, [:height, true], [:id, true])
|
196
202
|
expect(people.map(&:height)).to eq([1.69, 1.75, 1.75, 1.91])
|
197
203
|
expect(people.map(&:id)).to eq([4, 2, 3, 5])
|
198
204
|
end
|
199
205
|
|
200
206
|
it "should order on multiple fields (ASC + DESC)" do
|
201
|
-
people = Person.where(:id => [2,3,4,5]).
|
207
|
+
people = Person.where(:id => [2,3,4,5]).to_a
|
202
208
|
subject.sort!(people, [:height, true], [:id, false])
|
203
209
|
expect(people.map(&:height)).to eq([1.69, 1.75, 1.75, 1.91])
|
204
210
|
expect(people.map(&:id)).to eq([4, 3, 2, 5])
|
205
211
|
end
|
206
212
|
|
207
213
|
it "should order on multiple fields (DESC + ASC)" do
|
208
|
-
people = Person.where(:id => [2,3,4,5]).
|
214
|
+
people = Person.where(:id => [2,3,4,5]).to_a
|
209
215
|
subject.sort!(people, [:height, false], [:id, true])
|
210
216
|
expect(people.map(&:height)).to eq([1.91, 1.75, 1.75, 1.69])
|
211
217
|
expect(people.map(&:id)).to eq([5, 2, 3, 4])
|
212
218
|
end
|
213
219
|
|
214
220
|
it "should order on multiple fields (DESC + DESC)" do
|
215
|
-
people = Person.where(:id => [2,3,4,5]).
|
221
|
+
people = Person.where(:id => [2,3,4,5]).to_a
|
216
222
|
subject.sort!(people, [:height, false], [:id, false])
|
217
223
|
expect(people.map(&:height)).to eq([1.91, 1.75, 1.75, 1.69])
|
218
224
|
expect(people.map(&:id)).to eq([5, 3, 2, 4])
|
@@ -234,7 +240,7 @@ describe RecordCache::Strategy::Util do
|
|
234
240
|
ids << Person.create!(:name => "čedriĉ ꜩ Last").id # latin special, with latin non-collateable
|
235
241
|
|
236
242
|
names_asc = ["1 cedric", "a cedric", "cedric 1", "Cedric 2", "ċedriĉ 3", "čedriĉ 4", "ćedriĉ Last", "čedriĉ คฉ Almost last cedric", "čedriĉ ꜩ Last", "sedric 1", "Sedric 2", "คฉ Really last"]
|
237
|
-
people = Person.where(:id => ids).
|
243
|
+
people = Person.where(:id => ids).to_a
|
238
244
|
subject.sort!(people, [:name, true])
|
239
245
|
expect(people.map(&:name)).to eq(names_asc)
|
240
246
|
|