IEEE.org     |     IEEE Xplore Digital Library     |     IEEE Standards     |     IEEE Spectrum     |     More Sites

Verified Commit 6acc0e36 authored by Emi Simpson's avatar Emi Simpson
Browse files

[new req] Expand QueryRequest equality

- fix crash when comparing QR vs Tuple and they aren't equal
- support comparing despite differing punctuation
parent 7fa1a3d8
......@@ -34,21 +34,25 @@ class QueryRequest(NamedTuple):
.split('\n')\
for word in line\
.split('--')[0]\
.rstrip(';')
.lower()\
.replace(',', ' , ')
.split()
]
def __eq__(self, other: object) -> bool:
return\
isinstance(other, QueryRequest) and\
self.args == other.args and\
self._normalize_query_string(self.query) == self._normalize_query_string(other.query)\
or\
isinstance(other, Tuple) and\
other.__len__() == 2 and\
isinstance(other[0], str) and\
(isinstance(other[1], Tuple) or isinstance(other[1], List)) and\
self == QueryRequest(*cast(Tuple[str, Tuple[Any, ...] | List[Any]], other))
if isinstance(other, QueryRequest):
return\
self.args == other.args and\
self._normalize_query_string(self.query) == self._normalize_query_string(other.query)
elif isinstance(other, Tuple):
return\
other.__len__() == 2 and\
isinstance(other[0], str) and\
(isinstance(other[1], Tuple) or isinstance(other[1], List)) and\
self == QueryRequest(*cast(Tuple[str, Tuple[Any, ...] | List[Any]], other))
else:
return False
T = TypeVar('T', covariant=True)
E = TypeVar('E', covariant=True)
......
......@@ -32,7 +32,7 @@ class TestQueries(unittest.TestCase):
QueryRequest(' hewwo\tworld', (2,)),
QueryRequest('hewwo world', (1, 2))
)
self.assertEqual(
self.assertEqual( # Test with comments
QueryRequest('''
SELECT some_feild --TODO change this
FROM my_table
......@@ -41,14 +41,22 @@ class TestQueries(unittest.TestCase):
''', (1, 2)),
QueryRequest('select some_feild from my_table natural join other_table', (1, 2))
)
self.assertEqual(
self.assertEqual( # Test with tuple 2nd arg
QueryRequest('query req', tuple()),
('query req', tuple())
)
self.assertEqual(
self.assertEqual( # Test with tuple 1st arg
('query req', tuple()),
QueryRequest('query req', tuple())
)
self.assertNotEqual( # Test neq with tuple arg
QueryRequest('query req', tuple()),
('query weq', tuple())
)
self.assertEqual( # Test with punctuation
QueryRequest('part1,part2;', tuple()),
('part1 , part2', tuple())
)
def test_noop(self):
str_noop = Noop("hello")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment