from asyncpg import _testbase as tb


class TestPrepare(tb.ConnectedTestCase):

    async def test_prepare_1(self):
        st = await self.con.prepare('SELECT 1 = $1 AS test')

        rec = (await st.execute(1))[0]
        self.assertTrue(rec['test'])
        self.assertEqual(len(rec), 1)
        self.assertEqual(tuple(rec), (True,))

        self.assertEqual(False, (await st.execute(10))[0][0])

    async def test_prepare_2(self):
        with self.assertRaisesRegex(Exception, 'column "a" does not exist'):
            await self.con.prepare('SELECT a')

    async def test_prepare_3(self):
        st = await self.con.prepare('''
            SELECT CASE WHEN $1::text IS NULL THEN 'NULL'
                                              ELSE $1::text END''')

        self.assertEqual((await st.execute('aaa'))[0][0], 'aaa')
        self.assertEqual((await st.execute(None))[0][0], 'NULL')

from asyncpg import _testbase as tb


class TestPrepare(tb.ConnectedTestCase):

    async def test_prepare_1(self):
        st = await self.con.prepare('SELECT 1 = $1 AS test')

        rec = (await st.execute(1))[0]
        self.assertTrue(rec['test'])
        self.assertEqual(len(rec), 1)
        self.assertEqual(tuple(rec), (True,))

        self.assertEqual(False, (await st.execute(10))[0][0])

    async def test_prepare_2(self):
        with self.assertRaisesRegex(Exception, 'column "a" does not exist'):
            await self.con.prepare('SELECT a')

    async def test_prepare_3(self):
        cases = [
            ('text', ("'NULL'", 'NULL'), [
                'aaa',
                None
            ]),

            ('decimal', ('0', 0), [
                123,
                123.5,
                None
            ])
        ]

        for type, (none_name, none_val), vals in cases:
            st = await self.con.prepare('''
                    SELECT CASE WHEN $1::{type} IS NULL THEN {default}
                    ELSE $1::{type} END'''.format(
                type=type, default=none_name))

            for val in vals:
                with self.subTest(type=type, value=val):
                    res = (await st.execute(val))[0][0]
                    if val is None:
                        self.assertEqual(res, none_val)
                    else:
                        self.assertEqual(res, val)

Test that we handle None->NULL conversion for TEXT and BINARY
tests: Test that we handle None->NULL conversion for TEXT and BINARY

  from asyncpg import _testbase as tb
  
  
  class TestPrepare(tb.ConnectedTestCase):
  
      async def test_prepare_1(self):
          st = await self.con.prepare('SELECT 1 = $1 AS test')
  
          rec = (await st.execute(1))[0]
          self.assertTrue(rec['test'])
          self.assertEqual(len(rec), 1)
          self.assertEqual(tuple(rec), (True,))
  
          self.assertEqual(False, (await st.execute(10))[0][0])
  
      async def test_prepare_2(self):
          with self.assertRaisesRegex(Exception, 'column "a" does not exist'):
              await self.con.prepare('SELECT a')
  
      async def test_prepare_3(self):
-         st = await self.con.prepare('''
-             SELECT CASE WHEN $1::text IS NULL THEN 'NULL'
-                                               ELSE $1::text END''')
+         cases = [
+             ('text', ("'NULL'", 'NULL'), [
+                 'aaa',
+                 None
+             ]),
  
-         self.assertEqual((await st.execute('aaa'))[0][0], 'aaa')
-         self.assertEqual((await st.execute(None))[0][0], 'NULL')
+             ('decimal', ('0', 0), [
+                 123,
+                 123.5,
+                 None
+             ])
+         ]
  
+         for type, (none_name, none_val), vals in cases:
+             st = await self.con.prepare('''
+                     SELECT CASE WHEN $1::{type} IS NULL THEN {default}
+                     ELSE $1::{type} END'''.format(
+                 type=type, default=none_name))
+ 
+             for val in vals:
+                 with self.subTest(type=type, value=val):
+                     res = (await st.execute(val))[0][0]
+                     if val is None:
+                         self.assertEqual(res, none_val)
+                     else:
+                         self.assertEqual(res, val)
+ 
Test that we handle None->NULL conversion for TEXT and BINARY
## Code Before:
from asyncpg import _testbase as tb


class TestPrepare(tb.ConnectedTestCase):

    async def test_prepare_1(self):
        st = await self.con.prepare('SELECT 1 = $1 AS test')

        rec = (await st.execute(1))[0]
        self.assertTrue(rec['test'])
        self.assertEqual(len(rec), 1)
        self.assertEqual(tuple(rec), (True,))

        self.assertEqual(False, (await st.execute(10))[0][0])

    async def test_prepare_2(self):
        with self.assertRaisesRegex(Exception, 'column "a" does not exist'):
            await self.con.prepare('SELECT a')

    async def test_prepare_3(self):
        st = await self.con.prepare('''
            SELECT CASE WHEN $1::text IS NULL THEN 'NULL'
                                              ELSE $1::text END''')

        self.assertEqual((await st.execute('aaa'))[0][0], 'aaa')
        self.assertEqual((await st.execute(None))[0][0], 'NULL')

## Instruction:
Test that we handle None->NULL conversion for TEXT and BINARY
## Code After:
from asyncpg import _testbase as tb


class TestPrepare(tb.ConnectedTestCase):

    async def test_prepare_1(self):
        st = await self.con.prepare('SELECT 1 = $1 AS test')

        rec = (await st.execute(1))[0]
        self.assertTrue(rec['test'])
        self.assertEqual(len(rec), 1)
        self.assertEqual(tuple(rec), (True,))

        self.assertEqual(False, (await st.execute(10))[0][0])

    async def test_prepare_2(self):
        with self.assertRaisesRegex(Exception, 'column "a" does not exist'):
            await self.con.prepare('SELECT a')

    async def test_prepare_3(self):
        cases = [
            ('text', ("'NULL'", 'NULL'), [
                'aaa',
                None
            ]),

            ('decimal', ('0', 0), [
                123,
                123.5,
                None
            ])
        ]

        for type, (none_name, none_val), vals in cases:
            st = await self.con.prepare('''
                    SELECT CASE WHEN $1::{type} IS NULL THEN {default}
                    ELSE $1::{type} END'''.format(
                type=type, default=none_name))

            for val in vals:
                with self.subTest(type=type, value=val):
                    res = (await st.execute(val))[0][0]
                    if val is None:
                        self.assertEqual(res, none_val)
                    else:
                        self.assertEqual(res, val)
