Ensure bitwise negation is supported on bitfield.

This commit is contained in:
Charles Leifer
2026-03-15 16:00:49 -05:00
parent 3b86345dbb
commit 40091beade
3 changed files with 19 additions and 0 deletions
+2
View File
@@ -1417,6 +1417,8 @@ class BitwiseMixin(object):
class BitwiseNegated(BitwiseMixin, WrappedNode):
op = OP.BITWISE_NEGATION
def __invert__(self):
return self.node
+6
View File
@@ -571,6 +571,12 @@ class TestBitFields(ModelTestCase):
for i in range(1, 5):
Bits.create(flags=i)
q = Bits.select((~Bits.flags & 2).alias('bn')).order_by(Bits.id)
self.assertEqual([b.bn for b in q], [2, 0, 0, 2])
q = Bits.select().where(Bits.flags & 2).order_by(Bits.id)
self.assertEqual([b.flags for b in q], [2, 3])
Bits.update(flags=Bits.flags & ~2).execute()
assertFlags([1, 0, 1, 4])
+11
View File
@@ -1019,6 +1019,17 @@ class TestSelectQuery(BaseTestCase):
'SELECT "t1"."id" FROM "users" AS "t1" '
'WHERE ("t1"."username" ILIKE ?)'), ['%abc%'])
def test_bitwise_ops(self):
query = User.select(User.c.id).where(User.c.id.bin_and(4))
self.assertSQL(query, (
'SELECT "t1"."id" FROM "users" AS "t1" '
'WHERE ("t1"."id" & ?)'), [4])
query = User.select(User.c.id).where(User.c.id.bin_or(1))
self.assertSQL(query, (
'SELECT "t1"."id" FROM "users" AS "t1" '
'WHERE ("t1"."id" | ?)'), [1])
class TestInsertQuery(BaseTestCase):
def test_insert_simple(self):