-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcaching_spec.rb
101 lines (96 loc) · 2.61 KB
/
caching_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require 'spec_helper'
class Cache
def initialize
@cache = {}
end
def fetch_multi(*keys)
keys.each_with_object({}) do |k, h|
@cache[k] = yield(k) unless @cache.key?(k)
h[k] = @cache[k]
end
end
end
describe JSONAPI::Renderer, '#render' do
before(:all) do
@users = [
UserResource.new(1, 'User 1', '123 Example st.', []),
UserResource.new(2, 'User 2', '234 Example st.', []),
UserResource.new(3, 'User 3', '345 Example st.', []),
UserResource.new(4, 'User 4', '456 Example st.', [])
]
@posts = [
PostResource.new(1, 'Post 1', 'yesterday', @users[1]),
PostResource.new(2, 'Post 2', 'today', @users[0]),
PostResource.new(3, 'Post 3', 'tomorrow', @users[1])
]
@users[0].posts = [@posts[1]]
@users[1].posts = [@posts[0], @posts[2]]
end
it 'renders included relationships' do
cache = Cache.new
# Warm up the cache.
subject.render(data: @users[0],
include: 'posts',
cache: cache)
# Actual call on warm cache.
actual = subject.render(data: @users[0],
include: 'posts',
cache: cache)
expected = {
data: {
type: 'users',
id: '1',
attributes: {
name: 'User 1',
address: '123 Example st.'
},
relationships: {
posts: {
data: [{ type: 'posts', id: '2' }],
links: {
self: 'http://api.example.com/users/1/relationships/posts',
related: {
href: 'http://api.example.com/users/1/posts',
meta: {
do_not_use: true
}
}
},
meta: {
deleted_posts: 5
}
}
},
links: {
self: 'http://api.example.com/users/1'
},
meta: {
user_meta: 'is_meta'
}
},
included: [
{
type: 'posts',
id: '2',
attributes: {
title: 'Post 2',
date: 'today'
},
relationships: {
author: {
links: {
self: 'http://api.example.com/posts/2/relationships/author',
related: 'http://api.example.com/posts/2/author'
},
meta: {
author_active: true
}
}
}
}
]
}
expect(JSON.parse(actual.to_json)).to eq(JSON.parse(expected.to_json))
# expect(actual[:data]).to be_a(JSONAPI::Renderer::CachedResourcesProcessor::JSONString)
end
end