I'm developing some tests for a web app with a legacy MSSQL database. The database is rather large and has many tables interconnected with foreign keys. I'm using the fixture library to create tables containing test data, based on the models I have defined in my app in SQLalchemy.
The problem I'm running in to, is that when I add a table to my fixtures that has a foreign key, running the test also requires me to add the connected table, even though I'm not using it.
ProgrammingError: (ProgrammingError) ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Foreign key 'FK__countries__ctry___6C40A441' references invalid table 'directdebit_types'. (1767) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Could not create constraint.
For example, I need to have "club" data, but a club is part of an "area"; I would need to add an "area" table to my fixtures to continue. After adding an "area", the tables can't be created because Areas are linked to Countries, so now I need to add a countries table to my fixtures. Countries again, is linked to directdebit_types, so now I would need to add that too, even though I only want to mock up some club data!
The example error above is what I get every time I start the test, and fixture tries to setup the tables. In this specific case it's the foreign key from countries to direcdebit_types. Following and creating all interconnected tables would result in having to practically create the entire database for a simple test.
Is there any way around this?
For the sake of completeness, here's the relevant part of my fixture setup:
class ClubsData(DataSet):
"""
Fixture dataset class for Clubs
"""
class accesscontrol_test_club:
club_id = '22222222-2222-2222-2222-222222222222'
club_name = 'accesscontrol test country'
class AreasData(DataSet):
"""
Fixture dataset class for Areas
"""
class accesscontrol_test_area:
area_name = 'testarea'
club = ClubsData.accesscontrol_test_club
class CountriesData(DataSet):
"""
Fixture dataset class for Countries
"""
class accesscontrol_test_country:
ctry_shortname = 'accestan'
area = AreasData.accesscontrol_test_area
And my models
class Clubs(db.Model, clubs):
"""Club model class.
"""
shift_terminal = relationship(
"Terminals",
primaryjoin="Terminals.term_id==Clubs.club_terminal_for_shift",
foreign_keys=[clubs.club_terminal_for_shift],
backref=backref('shift_terminal_clubs'))
area = relationship("Areas", backref=backref('clubs'))
...
class Areas(db.Model, areas):
"""Area model class.
"""
country = relationship("Countries", backref=backref('areas'))
def __init__(self, *args, **kwargs):
self.area_id = newid()
super(Areas, self).__init__(*args, **kwargs)
...
class Countries(db.Model, countries):
"""
Country model class.
"""
directdebittype = relationship("DirectdebitTypes", backref=backref('countries'))
def __init__(self, *args, **kwargs):
self.ctry_id = newid()
super(Countries, self).__init__(*args, **kwargs)
...