File: test_weakref_checking.py

package info (click to toggle)
ormar 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,856 kB
  • sloc: python: 23,666; makefile: 34; sh: 14
file content (43 lines) | stat: -rw-r--r-- 1,052 bytes parent folder | download | duplicates (2)
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
import ormar

from tests.settings import create_config

base_ormar_config = create_config()
from tests.lifespan import init_tests


class Band(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="bands")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)


class Artist(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="artists")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)

    band: Band = ormar.ForeignKey(Band)


create_test_database = init_tests(base_ormar_config)


def test_weakref_init():
    band = Band(name="Band")
    artist1 = Artist(name="Artist 1", band=band)
    artist2 = Artist(name="Artist 2", band=band)
    artist3 = Artist(name="Artist 3", band=band)

    del artist1
    Artist(
        name="Artist 2", band=band
    )  # Force it to check for weakly-referenced objects
    del artist3

    band.artists  # Force it to clean

    assert len(band.artists) == 1
    assert band.artists[0].name == artist2.name