Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
4c2b5689
Commit
4c2b5689
authored
May 11, 2018
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add specs for #cached_attr_reader and cached_attr_time_reader
parent
8d49ec68
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
11 deletions
+70
-11
spec/models/concerns/redis_cacheable_spec.rb
spec/models/concerns/redis_cacheable_spec.rb
+70
-11
No files found.
spec/models/concerns/redis_cacheable_spec.rb
View file @
4c2b5689
require
'spec_helper'
require
'spec_helper'
describe
RedisCacheable
do
describe
RedisCacheable
do
let
(
:model
)
{
double
}
let
(
:model
)
do
Struct
.
new
(
:id
,
:attributes
)
do
def
read_attribute
(
attribute
)
attributes
[
attribute
]
end
end
end
let
(
:payload
)
{
{
name:
'value'
}
}
let
(
:instance
)
{
model
.
new
(
1
,
payload
)
}
let
(
:cache_key
)
{
instance
.
__send__
(
:cache_attribute_key
)
}
before
do
before
do
model
.
extend
(
described_class
)
model
.
include
(
described_class
)
allow
(
model
).
to
receive
(
:cache_attribute_key
).
and_return
(
'key'
)
end
end
describe
'#cached_attribute'
do
describe
'#cached_attribute'
do
let
(
:payload
)
{
{
attribute:
'value'
}
}
subject
{
instance
.
cached_attribute
(
payload
.
keys
.
first
)
}
subject
{
model
.
cached_attribute
(
payload
.
keys
.
first
)
}
it
'gets the cache attribute'
do
it
'gets the cache attribute'
do
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
expect
(
redis
).
to
receive
(
:get
).
with
(
'key'
)
expect
(
redis
).
to
receive
(
:get
).
with
(
cache_key
)
.
and_return
(
payload
.
to_json
)
.
and_return
(
payload
.
to_json
)
end
end
...
@@ -24,16 +31,68 @@ describe RedisCacheable do
...
@@ -24,16 +31,68 @@ describe RedisCacheable do
end
end
describe
'#cache_attributes'
do
describe
'#cache_attributes'
do
let
(
:values
)
{
{
name:
'new_name'
}
}
subject
{
instance
.
cache_attributes
(
payload
)
}
subject
{
model
.
cache_attributes
(
values
)
}
it
'sets the cache attributes'
do
it
'sets the cache attributes'
do
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
expect
(
redis
).
to
receive
(
:set
).
with
(
'key'
,
values
.
to_json
,
anything
)
expect
(
redis
).
to
receive
(
:set
).
with
(
cache_key
,
payload
.
to_json
,
anything
)
end
end
subject
subject
end
end
end
end
describe
'#cached_attr_reader'
do
subject
{
instance
.
name
}
before
do
model
.
cached_attr_reader
(
:name
)
end
context
'when there is no cached value'
do
it
'checks the cached value first then reads the attribute'
do
expect
(
instance
).
to
receive
(
:cached_attribute
).
and_return
(
nil
)
expect
(
instance
).
to
receive
(
:read_attribute
).
and_return
(
payload
[
:name
])
expect
(
subject
).
to
eq
(
payload
[
:name
])
end
end
context
'when there is a cached value'
do
it
'reads the cached value'
do
expect
(
instance
).
to
receive
(
:cached_attribute
).
and_return
(
payload
[
:name
])
expect
(
instance
).
not_to
receive
(
:read_attribute
)
expect
(
subject
).
to
eq
(
payload
[
:name
])
end
end
end
describe
'#cached_attr_time_reader'
do
subject
{
instance
.
time
}
before
do
model
.
cached_attr_time_reader
(
:time
)
end
context
'when there is no cached value'
do
it
'checks the cached value first then reads the attribute'
do
expect
(
instance
).
to
receive
(
:cached_attribute
).
and_return
(
nil
)
expect
(
instance
).
to
receive
(
:read_attribute
).
and_return
(
Time
.
zone
.
now
)
expect
(
subject
).
to
be_instance_of
(
ActiveSupport
::
TimeWithZone
)
expect
(
subject
).
to
be_within
(
1
.
minute
).
of
(
Time
.
zone
.
now
)
end
end
context
'when there is a cached value'
do
it
'reads the cached value'
do
expect
(
instance
).
to
receive
(
:cached_attribute
).
and_return
(
Time
.
zone
.
now
.
to_s
)
expect
(
instance
).
not_to
receive
(
:read_attribute
)
expect
(
subject
).
to
be_instance_of
(
ActiveSupport
::
TimeWithZone
)
expect
(
subject
).
to
be_within
(
1
.
minute
).
of
(
Time
.
zone
.
now
)
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment