Skip to content

Commit dba709a

Browse files
committed
setup helper and specs improved
1 parent f32a281 commit dba709a

4 files changed

Lines changed: 62 additions & 32 deletions

File tree

bin/setup

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
6+
cd "$ROOT_DIR"
7+
8+
echo "==> Installing dependencies"
9+
if [[ "${PARADEDB_WITH_PG:-0}" == "1" ]]; then
10+
echo " PARADEDB_WITH_PG=1 (installing pg gem)"
11+
fi
12+
13+
bundle install
14+
15+
echo ""
16+
echo "==> Done"
17+
echo "Run unit tests: scripts/run_unit_tests.sh"
18+
echo "Run integration tests (requires ParadeDB): PARADEDB_WITH_PG=1 scripts/run_integration_tests.sh"

spec/spec_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ def setup_test_schema
3838

3939
establish_test_connection
4040
setup_test_schema
41+
42+
def normalize_sql(sql)
43+
sql.to_s.strip.gsub(/\s+/, " ")
44+
end
45+
46+
def assert_sql_equal(expected, actual)
47+
assert_equal normalize_sql(expected), normalize_sql(actual)
48+
end

spec/user_api_integration_spec.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_matching_with_filters
3131
AND "products"."rating" >= 4
3232
SQL
3333

34-
assert_equal expected, sql
34+
assert_sql_equal expected, sql
3535
end
3636

