categories_test.rb
2.89 KB
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
require File.dirname(__FILE__) + '/test_helper'
class CategoriesTest < ActiveSupport::TestCase
def setup
login_api
end
should 'list categories' do
category = fast_create(Category)
get "/api/v1/categories/?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_includes json["categories"].map { |c| c["name"] }, category.name
end
should 'get category by id' do
category = fast_create(Category)
get "/api/v1/categories/#{category.id}/?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal category.name, json["category"]["name"]
end
should 'list parent and children when get category by id' do
parent = fast_create(Category)
child_1 = fast_create(Category)
child_2 = fast_create(Category)
category = fast_create(Category)
category.parent = parent
category.children << child_1
category.children << child_2
category.save
get "/api/v1/categories/#{category.id}/?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal({'id' => parent.id, 'name' => parent.name}, json['category']['parent'])
assert_equivalent [child_1.id, child_2.id], json['category']['children'].map { |c| c['id'] }
end
should 'include parent in categories list if params is true' do
parent_1 = fast_create(Category) # parent_1 has no parent category
child_1 = fast_create(Category)
child_2 = fast_create(Category)
parent_2 = fast_create(Category)
parent_2.parent = parent_1
parent_2.children << child_1
parent_2.children << child_2
parent_2.save
get "/api/v1/categories/?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal [nil], json['categories'].map { |c| c['parent'] }.uniq
params[:include_parent] = true
get "/api/v1/categories/?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [parent_1.parent, parent_2.parent.id, child_1.parent.id, child_2.parent.id],
json["categories"].map { |c| c['parent'] && c['parent']['id'] }
end
should 'include children in categories list if params is true' do
category = fast_create(Category)
child_1 = fast_create(Category)
child_2 = fast_create(Category)
child_3 = fast_create(Category)
category.children << child_1
category.children << child_2
category.save
child_1.children << child_3
child_1.save
get "/api/v1/categories/?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal [nil], json['categories'].map { |c| c['children'] }.uniq
params[:include_children] = true
get "/api/v1/categories/?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [category.children.map(&:id).sort, child_1.children.map(&:id).sort, child_2.children.map(&:id).sort, child_3.children.map(&:id).sort],
json["categories"].map{ |c| c['children'].map{ |child| child['id'] }.sort }
end
end