-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter_spec.rb
More file actions
144 lines (139 loc) · 3.73 KB
/
interpreter_spec.rb
File metadata and controls
144 lines (139 loc) · 3.73 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true
require 'spec_helper'
require 'filterly/node'
require 'examples/interpreter'
RSpec.describe Interpreter do
subject do
described_class.new(ast: ast)
end
let(:ast) do
Filterly::Node.new(
:root,
[
:filters,
Filterly::Node.new(
:statement,
[
:and,
Filterly::Node.new(
:statement,
[
:or,
Filterly::Node.new(
:expression,
[
:op_equal,
Filterly::Node.new(:attr_name, [:course_id, nil, nil]),
Filterly::Node.new(:attr_value, [23, nil, nil])
]
),
Filterly::Node.new(
:statement,
[
:or,
Filterly::Node.new(
:expression,
[
:op_equal,
Filterly::Node.new(:attr_name, [:course_id, nil, nil]),
Filterly::Node.new(:attr_value, [7, nil, nil])
]
),
Filterly::Node.new(
:expression,
[
:op_equal,
Filterly::Node.new(:attr_name, [:course_id, nil, nil]),
Filterly::Node.new(:attr_value, [56, nil, nil])
]
)
]
)
]
),
Filterly::Node.new(
:statement,
[
:and,
Filterly::Node.new(
:expression,
[
:op_equal,
Filterly::Node.new(:attr_name, [:annual, nil, nil]),
Filterly::Node.new(:attr_value, ['2017-2018', nil, nil])
]
),
Filterly::Node.new(
:expression,
[
:op_in,
Filterly::Node.new(:attr_name, [:category_ids, nil, nil]),
Filterly::Node.new(
:attr_array,
[
67,
Filterly::Node.new(:attr_array, [32, nil, nil]),
Filterly::Node.new(:attr_array, [34, nil, nil])
]
)
]
)
]
)
]
)
]
)
end
describe '#to_hash' do
it 'interprets ast to hash' do
result = subject.to_hash
expect(result).to eql(
filters: [
{
or: [
{
course_id: 23
},
{
or: [
{
course_id: 7
},
{
course_id: 56
}
]
}
]
},
{
annual: '2017-2018'
},
{
category_ids: [67, 32, 34]
}
]
)
end
end
describe '#to_sql' do
it 'returns sql query' do
expect(subject.to_sql.split.join(' ')).to eql(
<<~SQL.split.join(' ')
(course_id='23' OR (course_id='7' OR course_id='56')) AND annual='2017-2018'
AND EXISTS(
SELECT TRUE FROM category_courses
WHERE category_courses.category_id IN('67','32','34')
AND courses.id = category_courses.course_id
)
SQL
)
end
end
describe '#to_ast' do
it 'returns self ast' do
expect(subject.to_ast).to eql(ast)
end
end
end