source: main/trunk/openPLM/plmapp/tests/navigate.py @ 835

Revision 835, 12.7 KB checked in by pcosquer, 10 years ago (diff)

tests: navigate

Line 
1import lxml.html
2
3from django.contrib.auth.models import User
4
5from openPLM.plmapp.navigate import NavigationGraph
6from openPLM.plmapp.controllers import PartController, DocumentController,\
7        UserController, GroupController
8
9from openPLM.plmapp.tests.base import BaseTestCase
10
11
12class NavigateTestCase(BaseTestCase):
13    TYPE = "Part"
14    CONTROLLER = PartController
15    DATA = {}
16    REFERENCE = "Part1"
17
18    def setUp(self):
19        super(NavigateTestCase, self).setUp()
20        self.controller = self.CONTROLLER.create(self.REFERENCE, self.TYPE, "a",
21                    self.user, self.DATA, True, True)
22        self.user.first_name = "robert"
23        self.user.last_name = "oooo"
24        self.user.save()
25        self.root = self.json = None
26
27    def get_graph_data(self, options):
28        graph = NavigationGraph(self.controller)
29        graph.set_options(options)
30        graph.create_edges()
31        html, json = graph.render()
32        root = lxml.html.fragment_fromstring("<div>%s</div>" % html)
33        self.assertNodeIsMain(root)
34        self.root = root
35        self.json = json
36        self.nodes = root.find_class("node")
37        self.edges = root.find_class("edge")
38
39    def assertNodeIsMain(self, root):
40        """
41        Asserts the first div of the root is the main node.
42        """
43        main = root.find_class("main_node")
44        self.assertEqual(1, len(main))
45        self.assertEqual(root.getchildren()[0], main[0])
46
47    def assertCount(self, nb_nodes, nb_edges):
48        """
49        Asserts the graph contains *nb_nodes* nodes and *nb_edges* edges.
50        """
51        self.assertEqual(nb_nodes, len(self.nodes))
52        self.assertEqual(nb_edges, len(self.edges))
53        self.assertEqual(nb_edges, len(self.json["edges"]))
54        self.assertEqual(nb_edges, len(self.json["arrows"]))
55
56    def test_navigate_empty(self):
57        """ Tests that a graph with all options set to false contains only
58        the main node."""
59        self.get_graph_data({})
60        self.assertCount(1, 0)
61
62class PLMObjectNavigateTestCase(NavigateTestCase):
63    """
64    Tests common to part and document.
65    """
66
67    def test_navigate_owner(self):
68        """
69        Tests a navigate with the "owner" option set.
70        """
71        self.get_graph_data({"owner" : True })
72        self.assertCount(2, 1)
73        main, owner = self.nodes
74        self.assertEqual("owner", self.edges[0].text_content())
75        text = owner.text_content().strip()
76        self.assertTrue(self.user.first_name in text)
77        self.assertTrue(self.user.last_name in text)
78       
79    def test_navigate_signer(self):
80        """
81        Tests a navigate with the "signer" option set.
82        """
83        self.get_graph_data({"signer" : True })
84        self.assertCount(3, 2)
85        main, signer1, signer2 = self.nodes
86        for edge in self.edges:
87            self.assertTrue("sign" in edge.text_content())
88        for node in (signer1, signer2):
89            text = node.text_content().strip()
90            self.assertTrue(self.user.first_name in text)
91            self.assertTrue(self.user.last_name in text)
92        self.assertNotEqual(signer1.attrib["id"], signer2.attrib["id"])
93
94    def test_navigate_notified(self):
95        """
96        Tests a navigate with the "notified" option set.
97        """
98        self.controller.set_role(self.cie, "notified")
99        self.get_graph_data({"notified" : True })
100        self.assertCount(2, 1)
101        main, owner = self.nodes
102        self.assertEqual("notified", self.edges[0].text_content())
103        text = owner.text_content().strip()
104        self.assertTrue(self.cie.username in text)
105       
106
107class PartNavigateTestCase(NavigateTestCase):
108
109    def test_navigate_children(self):
110        """
111        Tests a navigate with the "child" option set.
112        """
113        data = self.DATA.copy()
114        data["name"] = "Coffee"
115        child1 = PartController.create("c1", "Part", "k",
116                self.user, data, True, True)
117        self.controller.add_child(child1, 15, 789, "kg")
118        self.get_graph_data({"child" : True })
119        self.assertCount(2, 1)
120        main, child_node = self.nodes
121        # check the edge
122        self.assertTrue("789" in self.edges[0].text_content())
123        self.assertTrue("15" in self.edges[0].text_content())
124        self.assertTrue("kg" in self.edges[0].text_content())
125        text = child_node.text_content().strip()
126        self.assertTrue(data["name"] in text)
127        # add another child to child1
128        child2 = PartController.create("c2", "Part", "k",
129                self.user, data, True, True)
130        child1.add_child(child2, 15, 789, "kg")
131        self.get_graph_data({"child" : True })
132        self.assertCount(3, 2)
133        # add child2 to the controller
134        # we should have 3 nodes (controller, child1, child2)
135        # and 3 edges (controller -> child1, controller -> child2 and
136        # child1 -> child2)
137        self.controller.add_child(child2, 15, 789, "kg")
138        self.get_graph_data({"child" : True })
139        self.assertCount(3, 3)
140
141    def test_navigate_parents(self):
142        """
143        Tests a navigate with the "parents" option set.
144        """
145        data = self.DATA.copy()
146        data["name"] = "Coffee"
147        parent1 = PartController.create("c1", "Part", "k",
148                self.user, data, True, True)
149        parent1.add_child(self.controller, 15, 789, "kg")
150        self.get_graph_data({"parents" : True })
151        self.assertCount(2, 1)
152        main, parent_node = self.nodes
153        self.assertTrue("789" in self.edges[0].text_content())
154        self.assertTrue("15" in self.edges[0].text_content())
155        self.assertTrue("kg" in self.edges[0].text_content())
156        text = parent_node.text_content().strip()
157        self.assertTrue(data["name"] in text)
158        # add another parent to parent1
159        parent2 = PartController.create("c2", "Part", "k",
160                self.user, data, True, True)
161        parent2.add_child(parent1, 15, 789, "kg")
162        self.get_graph_data({"parents" : True })
163        self.assertCount(3, 2)
164        # add the controller to parent2
165        parent2.add_child(self.controller, 5, 79, "kg")
166        self.get_graph_data({"parents" : True })
167        self.assertCount(3, 3)
168
169    def test_navigate_documents(self):
170        """
171        Tests a navigate with the "doc" option set.
172        """
173        data = self.DATA.copy()
174        data["name"] = "Coffee"
175        doc = DocumentController.create("doc", "Document", "d",
176                self.user, data, True, True)
177        self.controller.attach_to_document(doc)
178        self.get_graph_data({"doc" : True })
179        self.assertCount(2, 1)
180        main, doc_node = self.nodes
181        text = doc_node.text_content().strip()
182        self.assertTrue(data["name"] in text)
183
184    def test_navigate_doc_parts(self):
185        """
186        Tests a navigate with the "doc_parts" option set.
187        """
188        data = self.DATA.copy()
189        data["name"] = "Coffee"
190        doc = DocumentController.create("doc", "Document", "d",
191                self.user, data, True, True)
192        self.controller.attach_to_document(doc)
193        self.get_graph_data({"doc" : False,
194            "doc_parts" : [self.controller.id] })
195        self.assertCount(2, 1)
196        main, doc_node = self.nodes
197        text = doc_node.text_content().strip()
198        self.assertTrue(data["name"] in text)
199
200    def test_navigate_documents2(self):
201        """
202        Tests a navigate with the "doc" and "child" options set, both
203        parts are attached to the same document.
204        """
205        data = self.DATA.copy()
206        data["name"] = "Coffee"
207        child1 = PartController.create("c1", "Part", "k",
208                self.user, data, True, True)
209        self.controller.add_child(child1, 4, 8, "m")
210        doc = DocumentController.create("doc", "Document", "d",
211                self.user, data, True, True)
212        self.controller.attach_to_document(doc)
213        child1.attach_to_document(doc)
214        self.get_graph_data({"doc" : True, "child" : True})
215        self.assertCount(3, 3)
216
217
218class DocumentNavigateTestCase(NavigateTestCase):
219    TYPE = "Document"
220    CONTROLLER = DocumentController
221   
222    def test_navigate_part(self):
223        """
224        Tests a navigate with the "doc" option set.
225        """
226        data = self.DATA.copy()
227        data["name"] = "Coffee"
228        part = PartController.create("part", "Part", "d",
229                self.user, data, True, True)
230        self.controller.attach_to_part(part)
231        self.get_graph_data({"part" : True})
232        self.assertCount(2, 1)
233        main, part_node = self.nodes
234        text = part_node.text_content().strip()
235        self.assertTrue(data["name"] in text)
236
237class GroupNavigateTestCase(NavigateTestCase):
238   
239    def setUp(self):
240        super(GroupNavigateTestCase, self).setUp()
241        self.controller = GroupController(self.group, self.user)
242
243    def test_navigate_owner(self):
244        """
245        Tests a navigate with the "owner" option set.
246        """
247        self.get_graph_data({"owner" : True })
248        self.assertCount(2, 1)
249        main, owner = self.nodes
250        self.assertEqual("owner", self.edges[0].text_content())
251        text = owner.text_content().strip()
252        self.assertTrue(self.user.first_name in text)
253        self.assertTrue(self.user.last_name in text)
254
255    def test_navigate_member(self):
256        """
257        Tests a navigate with the "member" option set.
258        """
259        self.get_graph_data({"user" : True })
260        self.assertCount(2, 1)
261        main, owner = self.nodes
262        self.assertEqual("member", self.edges[0].text_content())
263        text = owner.text_content().strip()
264        self.assertTrue(self.user.first_name in text)
265        self.assertTrue(self.user.last_name in text)
266
267    def test_navigate_two_members(self):
268        """
269        Tests a navigate with the "member" option set.
270        The group has two members.
271        """
272        # add another user
273        brian = User.objects.create(username="Brian", password="life")
274        brian.get_profile().is_contributor = True
275        brian.get_profile().save()
276        brian.groups.add(self.group)
277        brian.save()
278        self.get_graph_data({"user" : True })
279        self.assertCount(3, 2)
280        for edge in self.edges:
281            self.assertEqual("member", edge.text_content())
282        for node in self.nodes[1:]:
283            text = node.text_content().strip()
284            self.assertTrue(self.user.first_name in text or brian.username in text)
285
286    def test_navigate_part(self):
287        """
288        Tests a navigate with the "part" option set.
289        """
290        # we already have one part
291        self.get_graph_data({"part" : True })
292        self.assertCount(2, 1)
293        main, part_node = self.nodes
294        self.assertEqual([part_node], self.root.find_class("node_part"))
295
296    def test_navigate_doc(self):
297        """
298        Tests a navigate with the "doc" option set.
299        """
300        doc = DocumentController.create("d", "Document", "doc",
301                self.user, self.DATA, True, True)
302        self.get_graph_data({"doc" : True })
303        self.assertCount(2, 1)
304        main, doc_node = self.nodes
305        self.assertEqual([doc_node], self.root.find_class("node_document"))
306
307class UserNavigateTestCase(NavigateTestCase):
308   
309    def setUp(self):
310        super(UserNavigateTestCase, self).setUp()
311        self.part = self.controller
312        self.controller = UserController(self.user, self.user)
313
314    def test_navigate_owned(self):
315        """
316        Tests a navigate with the "owned" option set.
317        """
318        # we already own self.part
319        self.get_graph_data({"owned" : True })
320        self.assertCount(2, 1)
321
322    def test_navigate_doc_parts(self):
323        """
324        Tests a navigate with the "doc_parts" option set.
325        """
326        data = self.DATA.copy()
327        data["name"] = "Coffee"
328        # the company owns doc so that it does not appear if only
329        # the "owned" option is set
330        doc = DocumentController.create("doc", "Document", "d",
331                self.cie, data, True, True)
332        self.part.attach_to_document(doc)
333        self.get_graph_data({"owned" : True,
334            "doc_parts" : [self.part.id] })
335        self.assertTrue(3, 2)
336       
337    def test_navigate_notified(self):
338        """
339        Tests a navigate with the "request_notification_from" option set.
340        """
341        self.get_graph_data({"request_notification_from" : True })
342        self.assertCount(1, 0)
343        self.part.set_role(self.user, "notified")
344        self.get_graph_data({"request_notification_from" : True })
345        self.assertCount(2, 1)
346
347    def test_navigate_signer(self):
348        """
349        Tests a navigate with the "to_sign" option set.
350        """
351        self.get_graph_data({"to_sign" : True })
352        self.assertCount(3, 2)
353        roles = set(e.text_content().strip() for e in self.edges)
354        self.assertEqual(set(("sign1stlevel", "sign2ndlevel")), roles)
355
Note: See TracBrowser for help on using the repository browser.