0
votes

i am trying to mock sqlalchemy session.add such that when i insert session.add(order) and commit it , it should give me order.orderId back which i will use to extends the test case further .

   session.add(order)
        session.commit()
        return order.orderId

i have mocked sessionMaker i.e mocker.patch.object(file_name, "create_pgdb_engine") which return me mock but i am getting error like

Instance has a NULL identity key. If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values. Ensure also that this flush() is not occurring at an inappropriate time, such as within a order() event.

1

1 Answers

1
votes

Trying to mock lower level database components to create mock objects is probably not a very good strategy for testing. If you're trying to mock sqlalchemy objects for unit testing purposes, look into Factory-boy. Check out the full example here of how a test framework can be constructed using sqlalchemy and factory-boy.