-
Notifications
You must be signed in to change notification settings - Fork 8
/
facade.py
45 lines (31 loc) · 901 Bytes
/
facade.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
# -*- coding: utf-8 -*-
import time
import unittest
from reobject.models import Model, Field
# Complex Parts
class A(Model):
def run(self):
time.sleep(0.1)
return 3
class B(Model):
def run(self):
time.sleep(0.5)
return 4
class Facade(Model):
def __new__(cls, *args, **kwargs):
if Facade.objects.count():
return Facade.objects.get()
else:
return super().__new__(cls, *args, **kwargs)
def run_all(self):
qs = A.objects.all() | B.objects.all()
return [i for i in qs.map(lambda x: x.run())] # QuerySet.map is lazy
class TestFacade(unittest.TestCase):
def setUp(self):
Facade()
A() and B() and A()
def test_facade(self):
response = Facade().run_all()
self.assertEqual(sum(response), 10) # 3 + 4 + 3
if __name__ == '__main__':
unittest.main()