test/best_index_test.rb in pghero-1.2.0 vs test/best_index_test.rb in pghero-1.2.1
- old
+ new
@@ -48,19 +48,50 @@
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 ?"
+ end
+
+ def test_like_where
+ assert_best_index ({table: "users", columns: ["city_id"]}), "SELECT * FROM users WHERE city_id = ? AND email LIKE ?"
+ end
+
+ def test_like_where2
+ assert_best_index ({table: "users", columns: ["email gist_trgm_ops"], using: "gist"}), "SELECT * FROM users WHERE email LIKE ? AND active = ?"
+ end
+
+ def test_ilike
+ assert_best_index ({table: "users", columns: ["email gist_trgm_ops"], using: "gist"}), "SELECT * FROM users WHERE email ILIKE ?"
+ end
+
+ def test_not_equals
+ assert_best_index ({table: "users", columns: ["login_attempts"]}), "SELECT * FROM users WHERE city_id != ? 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 (?) and login_attempts = 2"
+ end
+
def test_between
- skip
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 > ? and login_attempts > ?"
+ 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
@@ -89,12 +120,20 @@
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_multiple_tables
- assert_no_index "Unknown structure", "SELECT * FROM users INNER JOIN cities ON cities.id = users.city_id"
+ 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
@@ -117,9 +156,10 @@
protected
def assert_best_index(expected, statement)
index = PgHero.best_index(statement)
+ assert_nil index[:explanation]
assert index[:found]
assert_equal expected, index[:index]
end
def assert_no_index(explanation, statement)