File: test_upsert.py

package info (click to toggle)
ormar 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,952 kB
  • sloc: python: 24,085; makefile: 34; sh: 14
file content (50 lines) | stat: -rw-r--r-- 1,543 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from typing import Optional

import ormar
import pytest

from tests.lifespan import init_tests
from tests.settings import create_config

base_ormar_config = create_config()


class Director(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="directors")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100, nullable=False, name="first_name")
    last_name: str = ormar.String(max_length=100, nullable=False, name="last_name")


class Movie(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="movies")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100, nullable=False, name="title")
    year: int = ormar.Integer()
    profit: float = ormar.Float()
    director: Optional[Director] = ormar.ForeignKey(Director)


create_test_database = init_tests(base_ormar_config)


@pytest.mark.asyncio
async def test_updating_selected_columns():
    async with base_ormar_config.database:
        director1 = await Director(name="Peter", last_name="Jackson").save()

        await Movie(
            id=1, name="Lord of The Rings", year=2003, director=director1, profit=1.212
        ).upsert()

        with pytest.raises(ormar.NoMatch):
            await Movie.objects.get()

        await Movie(
            id=1, name="Lord of The Rings", year=2003, director=director1, profit=1.212
        ).upsert(__force_save__=True)
        lotr = await Movie.objects.get()
        assert lotr.year == 2003
        assert lotr.name == "Lord of The Rings"