Well, here's my solution that works exactly the way I want it,

@classmethod
def from_flat_json(cls, d=None, db_enable_relationship_loading=True,
                   **kwargs):
    # noinspection PyArgumentList
    new_instance = cls(**d, **kwargs)

    state = instance_state(new_instance)
    mapper = state.mapper
    # noinspection PyProtectedMember
    identity_class = mapper._identity_class
    pk_cols = mapper.primary_key
    identity_token = None

    identitykey = (
        identity_class,
        tuple([getattr(new_instance, column.key) for column in pk_cols]),
        identity_token
    )
    session = get_db_session()

    state.key = identitykey
    state.identity_token = identity_token
    state.session_id = session.hash_key

    # noinspection PyProtectedMember
    session.identity_map._add_unpresent(state, identitykey)

    if db_enable_relationship_loading:
        session.enable_relationship_loading(new_instance)
    return new_instance


It feels like you're against this approach, but this is the design / 
pattern that I'd like my tests to follow, and I'm sharing in case anyone 
else would like similar.

This now allows me, in my completely dB connection free testing environment 
to do,

sport = Sport.from_flat_json(dict(name='Soccer', id=1))
competition = Competition.from_flat_json(dict(name='MLS', id=1, sport_id=1))

And without doing anything else,

assert competition.sport == sport

Is now also correct, which again is exactly how I want it to be.  With 
this, when a situation (bug/crash) occurs, I can just "dump" a snapshot of 
my system to json, load that into my local environment, and debug it "as 
live", with everything set up correctly, as if all the dB rows had come 
from the database, without any need for double-jobbing anything.


On Saturday, September 29, 2018 at 9:12:51 PM UTC+1, Mike Bayer wrote:
>
> My advice is to simply make a sqlite memory database for testing which is 
> an almost universal pattern for test suites.
>
>
> You can even make a database per test, that's a common pattern.  
>  Otherwise you have to mock everything and yes "double job" it.  The ORM 
> doesn't want to "double job" it either, with a sqlite memory database 
> easily available it doesn't have to reinvent what the database does, which 
> would be not only a massively complicated endeavor it also would never 
> behave quite the same way either.
>
 

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to