Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query: process RHS according to its type + tests #106

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions bitfield/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@


class BitQueryLookupWrapper(Exact): # NOQA
def process_lhs(self, qn, connection, lhs=None):
lhs_sql, params = super(BitQueryLookupWrapper, self).process_lhs(
qn, connection, lhs)
if self.rhs:
lhs_sql = lhs_sql + ' & %s'
else:
lhs_sql = lhs_sql + ' | %s'
params.extend(self.get_db_prep_lookup(self.rhs, connection)[1])
return lhs_sql, params
def process_lhs(self, compiler, connection, lhs=None):
lhs_sql, lhs_params = super(BitQueryLookupWrapper, self).process_lhs(
compiler, connection, lhs)

if not isinstance(self.rhs, (BitHandler, Bit)):
return lhs_sql, lhs_params

op = ' & ' if self.rhs else ' | '
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
params = list(lhs_params)
params.extend(rhs_params)

return op.join((lhs_sql, rhs_sql)), params

def get_db_prep_lookup(self, value, connection, prepared=False):
v = value.mask if isinstance(value, (BitHandler, Bit)) else value
return super(BitQueryLookupWrapper, self).get_db_prep_lookup(v, connection)

def get_prep_lookup(self):
return self.rhs
if isinstance(self.rhs, (BitHandler, Bit)):
return self.rhs # resolve at later stage, in get_db_prep_lookup
return super(BitQueryLookupWrapper, self).get_prep_lookup()


class BitQuerySaveWrapper(BitQueryLookupWrapper):
Expand Down
22 changes: 22 additions & 0 deletions bitfield/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ def test_select(self):
self.assertFalse(BitFieldTestModel.objects.exclude(flags=BitFieldTestModel.flags.FLAG_0).exists())
self.assertFalse(BitFieldTestModel.objects.exclude(flags=BitFieldTestModel.flags.FLAG_1).exists())

def test_select_complex_expression(self):
BitFieldTestModel.objects.create(flags=3)
self.assertTrue(BitFieldTestModel.objects.filter(flags=F('flags').bitor(BitFieldTestModel.flags.FLAG_1)).exists())
self.assertTrue(BitFieldTestModel.objects.filter(flags=F('flags').bitor(BitFieldTestModel.flags.FLAG_0)).exists())
self.assertTrue(BitFieldTestModel.objects.filter(flags=F('flags').bitor(BitFieldTestModel.flags.FLAG_0).bitor(BitFieldTestModel.flags.FLAG_1)).exists())
self.assertTrue(BitFieldTestModel.objects.filter(flags=F('flags').bitand(BitFieldTestModel.flags.FLAG_0 | BitFieldTestModel.flags.FLAG_1)).exists())
self.assertTrue(BitFieldTestModel.objects.filter(flags=F('flags').bitand(15)).exists())
self.assertTrue(BitFieldTestModel.objects.exclude(flags=F('flags').bitand(BitFieldTestModel.flags.FLAG_2)).exists())
self.assertTrue(BitFieldTestModel.objects.exclude(flags=F('flags').bitand(BitFieldTestModel.flags.FLAG_3)).exists())
self.assertTrue(BitFieldTestModel.objects.exclude(flags=F('flags').bitand(BitFieldTestModel.flags.FLAG_2 | BitFieldTestModel.flags.FLAG_3)).exists())
self.assertTrue(BitFieldTestModel.objects.exclude(flags=F('flags').bitand(12)).exists())

self.assertFalse(BitFieldTestModel.objects.exclude(flags=F('flags').bitor(BitFieldTestModel.flags.FLAG_1)).exists())
self.assertFalse(BitFieldTestModel.objects.exclude(flags=F('flags').bitor(BitFieldTestModel.flags.FLAG_0)).exists())
self.assertFalse(BitFieldTestModel.objects.exclude(flags=F('flags').bitor(BitFieldTestModel.flags.FLAG_0).bitor(BitFieldTestModel.flags.FLAG_1)).exists())
self.assertFalse(BitFieldTestModel.objects.exclude(flags=F('flags').bitand(BitFieldTestModel.flags.FLAG_0 | BitFieldTestModel.flags.FLAG_1)).exists())
self.assertFalse(BitFieldTestModel.objects.exclude(flags=F('flags').bitand(15)).exists())
self.assertFalse(BitFieldTestModel.objects.filter(flags=F('flags').bitand(BitFieldTestModel.flags.FLAG_2)).exists())
self.assertFalse(BitFieldTestModel.objects.filter(flags=F('flags').bitand(BitFieldTestModel.flags.FLAG_3)).exists())
self.assertFalse(BitFieldTestModel.objects.filter(flags=F('flags').bitand(BitFieldTestModel.flags.FLAG_2 | BitFieldTestModel.flags.FLAG_3)).exists())
self.assertFalse(BitFieldTestModel.objects.filter(flags=F('flags').bitand(12)).exists())

def test_update(self):
instance = BitFieldTestModel.objects.create(flags=0)
self.assertFalse(instance.flags.FLAG_0)
Expand Down