OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 | |
5 """Test cases for Twisted Model-View-Controller architecture.""" | |
6 | |
7 import random | |
8 | |
9 try: | |
10 import cPickle as pickle | |
11 except ImportError: | |
12 import pickle | |
13 | |
14 from twisted.trial import unittest | |
15 | |
16 from twisted.web.woven import model, view, controller, interfaces | |
17 from twisted.python import components | |
18 | |
19 # simple pickled string storage to test persistence | |
20 persisted_model = "" | |
21 | |
22 class MyModel(model.Model): | |
23 def __init__(self, foo, random=None): | |
24 # I hate having to explicitly initialize the super | |
25 model.Model.__init__(self) | |
26 self.foo=foo | |
27 self.random=random | |
28 | |
29 class MyView(view.View): | |
30 def __init__(self, model, *args, **kwargs): | |
31 view.View.__init__(self, model, *args, **kwargs) | |
32 self.model.addView(self) | |
33 # pretend self.foo is what the user now sees on their screen | |
34 self.foo = self.model.foo | |
35 self.random = self.model.random | |
36 self.controller = interfaces.IController(self.model, None) | |
37 | |
38 def modelChanged(self, changed): | |
39 if changed.has_key('foo'): | |
40 self.foo = changed['foo'] | |
41 if changed.has_key('random'): | |
42 self.random = changed['random'] | |
43 | |
44 def twiddleControl(self, newValue): | |
45 """ | |
46 The user twiddled a control onscreen, causing this event | |
47 """ | |
48 self.controller.setFoo(newValue) | |
49 | |
50 def pushButton(self): | |
51 """ | |
52 The user hit a button onscreen, causing this event | |
53 """ | |
54 return self.controller.doRandom() | |
55 | |
56 # Register MyView as the view for instances of type MyModel | |
57 components.registerAdapter(MyView, MyModel, interfaces.IView) | |
58 | |
59 class MyController(controller.Controller): | |
60 def setFoo(self, newValue): | |
61 self.model.foo = newValue | |
62 self.model.notify({'foo': newValue}) | |
63 self.persist() | |
64 | |
65 def doRandom(self): | |
66 rnd = random.choice(range(100)) | |
67 self.model.random = rnd | |
68 self.model.notify({'random': rnd}) | |
69 self.persist() | |
70 return rnd | |
71 | |
72 def persist(self): | |
73 """ | |
74 Save the model object to persistent storage | |
75 """ | |
76 global persisted_model | |
77 | |
78 persisted_model = pickle.dumps(self.model) | |
79 | |
80 # Register MyController as the controller for instances of type MyModel | |
81 components.registerAdapter(MyController, MyModel, interfaces.IController) | |
82 | |
83 class MVCTestCase(unittest.TestCase): | |
84 """Test MVC.""" | |
85 def setUp(self): | |
86 self.model = MyModel("foo") | |
87 | |
88 def getView(self): | |
89 return interfaces.IView(self.model, None) | |
90 | |
91 def testViewConstruction(self): | |
92 view = self.getView() | |
93 self.assert_(isinstance(view, MyView)) | |
94 | |
95 def testControllerConstruction(self): | |
96 view = self.getView() | |
97 self.assert_(isinstance(view.controller, MyController)) | |
98 | |
99 def testModelManipulation(self): | |
100 view = self.getView() | |
101 view.twiddleControl("bar") | |
102 self.assertEquals("bar", self.model.foo) | |
103 | |
104 def testMoreModelManipulation(self): | |
105 view = self.getView() | |
106 value = view.pushButton() | |
107 self.assertEquals(value, self.model.random) | |
108 | |
109 def testViewManipulation(self): | |
110 """When the model updates the view should too""" | |
111 view = self.getView() | |
112 view.twiddleControl("bar") | |
113 self.assertEquals("bar", view.foo) | |
114 | |
115 def testMoreViewManipulation(self): | |
116 """When the model updates the view should too""" | |
117 view = self.getView() | |
118 value = view.pushButton() | |
119 self.assertEquals(value, view.random) | |
120 | |
121 | |
122 testCases = [MVCTestCase] | |
OLD | NEW |