How do you compare two SQLAlchemy column objects with Python unittest?

I wrote a unit test that should compare the Column object returned from my method. I create one in the test and then get one from my class. My test code is as follows:

def test_getting_correct_sa_object_from_column(self):
    table = self.database.get_table_by_name('ESEventlogMain')
    column = table.get_column_by_name('eventdata')
    sa_column = sqlalchemy.Column('eventdata', sqlalchemy.Integer, primary_key=False)

    self.assertEqual(sa_column, column.get_sa_object())

And the method of my class returns this:

def get_sa_object(self):
    if self.type == 'int':
        sa_type = sqlalchemy.Integer
    elif self.type == 'varchar':
        sa_type = sqlalchemy.String(self.length)

    return sqlalchemy.Column(self.name, sa_type, primary_key=self.is_primary_key)

When I run the test, it does not work with this output:

Failure
Traceback (most recent call last):
  File "C:\Users\tames.mctigue\PycharmProjects\db_setup_wizard\tests.py", line 107, in test_getting_correct_sa_object_from_column
    self.assertEqual(sa_column, column.get_sa_object())
AssertionError: Column('eventdata', Integer(), table=None) != Column('eventdata', Integer(), table=None)

AssertionError shows what looks like identical data, so I'm stuck on what to look for. Is there any other comparison I should use besides "assertEqual"?

+4
source share
1 answer

Try using a straight column, for example:

def test_getting_correct_sa_object_from_column(self):
    table = self.database.get_table_by_name('ESEventlogMain')
    column = table.get_column_by_name('eventdata')
    sa_column = table.eventdata # <- Change this column

    self.assertEqual(sa_column, column.get_sa_object())
0
source

Source: https://habr.com/ru/post/1528225/


All Articles