async def test_update(repo): await repo.insert(User(name="Foo")) user = await repo.insert(User(name="Bar")) user = await repo.update( Changeset(user).cast({"name": "Changed Bar"}, ["name"])) assert isinstance(user, User) assert user.name == "Changed Bar" assert await list_users(repo) == ["Foo", "Changed Bar"]
async def test_preload_collection(repo): user1 = await repo.insert(User()) user2 = await repo.insert(User()) home1 = await repo.insert(Home(owner_id=user1.id)) home2 = await repo.insert(Home(owner_id=user2.id)) await repo.preload([user1, user2], "home") assert user1.home.id == home1.id assert user2.home.id == home2.id
async def test_update_from_valid_changeset(repo): user = await repo.insert(User()) changeset = Changeset(user).cast({"name": "Richard"}, ["name"]) updated_user = await repo.update(changeset) assert updated_user.id == user.id assert updated_user.name == "Richard"
def test_has_one(): assoc = HasOne("user", User, "user_id") assert assoc.name == "user" assert assoc.related == User assert assoc.owner_key == "id" assert assoc.related_key == "user_id" assert assoc.cardinality == Cardinality.ONE assert assoc.values(User(id=9)) == {}
async def test_get_by(repo): await repo.insert(User(name="Foo")) user = await repo.get_by(Query(User), name="Foo") assert user.name == "Foo" user = await repo.get_by(User, name="Foo") assert user.name == "Foo"
async def test_one(repo): await repo.insert(User(name="Foo")) user = await repo.one(Query(User)) assert user.name == "Foo" user = await repo.one(User) assert user.name == "Foo"
async def test_select_dict(repo): user = await repo.insert(User()) pet = await repo.insert(Pet(owner_id=user.id)) query = Query(User).join("pets", "p").select({ "user_id": "id", "pet_id": "p__id" }) assert await repo.one(query) == {"user_id": user.id, "pet_id": pet.id}
def test_has_many(): assoc = HasMany("user", User, "user_id") assert assoc.name == "user" assert assoc.related == User assert assoc.owner_key == "id" assert assoc.related_key == "user_id" assert assoc.cardinality == Cardinality.MANY assert assoc.values(User(id=9)) == {}
def test_put_assoc(): user = User(id=1, name="Bear") changeset = ( Changeset(Home()) .cast({"name": "Big Blue House"}, ["name"]) .put_assoc("owner", user) ) assert changeset.changes == {"owner_id": 1, "name": "Big Blue House"}
async def test_all(repo): await repo.insert(User(name="Foo")) users = await repo.all(Query(User)) assert users[0].name == "Foo" users = await repo.all(User) assert users[0].name == "Foo"
async def test_preload_multiple(repo): user = await repo.insert(User()) home = await repo.insert(Home(owner_id=user.id)) pet = await repo.insert(Pet(owner_id=user.id)) await repo.preload(user, ["home", "pets"]) assert user.home.id == home.id assert user.pets[0].id == pet.id
def test_put_assoc_with_dict_data_is_invalid(): user = User(id=1, name="Bear") with pytest.raises(ValueError): ( Changeset(({}, {})) .cast({"name": "Big Blue House"}, ["name"]) .put_assoc("owner", user) )
def test_belongs_to(): assoc = BelongsTo("user", User, "user_id") assert assoc.name == "user" assert assoc.related == User assert assoc.owner_key == "user_id" assert assoc.related_key == "id" assert assoc.cardinality == Cardinality.ONE assert assoc.values(User(id=9)) == {"user_id": 9}
async def test_select_nested(repo): user = await repo.insert(User()) pet = await repo.insert(Pet(owner_id=user.id)) query = Query(User).join("pets", "p").select(("id", { "pet": [{ "id": "p__id" }] })) assert await repo.one(query) == (user.id, {"pet": [{"id": pet.id}]})
async def test_preload_nested(repo): user = await repo.insert(User()) home = await repo.insert(Home(owner_id=user.id)) pet = await repo.insert(Pet(owner_id=user.id)) await repo.preload(pet, "owner.home.owner") assert pet.owner.id == user.id assert pet.owner.home.id == home.id assert pet.owner.home.owner.id == user.id
async def test_get(repo): user = await repo.insert(User(name="Foo")) user_id = user.id user = await repo.get(Query(User), user_id) assert user.id == user_id user = await repo.get(User, user_id) assert user.id == user_id
async def test_preload_has_many(repo): user = await repo.insert(User()) await repo.insert(Pet(owner_id=user.id)) await repo.preload(user, "pets") assert len(user.pets) == 1 await repo.insert(Pet(owner_id=user.id)) await repo.preload(user, "pets") assert len(user.pets) == 2
async def test_insert_from_invalid_changeset(repo): changeset = (Changeset(User()).cast({ "name": "Richard" }, ["name"]).validate_required(["foo"])) with pytest.raises(InvalidChangesetError) as info: await repo.insert(changeset) assert info.value.action == "insert" assert isinstance(info.value.changeset, Changeset) assert "could not perform insert" in str(info.value)
async def test_first(repo): user = await repo.first(User) assert user is None await repo.insert(User(name="Foo")) user = await repo.first(Query(User)) assert user.name == "Foo" user = await repo.first(User) assert user.name == "Foo"
async def test_update_belongs_to(repo): user = await repo.insert(User()) home = await repo.insert(Home()) home = await repo.update(Changeset(home).put_assoc("owner", user)) await repo.preload(home, "owner") assert home.owner_id == user.id assert home.owner.id == user.id home = await repo.get(Home, home.id) assert home.owner_id == user.id
def test_cast(): changeset = Changeset(User()).cast({"name": "Richard", "foo": "bar"}, ["name"]) assert changeset.is_valid assert changeset.changes == {"name": "Richard"}
def test_cast_empty(): changeset = Changeset(User()).cast({}, []) assert changeset.is_valid assert changeset.changes == {}
def test_apply_changes_to_model(): changeset = Changeset(User(name="foo")).cast({"name": "bar"}, ["name"]) changed_user = changeset.apply_changes() assert changed_user.name == "bar"
def test_changes_with_invalid_changeset(): changeset = Changeset(User(name="foo")).cast({"name": 42}, ["name"]) assert changeset.changes == {}
def test_validate_required(): changeset = Changeset(User()).cast({}, ["foo"]).validate_required(["foo", "bar"]) assert changeset.errors == { "foo": ["Missing data for required field."], "bar": ["Missing data for required field."], }
def test_put_assoc_works_only_for_belongs_to(): with pytest.raises(NotImplementedError): Changeset(User()).put_assoc("home", Home())
def test_cast_unknown_type(): changeset = Changeset(User()).cast({"unknown": 1}, ["unknown"]) assert changeset.changes == {}
def test_model_setattr_belongs_to(): user = User(id=99) home = Home() home.owner = user assert home.owner == user assert home.owner_id == user.id
def test_join_find_association() -> None: join = Join(User, ["pets"]) assoc = join.find_association() assert assoc == User.association("pets")
def test_model_setattr_attribute(): user = User() user.name = "Ray" assert user.name == "Ray"