/
builder_test.rb
106 lines (81 loc) · 2.99 KB
/
builder_test.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
# frozen_string_literal: true
require 'test_helper'
class MiniSql::Mysql::TestBuilder < MiniTest::Test
def setup
@connection = mysql_connection
end
include MiniSql::BuilderTests
def test_where
builder = @connection.build("select 1 as one from for_testing /*where*/")
builder.where("1 = :zero")
l = builder.query(zero: 0).length
assert_equal(0, l)
end
def test_join
@connection.exec("create TEMPORARY table ta(x int)")
@connection.exec("insert into ta(x) values(1),(2),(3)")
builder = @connection.build("select * from ta /*join*/")
builder.join("(select 1 as _one) as X on _one = x")
assert_equal(1, builder.exec)
builder.join("(select 2 as _two) as Y on _two = x")
assert_equal(0, builder.exec)
end
def test_mixing_params
builder = @connection.build("select * from for_testing /*where*/ limit 1")
builder.where("1 = ?", 1)
builder.where("1 = :one", one: 1)
assert_equal(1, builder.exec)
end
def test_accepts_params_at_end
builder = @connection.build("select :bob as a from for_testing /*where*/ limit 1")
builder.where('1 = :one', one: 1)
r = builder.query_hash(bob: 1)
assert_equal([{ "a" => 1 }], r)
r = builder.query_hash(bob: 1, one: 2)
assert_equal([], r)
end
def test_where2
builder = @connection.build("select 1 as one from for_testing /*where2*/")
builder.where2("1 = -1")
assert_equal(0, builder.exec)
end
def test_append_params
builder = @connection.build("select 1 as one from for_testing /*where*/")
builder.where("1 = :zero", zero: 0)
assert_equal(0, builder.exec)
end
def test_offset_limit
@connection.exec("create TEMPORARY table ta(x int)")
@connection.exec("insert into ta(x) values(1),(2),(3)")
builder = @connection.build("select * from ta ORDER BY x /*limit*/ /*offset*/")
builder.limit(1)
builder.offset(1)
assert_equal([2], builder.query_single)
end
def test_left_join
@connection.exec("create TEMPORARY table ta(x int)")
@connection.exec("insert into ta(x) values(1),(2),(3)")
builder = @connection.build("select * from ta /*left_join*/ /*order_by*/")
builder.left_join("(select 1 as id, 'hello' as name) AS j ON id = x")
builder.order_by('x asc')
r = builder.query
assert_equal('hello', r[0].name)
assert_nil(r[1].name)
assert_equal(3, r.length)
end
def test_set
@connection.exec("create TEMPORARY table ta(x int, y int)")
@connection.exec("insert into ta values(1,2)")
builder = @connection.build("update ta /*set*/")
builder.set('x = ?', 7)
builder.set('y = ?', 8)
assert_equal(1, builder.exec)
assert_equal([7, 8], @connection.query_single("select * from ta"))
end
def test_query_decorator
builder = @connection.build("select :price AS price, :quantity AS quantity from for_testing /*where*/")
builder.where('1 = :one', one: 1)
r = builder.query_decorator(ProductDecorator, price: 20, quantity: 3).first
assert_equal(60, r.amount_price)
end
end