Closing all sqlalchemy sessions, connections, pools, metadata

117 views
Skip to first unread message

Don Smiley

unread,
Mar 9, 2020, 5:40:02 PM3/9/20
to sqlalchemy
In unit testing with multiple configurations, I am having trouble closing out everything to go on to the next iteration. For example, the test creates a class Address. I get the following kind of error:

```
/venv/lib/python3.6/site-packages/SQLAlchemy-1.3.13-py3.6-linux-x86_64.egg/sqlalchemy/ext/declarative/clsregistry.py", line 97, in attempt_get\n    % (".".join(path + [key]))\nsqlalchemy.exc.InvalidRequestError: Multiple classes found for path "Address" in the registry of this declarative base. Please use a fully module-qualified path.\n')
```

My endcycle consists of among other things:
* drop_all(echo=True)
* drop database

```
    engine = create_engine(config)
    conn = engine.connect()
    conn.execute("COMMIT")
    result = conn.execute(f"DROP DATABASE IF EXISTS {dbname}")
    result.close()
    conn.close()
    engine.dispose()
```
* orm.session.close_all_sessions()
* I have even tried del objects that have been created, but that seemed futile and desperate.
   
Is there something else that I should be doing here, or in a different order?
Or, should approach unit testing in a different way?

I am thinking that I could probably unittest by shelling out each configuration to a separate process, but it seems to me that if I had a better understanding I would not need to do that.

Any advice is welcome.


Mike Bayer

unread,
Mar 9, 2020, 7:19:31 PM3/9/20
to noreply-spamdigest via sqlalchemy


On Mon, Mar 9, 2020, at 5:40 PM, Don Smiley wrote:
In unit testing with multiple configurations, I am having trouble closing out everything to go on to the next iteration. For example, the test creates a class Address. I get the following kind of error:

```
/venv/lib/python3.6/site-packages/SQLAlchemy-1.3.13-py3.6-linux-x86_64.egg/sqlalchemy/ext/declarative/clsregistry.py", line 97, in attempt_get\n    % (".".join(path + [key]))\nsqlalchemy.exc.InvalidRequestError: Multiple classes found for path "Address" in the registry of this declarative base. Please use a fully module-qualified path.\n')


that error refers to the declarative base class.  If your tests set up new mappers against a Base each time,, you should use a new Base for each test run that sets up those mappers.  otherwise you will get conflicting names like above.




```

My endcycle consists of among other things:
* drop_all(echo=True)
* drop database

```
    engine = create_engine(config)
    conn = engine.connect()
    conn.execute("COMMIT")
    result = conn.execute(f"DROP DATABASE IF EXISTS {dbname}")
    result.close()
    conn.close()
    engine.dispose()
```
* orm.session.close_all_sessions()
* I have even tried del objects that have been created, but that seemed futile and desperate.
   
Is there something else that I should be doing here, or in a different order?
Or, should approach unit testing in a different way?

I am thinking that I could probably unittest by shelling out each configuration to a separate process, but it seems to me that if I had a better understanding I would not need to do that.

Any advice is welcome.



--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Don Smiley

unread,
Mar 9, 2020, 9:55:03 PM3/9/20
to sqlalchemy

That's an interesting point. I actually pull it in as an imported class using
@as_declarative()
class MyClass

Forcing an import reload for each initialization made all the difference. Thanks for your help.
Reply all
Reply to author
Forward
0 new messages