Beispiel #1
0
def test_database_begin__starts_new_transaction(db: Database):
    with db.begin() as session:
        assert isinstance(session, Session)
        assert session.in_transaction()

        with db.begin() as other_session:
            assert other_session is not session
Beispiel #2
0
def test_session_save_all__raises_on_duplicate_primary_keys_in_list(
        db: Database):
    users = [User(id=1), User(id=1)]

    with pytest.raises(TypeError):
        with db.begin() as session:
            session.save_all(users)
Beispiel #3
0
def create_users(db: Database,
                 count: int = 3,
                 overrides: t.Optional[dict] = None) -> t.List[User]:
    if overrides is None:
        overrides = {}
    users = [User(id=i, **overrides) for i in range(1, count + 1)]
    with db.begin(expire_on_commit=False) as session:
        session.add_all(users)
    return users
Beispiel #4
0
def test_session_save_all__accepts_single_model_and_returns_list(db: Database):
    user = User()
    with db.begin() as session:
        result = session.save_all(user)
        assert isinstance(result, list)
        assert result == [user]

    with db.session() as session:
        select_count = sa.select(sa.func.count(User.id))
        assert session.one(select_count) == 1
Beispiel #5
0
def test_session_save_all__returns_list(db: Database):
    users = [User(), User(), User()]
    with db.begin() as session:
        result = session.save_all(users)
        assert isinstance(result, list)
        assert result == users

    with db.session() as session:
        select_count = sa.select(sa.func.count(User.id))
        assert session.one(select_count) == len(users)
Beispiel #6
0
def test_session_save__inserts_new_without_pk(db: Database):
    user = User(name="n")
    with db.begin() as session:
        session.save(user)

    assert user.id == 1
    assert user.name == "n"

    with db.session() as session:
        select_count = sa.select(sa.func.count(User.id))
        assert session.one(select_count) == 1
Beispiel #7
0
def test_model_to_dict__loaded_from_database(db: Database, model: ModelBase,
                                             args: dict, load_options: list,
                                             expected: dict):
    with db.begin() as session:
        session.add(model)

    with db.session() as session:
        fetched_model = session.first(
            type(model).select().options(*load_options))
        assert fetched_model is not None
        assert fetched_model.to_dict(**args) == expected
Beispiel #8
0
def test_session_save_all__inserts_and_updates_multiple_types(db: Database):
    existing_users = [User(name="uA"), User(name="uB"), User(name="uC")]
    new_users = [User(name="u1"), User(name="u2"), User(name="u3")]

    existing_groups = [Group(name="gA"), Group(name="gB"), Group(name="gC")]
    new_groups = [Group(name="g1"), Group(name="g2"), Group(name="g3")]

    with db.begin(expire_on_commit=False) as session:
        session.save_all(existing_users)
        session.save_all(existing_groups)

    all_models = existing_users + existing_groups + new_users + new_groups  # type: ignore
    all_users = existing_users + new_users
    all_groups = existing_groups + new_groups

    with db.begin() as session:
        session.save_all(all_models)

    with db.session() as session:
        count_users = sa.select(sa.func.count(User.id))
        assert session.one(count_users) == len(all_users)

        count_groups = sa.select(sa.func.count(Group.id))
        assert session.one(count_groups) == len(all_groups)
Beispiel #9
0
def test_session_save__updates_existing(db: Database):
    existing_user = create_users(db, count=1)[0]
    assert existing_user.id == 1
    assert existing_user.name is None

    new_user = User(id=1, name="n")
    with db.begin() as session:
        session.save(existing_user)

    assert new_user.id == 1
    assert new_user.name == "n"

    with db.session() as session:
        select_count = sa.select(sa.func.count(User.id))
        assert session.one(select_count) == 1
Beispiel #10
0
def test_session_all__uniquifies_joinedload_results(db: Database):
    users = [
        User(id=1, addresses=[Address(id=11),
                              Address(id=12),
                              Address(id=13)]),
        User(id=2, addresses=[Address(id=21),
                              Address(id=22),
                              Address(id=23)]),
        User(id=3, addresses=[Address(id=31),
                              Address(id=32),
                              Address(id=33)]),
    ]
    with db.begin() as session:
        session.add_all(users)

    stmt = sa.select(User).options(joinedload(User.addresses))
    with db.session() as session:
        results = session.all(stmt)
        assert len(results) == len(users)