MCPcopy
hub / github.com/MagicStack/asyncpg / test_numeric

Method test_numeric

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

Source from the content-addressed store, hash-verified

598 self.assertEqual(res, datetime.timedelta(days=-1855))
599
600 async def test_numeric(self):
601 # Test that we handle dscale correctly.
602 cases = [
603 '0.001',
604 '0.001000',
605 '1',
606 '1.00000'
607 ]
608
609 for case in cases:
610 res = await self.con.fetchval(
611 "SELECT $1::numeric", case)
612
613 self.assertEqual(str(res), case)
614
615 try:
616 await self.con.execute(
617 '''
618 CREATE TABLE tab (v numeric(3, 2));
619 INSERT INTO tab VALUES (0), (1);
620 ''')
621 res = await self.con.fetchval("SELECT v FROM tab WHERE v = $1", 0)
622 self.assertEqual(str(res), '0.00')
623 res = await self.con.fetchval("SELECT v FROM tab WHERE v = $1", 1)
624 self.assertEqual(str(res), '1.00')
625 finally:
626 await self.con.execute('DROP TABLE tab')
627
628 res = await self.con.fetchval(
629 "SELECT $1::numeric", decimal.Decimal('NaN'))
630 self.assertTrue(res.is_nan())
631
632 res = await self.con.fetchval(
633 "SELECT $1::numeric", decimal.Decimal('sNaN'))
634 self.assertTrue(res.is_nan())
635
636 if self.server_version < (14, 0):
637 with self.assertRaisesRegex(
638 asyncpg.DataError,
639 'invalid sign in external "numeric" value'
640 ):
641 await self.con.fetchval(
642 "SELECT $1::numeric", decimal.Decimal('-Inf'))
643
644 with self.assertRaisesRegex(
645 asyncpg.DataError,
646 'invalid sign in external "numeric" value'
647 ):
648 await self.con.fetchval(
649 "SELECT $1::numeric", decimal.Decimal('+Inf'))
650
651 with self.assertRaisesRegex(asyncpg.DataError, 'invalid'):
652 await self.con.fetchval(
653 "SELECT $1::numeric", 'invalid')
654 else:
655 res = await self.con.fetchval(
656 "SELECT $1::numeric", decimal.Decimal("-Inf"))
657 self.assertTrue(res.is_infinite())

Callers

nothing calls this directly

Calls 2

fetchvalMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected