Exemplo n.º 1
0
 def compare_columns_equal(self, columns1, columns2, ignore=None):
     """Loop through all columns and compare them"""
     def key(column):
         return column.name
     for c1, c2 in zip(sorted(columns1, key=key), sorted(columns2, key=key)):
         diffs = ColumnDelta(c1, c2).diffs
         if ignore:
             for key in ignore:
                 diffs.pop(key, None)
         if diffs:
             self.fail("Comparing %s to %s failed: %s" % (columns1, columns2, diffs))
Exemplo n.º 2
0
 def compare_columns_equal(self, columns1, columns2, ignore=None):
     """Loop through all columns and compare them"""
     def key(column):
         return column.name
     for c1, c2 in zip(sorted(columns1, key=key), sorted(columns2, key=key)):
         diffs = ColumnDelta(c1, c2).diffs
         if ignore:
             for key in ignore:
                 diffs.pop(key, None)
         if diffs:
             self.fail("Comparing %s to %s failed: %s" % (columns1, columns2, diffs))
 def verify(self, expected, original, *p, **k):
     self.delta = ColumnDelta(original, *p, **k)
     result = self.delta.keys()
     result.sort()
     self.assertEquals(expected, result)
     return self.delta
class TestColumnDelta(fixture.DB):
    """Tests ColumnDelta class"""

    level = fixture.DB.CONNECT
    table_name = 'tmp_coldelta'
    table_int = 0

    def _setup(self, url):
        super(TestColumnDelta, self)._setup(url)
        self.meta = MetaData()
        self.table = Table(
            self.table_name,
            self.meta,
            Column('ids', String(10)),
        )
        self.meta.bind = self.engine
        if self.engine.has_table(self.table.name):
            self.table.drop()
        self.table.create()

    def _teardown(self):
        if self.engine.has_table(self.table.name):
            self.table.drop()
        self.meta.clear()
        super(TestColumnDelta, self)._teardown()

    def mkcol(self, name='id', type=String, *p, **k):
        return Column(name, type, *p, **k)

    def verify(self, expected, original, *p, **k):
        self.delta = ColumnDelta(original, *p, **k)
        result = self.delta.keys()
        result.sort()
        self.assertEquals(expected, result)
        return self.delta

    def test_deltas_two_columns(self):
        """Testing ColumnDelta with two columns"""
        col_orig = self.mkcol(primary_key=True)
        col_new = self.mkcol(name='ids', primary_key=True)
        self.verify([], col_orig, col_orig)
        self.verify(['name'], col_orig, col_orig, 'ids')
        self.verify(['name'], col_orig, col_orig, name='ids')
        self.verify(['name'], col_orig, col_new)
        self.verify(['name', 'type'], col_orig, col_new, type=String)

        # Type comparisons
        self.verify([], self.mkcol(type=String), self.mkcol(type=String))
        self.verify(['type'], self.mkcol(type=String),
                    self.mkcol(type=Integer))
        self.verify(['type'], self.mkcol(type=String),
                    self.mkcol(type=String(42)))
        self.verify([], self.mkcol(type=String(42)),
                    self.mkcol(type=String(42)))
        self.verify(['type'], self.mkcol(type=String(24)),
                    self.mkcol(type=String(42)))
        self.verify(['type'], self.mkcol(type=String(24)),
                    self.mkcol(type=Text(24)))

        # Other comparisons
        self.verify(['primary_key'], self.mkcol(nullable=False),
                    self.mkcol(primary_key=True))

        # PK implies nullable=False
        self.verify(['nullable', 'primary_key'], self.mkcol(nullable=True),
                    self.mkcol(primary_key=True))
        self.verify([], self.mkcol(primary_key=True),
                    self.mkcol(primary_key=True))
        self.verify(['nullable'], self.mkcol(nullable=True),
                    self.mkcol(nullable=False))
        self.verify([], self.mkcol(nullable=True), self.mkcol(nullable=True))
        self.verify([], self.mkcol(server_default=None),
                    self.mkcol(server_default=None))
        self.verify([], self.mkcol(server_default='42'),
                    self.mkcol(server_default='42'))

        # test server default
        delta = self.verify(['server_default'], self.mkcol(),
                            self.mkcol('id', String, DefaultClause('foobar')))
        self.assertEqual(delta['server_default'].arg, 'foobar')

        self.verify([], self.mkcol(server_default='foobar'),
                    self.mkcol('id', String, DefaultClause('foobar')))
        self.verify(['type'], self.mkcol(server_default='foobar'),
                    self.mkcol('id', Text, DefaultClause('foobar')))

        col = self.mkcol(server_default='foobar')
        self.verify(['type'],
                    col,
                    self.mkcol('id', Text, DefaultClause('foobar')),
                    alter_metadata=True)
        self.assert_(isinstance(col.type, Text))

        col = self.mkcol()
        self.verify(['name', 'server_default', 'type'],
                    col,
                    self.mkcol('beep', Text, DefaultClause('foobar')),
                    alter_metadata=True)
        self.assert_(isinstance(col.type, Text))
        self.assertEqual(col.name, 'beep')
        self.assertEqual(col.server_default.arg, 'foobar')

    @fixture.usedb()
    def test_deltas_zero_columns(self):
        """Testing ColumnDelta with zero columns"""

        self.verify(['name'], 'ids', table=self.table, name='hey')

        # test reflection
        self.verify(['type'],
                    'ids',
                    table=self.table.name,
                    type=String(80),
                    engine=self.engine)
        self.verify(['type'],
                    'ids',
                    table=self.table.name,
                    type=String(80),
                    metadata=self.meta)

        self.meta.clear()
        delta = self.verify(['type'],
                            'ids',
                            table=self.table.name,
                            type=String(80),
                            metadata=self.meta,
                            alter_metadata=True)
        self.assert_(self.table.name in self.meta)
        self.assertEqual(delta.result_column.type.length, 80)
        self.assertEqual(
            self.meta.tables.get(self.table.name).c.ids.type.length, 80)

        # test defaults
        self.meta.clear()
        self.verify(['server_default'],
                    'ids',
                    table=self.table.name,
                    server_default='foobar',
                    metadata=self.meta,
                    alter_metadata=True)
        self.meta.tables.get(
            self.table.name).c.ids.server_default.arg == 'foobar'

        # test missing parameters
        self.assertRaises(ValueError, ColumnDelta, table=self.table.name)
        self.assertRaises(ValueError,
                          ColumnDelta,
                          'ids',
                          table=self.table.name,
                          alter_metadata=True)
        self.assertRaises(ValueError,
                          ColumnDelta,
                          'ids',
                          table=self.table.name,
                          alter_metadata=False)

    def test_deltas_one_column(self):
        """Testing ColumnDelta with one column"""
        col_orig = self.mkcol(primary_key=True)

        self.verify([], col_orig)
        self.verify(['name'], col_orig, 'ids')
        # Parameters are always executed, even if they're 'unchanged'
        # (We can't assume given column is up-to-date)
        self.verify(['name', 'primary_key', 'type'],
                    col_orig,
                    'id',
                    Integer,
                    primary_key=True)
        self.verify(['name', 'primary_key', 'type'],
                    col_orig,
                    name='id',
                    type=Integer,
                    primary_key=True)

        # Change name, given an up-to-date definition and the current name
        delta = self.verify(['name'], col_orig, name='blah')
        self.assertEquals(delta.get('name'), 'blah')
        self.assertEquals(delta.current_name, 'id')

        col_orig = self.mkcol(primary_key=True)
        self.verify(['name', 'type'],
                    col_orig,
                    name='id12',
                    type=Text,
                    alter_metadata=True)
        self.assert_(isinstance(col_orig.type, Text))
        self.assertEqual(col_orig.name, 'id12')

        # test server default
        col_orig = self.mkcol(primary_key=True)
        delta = self.verify(['server_default'], col_orig,
                            DefaultClause('foobar'))
        self.assertEqual(delta['server_default'].arg, 'foobar')

        delta = self.verify(['server_default'],
                            col_orig,
                            server_default=DefaultClause('foobar'))
        self.assertEqual(delta['server_default'].arg, 'foobar')

        # no change
        col_orig = self.mkcol(server_default=DefaultClause('foobar'))
        delta = self.verify(['type'],
                            col_orig,
                            DefaultClause('foobar'),
                            type=PickleType)
        self.assert_(isinstance(delta.result_column.type, PickleType))
Exemplo n.º 5
0
 def verify(self, expected, original, *p, **k):
     self.delta = ColumnDelta(original, *p, **k)
     result = self.delta.keys()
     result.sort()
     self.assertEqual(expected, result)
     return self.delta
Exemplo n.º 6
0
class TestColumnDelta(fixture.DB):
    """Tests ColumnDelta class"""

    level = fixture.DB.CONNECT
    table_name = "tmp_coldelta"
    table_int = 0

    def _setup(self, url):
        super(TestColumnDelta, self)._setup(url)
        self.meta = MetaData()
        self.table = Table(self.table_name, self.meta, Column("ids", String(10)))
        self.meta.bind = self.engine
        if self.engine.has_table(self.table.name):
            self.table.drop()
        self.table.create()

    def _teardown(self):
        if self.engine.has_table(self.table.name):
            self.table.drop()
        self.meta.clear()
        super(TestColumnDelta, self)._teardown()

    def mkcol(self, name="id", type=String, *p, **k):
        return Column(name, type, *p, **k)

    def verify(self, expected, original, *p, **k):
        self.delta = ColumnDelta(original, *p, **k)
        result = self.delta.keys()
        result.sort()
        self.assertEqual(expected, result)
        return self.delta

    def test_deltas_two_columns(self):
        """Testing ColumnDelta with two columns"""
        col_orig = self.mkcol(primary_key=True)
        col_new = self.mkcol(name="ids", primary_key=True)
        self.verify([], col_orig, col_orig)
        self.verify(["name"], col_orig, col_orig, "ids")
        self.verify(["name"], col_orig, col_orig, name="ids")
        self.verify(["name"], col_orig, col_new)
        self.verify(["name", "type"], col_orig, col_new, type=String)

        # Type comparisons
        self.verify([], self.mkcol(type=String), self.mkcol(type=String))
        self.verify(["type"], self.mkcol(type=String), self.mkcol(type=Integer))
        self.verify(["type"], self.mkcol(type=String), self.mkcol(type=String(42)))
        self.verify([], self.mkcol(type=String(42)), self.mkcol(type=String(42)))
        self.verify(["type"], self.mkcol(type=String(24)), self.mkcol(type=String(42)))
        self.verify(["type"], self.mkcol(type=String(24)), self.mkcol(type=Text(24)))

        # Other comparisons
        self.verify(["primary_key"], self.mkcol(nullable=False), self.mkcol(primary_key=True))

        # PK implies nullable=False
        self.verify(["nullable", "primary_key"], self.mkcol(nullable=True), self.mkcol(primary_key=True))
        self.verify([], self.mkcol(primary_key=True), self.mkcol(primary_key=True))
        self.verify(["nullable"], self.mkcol(nullable=True), self.mkcol(nullable=False))
        self.verify([], self.mkcol(nullable=True), self.mkcol(nullable=True))
        self.verify([], self.mkcol(server_default=None), self.mkcol(server_default=None))
        self.verify([], self.mkcol(server_default="42"), self.mkcol(server_default="42"))

        # test server default
        delta = self.verify(["server_default"], self.mkcol(), self.mkcol("id", String, DefaultClause("foobar")))
        self.assertEqual(delta["server_default"].arg, "foobar")

        self.verify([], self.mkcol(server_default="foobar"), self.mkcol("id", String, DefaultClause("foobar")))
        self.verify(["type"], self.mkcol(server_default="foobar"), self.mkcol("id", Text, DefaultClause("foobar")))

        col = self.mkcol(server_default="foobar")
        self.verify(["type"], col, self.mkcol("id", Text, DefaultClause("foobar")), alter_metadata=True)
        self.assert_(isinstance(col.type, Text))

        col = self.mkcol()
        self.verify(
            ["name", "server_default", "type"],
            col,
            self.mkcol("beep", Text, DefaultClause("foobar")),
            alter_metadata=True,
        )
        self.assert_(isinstance(col.type, Text))
        self.assertEqual(col.name, "beep")
        self.assertEqual(col.server_default.arg, "foobar")

    @fixture.usedb()
    def test_deltas_zero_columns(self):
        """Testing ColumnDelta with zero columns"""

        self.verify(["name"], "ids", table=self.table, name="hey")

        # test reflection
        self.verify(["type"], "ids", table=self.table.name, type=String(80), engine=self.engine)
        self.verify(["type"], "ids", table=self.table.name, type=String(80), metadata=self.meta)

        self.meta.clear()
        delta = self.verify(
            ["type"], "ids", table=self.table.name, type=String(80), metadata=self.meta, alter_metadata=True
        )
        self.assert_(self.table.name in self.meta)
        self.assertEqual(delta.result_column.type.length, 80)
        self.assertEqual(self.meta.tables.get(self.table.name).c.ids.type.length, 80)

        # test defaults
        self.meta.clear()
        self.verify(
            ["server_default"],
            "ids",
            table=self.table.name,
            server_default="foobar",
            metadata=self.meta,
            alter_metadata=True,
        )
        self.meta.tables.get(self.table.name).c.ids.server_default.arg == "foobar"

        # test missing parameters
        self.assertRaises(ValueError, ColumnDelta, table=self.table.name)
        self.assertRaises(ValueError, ColumnDelta, "ids", table=self.table.name, alter_metadata=True)
        self.assertRaises(ValueError, ColumnDelta, "ids", table=self.table.name, alter_metadata=False)

    def test_deltas_one_column(self):
        """Testing ColumnDelta with one column"""
        col_orig = self.mkcol(primary_key=True)

        self.verify([], col_orig)
        self.verify(["name"], col_orig, "ids")
        # Parameters are always executed, even if they're 'unchanged'
        # (We can't assume given column is up-to-date)
        self.verify(["name", "primary_key", "type"], col_orig, "id", Integer, primary_key=True)
        self.verify(["name", "primary_key", "type"], col_orig, name="id", type=Integer, primary_key=True)

        # Change name, given an up-to-date definition and the current name
        delta = self.verify(["name"], col_orig, name="blah")
        self.assertEqual(delta.get("name"), "blah")
        self.assertEqual(delta.current_name, "id")

        col_orig = self.mkcol(primary_key=True)
        self.verify(["name", "type"], col_orig, name="id12", type=Text, alter_metadata=True)
        self.assert_(isinstance(col_orig.type, Text))
        self.assertEqual(col_orig.name, "id12")

        # test server default
        col_orig = self.mkcol(primary_key=True)
        delta = self.verify(["server_default"], col_orig, DefaultClause("foobar"))
        self.assertEqual(delta["server_default"].arg, "foobar")

        delta = self.verify(["server_default"], col_orig, server_default=DefaultClause("foobar"))
        self.assertEqual(delta["server_default"].arg, "foobar")

        # no change
        col_orig = self.mkcol(server_default=DefaultClause("foobar"))
        delta = self.verify(["type"], col_orig, DefaultClause("foobar"), type=PickleType)
        self.assert_(isinstance(delta.result_column.type, PickleType))
class TestColumnDelta(fixture.DB):
    """Tests ColumnDelta class"""

    level = fixture.DB.CONNECT
    table_name = 'tmp_coldelta'
    table_int = 0

    def _setup(self, url):
        super(TestColumnDelta, self)._setup(url)
        self.meta = MetaData()
        self.table = Table(self.table_name, self.meta,
            Column('ids', String(10)),
        )
        self.meta.bind = self.engine
        if self.engine.has_table(self.table.name):
            self.table.drop()
        self.table.create()

    def _teardown(self):
        if self.engine.has_table(self.table.name):
            self.table.drop()
        self.meta.clear()
        super(TestColumnDelta,self)._teardown()

    def mkcol(self, name='id', type=String, *p, **k):
        return Column(name, type, *p, **k)

    def verify(self, expected, original, *p, **k):
        self.delta = ColumnDelta(original, *p, **k)
        result = list(self.delta.keys())
        result.sort()
        self.assertEqual(expected, result)
        return self.delta

    def test_deltas_two_columns(self):
        """Testing ColumnDelta with two columns"""
        col_orig = self.mkcol(primary_key=True)
        col_new = self.mkcol(name='ids', primary_key=True)
        self.verify([], col_orig, col_orig)
        self.verify(['name'], col_orig, col_orig, 'ids')
        self.verify(['name'], col_orig, col_orig, name='ids')
        self.verify(['name'], col_orig, col_new)
        self.verify(['name', 'type'], col_orig, col_new, type=String)

        # Type comparisons
        self.verify([], self.mkcol(type=String), self.mkcol(type=String))
        self.verify(['type'], self.mkcol(type=String), self.mkcol(type=Integer))
        self.verify(['type'], self.mkcol(type=String), self.mkcol(type=String(42)))
        self.verify([], self.mkcol(type=String(42)), self.mkcol(type=String(42)))
        self.verify(['type'], self.mkcol(type=String(24)), self.mkcol(type=String(42)))
        self.verify(['type'], self.mkcol(type=String(24)), self.mkcol(type=Text(24)))

        # Other comparisons
        self.verify(['primary_key'], self.mkcol(nullable=False), self.mkcol(primary_key=True))

        # PK implies nullable=False
        self.verify(['nullable', 'primary_key'], self.mkcol(nullable=True), self.mkcol(primary_key=True))
        self.verify([], self.mkcol(primary_key=True), self.mkcol(primary_key=True))
        self.verify(['nullable'], self.mkcol(nullable=True), self.mkcol(nullable=False))
        self.verify([], self.mkcol(nullable=True), self.mkcol(nullable=True))
        self.verify([], self.mkcol(server_default=None), self.mkcol(server_default=None))
        self.verify([], self.mkcol(server_default='42'), self.mkcol(server_default='42'))

        # test server default
        delta = self.verify(['server_default'], self.mkcol(), self.mkcol('id', String, DefaultClause('foobar')))
        self.assertEqual(delta['server_default'].arg, 'foobar')

        self.verify([], self.mkcol(server_default='foobar'), self.mkcol('id', String, DefaultClause('foobar')))
        self.verify(['type'], self.mkcol(server_default='foobar'), self.mkcol('id', Text, DefaultClause('foobar')))

        col = self.mkcol(server_default='foobar')
        self.verify(['type'], col, self.mkcol('id', Text, DefaultClause('foobar')), alter_metadata=True)
        self.assertTrue(isinstance(col.type, Text))

        col = self.mkcol()
        self.verify(['name', 'server_default', 'type'], col, self.mkcol('beep', Text, DefaultClause('foobar')),
                    alter_metadata=True)
        self.assertTrue(isinstance(col.type, Text))
        self.assertEqual(col.name, 'beep')
        self.assertEqual(col.server_default.arg, 'foobar')

    @fixture.usedb()
    def test_deltas_zero_columns(self):
        """Testing ColumnDelta with zero columns"""

        self.verify(['name'], 'ids', table=self.table, name='hey')

        # test reflection
        self.verify(['type'], 'ids', table=self.table.name, type=String(80), engine=self.engine)
        self.verify(['type'], 'ids', table=self.table.name, type=String(80), metadata=self.meta)

        self.meta.clear()
        delta = self.verify(['type'], 'ids', table=self.table.name, type=String(80), metadata=self.meta,
                            alter_metadata=True)
        self.assertTrue(self.table.name in self.meta)
        self.assertEqual(delta.result_column.type.length, 80)
        self.assertEqual(self.meta.tables.get(self.table.name).c.ids.type.length, 80)

        # test defaults
        self.meta.clear()
        self.verify(['server_default'], 'ids', table=self.table.name, server_default='foobar',
                    metadata=self.meta,
                    alter_metadata=True)
        self.meta.tables.get(self.table.name).c.ids.server_default.arg == 'foobar'

        # test missing parameters
        self.assertRaises(ValueError, ColumnDelta, table=self.table.name)
        self.assertRaises(ValueError, ColumnDelta, 'ids', table=self.table.name, alter_metadata=True)
        self.assertRaises(ValueError, ColumnDelta, 'ids', table=self.table.name, alter_metadata=False)

    def test_deltas_one_column(self):
        """Testing ColumnDelta with one column"""
        col_orig = self.mkcol(primary_key=True)

        self.verify([], col_orig)
        self.verify(['name'], col_orig, 'ids')
        # Parameters are always executed, even if they're 'unchanged'
        # (We can't assume given column is up-to-date)
        self.verify(['name', 'primary_key', 'type'], col_orig, 'id', Integer, primary_key=True)
        self.verify(['name', 'primary_key', 'type'], col_orig, name='id', type=Integer, primary_key=True)

        # Change name, given an up-to-date definition and the current name
        delta = self.verify(['name'], col_orig, name='blah')
        self.assertEqual(delta.get('name'), 'blah')
        self.assertEqual(delta.current_name, 'id')

        col_orig = self.mkcol(primary_key=True)
        self.verify(['name', 'type'], col_orig, name='id12', type=Text, alter_metadata=True)
        self.assertTrue(isinstance(col_orig.type, Text))
        self.assertEqual(col_orig.name, 'id12')

        # test server default
        col_orig = self.mkcol(primary_key=True)
        delta = self.verify(['server_default'], col_orig, DefaultClause('foobar'))
        self.assertEqual(delta['server_default'].arg, 'foobar')

        delta = self.verify(['server_default'], col_orig, server_default=DefaultClause('foobar'))
        self.assertEqual(delta['server_default'].arg, 'foobar')

        # no change
        col_orig = self.mkcol(server_default=DefaultClause('foobar'))
        delta = self.verify(['type'], col_orig, DefaultClause('foobar'), type=PickleType)
        self.assertTrue(isinstance(delta.result_column.type, PickleType))