jgirardet / pytest-ponyorm

PonyORM in Pytest
GNU General Public License v3.0
4 stars 2 forks source link

generate mapping at the correct stage #8

Open peteretep opened 5 years ago

peteretep commented 5 years ago

I would like to use your plugin but am having some trouble configuring it correctly.

My package provides an "agent" object, which is used to connect to a server API and retrieve "events". On initialisation of the agent, the pony orm is setup and bound to a sqlite db, either in memory (for testing) or as a file.

def setup_db(filepath=None):
    if filepath:
        db.bind(provider="sqlite", filename=filepath, create_db=True)
    else:
        db.bind(provider="sqlite", filename=":memory:", create_db=True)
    db.provider.converter_classes.append((Enum, EnumConverter))
    db.generate_mapping(create_tables=True)

An agent is created as a fixture in the conftest.py file, pony complains that no mapping was generated for the database, as the setup_db function is only called when the agent is initialised. If I remove the call to setup_db from the agent initialisation, and call it manually in the conftest.py file, it works as expected.

I am wondering if there is a better way you could suggest? Related SO question - https://stackoverflow.com/questions/57639915/pony-orm-tear-down-in-testing