-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathbest_index_test.rb
183 lines (141 loc) · 6.32 KB
/
best_index_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require_relative "test_helper"
class BestIndexTest < Minitest::Test
def test_where
assert_best_index ({table: "users", columns: ["city_id"]}), "SELECT * FROM users WHERE city_id = 1"
end
def test_all_values
index = database.best_index("SELECT * FROM users WHERE login_attempts = 1 ORDER BY created_at")
expected = {
found: true,
structure: {table: "users", where: [{column: "login_attempts", op: "="}], sort: [{column: "created_at", direction: "asc"}]},
index: {table: "users", columns: ["login_attempts", "created_at"]},
rows: 5000,
row_estimates: {"login_attempts (=)" => 167, "created_at (sort)" => 1},
row_progression: [5000, 167, 0]
}
assert_equal expected, index
end
def test_where_multiple_columns
assert_best_index ({table: "users", columns: ["city_id", "login_attempts"]}), "SELECT * FROM users WHERE city_id = 1 and login_attempts = 2"
end
def test_where_unique
assert_best_index ({table: "users", columns: ["email"]}), "SELECT * FROM users WHERE city_id = 1 AND email = '[email protected]'"
end
def test_order
assert_best_index ({table: "users", columns: ["created_at"]}), "SELECT * FROM users ORDER BY created_at"
end
def test_order_multiple
assert_best_index ({table: "users", columns: ["login_attempts", "created_at"]}), "SELECT * FROM users ORDER BY login_attempts, created_at"
end
def test_order_multiple_direction
assert_best_index ({table: "users", columns: ["login_attempts"]}), "SELECT * FROM users ORDER BY login_attempts DESC, created_at"
end
def test_order_multiple_unique
assert_best_index ({table: "users", columns: ["id"]}), "SELECT * FROM users ORDER BY id, created_at"
end
def test_where_unique_order
assert_best_index ({table: "users", columns: ["email"]}), "SELECT * FROM users WHERE email = '[email protected]' ORDER BY created_at"
end
def test_where_order
assert_best_index ({table: "users", columns: ["login_attempts", "created_at"]}), "SELECT * FROM users WHERE login_attempts = 1 ORDER BY created_at"
end
def test_where_order_unknown
assert_best_index ({table: "users", columns: ["login_attempts"]}), "SELECT * FROM users WHERE login_attempts = 1 ORDER BY NOW()"
end
def test_where_in
assert_best_index ({table: "users", columns: ["city_id"]}), "SELECT * FROM users WHERE city_id IN (1, 2)"
end
def test_like
assert_best_index ({table: "users", columns: ["email gist_trgm_ops"], using: "gist"}), "SELECT * FROM users WHERE email LIKE $1"
end
def test_like_where
assert_best_index ({table: "users", columns: ["city_id"]}), "SELECT * FROM users WHERE city_id = $1 AND email LIKE $2"
end
def test_like_where2
assert_best_index ({table: "users", columns: ["email gist_trgm_ops"], using: "gist"}), "SELECT * FROM users WHERE email LIKE $1 AND active = $2"
end
def test_ilike
assert_best_index ({table: "users", columns: ["email gist_trgm_ops"], using: "gist"}), "SELECT * FROM users WHERE email ILIKE $1"
end
def test_not_equals
assert_best_index ({table: "users", columns: ["login_attempts"]}), "SELECT * FROM users WHERE city_id != $1 and login_attempts = 2"
end
def test_not_in
assert_best_index ({table: "users", columns: ["login_attempts"]}), "SELECT * FROM users WHERE city_id NOT IN ($1) and login_attempts = 2"
end
def test_between
assert_best_index ({table: "users", columns: ["city_id"]}), "SELECT * FROM users WHERE city_id BETWEEN 1 AND 2"
end
def test_multiple_range
assert_best_index ({table: "users", columns: ["city_id"]}), "SELECT * FROM users WHERE city_id > $1 and login_attempts > $2"
end
def test_where_prepared
assert_best_index ({table: "users", columns: ["city_id"]}), "SELECT * FROM users WHERE city_id = $1"
end
def test_where_normalized
assert_best_index ({table: "users", columns: ["city_id"]}), "SELECT * FROM users WHERE city_id = $1"
end
def test_is_null
assert_best_index ({table: "users", columns: ["zip_code"]}), "SELECT * FROM users WHERE zip_code IS NULL"
end
def test_is_null_equal
assert_best_index ({table: "users", columns: ["zip_code", "login_attempts"]}), "SELECT * FROM users WHERE zip_code IS NULL AND login_attempts = $1"
end
def test_is_not_null
assert_best_index ({table: "users", columns: ["login_attempts"]}), "SELECT * FROM users WHERE zip_code IS NOT NULL AND login_attempts = $1"
end
def test_update
assert_best_index ({table: "users", columns: ["city_id"]}), "UPDATE users SET email = 'test' WHERE city_id = 1"
end
def test_delete
assert_best_index ({table: "users", columns: ["city_id"]}), "DELETE FROM users WHERE city_id = 1"
end
def test_parse_error
assert_no_index "Parse error", "SELECT *123'"
end
def test_stats_not_found
assert_no_index "Stats not found", "SELECT * FROM non_existent_table WHERE id = 1"
end
def test_unknown_structure
assert_no_index "Unknown structure", "SELECT NOW()"
end
def test_where_or
assert_no_index "Unknown structure", "SELECT FROM users WHERE login_attempts = 0 OR login_attempts = 1"
end
def test_where_nested_or
assert_no_index "Unknown structure", "SELECT FROM users WHERE city_id = 1 AND (login_attempts = 0 OR login_attempts = 1)"
end
def test_multiple_tables
assert_no_index "JOIN not supported yet", "SELECT * FROM users INNER JOIN cities ON cities.id = users.city_id"
end
def test_no_columns
assert_no_index "No columns to index", "SELECT * FROM users"
end
def test_small_table
assert_no_index "No index needed if less than 500 rows", "SELECT * FROM states WHERE name = 'State 1'"
end
def test_system_table
assert_no_index "System table", "SELECT COUNT(*) AS count FROM pg_extension WHERE extname = $1"
end
def test_insert
assert_no_index "INSERT statement", "INSERT INTO users (login_attempts) VALUES (1)"
end
def test_set
assert_no_index "SET statement", "set client_encoding to 'UTF8'"
end
def test_empty_statement
assert_no_index "Empty statement", nil
end
protected
def assert_best_index(expected, statement)
index = database.best_index(statement)
assert_nil index[:explanation]
assert index[:found]
assert_equal expected, index[:index]
end
def assert_no_index(explanation, statement)
index = database.best_index(statement)
assert !index[:found]
assert_equal explanation, index[:explanation]
end
end