MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / test_numeric

Method test_numeric

tests/test_codecs.py:646–711  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

644 self.assertEqual(res, datetime.timedelta(days=-1855))
645
646 async def test_numeric(self):
647 # Test that we handle dscale correctly.
648 cases = [
649 '0.001',
650 '0.001000',
651 '1',
652 '1.00000'
653 ]
654
655 for case in cases:
656 res = await self.con.fetchval(
657 "SELECT $1::numeric", case)
658
659 self.assertEqual(str(res), case)
660
661 try:
662 await self.con.execute(
663 '''
664 CREATE TABLE tab (v numeric(3, 2));
665 INSERT INTO tab VALUES (0), (1);
666 ''')
667 res = await self.con.fetchval("SELECT v FROM tab WHERE v = $1", 0)
668 self.assertEqual(str(res), '0.00')
669 res = await self.con.fetchval("SELECT v FROM tab WHERE v = $1", 1)
670 self.assertEqual(str(res), '1.00')
671 finally:
672 await self.con.execute('DROP TABLE tab')
673
674 res = await self.con.fetchval(
675 "SELECT $1::numeric", decimal.Decimal('NaN'))
676 self.assertTrue(res.is_nan())
677
678 res = await self.con.fetchval(
679 "SELECT $1::numeric", decimal.Decimal('sNaN'))
680 self.assertTrue(res.is_nan())
681
682 if self.server_version < (14, 0):
683 with self.assertRaisesRegex(
684 asyncpg.DataError,
685 'invalid sign in external "numeric" value'
686 ):
687 await self.con.fetchval(
688 "SELECT $1::numeric", decimal.Decimal('-Inf'))
689
690 with self.assertRaisesRegex(
691 asyncpg.DataError,
692 'invalid sign in external "numeric" value'
693 ):
694 await self.con.fetchval(
695 "SELECT $1::numeric", decimal.Decimal('+Inf'))
696
697 with self.assertRaisesRegex(asyncpg.DataError, 'invalid'):
698 await self.con.fetchval(
699 "SELECT $1::numeric", 'invalid')
700 else:
701 res = await self.con.fetchval(
702 "SELECT $1::numeric", decimal.Decimal("-Inf"))
703 self.assertTrue(res.is_infinite())

Callers

nothing calls this directly

Calls 2

fetchvalMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected