-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathflyweight.py
37 lines (23 loc) · 834 Bytes
/
flyweight.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
# -*- coding: utf-8 -*-
import unittest
from reobject.models import Model, Field
class Card(Model):
suit = Field()
color = Field()
class TestCard(unittest.TestCase):
def test_flyweight(self):
c1, _ = Card.objects.get_or_create(suit='K', color='♥')
c2, _ = Card.objects.get_or_create(suit='K', color='♥')
c3, _ = Card.objects.get_or_create(suit='A', color='♠')
self.assertEqual(c1, c2)
self.assertNotEqual(c1, c3)
self.assertIs(c1, c2)
self.assertIsNot(c1, c3)
self.assertEqual(Card.objects.count(), 2)
c1.delete()
self.assertEqual(Card.objects.count(), 1)
self.assertNotIn(c2, Card.objects.all())
c3.delete()
self.assertEqual(Card.objects.count(), 0)
if __name__ == '__main__':
unittest.main()