From 7ff0db8911fbf396cb2569e3f09a43e0eec30a6d Mon Sep 17 00:00:00 2001
From: Samuel Gaist <samuel.gaist@idiap.ch>
Date: Wed, 22 Apr 2020 12:14:21 +0200
Subject: [PATCH] [algoritms][tests] Update tests for new put implementation

Also improves coverage, code related change tests were not
properly done.
---
 beat/web/algorithms/tests/tests_api.py | 73 ++++++++++++++++++--------
 1 file changed, 52 insertions(+), 21 deletions(-)

diff --git a/beat/web/algorithms/tests/tests_api.py b/beat/web/algorithms/tests/tests_api.py
index 58b92349d..cd1c829f2 100755
--- a/beat/web/algorithms/tests/tests_api.py
+++ b/beat/web/algorithms/tests/tests_api.py
@@ -1229,20 +1229,26 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
     def test_no_update_without_content(self):
         self.login_jackdoe()
         response = self.client.put(self.url)
-        self.checkResponse(response, 400)
+        self.checkResponse(response, 200, content_type="application/json")
 
     def test_successfull_update(self):
         self.login_jackdoe()
 
+        code = b"""import numpy as np"""
+
         response = self.client.put(
             self.url,
             json.dumps(
-                {"description": "blah", "declaration": AlgorithmsAPIBase.UPDATE}
+                {
+                    "description": "blah",
+                    "declaration": AlgorithmsAPIBase.UPDATE,
+                    "code": code,
+                }
             ),
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1252,7 +1258,10 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
         storage = beat.core.algorithm.Storage(settings.PREFIX, algorithm.fullname())
         storage.language = "python"
         self.assertTrue(storage.exists())
-        self.assertEqual(storage.json.load(), AlgorithmsAPIBase.UPDATE)
+        self.assertEqual(
+            json.loads(storage.json.load()), json.loads(AlgorithmsAPIBase.UPDATE)
+        )
+        self.assertEqual(storage.code.load(), code)
 
     def test_successfull_update_description_only(self):
         self.login_jackdoe()
@@ -1263,14 +1272,14 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
         )
         self.assertEqual(algorithm.description, b"blah")
 
-    def test_successfull_update_code_only(self):
+    def test_successfull_declaration_only(self):
         self.login_jackdoe()
 
         response = self.client.put(
@@ -1279,7 +1288,29 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
+
+        algorithm = Algorithm.objects.get(
+            author__username="jackdoe", name="personal", version=1
+        )
+
+        storage = beat.core.algorithm.Storage(settings.PREFIX, algorithm.fullname())
+        storage.language = "python"
+        self.assertTrue(storage.exists())
+        self.assertEqual(
+            json.loads(storage.json.load()), json.loads(AlgorithmsAPIBase.UPDATE)
+        )
+
+    def test_successfull_update_code_only(self):
+        self.login_jackdoe()
+
+        code = b"""import pandas"""
+
+        response = self.client.put(
+            self.url, json.dumps({"code": code}), content_type="application/json"
+        )
+
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1288,7 +1319,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
         storage = beat.core.algorithm.Storage(settings.PREFIX, algorithm.fullname())
         storage.language = "python"
         self.assertTrue(storage.exists())
-        self.assertEqual(storage.json.load(), AlgorithmsAPIBase.UPDATE)
+        self.assertEqual(storage.code.load(), code)
 
     def test_successfull_update_change_input_name(self):
         declaration = """{
@@ -1317,7 +1348,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1369,7 +1400,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1407,7 +1438,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
   "parameters": {
   }
 }"""
-        code = """class Algorithm:
+        code = b"""class Algorithm:
 
     def process(self, inputs, outputs):
         return True
@@ -1422,7 +1453,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1474,7 +1505,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1522,7 +1553,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1572,7 +1603,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1616,7 +1647,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1695,7 +1726,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         response = self.client.put(
             self.url,
@@ -1703,7 +1734,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1751,7 +1782,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
@@ -1827,7 +1858,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         response = self.client.put(
             self.url,
@@ -1835,7 +1866,7 @@ class AlgorithmUpdate(AlgorithmsAPIBase):
             content_type="application/json",
         )
 
-        self.checkResponse(response, 204)
+        self.checkResponse(response, 200, content_type="application/json")
 
         algorithm = Algorithm.objects.get(
             author__username="jackdoe", name="personal", version=1
-- 
GitLab