-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathtest_model_lifecycle.py
90 lines (64 loc) · 2.61 KB
/
test_model_lifecycle.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pytest
from django_unicorn.components import UnicornView
from django_unicorn.serializer import dumps, loads
from django_unicorn.typer import _construct_model
from django_unicorn.views.utils import set_property_from_data
from example.coffee.models import Flavor
class FakeComponent(UnicornView):
flavors = Flavor.objects.none()
def __init__(self, **kwargs):
self.flavors = Flavor.objects.none()
super().__init__(**kwargs)
@pytest.mark.django_db
def test_model():
flavor = Flavor(name="first-flavor")
flavor.save()
str_data = dumps({"flavor": flavor})
data = loads(str_data)
flavor_data = data["flavor"]
actual = _construct_model(Flavor, flavor_data)
assert actual.pk == flavor.id
assert actual.name == flavor.name
assert actual.parent is None
@pytest.mark.django_db
def test_model_foreign_key():
parent = Flavor(name="parent-flavor")
parent.save()
flavor = Flavor(name="first-flavor", parent=parent)
flavor.save()
str_data = dumps({"flavor": flavor})
data = loads(str_data)
flavor_data = data["flavor"]
actual = _construct_model(Flavor, flavor_data)
assert actual.pk == flavor.id
assert actual.name == flavor.name
assert actual.parent.pk == parent.id
assert actual.parent.name == parent.name
@pytest.mark.django_db
def test_queryset():
test_component = FakeComponent(component_name="test", component_id="model_lifecycle_test_queryset")
assert test_component.flavors.count() == 0
flavor = Flavor(name="qs-first-flavor")
flavor.save()
flavors = Flavor.objects.filter(name="qs-first-flavor")
str_data = dumps({"flavors": flavors})
data = loads(str_data)
flavors_data = data["flavors"]
set_property_from_data(test_component, "flavors", flavors_data)
assert test_component.flavors.count() == 1
assert test_component.flavors[0].uuid == str(flavor.uuid)
assert test_component.flavors[0].id == flavor.id
@pytest.mark.django_db
def test_queryset_values():
test_component = FakeComponent(component_name="test", component_id="model_lifecycle_test_queryset_values")
assert test_component.flavors.count() == 0
flavor = Flavor(name="values-first-flavor")
flavor.save()
flavors = Flavor.objects.filter(name="values-first-flavor").values("uuid")
str_data = dumps({"flavors": flavors})
data = loads(str_data)
flavors_data = data["flavors"]
set_property_from_data(test_component, "flavors", flavors_data)
assert test_component.flavors.count() == 1
assert test_component.flavors[0].uuid == str(flavor.uuid)
assert test_component.flavors[0].id is None