/
query_spec.rb
119 lines (98 loc) · 3.34 KB
/
query_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require 'spec_helper'
describe Kashiwamochi::Query do
describe '#initialize' do
context "with {:foo => 1, :bar => 2, :to_s => 3, :s => ['name asc', ' ', 'created_at desc']}" do
before { @q = Kashiwamochi::Query.new(:foo => 1, :bar => 2, :to_s => 3, :s => ['name asc', ' ', 'created_at desc']) }
subject { @q }
describe 'length' do
context 'search_params' do
subject { @q.search_params }
its(:length) { should eq 3 }
end
context 'sort_params' do
subject { @q.sort_params }
its(:length) { should eq 2 }
end
end
describe 'having' do
its(:foo) { should eq 1 }
its(:bar) { should eq 2 }
its(:to_s) { should eq 3 }
end
describe 'missing' do
its(:buzz) { should be_nil }
end
describe 'alias' do
its(:attribute_foo) { should eq 1 }
its(:original_to_s) { should be_an_instance_of String }
end
end
end
describe '#sorts_query' do
context "build with {:s => ['name asc', ' ', 'created_at desc']}" do
context 'default' do
before { @q = Kashiwamochi::Query.new(:s => ['name asc', ' ', 'created_at desc']) }
subject { @q.sorts_query(keys) }
context 'with empty' do
let(:keys) { [] }
it { should eq 'name asc, created_at desc' }
end
context 'with :name' do
let(:keys) { [:name] }
it { should eq 'name asc' }
end
context 'with :created_at' do
let(:keys) { [:created_at] }
it { should eq 'created_at desc' }
end
context 'with [:name, :created_at]' do
let(:keys) { [:name, :created_at] }
it { should eq 'name asc, created_at desc' }
end
context 'with [:created_at, :name]' do
let(:keys) { [:created_at, :name] }
it { should eq 'created_at desc, name asc' }
end
context 'with [:foo, :bar]' do
let(:keys) { [:foo, :bar] }
it { should be_nil }
end
end
context 'unordered' do
before { @q = Kashiwamochi::Query.new(:s => ['name asc', ' ', 'created_at desc']) }
subject { @q.sorts_query(keys, :unordered => true) }
context 'with [:name, :created_at]' do
let(:keys) { [:name, :created_at] }
it { should eq 'name asc, created_at desc' }
end
context 'with [:created_at, :name]' do
let(:keys) { [:created_at, :name] }
it { should eq 'name asc, created_at desc' }
end
end
end
end
describe '#searched?' do
context 'with query' do
before { @q = Kashiwamochi::Query.new(:name => 'aira', :s => ["created_at desc"]) }
subject { @q }
it { should be_searched }
end
context 'without query' do
before { @q = Kashiwamochi::Query.new }
subject { @q }
it { should_not be_searched }
end
end
describe '#to_option' do
before { @q = Kashiwamochi::Query.new(:name => 'aira', :s => ["created_at desc"]) }
subject { @q.to_option }
it { should be_an_instance_of Hash }
it { should eq ({:name => 'aira', :s => 'created_at desc'}) }
end
describe '#build' do
subject { Kashiwamochi.build(:name => 'rizumu') }
it { should be_an_instance_of Kashiwamochi::Query }
its(:name) { should eq 'rizumu' }
end
end