source: main/branches/3D/openPLM/plmapp/tests/ajax.py @ 662

Revision 662, 9.1 KB checked in by pcosquer, 9 years ago (diff)

3D branch: merge changes from trunk (rev [661])

Line 
1from django.utils.simplejson import JSONDecoder
2from django.core.files.base import ContentFile
3
4from openPLM.plmapp.tests.views import CommonViewTest
5from openPLM.plmapp.controllers.document import DocumentController
6
7
8class AjaxTestCase(CommonViewTest):
9
10
11    def get(self, url, **get):
12        return JSONDecoder().decode(self.client.get(url, get).content)
13
14    def post(self, url, **post):
15        return JSONDecoder().decode(self.client.post(url, post).content)
16
17    def test_search_form(self):
18        for t in ("Part", "Document", "User", "Group"):
19            response = self.client.get("/ajax/search/", {"type" : t})
20            self.assertEqual(response.status_code, 200)
21
22    def test_auto_complete_part(self):
23        self.create("Other")
24        completions = self.get("/ajax/complete/Part/reference/", term="Pa")
25        self.assertEquals(["Part1"], completions)
26
27    def test_auto_complete_part2(self):
28        self.create("Part2")
29        completions = self.get("/ajax/complete/Part/reference/", term="Pa")
30        self.assertEquals(["Part1", "Part2"], completions)
31
32    def test_auto_complete_empty(self):
33        completions = self.get("/ajax/complete/Part/reference/", term="Nothing")
34        self.assertEquals([], completions)
35
36    def test_navigate(self):
37        data = self.get("/ajax/navigate/Part/Part1/a/")
38        self.assertTrue("img" in data)
39        self.assertTrue(int(data["width"] > 0))
40        self.assertTrue(int(data["height"] > 0))
41        left = int(data["left"])
42        top = int(data["top"])
43        self.assertTrue("form" in data)
44        self.assertTrue("divs" in data)
45        # load the image
46        image = self.client.get(data["img"])
47        self.assertEqual(image.status_code, 200)
48
49    def test_can_add_child_ok(self):
50        p2 = self.create("part2")
51        data = self.get("/ajax/can_add_child/%d/" % self.controller.id,
52                type=p2.type, reference=p2.reference, revision=p2.revision)
53        self.assertTrue(data["can_add"])
54
55    def test_can_add_child_existing_child(self):
56        p2 = self.create("part2")
57        self.controller.add_child(p2, 10, 10)
58        data = self.get("/ajax/can_add_child/%d/" % self.controller.id,
59                type=p2.type, reference=p2.reference, revision=p2.revision)
60        self.assertFalse(data["can_add"])
61
62    def test_can_add_child_parent(self):
63        p2 = self.create("part2")
64        p2.add_child(self.controller, 10, 10)
65        data = self.get("/ajax/can_add_child/%d/" % self.controller.id,
66                type=p2.type, reference=p2.reference, revision=p2.revision)
67        self.assertFalse(data["can_add"])
68
69    def test_can_add_child_self(self):
70        p2 = self.controller
71        data = self.get("/ajax/can_add_child/%d/" % self.controller.id,
72                type=p2.type, reference=p2.reference, revision=p2.revision)
73        self.assertFalse(data["can_add"])
74
75    def test_can_add_child_document(self):
76        doc = DocumentController.create("Doc", "Document", "a", self.user,
77                self.DATA)
78        data = self.get("/ajax/can_add_child/%d/" % self.controller.id,
79                type=doc.type, reference=doc.reference, revision=doc.revision)
80        self.assertFalse(data["can_add"])
81
82    def test_add_child_get(self):
83        p2 = self.create("part2")
84        data = self.get("/ajax/add_child/%d/" % self.controller.id,
85                type=p2.type, reference=p2.reference, revision=p2.revision)
86        parent = data["parent"]
87        self.assertEqual(self.controller.id, parent["id"])
88        self.assertEqual(self.controller.type, parent["type"])
89        self.assertEqual(self.controller.reference, parent["reference"])
90        self.assertEqual(self.controller.revision, parent["revision"])
91        self.assertTrue("form" in data)
92       
93    def test_add_child_post(self):
94        p2 = self.create("part2")
95        data = self.post("/ajax/add_child/%d/" % self.controller.id,
96                type=p2.type, reference=p2.reference, revision=p2.revision,
97                quantity="10", order="10", unit="g")
98        self.assertEqual("ok", data["result"])
99
100        link = self.controller.get_children()[0].link
101        self.assertEqual(link.parent.id, self.controller.id)
102        self.assertEqual(link.child.id, p2.id)
103        self.assertEqual(link.quantity, 10)
104        self.assertEqual(link.order, 10)
105       
106    def test_add_child_post_error(self):
107        p2 = self.create("part2")
108        data = self.post("/ajax/add_child/%d/" % self.controller.id,
109                type=p2.type, reference=p2.reference, revision=p2.revision,
110                quantity="badvalue", order="10")
111        self.assertEqual("error", data["result"])
112        self.assertEqual([], self.controller.get_children())
113
114    def test_can_attach_part_doc(self):
115        doc = DocumentController.create("Doc", "Document", "a", self.user,
116                self.DATA)
117        data = self.get("/ajax/can_attach/%d/" % self.controller.id,
118                type=doc.type, reference=doc.reference, revision=doc.revision)
119        self.assertTrue(data["can_attach"])
120
121    def test_can_attach_doc_part(self):
122        doc = DocumentController.create("Doc", "Document", "a", self.user,
123                self.DATA)
124        data = self.get("/ajax/can_attach/%d/" % doc.id,
125                type=self.controller.type, reference=self.controller.reference,
126                revision=self.controller.revision)
127        self.assertTrue(data["can_attach"])
128
129    def test_can_attach_part_part(self):
130        p2 = self.create("part2")
131        data = self.post("/ajax/can_attach/%d/" % self.controller.id,
132                type=p2.type, reference=p2.reference, revision=p2.revision)
133        self.assertFalse(data["can_attach"])
134   
135    def test_can_attach_doc_doc(self):
136        doc = DocumentController.create("Doc", "Document", "a", self.user,
137                self.DATA)
138        data = self.post("/ajax/can_attach/%d/" % doc.id,
139                type=doc.type, reference=doc.reference, revision=doc.revision)
140        self.assertFalse(data["can_attach"])
141
142    def test_can_attach_attached(self):
143        doc = DocumentController.create("Doc", "Document", "a", self.user,
144                self.DATA)
145        self.controller.attach_to_document(doc)
146        data = self.get("/ajax/can_attach/%d/" % self.controller.id,
147                type=doc.type, reference=doc.reference, revision=doc.revision)
148        self.assertFalse(data["can_attach"])
149
150    def test_attach_get(self):
151        doc = DocumentController.create("Doc", "Document", "a", self.user,
152                self.DATA)
153        data = self.get("/ajax/attach/%d/" % self.controller.id,
154                type=doc.type, reference=doc.reference, revision=doc.revision)
155        plmobject = data["plmobject"]
156        self.assertEqual(self.controller.id, plmobject["id"])
157        self.assertEqual(self.controller.type, plmobject["type"])
158        self.assertEqual(self.controller.reference, plmobject["reference"])
159        self.assertEqual(self.controller.revision, plmobject["revision"])
160        self.assertTrue("form" in data)
161       
162    def test_attach_part_doc_post(self):
163        doc = DocumentController.create("Doc", "Document", "a", self.user,
164                self.DATA)
165        data = self.post("/ajax/attach/%d/" % self.controller.id,
166                type=doc.type, reference=doc.reference, revision=doc.revision)
167        self.assertEqual("ok", data["result"])
168        link = self.controller.get_attached_documents()[0]
169        self.assertEqual(doc.id, link.document.id)
170
171    def test_attach_doc_part_post(self):
172        doc = DocumentController.create("Doc", "Document", "a", self.user,
173                self.DATA)
174        data = self.post("/ajax/attach/%d/" % doc.id,
175                type=self.controller.type, reference=self.controller.reference,
176                revision=self.controller.revision)
177        self.assertEqual("ok", data["result"])
178        link = self.controller.get_attached_documents()[0]
179        self.assertEqual(doc.id, link.document.id)
180
181    def test_attach_doc_part_post_error(self):
182        doc = DocumentController.create("Doc", "Document", "a", self.user,
183                self.DATA)
184        self.controller.attach_to_document(doc)
185        data = self.post("/ajax/attach/%d/" % doc.id,
186                type=self.controller.type, reference=self.controller.reference,
187                revision=self.controller.revision)
188        self.assertEqual("error", data["result"])
189
190    def test_thumbnails(self):
191        # create a document with a file
192        doc = DocumentController.create("Doc", "Document", "a", self.user,
193                self.DATA)
194        thumbnail = ContentFile(file("datatests/thumbnail.png").read())
195        thumbnail.name = "Thumbnail.png"
196        doc.add_file(self.get_file())
197        f2 = doc.files.all()[0]
198        doc.add_thumbnail(f2, thumbnail)
199       
200        data = self.get("/ajax/thumbnails/%s/%s/%s/" % (doc.type, doc.reference,
201            doc.revision))
202        files = data["files"]
203        self.assertEqual(1, len(files))
204        # download the file and the thumbnail
205        name, url, img = files[0]
206        response_file = self.client.get(url)
207        self.assertEqual(response_file.status_code, 200)
208        response_img = self.client.get(img)
209        self.assertEqual(response_img.status_code, 200)
210
Note: See TracBrowser for help on using the repository browser.