Exemplo n.º 1
0
 def test_make_placeholders_empty(self):
     with dbkit.connect(fakedb, 'db'):
         try:
             dbkit.make_placeholders([])
             self.fail("Expected ValueError")
         except ValueError:
             pass
Exemplo n.º 2
0
 def test_make_placeholders_unsupported_named(self):
     with utils.set_temporarily(fakedb, 'paramstyle', 'named'):
         with dbkit.connect(fakedb, 'db'):
             try:
                 dbkit.make_placeholders(['foo'])
                 self.fail("Should've got 'NotSupported' exception.")
             except dbkit.NotSupported as exc:
                 self.assertEqual(str(exc), "Param style 'named' does not support sequence type 'list'")
Exemplo n.º 3
0
 def assert_named(self, keys, pattern):
     keys = sorted(keys)
     dct = dict(zip(keys, [None] * len(keys)))
     self.assertEqual(utils.sort_fields(dbkit.make_placeholders(dct)),
                      ', '.join(pattern % (key,) for key in keys))
Exemplo n.º 4
0
 def assert_placeholders(self, n, expected, start=1):
     self.assertEqual(dbkit.make_placeholders([0] * n, start), expected)
Exemplo n.º 5
0
 def make_sorted(seq):
     """Wrap repetitive code for the next few checks."""
     return sort_fields(dbkit.make_placeholders(seq))
Exemplo n.º 6
0
def test_make_placeholders():
    with dbkit.connect(fakedb, 'db') as ctx:
        try:
            dbkit.make_placeholders([])
            assert False, "Expected ValueError"
        except ValueError:
            pass

    with utils.set_temporarily(fakedb, 'paramstyle', 'qmark'):
        with dbkit.connect(fakedb, 'db') as ctx:
            assert dbkit.make_placeholders([0]) == '?'
            assert dbkit.make_placeholders([0, 1]) == '?, ?'
            assert dbkit.make_placeholders([0, 1, 4]) == '?, ?, ?'

    for style in ('format', 'pyformat'):
        with utils.set_temporarily(fakedb, 'paramstyle', style):
            with dbkit.connect(fakedb, 'db') as ctx:
                assert dbkit.make_placeholders([0]) == '%s'
                assert dbkit.make_placeholders([0, 2]) == '%s, %s'
                assert dbkit.make_placeholders([0, 2, 7]) == '%s, %s, %s'

    with utils.set_temporarily(fakedb, 'paramstyle', 'numeric'):
        with dbkit.connect(fakedb, 'db') as ctx:
            assert dbkit.make_placeholders([0], 7) == ':7'
            assert dbkit.make_placeholders([0, 1], 7) == ':7, :8'
            assert dbkit.make_placeholders([0, 1, 4], 7) == ':7, :8, :9'

    def sort_fields(fields):
        """Helper to ensure named fields are sorted for the test."""
        return ', '.join(sorted(field.lstrip() for field in fields.split(',')))

    def make_sorted(seq):
        """Wrap repetitive code for the next few checks."""
        return sort_fields(dbkit.make_placeholders(seq))

    with utils.set_temporarily(fakedb, 'paramstyle', 'pyformat'):
        with dbkit.connect(fakedb, 'db') as ctx:
            assert make_sorted({'foo': None}) == '%(foo)s'
            assert make_sorted({'foo': None, 'bar': None}) == '%(bar)s, %(foo)s'
            assert make_sorted({'foo': None, 'bar': None, 'baz': None}) == '%(bar)s, %(baz)s, %(foo)s'

    with utils.set_temporarily(fakedb, 'paramstyle', 'named'):
        with dbkit.connect(fakedb, 'db') as ctx:
            assert make_sorted({'foo': None}) == ':foo'
            assert make_sorted({'foo': None, 'bar': None}) == ':bar, :foo'
            assert make_sorted({'foo': None, 'bar': None, 'baz': None}) == ':bar, :baz, :foo'

    with utils.set_temporarily(fakedb, 'paramstyle', 'qmark'):
        with dbkit.connect(fakedb, 'db') as ctx:
            try:
                print dbkit.make_placeholders({'foo': None})
                assert False, "Should've got 'NotSupported' exception."
            except dbkit.NotSupported, exc:
                assert str(exc) == "Param style 'qmark' does not support sequence type 'dict'"
Exemplo n.º 7
0
    with utils.set_temporarily(fakedb, 'paramstyle', 'pyformat'):
        with dbkit.connect(fakedb, 'db') as ctx:
            assert make_sorted({'foo': None}) == '%(foo)s'
            assert make_sorted({'foo': None, 'bar': None}) == '%(bar)s, %(foo)s'
            assert make_sorted({'foo': None, 'bar': None, 'baz': None}) == '%(bar)s, %(baz)s, %(foo)s'

    with utils.set_temporarily(fakedb, 'paramstyle', 'named'):
        with dbkit.connect(fakedb, 'db') as ctx:
            assert make_sorted({'foo': None}) == ':foo'
            assert make_sorted({'foo': None, 'bar': None}) == ':bar, :foo'
            assert make_sorted({'foo': None, 'bar': None, 'baz': None}) == ':bar, :baz, :foo'

    with utils.set_temporarily(fakedb, 'paramstyle', 'qmark'):
        with dbkit.connect(fakedb, 'db') as ctx:
            try:
                print dbkit.make_placeholders({'foo': None})
                assert False, "Should've got 'NotSupported' exception."
            except dbkit.NotSupported, exc:
                assert str(exc) == "Param style 'qmark' does not support sequence type 'dict'"

    with utils.set_temporarily(fakedb, 'paramstyle', 'named'):
        with dbkit.connect(fakedb, 'db') as ctx:
            try:
                print dbkit.make_placeholders(['foo'])
                assert False, "Should've got 'NotSupported' exception."
            except dbkit.NotSupported, exc:
                assert str(exc) == "Param style 'named' does not support sequence type 'list'"

# vim:set et ai: