-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.rb
executable file
·135 lines (121 loc) · 3.02 KB
/
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
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
#!/usr/bin/env ruby
require 'open3'
# Test Utilities
@tests = 0
@test_description = 0
def try(descr)
start = Time.now
@tests += 1
@test_description = descr
yield
delta = format('%.3f', (Time.now - start))
# highlight slow tests
delta = "\e[7m#{delta}\e[27m" if (Time.now - start) > 0.03
puts "#{delta}: #{descr}"
end
def eq(result, expected)
a = result.to_s.gsub(/^/, '> ')
b = expected.to_s.gsub(/^/, '< ')
raise "\"#{@test_description}\"\n#{a}\n#{b}" unless result == expected
end
# Setup
@url = `pg_tmp -o "-c shared_preload_libraries=#{Dir.pwd}/safeupdate"`
psql = "psql --no-psqlrc -At -q #{@url}"
puts "using #{@url}"
q = %{
CREATE TABLE employees (name varchar(30));
INSERT INTO employees VALUES ('Eric'),('kevin'),('Robert');
}
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, ''
eq out, ''
eq status.success?, true
# Tests
try 'Block unqualified DELETE' do
q = %(
DELETE FROM employees;
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, "ERROR: DELETE requires a WHERE clause\n"
eq status.success?, true
eq out, ''
end
try 'Block unqualified UPDATE' do
q = %(
UPDATE employees SET name='Kevin';
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, "ERROR: UPDATE requires a WHERE clause\n"
eq out, ''
eq status.success?, true
end
try 'Allow qualified DELETE' do
q = %(
BEGIN;
DELETE FROM employees WHERE name='Robert' RETURNING name;
ROLLBACK;
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err.empty?, true
eq out, "Robert\n"
eq status.success?, true
end
try 'Allow qualified UPDATE' do
q = %(
BEGIN;
UPDATE employees SET name='Kevin'
WHERE name='kevin'
RETURNING name;
ROLLBACK;
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, ''
eq out, "Kevin\n"
eq status.success?, true
end
try 'Block modifying CTE with unqualified UPDATE' do
q = %{
WITH updates AS (
UPDATE employees SET name='Kevin'
RETURNING name
)
SELECT *
FROM updates;
}
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, "ERROR: UPDATE requires a WHERE clause\n"
eq out, ''
eq status.success?, true
end
try 'Allow modifying CTE with qualified UPDATE' do
q = %{
BEGIN;
WITH updates AS (
UPDATE employees SET name='Kevin'
WHERE name='kevin'
RETURNING name
)
SELECT *
FROM updates;
ROLLBACK;
}
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, ''
eq out, "Kevin\n"
eq status.success?, true
end
try 'Disable safeupdate' do
q = %(
SHOW safeupdate.enabled;
SET safeupdate.enabled=0;
BEGIN;
DELETE FROM employees;
ROLLBACK;
SET safeupdate.enabled=1;
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, ''
eq out, "on\n"
eq status.success?, true
end
puts "\n#{@tests} tests PASSED"