Skip to content

Commit 2df451e

Browse files
committed
test(postgres): add hstore function tests for full coverage
1 parent e4aebb3 commit 2df451e

1 file changed

Lines changed: 307 additions & 0 deletions

File tree

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
# tests/rhosocial/activerecord_postgres_test/feature/backend/postgres/test_postgres_hstore_functions.py
2+
"""
3+
Tests for PostgreSQL hstore functions.
4+
5+
Functions: hstore_from_record, hstore_from_key_value, hstore_akeys,
6+
hstore_skeys, hstore_avals, hstore_svals, hstore_each,
7+
hstore_to_array, hstore_to_matrix, hstore_to_json,
8+
hstore_to_jsonb, hstore_to_json_loose, hstore_to_jsonb_loose,
9+
hstore_slice, hstore_exist, hstore_defined,
10+
hstore_delete, hstore_delete_keys, hstore_delete_pairs,
11+
hstore_populate_record,
12+
hstore_get_value, hstore_get_values, hstore_concat,
13+
hstore_key_exists, hstore_all_keys_exist, hstore_any_key_exists,
14+
hstore_contains, hstore_contained_by,
15+
hstore_subtract_key, hstore_subtract_keys, hstore_subtract_pairs,
16+
hstore_to_array_operator, hstore_to_matrix_operator,
17+
hstore_record_update,
18+
hstore_subscript_get, hstore_subscript_set
19+
"""
20+
21+
from unittest.mock import MagicMock
22+
23+
from rhosocial.activerecord.backend.impl.postgres.functions.hstore import (
24+
_to_sql,
25+
hstore_from_record,
26+
hstore_from_key_value,
27+
hstore_akeys,
28+
hstore_skeys,
29+
hstore_avals,
30+
hstore_svals,
31+
hstore_each,
32+
hstore_to_array,
33+
hstore_to_matrix,
34+
hstore_to_json,
35+
hstore_to_jsonb,
36+
hstore_to_json_loose,
37+
hstore_to_jsonb_loose,
38+
hstore_slice,
39+
hstore_exist,
40+
hstore_defined,
41+
hstore_delete,
42+
hstore_delete_keys,
43+
hstore_delete_pairs,
44+
hstore_populate_record,
45+
hstore_get_value,
46+
hstore_get_values,
47+
hstore_concat,
48+
hstore_key_exists,
49+
hstore_all_keys_exist,
50+
hstore_any_key_exists,
51+
hstore_contains,
52+
hstore_contained_by,
53+
hstore_subtract_key,
54+
hstore_subtract_keys,
55+
hstore_subtract_pairs,
56+
hstore_to_array_operator,
57+
hstore_to_matrix_operator,
58+
hstore_record_update,
59+
hstore_subscript_get,
60+
hstore_subscript_set,
61+
)
62+
63+
64+
class TestToSql:
65+
"""Tests for the internal _to_sql helper function."""
66+
67+
def test_to_sql_with_to_sql_method(self):
68+
"""Test _to_sql with an object that has a to_sql method."""
69+
mock_expr = MagicMock()
70+
mock_expr.to_sql.return_value = ("mocked_sql",)
71+
result = _to_sql(mock_expr)
72+
assert result == "mocked_sql"
73+
mock_expr.to_sql.assert_called_once()
74+
75+
def test_to_sql_with_plain_string(self):
76+
"""Test _to_sql with a plain string value."""
77+
result = _to_sql("plain_value")
78+
assert result == "plain_value"
79+
80+
def test_to_sql_with_number(self):
81+
"""Test _to_sql with a number value."""
82+
result = _to_sql(42)
83+
assert result == "42"
84+
85+
86+
class TestHstoreConstructors:
87+
"""Tests for hstore constructor functions."""
88+
89+
def test_hstore_from_record(self):
90+
"""Test hstore_from_record() function."""
91+
result = hstore_from_record("ROW(1,2)")
92+
assert result == "hstore(ROW(1,2))"
93+
94+
def test_hstore_from_key_value(self):
95+
"""Test hstore_from_key_value() function."""
96+
result = hstore_from_key_value("'name'", "'value'")
97+
assert result == "hstore('name', 'value')"
98+
99+
100+
class TestHstoreKeyValueExtraction:
101+
"""Tests for hstore key/value extraction functions."""
102+
103+
def test_hstore_akeys(self):
104+
"""Test hstore_akeys() function."""
105+
result = hstore_akeys("data")
106+
assert result == "akeys(data)"
107+
108+
def test_hstore_skeys(self):
109+
"""Test hstore_skeys() function."""
110+
result = hstore_skeys("data")
111+
assert result == "skeys(data)"
112+
113+
def test_hstore_avals(self):
114+
"""Test hstore_avals() function."""
115+
result = hstore_avals("data")
116+
assert result == "avals(data)"
117+
118+
def test_hstore_svals(self):
119+
"""Test hstore_svals() function."""
120+
result = hstore_svals("data")
121+
assert result == "svals(data)"
122+
123+
def test_hstore_each(self):
124+
"""Test hstore_each() function."""
125+
result = hstore_each("data")
126+
assert result == "each(data)"
127+
128+
129+
class TestHstoreConversion:
130+
"""Tests for hstore conversion functions."""
131+
132+
def test_hstore_to_array(self):
133+
"""Test hstore_to_array() function."""
134+
result = hstore_to_array("data")
135+
assert result == "hstore_to_array(data)"
136+
137+
def test_hstore_to_matrix(self):
138+
"""Test hstore_to_matrix() function."""
139+
result = hstore_to_matrix("data")
140+
assert result == "hstore_to_matrix(data)"
141+
142+
def test_hstore_to_json(self):
143+
"""Test hstore_to_json() function."""
144+
result = hstore_to_json("data")
145+
assert result == "hstore_to_json(data)"
146+
147+
def test_hstore_to_jsonb(self):
148+
"""Test hstore_to_jsonb() function."""
149+
result = hstore_to_jsonb("data")
150+
assert result == "hstore_to_jsonb(data)"
151+
152+
def test_hstore_to_json_loose(self):
153+
"""Test hstore_to_json_loose() function."""
154+
result = hstore_to_json_loose("data")
155+
assert result == "hstore_to_json_loose(data)"
156+
157+
def test_hstore_to_jsonb_loose(self):
158+
"""Test hstore_to_jsonb_loose() function."""
159+
result = hstore_to_jsonb_loose("data")
160+
assert result == "hstore_to_jsonb_loose(data)"
161+
162+
163+
class TestHstoreSubset:
164+
"""Tests for hstore subset functions."""
165+
166+
def test_hstore_slice(self):
167+
"""Test hstore_slice() function."""
168+
result = hstore_slice("data", "ARRAY['a', 'b']")
169+
assert result == "slice(data, ARRAY['a', 'b'])"
170+
171+
172+
class TestHstoreExistence:
173+
"""Tests for hstore existence functions."""
174+
175+
def test_hstore_exist(self):
176+
"""Test hstore_exist() function."""
177+
result = hstore_exist("data", "'name'")
178+
assert result == "exist(data, 'name')"
179+
180+
def test_hstore_defined(self):
181+
"""Test hstore_defined() function."""
182+
result = hstore_defined("data", "'name'")
183+
assert result == "defined(data, 'name')"
184+
185+
186+
class TestHstoreDelete:
187+
"""Tests for hstore delete functions."""
188+
189+
def test_hstore_delete(self):
190+
"""Test hstore_delete() function."""
191+
result = hstore_delete("data", "'name'")
192+
assert result == "delete(data, 'name')"
193+
194+
def test_hstore_delete_keys(self):
195+
"""Test hstore_delete_keys() function."""
196+
result = hstore_delete_keys("data", "ARRAY['name', 'age']")
197+
assert result == "delete(data, ARRAY['name', 'age'])"
198+
199+
def test_hstore_delete_pairs(self):
200+
"""Test hstore_delete_pairs() function."""
201+
result = hstore_delete_pairs("data", "'a=>1'::hstore")
202+
assert result == "delete(data, 'a=>1'::hstore)"
203+
204+
205+
class TestHstoreRecord:
206+
"""Tests for hstore record functions."""
207+
208+
def test_hstore_populate_record(self):
209+
"""Test hstore_populate_record() function."""
210+
result = hstore_populate_record("ROW(1,2)", "'f1=>42'::hstore")
211+
assert result == "populate_record(ROW(1,2), 'f1=>42'::hstore)"
212+
213+
214+
class TestHstoreOperators:
215+
"""Tests for hstore operator functions."""
216+
217+
def test_hstore_get_value(self):
218+
"""Test hstore_get_value() function (operator ->)."""
219+
result = hstore_get_value("data", "'name'")
220+
assert result == "data -> 'name'"
221+
222+
def test_hstore_get_values(self):
223+
"""Test hstore_get_values() function (operator -> text[])."""
224+
result = hstore_get_values("data", "ARRAY['name', 'age']")
225+
assert result == "data -> ARRAY['name', 'age']"
226+
227+
def test_hstore_concat(self):
228+
"""Test hstore_concat() function (operator ||)."""
229+
result = hstore_concat("'a=>1'::hstore", "'b=>2'::hstore")
230+
assert result == "'a=>1'::hstore || 'b=>2'::hstore"
231+
232+
def test_hstore_key_exists(self):
233+
"""Test hstore_key_exists() function (operator ?)."""
234+
result = hstore_key_exists("data", "'name'")
235+
assert result == "data ? 'name'"
236+
237+
def test_hstore_all_keys_exist(self):
238+
"""Test hstore_all_keys_exist() function (operator ?&)."""
239+
result = hstore_all_keys_exist("data", "ARRAY['a', 'b']")
240+
assert result == "data ?& ARRAY['a', 'b']"
241+
242+
def test_hstore_any_key_exists(self):
243+
"""Test hstore_any_key_exists() function (operator ?|)."""
244+
result = hstore_any_key_exists("data", "ARRAY['a', 'b']")
245+
assert result == "data ?| ARRAY['a', 'b']"
246+
247+
def test_hstore_contains(self):
248+
"""Test hstore_contains() function (operator @>)."""
249+
result = hstore_contains("data", "'a=>1'")
250+
assert result == "data @> 'a=>1'"
251+
252+
def test_hstore_contained_by(self):
253+
"""Test hstore_contained_by() function (operator <@)."""
254+
result = hstore_contained_by("'a=>1'", "data")
255+
assert result == "'a=>1' <@ data"
256+
257+
258+
class TestHstoreSubtraction:
259+
"""Tests for hstore subtraction operator functions."""
260+
261+
def test_hstore_subtract_key(self):
262+
"""Test hstore_subtract_key() function (operator - text)."""
263+
result = hstore_subtract_key("data", "'name'")
264+
assert result == "data - 'name'"
265+
266+
def test_hstore_subtract_keys(self):
267+
"""Test hstore_subtract_keys() function (operator - text[])."""
268+
result = hstore_subtract_keys("data", "ARRAY['a', 'b']")
269+
assert result == "data - ARRAY['a', 'b']"
270+
271+
def test_hstore_subtract_pairs(self):
272+
"""Test hstore_subtract_pairs() function (operator - hstore)."""
273+
result = hstore_subtract_pairs("data", "'a=>1'::hstore")
274+
assert result == "data - 'a=>1'::hstore"
275+
276+
277+
class TestHstoreSpecialOperators:
278+
"""Tests for hstore special operator functions."""
279+
280+
def test_hstore_to_array_operator(self):
281+
"""Test hstore_to_array_operator() function (operator %%)."""
282+
result = hstore_to_array_operator("data")
283+
assert result == "%%data"
284+
285+
def test_hstore_to_matrix_operator(self):
286+
"""Test hstore_to_matrix_operator() function (operator %#)."""
287+
result = hstore_to_matrix_operator("data")
288+
assert result == "%#data"
289+
290+
def test_hstore_record_update(self):
291+
"""Test hstore_record_update() function (operator #=)."""
292+
result = hstore_record_update("ROW(1,2)", "'f1=>42'::hstore")
293+
assert result == "ROW(1,2) #= 'f1=>42'::hstore"
294+
295+
296+
class TestHstoreSubscript:
297+
"""Tests for hstore subscript access functions."""
298+
299+
def test_hstore_subscript_get(self):
300+
"""Test hstore_subscript_get() function."""
301+
result = hstore_subscript_get("data", "'name'")
302+
assert result == "data['name']"
303+
304+
def test_hstore_subscript_set(self):
305+
"""Test hstore_subscript_set() function."""
306+
result = hstore_subscript_set("data", "'name'", "'value'")
307+
assert result == "data['name'] = 'value'"

0 commit comments

Comments
 (0)