6
0
ソースを参照

added a test for creation of multiple and single labels. Fixed the code for the creation of multiple labels

Dimitri Korsch 3 年 前
コミット
2d9b40ce6b
2 ファイル変更87 行追加1 行削除
  1. 8 1
      pycs/frontend/endpoints/labels/CreateLabel.py
  2. 79 0
      test/test_client.py

+ 8 - 1
pycs/frontend/endpoints/labels/CreateLabel.py

@@ -23,6 +23,7 @@ class CreateLabel(View):
             abort(400)
             abort(400)
 
 
         name = data['name']
         name = data['name']
+        reference = data.get('reference', name.lower())
         parent = data.get('parent')
         parent = data.get('parent')
 
 
         # find project
         # find project
@@ -31,7 +32,13 @@ class CreateLabel(View):
             abort(404)
             abort(404)
 
 
         # insert label
         # insert label
-        label, _ = project.create_label(name, parent_id=parent)
+        label, is_new = project.create_label(name,
+            reference=reference,
+            parent_id=parent)
+
+        if not is_new:
+            msg = f"Label creation invoked, but this label already existed: {label}"
+            abort(400, msg)
 
 
         # send notification
         # send notification
         NotificationManager.created("label", label.id, Label)
         NotificationManager.created("label", label.id, Label)

+ 79 - 0
test/test_client.py

@@ -46,6 +46,31 @@ class ClientTests(BaseTestCase):
             content_type=content_type,
             content_type=content_type,
         )
         )
 
 
+    def test_project_creation_without_label_provider(self):
+        self.assertEqual(0, Project.query.count())
+        self.assertEqual(0, Label.query.count())
+
+        self._post(
+            "/projects",
+            json=dict(
+                name="some name",
+                description="some description",
+                model=self.model_id,
+                label=None,
+                external=None,
+            )
+        )
+        self.assertEqual(1, Project.query.count())
+
+        project = Project.query.first()
+
+        self.assertIsNotNone(project)
+        self.assertIsNotNone(project.model)
+        self.assertIsNone(project.label_provider)
+
+        self.wait_for_coroutines()
+        self.assertEqual(0, Label.query.count())
+
     def test_project_creation(self):
     def test_project_creation(self):
 
 
         self.assertEqual(0, Project.query.count())
         self.assertEqual(0, Project.query.count())
@@ -216,3 +241,57 @@ class ClientTests(BaseTestCase):
         self.assertEqual(returned_result["label"]["id"], result.label.id)
         self.assertEqual(returned_result["label"]["id"], result.label.id)
         self.assertEqual(returned_result["label"]["name"], result.label.name)
         self.assertEqual(returned_result["label"]["name"], result.label.name)
 
 
+    def test_single_label_creation(self):
+        self._post(
+            "/projects",
+            json=dict(
+                name="some project",
+                description="project description",
+                model=self.model_id,
+                label=None,
+                external=None,
+            )
+        )
+
+        project = Project.query.first()
+        project_id = project.id
+        self.assertIsNotNone(project)
+
+        self.assertEqual(0, Label.query.filter(Label.project_id == project_id).count())
+        self._post(
+            f"/projects/{project_id}/labels",
+            json=dict(
+                name=f"Label 1",
+            )
+        )
+        self.assertEqual(1, Label.query.filter(Label.project_id == project_id).count())
+
+
+    def test_multiple_label_creation(self):
+        self._post(
+            "/projects",
+            json=dict(
+                name="some project",
+                description="project description",
+                model=self.model_id,
+                label=None,
+                external=None,
+            )
+        )
+
+        project = Project.query.first()
+        project_id = project.id
+        self.assertIsNotNone(project)
+
+        self.assertEqual(0, Label.query.filter(Label.project_id == project_id).count())
+
+        for i in range(1, 11):
+
+            self._post(
+                f"/projects/{project_id}/labels",
+                json=dict(
+                    name=f"Label {i}",
+                )
+            )
+
+            self.assertEqual(i, Label.query.filter(Label.project_id == project_id).count())