forked from tortoise/tortoise-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_only.py
More file actions
66 lines (47 loc) · 2.54 KB
/
test_only.py
File metadata and controls
66 lines (47 loc) · 2.54 KB
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
from tests.testmodels import SourceFields, StraightFields
from tortoise.contrib import test
from tortoise.exceptions import IncompleteInstanceError
class TestOnlyStraight(test.TestCase):
async def asyncSetUp(self) -> None:
await super(TestOnlyStraight, self).asyncSetUp()
self.model = StraightFields
self.instance = await self.model.create(chars="Test")
async def test_get(self):
instance_part = await self.model.get(chars="Test").only("chars", "blip")
self.assertEqual(instance_part.chars, "Test")
with self.assertRaises(AttributeError):
_ = instance_part.nullable
async def test_filter(self):
instances = await self.model.filter(chars="Test").only("chars", "blip")
self.assertEqual(len(instances), 1)
self.assertEqual(instances[0].chars, "Test")
with self.assertRaises(AttributeError):
_ = instances[0].nullable
async def test_first(self):
instance_part = await self.model.filter(chars="Test").only("chars", "blip").first()
self.assertEqual(instance_part.chars, "Test")
with self.assertRaises(AttributeError):
_ = instance_part.nullable
async def test_save(self):
instance_part = await self.model.get(chars="Test").only("chars", "blip")
with self.assertRaisesRegex(IncompleteInstanceError, " is a partial model"):
await instance_part.save()
async def test_partial_save(self):
instance_part = await self.model.get(chars="Test").only("chars", "blip")
with self.assertRaisesRegex(IncompleteInstanceError, "Partial update not available"):
await instance_part.save(update_fields=["chars"])
async def test_partial_save_with_pk_wrong_field(self):
instance_part = await self.model.get(chars="Test").only("chars", "eyedee")
with self.assertRaisesRegex(IncompleteInstanceError, "field 'nullable' is not available"):
await instance_part.save(update_fields=["nullable"])
async def test_partial_save_with_pk(self):
instance_part = await self.model.get(chars="Test").only("chars", "eyedee")
instance_part.chars = "Test1"
await instance_part.save(update_fields=["chars"])
instance2 = await self.model.get(pk=self.instance.pk)
self.assertEqual(instance2.chars, "Test1")
class TestOnlySource(TestOnlyStraight):
async def asyncSetUp(self) -> None:
await super().asyncSetUp()
self.model = SourceFields # type: ignore
self.instance = await self.model.create(chars="Test")