3737
def test_chain_multiple_search_fields_and
@@ -44,12 +44,12 @@ def test_chain_multiple_search_fields_and
4444
WHERE ("products"."description" &&& 'running shoes' AND "products"."category" ### 'Footwear')
4545
SQL
4646

47-
assert_equal expected, sql
47+
assert_sql_equal expected, sql
4848
end
4949

5050
def test_match_any_or_semantics
5151
sql = Product.search(:description).matching(any: ["wireless", "bluetooth"]).to_sql
52-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" ||| 'wireless bluetooth'), sql
52+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" ||| 'wireless bluetooth'), sql
5353
end
5454

5555
def test_excluding_terms
@@ -63,42 +63,42 @@ def test_excluding_terms
6363
WHERE ("products"."description" &&& 'shoes' AND NOT ("products"."description" &&& 'cheap budget'))
6464
SQL
6565

66-
assert_equal expected, sql
66+
assert_sql_equal expected, sql
6767
end
6868

6969
def test_phrase_with_slop
7070
sql = Product.search(:description).phrase("running shoes", slop: 2).to_sql
71-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" ### 'running shoes'::pdb.slop(2)), sql
71+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" ### 'running shoes'::pdb.slop(2)), sql
7272
end
7373

7474
def test_fuzzy_with_prefix
7575
sql = Product.search(:description).fuzzy("runn", distance: 1, prefix: true).to_sql
76-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" === 'runn'::pdb.fuzzy(1, "true")), sql
76+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" === 'runn'::pdb.fuzzy(1, "true")), sql
7777
end
7878

7979
def test_regex
8080
sql = Product.search(:description).regex("run.*shoes").to_sql
81-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" @@@ pdb.regex('run.*shoes')), sql
81+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" @@@ pdb.regex('run.*shoes')), sql
8282
end
8383

8484
def test_term_exact
8585
sql = Product.search(:description).term("shoes").to_sql
86-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" === 'shoes'), sql
86+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" === 'shoes'), sql
8787
end
8888

8989
def test_near_proximity
9090
sql = Product.search(:description).near("sleek", "shoes", distance: 1).to_sql
91-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" @@@ ('sleek' ## 1 ## 'shoes')), sql
91+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" @@@ ('sleek' ## 1 ## 'shoes')), sql
9292
end
9393

9494
def test_phrase_prefix
9595
sql = Product.search(:description).phrase_prefix("run", "sh").to_sql
96-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" @@@ pdb.phrase_prefix(ARRAY['run', 'sh'])), sql
96+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" @@@ pdb.phrase_prefix(ARRAY['run', 'sh'])), sql
9797
end
9898

9999
def test_similar_to
100100
sql = Product.similar_to(3, fields: [:description]).to_sql
101-
assert_equal %(SELECT * FROM products\nWHERE "products"."id" @@@ pdb.more_like_this(3, ARRAY['description'])), sql
101+
assert_sql_equal %(SELECT * FROM products WHERE "products"."id" @@@ pdb.more_like_this(3, ARRAY['description'])), sql
102102
end
103103

104104
def test_with_score_and_order
@@ -114,7 +114,7 @@ def test_with_score_and_order
114114
ORDER BY search_score DESC
115115
SQL
116116

117-
assert_equal expected, sql
117+
assert_sql_equal expected, sql
118118
end
119119

120120
def test_with_snippet_default
@@ -128,7 +128,7 @@ def test_with_snippet_default
128128
WHERE "products"."description" &&& 'running shoes'
129129
SQL
130130

131-
assert_equal expected, sql
131+
assert_sql_equal expected, sql
132132
end
133133

134134
def test_with_snippet_custom
@@ -142,7 +142,7 @@ def test_with_snippet_custom
142142
WHERE "products"."description" &&& 'running shoes'
143143
SQL
144144

145-
assert_equal expected, sql
145+
assert_sql_equal expected, sql
146146
end
147147

148148
def test_or_across_fields
@@ -156,7 +156,7 @@ def test_or_across_fields
156156
WHERE (("products"."description" &&& 'shoes') OR ("products"."category" &&& 'footwear'))
157157
SQL
158158

159-
assert_equal expected, sql
159+
assert_sql_equal expected, sql
160160
end
161161

162162
def test_facets_only
@@ -172,7 +172,7 @@ def test_facets_only
172172
WHERE "products"."description" &&& 'shoes'
173173
SQL
174174

175-
assert_equal expected, facet_sql
175+
assert_sql_equal expected, facet_sql
176176
end
177177

178178
def test_with_facets_rows_plus_facets
@@ -190,6 +190,6 @@ def test_with_facets_rows_plus_facets
190190
LIMIT 10
191191
SQL
192192

193-
assert_equal expected, sql
193+
assert_sql_equal expected, sql
194194
end
195195
end

spec/user_api_unit_spec.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,74 +21,78 @@ def test_matching_and_filters
2121
.where(in_stock: true)
2222
.to_sql
2323

24-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" &&& 'running shoes' AND "products"."in_stock" = true), sql
24+
assert_sql_equal %(SELECT * FROM products
25+
WHERE "products"."description" &&& 'running shoes' AND "products"."in_stock" = true), sql
2526
end
2627

2728
def test_match_any
2829
sql = UnitProduct.search(:description).matching(any: %w[wireless bluetooth]).to_sql
29-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" ||| 'wireless bluetooth'), sql
30+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" ||| 'wireless bluetooth'), sql
3031
end
3132

3233
def test_phrase_slop
3334
sql = UnitProduct.search(:description).phrase("running shoes", slop: 2).to_sql
34-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" ### 'running shoes'::pdb.slop(2)), sql
35+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" ### 'running shoes'::pdb.slop(2)), sql
3536
end
3637

3738
def test_fuzzy_prefix_boost
3839
sql = UnitProduct.search(:description).fuzzy("shose", distance: 2, prefix: false, boost: 2).to_sql
39-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" === 'shose'::pdb.fuzzy(2)::pdb.boost(2)), sql
40+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" === 'shose'::pdb.fuzzy(2)::pdb.boost(2)), sql
4041
end
4142

4243
def test_term_exact
4344
sql = UnitProduct.search(:description).term("literal").to_sql
44-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" === 'literal'), sql
45+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" === 'literal'), sql
4546
end
4647

4748
def test_regex
4849
sql = UnitProduct.search(:description).regex("run.*").to_sql
49-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" @@@ pdb.regex('run.*')), sql
50+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" @@@ pdb.regex('run.*')), sql
5051
end
5152

5253
def test_near
5354
sql = UnitProduct.search(:description).near("sleek", "shoes", distance: 1).to_sql
54-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" @@@ ('sleek' ## 1 ## 'shoes')), sql
55+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" @@@ ('sleek' ## 1 ## 'shoes')), sql
5556
end
5657

5758
def test_phrase_prefix
5859
sql = UnitProduct.search(:description).phrase_prefix("run", "sh").to_sql
59-
assert_equal %(SELECT * FROM products\nWHERE "products"."description" @@@ pdb.phrase_prefix(ARRAY['run', 'sh'])), sql
60+
assert_sql_equal %(SELECT * FROM products WHERE "products"."description" @@@ pdb.phrase_prefix(ARRAY['run', 'sh'])), sql
6061
end
6162

6263
def test_similar_to
6364
sql = UnitProduct.similar_to(5, fields: [:description]).to_sql
64-
assert_equal %(SELECT * FROM products\nWHERE "products"."id" @@@ pdb.more_like_this(5, ARRAY['description'])), sql
65+
assert_sql_equal %(SELECT * FROM products WHERE "products"."id" @@@ pdb.more_like_this(5, ARRAY['description'])), sql
6566
end
6667

6768
def test_excluding
6869
sql = UnitProduct.search(:description).matching("shoes").excluding("cheap").to_sql
69-
assert_equal %(SELECT * FROM products\nWHERE ("products"."description" &&& 'shoes' AND NOT ("products"."description" &&& 'cheap'))), sql
70+
assert_sql_equal %(SELECT * FROM products WHERE ("products"."description" &&& 'shoes' AND NOT ("products"."description" &&& 'cheap'))), sql
7071
end
7172

7273
def test_or_composition
7374
left = UnitProduct.search(:description).matching("shoes")
7475
right = UnitProduct.search(:category).matching("footwear")
7576
sql = left.or(right).to_sql
76-
assert_equal %(SELECT * FROM products\nWHERE (("products"."description" &&& 'shoes') OR ("products"."category" &&& 'footwear'))), sql
77+
assert_sql_equal %(SELECT * FROM products WHERE (("products"."description" &&& 'shoes') OR ("products"."category" &&& 'footwear'))), sql
7778
end
7879

7980
def test_with_score
8081
sql = UnitProduct.search(:description).matching("shoes").with_score.to_sql
81-
assert_equal %(SELECT products.*, pdb.score("products"."id") AS search_score FROM products\nWHERE "products"."description" &&& 'shoes'), sql
82+
assert_sql_equal %(SELECT products.*, pdb.score("products"."id") AS search_score FROM products
83+
WHERE "products"."description" &&& 'shoes'), sql
8284
end
8385

8486
def test_with_snippet_default
8587
sql = UnitProduct.search(:description).matching("shoes").with_snippet(:description).to_sql
86-
assert_equal %(SELECT products.*, pdb.snippet("products"."description") AS description_snippet FROM products\nWHERE "products"."description" &&& 'shoes'), sql
88+
assert_sql_equal %(SELECT products.*, pdb.snippet("products"."description") AS description_snippet FROM products
89+
WHERE "products"."description" &&& 'shoes'), sql
8790
end
8891

8992
def test_with_snippet_custom
9093
sql = UnitProduct.search(:description).matching("shoes").with_snippet(:description, start_tag: "<b>", end_tag: "</b>", max_chars: 50).to_sql
91-
assert_equal %(SELECT products.*, pdb.snippet("products"."description", '<b>', '</b>', 50) AS description_snippet FROM products\nWHERE "products"."description" &&& 'shoes'), sql
94+
assert_sql_equal %(SELECT products.*, pdb.snippet("products"."description", '<b>', '</b>', 50) AS description_snippet FROM products
95+
WHERE "products"."description" &&& 'shoes'), sql
9296
end
9397

9498
def test_facets_only
@@ -104,6 +108,6 @@ def test_facets_only
104108
WHERE "products"."description" &&& 'shoes'
105109
SQL
106110

107-
assert_equal expected, facet_sql
111+
assert_sql_equal expected, facet_sql
108112
end
109113
end

0 commit comments

Comments
 (0)