Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Objects left on transient state #47

Open
sponsfreixes opened this issue Mar 29, 2021 · 0 comments
Open

Objects left on transient state #47

sponsfreixes opened this issue Mar 29, 2021 · 0 comments

Comments

@sponsfreixes
Copy link

sponsfreixes commented Mar 29, 2021

I'm finding that my objects are left on a transient state, and that attributes have the wrong type. I've review the docs and other issues on the issue tracker, and I can't see what I am doing wrong.

I have a pytest.ini file with:

[pytest]
mocked-sessions = my_project.models.db.session

And a conftest.py:

import pytest

from my_project.api.app import create_app
from my_project.models import db


@pytest.fixture(scope="session")
def app():
    app = create_app("my_config_file.py")
    app.testing = True
    return app


@pytest.fixture(scope="session")
def _db(app):
    return db

If you are curious about create_app, it's a straightforward Flask app factory:

def create_app(config_filename):
    app = Flask(__name__)
    app.config.from_pyfile(config_filename)

    from my_project.models import db

    db.init_app(app)

    from my_project.api.schemas import ma

    ma.init_app(app)

    from my_project.api.resources import api

    api.init_app(app)

    return app

I have this simple test which POSTs a string and a date, used to instantiate and commit a SQLAlchemy model:

@pytest.mark.usefixtures("client_class")
class TestMyRoute:
    def test_create_simple(self, db_session):
        request_payload = {"name": "foo", "my_date": "2022-07-29"}
        resp = self.client.post(LicenseConfigurationCollection.route, json=request_payload)
        assert resp.status_code == HTTPStatus.OK
        assert is_sub_dict(request_payload,resp.json["data"])

The serialization of this response fails. If I set a breakpoint, I see that just before generating the response, after the object has been created, added to the session and commited, this is how the object looks like:

(Pdb) obj
<MyObject (transient 140384149977696)>
(Pdb) obj.my_date
'2022-07-29'
(Pdb) obj.id
(Pdb) obj.created
(Pdb) 

As you can see, the date is on string format, which is not correct as it should be a datetime.date, given that it's declared on the model as my_date = db.Column(db.Date, nullable=True). Also, id and created are not initialized either, as expected of a transient object.

The created object is added to the session and it is committed properly. Proof is that if I remove the db_session fixture, the code works and the record is created on the DB. Using postman I am also able to verify it. Any insight?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant