On Sun, Sep 30, 2018 at 9:39 AM seaders <seader...@gmail.com> wrote:
>
> 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.

it's cool with me whatever you want to do that works :)   as I noted
earlier, I do have mock test setups that don't use the DB at all, it's
just they're hard to implement and understand, but if you have one you
like then you're GTG.  However we have to clean up all that private
attribute stuff, as what you are doing there is still available
through public APIs.

Most of what you need you can get with make_transient_to_detached.
That will create your identity key, assign it, and the object is
addable to the Session as though you loaded it.  Here's the whole
thing and you don't need any of that stuff:

s = Session()

a1 = A(id=1)
make_transient_to_detached(a1)
s.add(a1)

b1 = B(a_id=1)
s.add(b1)
s.enable_relationship_loading(b1)
assert b1.a is a1


However, the reason I don't usually give this out as a real recipe, is
that it is extremely limited in its functionality.  It only works for
a simple many-to-one relationship with a default "primaryjoin"
condition.    Wont work for collections, many to many, complicated
joins, none of that.   If you truly only need simplistic many-to-one
loading, then that approach will be enough.


>
> 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.

-- 
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