pytest-flask-sqlalchemy icon indicating copy to clipboard operation
pytest-flask-sqlalchemy copied to clipboard

Pre-populating the database

Open Carelvd opened this issue 4 years ago • 2 comments

Dear Ms. Cochrane,

I am trying to use your package with Sandman 2 and I was wondering if you were aware of any examples that use both packages ? I was wondering if there were any examples using the client fixture from pytest-flask and db_session or db_engine from your package.

Extending the example from the documentation I am pre-populate an empty database as below :

@pytest.fixture()
def session(db_session):
    db_session.add(Table(id=0, name="test"))
    db_session.commit() # This triggers an error
    return db_session

def test_FUNCTION(client, session):
    client.post(/some/path/, data={"name":"testing"})
    record = session.query(Table).get(1)
    assert record.name == "testing"

I find that either the rollback mechanism is failing with the pymssql driver at the line with the commit.

Table 'table' does not have the identity property. Cannot perform SET operation.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n

I was hoping you may have come across this before and might be able to provide me with the odd pointer, the full code is available through a testing branch; explained in issue #22.

Regards,

Carel van Dam

Carelvd avatar Sep 20 '19 13:09 Carelvd

The initial error was due to the create statement for the table in SQL; I hadn't set an index upon the table, Doing so resolved my original issue now I find that SQLAlchemy errors out as follows :

sqlalchemy.exc.InvalidRequestError: Object '<Table at 0x4c4a0f0>' is already attached to session '2' (this is '3')

The client also seems to hang in some scenarios when I try to GET/POST data. I also went through the other issues and Issues #12, #19 and #7 appear similar in nature to what I am trying.

Carelvd avatar Sep 23 '19 09:09 Carelvd

Very strange. The first thing I'd want to rule out is some kind of SQL Server incompatibility. Seems like you worked on testing multiple database engines in #22 -- were you able to reproduce the most recent error in Postgres?

jeancochrane avatar Sep 24 '19 22:09 jeancochrane