Skip to content

Commit

Permalink
Merge branch 'piccolo-orm:master' into reverse_lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
sinisaos authored Jan 9, 2025
2 parents bda0815 + 965c5b3 commit 382a100
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
16 changes: 14 additions & 2 deletions piccolo/engine/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,21 @@ def convert_numeric_out(value: str) -> Decimal:
@decode_to_string
def convert_int_out(value: str) -> int:
"""
Make sure Integer values are actually of type int.
Make sure INTEGER values are actually of type ``int``.
SQLite doesn't enforce that the values in INTEGER columns are actually
integers - they could be strings ('hello'), or floats (1.0).
There's not much we can do if the value is something like 'hello' - a
``ValueError`` is appropriate in this situation.
For a value like ``1.0``, it seems reasonable to handle this, and return a
value of ``1``.
"""
return int(float(value))
# We used to use int(float(value)), but it was incorrect, because float has
# limited precision for large numbers.
return int(Decimal(value))


@decode_to_string
Expand Down
2 changes: 1 addition & 1 deletion requirements/test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
coveralls==3.3.1
httpx==0.27.2
httpx==0.28.0
pytest-cov==3.0.0
pytest==6.2.5
python-dateutil==2.8.2
Expand Down
32 changes: 32 additions & 0 deletions tests/columns/test_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from piccolo.columns.column_types import Integer
from piccolo.table import Table
from piccolo.testing.test_case import AsyncTableTest
from tests.base import sqlite_only


class MyTable(Table):
integer = Integer()


@sqlite_only
class TestInteger(AsyncTableTest):
tables = [MyTable]

async def test_large_integer(self):
"""
Make sure large integers can be inserted and retrieved correctly.
There was a bug with this in SQLite:
https://github.com/piccolo-orm/piccolo/issues/1127
"""
integer = 625757527765811240

row = MyTable(integer=integer)
await row.save()

_row = MyTable.objects().first().run_sync()
assert _row is not None

self.assertEqual(_row.integer, integer)

0 comments on commit 382a100

Please sign in to comment.