This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
/
custom_spec.rb
87 lines (77 loc) · 3.59 KB
/
custom_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
require 'spec_helper'
describe Clearly::Query::Compose::Custom do
include_context 'shared_setup'
# for access to compose_and, relation_none, and relation_all
include Clearly::Query::Compose::Core
it 'can be instantiated' do
Clearly::Query::Compose::Custom.new
end
it 'constructs sql for select all' do
query = self.send(:relation_all, Order).where(customer_id: 10)
expect(query.to_sql).to eq('SELECT "orders".* FROM "orders" WHERE "orders"."customer_id" = 10')
end
it 'constructs sql for select none' do
query = self.send(:relation_none, Order).where(customer_id: 10)
expect(query.to_sql).to eq('')
end
it 'builds expected sql' do
custom = Clearly::Query::Compose::Custom.new
table = product_def.table
column_name = :name
allowed = product_def.all_fields
value = 'test'
values = [value]
value_bool = true
value_range = {interval: '(test1,test2]'}
combined = self.send(
:compose_and,
custom.compose_eq(table, column_name, allowed, value),
custom.compose_not_eq(table, column_name, allowed, value),
custom.compose_lt(table, column_name, allowed, value),
custom.compose_not_lt(table, column_name, allowed, value),
custom.compose_gt(table, column_name, allowed, value),
custom.compose_not_gt(table, column_name, allowed, value),
custom.compose_lteq(table, column_name, allowed, value),
custom.compose_not_lteq(table, column_name, allowed, value),
custom.compose_gteq(table, column_name, allowed, value),
custom.compose_not_gteq(table, column_name, allowed, value),
custom.compose_contains(table, column_name, allowed, value),
custom.compose_not_contains(table, column_name, allowed, value),
custom.compose_starts_with(table, column_name, allowed, value),
custom.compose_not_starts_with(table, column_name, allowed, value),
custom.compose_ends_with(table, column_name, allowed, value),
custom.compose_not_ends_with(table, column_name, allowed, value),
custom.compose_in(table, column_name, allowed, values),
custom.compose_not_in(table, column_name, allowed, values),
#custom.compose_regex(table, column_name, allowed, value),
#custom.compose_not_regex(table, column_name, allowed, value),
custom.compose_null(table, column_name, allowed, value_bool),
custom.compose_range(table, column_name, allowed, value_range),
custom.compose_not_range(table, column_name, allowed, value_range),
)
expected_sql = [
"\"products\".\"name\" = 'test'",
"\"products\".\"name\" != 'test'",
"\"products\".\"name\" < 'test'",
"\"products\".\"name\" >= 'test'",
"\"products\".\"name\" > 'test'",
"\"products\".\"name\" <= 'test'",
"\"products\".\"name\" <= 'test'",
"\"products\".\"name\" > 'test'",
"\"products\".\"name\" >= 'test'",
"\"products\".\"name\" < 'test'",
"\"products\".\"name\" LIKE '%test%'",
"\"products\".\"name\" NOT LIKE '%test%'",
"\"products\".\"name\" LIKE 'test%'",
"\"products\".\"name\" NOT LIKE 'test%'",
"\"products\".\"name\" LIKE '%test'",
"\"products\".\"name\" NOT LIKE '%test'",
"\"products\".\"name\" IN ('test')",
"\"products\".\"name\" NOT IN ('test')",
"\"products\".\"name\" IS NULL",
"\"products\".\"name\" > 'test1' AND \"products\".\"name\" <= 'test2'",
"(\"products\".\"name\" <= 'test1' OR \"products\".\"name\" > 'test2')"
]
expect(combined.to_sql).to eq(expected_sql.join(' AND '))
end
end