Skip to content

Commit

Permalink
Provide bytes override for BigBitFieldData.
Browse files Browse the repository at this point in the history
This fixes issues coercing values to appropriate db-value.

Refs #2743
  • Loading branch information
coleifer committed Jun 14, 2023
1 parent 95b9889 commit f06d61e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -5042,6 +5042,12 @@ def is_set(self, idx):

def __repr__(self):
return repr(self._buffer)
if sys.version_info[0] < 3:
def __str__(self):
return bytes_type(self._buffer)
else:
def __bytes__(self):
return bytes_type(self._buffer)


class BigBitFieldAccessor(FieldAccessor):
Expand Down
24 changes: 24 additions & 0 deletions tests/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,30 @@ def test_bigbit_field(self):
else:
self.assertFalse(b_db.data.is_set(x))

def test_bigbit_field_bulk_create(self):
b1, b2, b3 = Bits(), Bits(), Bits()
b1.data.set_bit(1)
b2.data.set_bit(2)
b3.data.set_bit(3)
Bits.bulk_create([b1, b2, b3])
self.assertEqual(len(Bits), 3)
for b in Bits.select():
self.assertEqual(sum(1 if b.data.is_set(i) else 0
for i in (1, 2, 3)), 1)

def test_bigbit_field_bulk_update(self):
b1, b2, b3 = Bits.create(), Bits.create(), Bits.create()

b1.data.set_bit(11)
b2.data.set_bit(12)
b3.data.set_bit(13)
Bits.bulk_update([b1, b2, b3], fields=[Bits.data])

mapping = {b1.id: 11, b2.id: 12, b3.id: 13}
for b in Bits.select():
bit = mapping[b.id]
self.assertTrue(b.data.is_set(bit))


class BlobModel(TestModel):
data = BlobField()
Expand Down

0 comments on commit f06d61e

Please sign in to comment.