diff --git a/beat/web/experiments/tests.py b/beat/web/experiments/tests.py index d2ad2e43b4c8ecfb642df38195bde123773b3b92..d802fed3621cea9b88e4f7f2b9615c8c11ea074b 100755 --- a/beat/web/experiments/tests.py +++ b/beat/web/experiments/tests.py @@ -35,11 +35,6 @@ from django.conf import settings from django.contrib.auth.models import User from django.core.urlresolvers import reverse -from .models import Experiment -from .models import CachedFile -from .models import Block -from .models import Result - from ..dataformats.models import DataFormat from ..algorithms.models import Algorithm from ..toolchains.models import Toolchain @@ -51,46 +46,41 @@ from ..backend.models import Queue from ..attestations.models import Attestation from ..databases.models import Database -from ..common.testutils import BaseTestCase, tearDownModule +from ..common.testutils import BaseTestCase +from ..common.testutils import ( # noqa test runner will call it, tearDownModule + tearDownModule, +) + +from .models import Experiment +from .models import CachedFile +from .models import Block +from .models import Result + +TEST_PWD = "1234" HASHES = { - 'addition1': 'ff59a471cec5c17b45d1dfa5aff3ed897ee2d7ed87de205365b372be1c726c87', - 'addition2': '41bd0ffd85bef70ea7b1499921797e269471b423c117e261349489d956df41a4', - 'analysis' : 'ca9e22ea791bcf1cc63627c4429ae94bf644b1e7b2862e6ee70328f5661af0b6', + "addition1": "ff59a471cec5c17b45d1dfa5aff3ed897ee2d7ed87de205365b372be1c726c87", + "addition2": "41bd0ffd85bef70ea7b1499921797e269471b423c117e261349489d956df41a4", + "analysis": "ca9e22ea791bcf1cc63627c4429ae94bf644b1e7b2862e6ee70328f5661af0b6", } class ExperimentTestBase(BaseTestCase): - DECLARATION1 = { - "globals": { - }, "blocks": { "addition1": { "algorithm": "johndoe/sum/1", - "parameters": { - }, - "inputs": { - "a": "a", - "b": "b" - }, - "outputs": { - "sum": "sum" - } + "parameters": {}, + "inputs": {"a": "a", "b": "b"}, + "outputs": {"sum": "sum"}, }, "addition2": { "algorithm": "johndoe/sum/1", - "parameters": { - }, - "inputs": { - "a": "a", - "b": "b" - }, - "outputs": { - "sum": "sum" - } - } + "parameters": {}, + "inputs": {"a": "a", "b": "b"}, + "outputs": {"sum": "sum"}, + }, }, "datasets": { "dataset1": { @@ -102,36 +92,24 @@ class ExperimentTestBase(BaseTestCase): "analyzers": { "analysis": { "algorithm": "johndoe/analysis/1", - "parameters": { - }, - "inputs": { - "in": "input" - } + "parameters": {}, + "inputs": {"in": "input"}, } }, "globals": { - "environment": { - "name": "env1", - "version": "1.0" - }, - "queue": "queue1" - } + "environment": {"name": "env1", "version": "1.0"}, + "queue": "queue1", + }, } DECLARATION2 = { "blocks": { "addition1": { "algorithm": "johndoe/sum/1", - "parameters": { - }, - "inputs": { - "a": "a", - "b": "b" - }, - "outputs": { - "sum": "sum" - } - }, + "parameters": {}, + "inputs": {"a": "a", "b": "b"}, + "outputs": {"sum": "sum"}, + } }, "datasets": { "dataset1": { @@ -143,170 +121,164 @@ class ExperimentTestBase(BaseTestCase): "analyzers": { "analysis": { "algorithm": "johndoe/analysis/1", - "parameters": { - }, - "inputs": { - "in": "input" - } + "parameters": {}, + "inputs": {"in": "input"}, } }, "globals": { - "environment": { - "name": "env1", - "version": "1.0" - }, - "queue": "queue1" - } + "environment": {"name": "env1", "version": "1.0"}, + "queue": "queue1", + }, } DATABASE = { "root_folder": "/path/to/root_folder", - "protocols": [ - { - "name": "triple", - "template": "test", - "sets": [ - { - "name": "default", - "template": "set", - "view": "dummy", - "outputs": { - "output1": "johndoe/single_integer/1", - "output2": "johndoe/single_integer/1", - "output3": "johndoe/single_integer/1" - } - } - ] - } - ] + "protocols": [ + { + "name": "triple", + "template": "test", + "sets": [ + { + "name": "default", + "template": "set", + "view": "dummy", + "outputs": { + "output1": "johndoe/single_integer/1", + "output2": "johndoe/single_integer/1", + "output3": "johndoe/single_integer/1", + }, + } + ], + } + ], } def setUp(self): - for path in [settings.TOOLCHAINS_ROOT, settings.EXPERIMENTS_ROOT, - settings.DATAFORMATS_ROOT, settings.ALGORITHMS_ROOT, - settings.CACHE_ROOT]: + for path in [ + settings.TOOLCHAINS_ROOT, + settings.EXPERIMENTS_ROOT, + settings.DATAFORMATS_ROOT, + settings.ALGORITHMS_ROOT, + settings.CACHE_ROOT, + ]: if os.path.exists(path): shutil.rmtree(path) - user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234') - + user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD) # Create an environment and queue - environment = Environment(name='env1', version='1.0') + environment = Environment(name="env1", version="1.0") environment.save() environment.share() - environment2 = Environment(name='private_env', version='1.0') + environment2 = Environment(name="private_env", version="1.0") environment2.save() - queue = Queue(name='queue1', memory_limit=1024, time_limit=60, cores_per_slot=1, max_slots_per_user=10) + queue = Queue( + name="queue1", + memory_limit=1024, + time_limit=60, + cores_per_slot=1, + max_slots_per_user=10, + ) queue.save() queue.environments.add(environment) queue.environments.add(environment2) + (integer, errors) = DataFormat.objects.create_dataformat( + author=user, + name="integer", + short_description="Default integer type need for algorithm prototype", + ) + + self.assertIsNotNone(integer, msg=errors) DataFormat.objects.create_dataformat( author=user, - name='single_integer', - short_description='description', - declaration={ - "value": "int32" - }, + name="single_integer", + short_description="description", + declaration={"value": "int32"}, ) database, errors = Database.objects.create_database( - 'integers', - declaration=self.DATABASE, + "integers", declaration=self.DATABASE ) - assert database, errors + self.assertIsNotNone(database, msg=errors) database.sharing = Database.PUBLIC database.save() - (self.toolchain1, errors) = Toolchain.objects.create_toolchain(user, - 'toolchain1', 'short description 1', - declaration={ - "blocks": [ { - "name": "addition1", - "inputs": [ - "a", - "b" - ], - "outputs": [ - "sum" - ], - "synchronized_channel": "dataset1" - }, - { - "name": "addition2", - "inputs": [ - "a", - "b" - ], - "outputs": [ - "sum" - ], - "synchronized_channel": "dataset1" - } - ], - "datasets": [ { - "name": "dataset1", - "outputs": [ - "output1", - "output2", - "output3" - ] - } - ], - "connections": [ { - "from": "dataset1.output1", - "to": "addition1.a", - "channel": "dataset1" - }, - { - "from": "dataset1.output2", - "to": "addition1.b", - "channel": "dataset1" - }, - { - "from": "addition1.sum", - "to": "addition2.a", - "channel": "dataset1" - }, - { - "from": "dataset1.output3", - "to": "addition2.b", - "channel": "dataset1" - }, - { - "to": "analysis.input", - "from": "addition2.sum", - "channel": "dataset1" - } - ], - "analyzers": [ - { - "inputs": [ - "input" - ], - "synchronized_channel": "dataset1", - "name": "analysis" - } - ], - "representation": { - "connections": {}, - "blocks": {}, - "channel_colors": {}, - }, - }) - assert not errors, 'Toolchain errors: %s' % errors + (self.toolchain1, errors) = Toolchain.objects.create_toolchain( + user, + "toolchain1", + "short description 1", + declaration={ + "blocks": [ + { + "name": "addition1", + "inputs": ["a", "b"], + "outputs": ["sum"], + "synchronized_channel": "dataset1", + }, + { + "name": "addition2", + "inputs": ["a", "b"], + "outputs": ["sum"], + "synchronized_channel": "dataset1", + }, + ], + "datasets": [ + {"name": "dataset1", "outputs": ["output1", "output2", "output3"]} + ], + "connections": [ + { + "from": "dataset1.output1", + "to": "addition1.a", + "channel": "dataset1", + }, + { + "from": "dataset1.output2", + "to": "addition1.b", + "channel": "dataset1", + }, + { + "from": "addition1.sum", + "to": "addition2.a", + "channel": "dataset1", + }, + { + "from": "dataset1.output3", + "to": "addition2.b", + "channel": "dataset1", + }, + { + "to": "analysis.input", + "from": "addition2.sum", + "channel": "dataset1", + }, + ], + "analyzers": [ + { + "inputs": ["input"], + "synchronized_channel": "dataset1", + "name": "analysis", + } + ], + "representation": { + "connections": {}, + "blocks": {}, + "channel_colors": {}, + }, + }, + ) + self.assertIsNone(errors, "Toolchain errors: %s" % errors) (self.algorithm, errors) = Algorithm.objects.create_algorithm( author=user, - name='sum', - short_description='description', + name="sum", + short_description="description", declaration="""{ "language": "python", "splittable": false, @@ -324,7 +296,6 @@ class ExperimentTestBase(BaseTestCase): "parameters": { } }""", - code="""class Algorithm: def process(self, inputs, outputs): @@ -332,96 +303,86 @@ class ExperimentTestBase(BaseTestCase): data.value = inputs['a'].data.value + inputs['b'].data.value outputs['sum'].write(data) return True -""") - - assert self.algorithm, errors - - system_user = User.objects.create_user(settings.SYSTEM_ACCOUNT, 'system@test.org', '1234') - - (self.toolchain2, errors) = Toolchain.objects.create_toolchain(user, - 'toolchain2', 'short description 2', - declaration={ - "blocks": [ { - "name": "addition1", - "inputs": [ - "a", - "b" - ], - "outputs": [ - "sum" - ], - "synchronized_channel": "dataset1" - } - ], - "datasets": [ { - "name": "dataset1", - "outputs": [ - "output1", - "output2", - "output3" - ] - } - ], - "connections": [ - { - "from": "dataset1.output1", - "to": "addition1.a", - "channel": "dataset1" - }, - { - "from": "dataset1.output2", - "to": "addition1.b", - "channel": "dataset1" - }, - { - "to": "analysis.input", - "from": "addition1.sum", - "channel": "dataset1" - } - ], - "analyzers": [ - { - "inputs": [ - "input" - ], - "synchronized_channel": "dataset1", - "name": "analysis" - } - ], - "representation": { - "connections": {}, - "blocks": {}, - "channel_colors": {}, - }, - }) - assert not errors, 'Toolchain errors: %s' % errors +""", + ) - (dataformat, errors) = DataFormat.objects.create_dataformat( - author=system_user, - name='float', - short_description='description', + self.assertIsNotNone(self.algorithm, errors) + + system_user = User.objects.create_user( + settings.SYSTEM_ACCOUNT, "system@test.org", TEST_PWD + ) + + (self.toolchain2, errors) = Toolchain.objects.create_toolchain( + user, + "toolchain2", + "short description 2", declaration={ - "value": "float64" + "blocks": [ + { + "name": "addition1", + "inputs": ["a", "b"], + "outputs": ["sum"], + "synchronized_channel": "dataset1", + } + ], + "datasets": [ + {"name": "dataset1", "outputs": ["output1", "output2", "output3"]} + ], + "connections": [ + { + "from": "dataset1.output1", + "to": "addition1.a", + "channel": "dataset1", + }, + { + "from": "dataset1.output2", + "to": "addition1.b", + "channel": "dataset1", + }, + { + "to": "analysis.input", + "from": "addition1.sum", + "channel": "dataset1", + }, + ], + "analyzers": [ + { + "inputs": ["input"], + "synchronized_channel": "dataset1", + "name": "analysis", + } + ], + "representation": { + "connections": {}, + "blocks": {}, + "channel_colors": {}, + }, }, ) + self.assertIsNone(errors, "Toolchain errors: %s" % errors) + + (dataformat, errors) = DataFormat.objects.create_dataformat( + author=system_user, + name="float", + short_description="description", + declaration={"value": "float64"}, + ) - assert dataformat, errors + self.assertIsNotNone(dataformat, errors) (dataformat, errors) = DataFormat.objects.create_dataformat( author=system_user, - name='text', - short_description='description', - declaration={ - "text": "string" - }, + name="text", + short_description="description", + declaration={"text": "string"}, ) - assert dataformat, errors + self.assertIsNotNone(dataformat, errors) (self.algorithm, errors) = Algorithm.objects.create_algorithm( author=user, - name='analysis', - short_description='description', + name="analysis", + short_description="description", declaration="""{ "language": "python", "groups": [ @@ -438,269 +399,287 @@ class ExperimentTestBase(BaseTestCase): "parameters": { } }""", - code="""class Algorithm: def process(self, inputs, output): # We don't really care return True -""") - - assert self.algorithm, errors +""", + ) + self.assertIsNotNone(self.algorithm, errors) def tearDown(self): - for path in [settings.TOOLCHAINS_ROOT, settings.EXPERIMENTS_ROOT, - settings.DATAFORMATS_ROOT, settings.ALGORITHMS_ROOT, - settings.CACHE_ROOT]: + for path in [ + settings.TOOLCHAINS_ROOT, + settings.EXPERIMENTS_ROOT, + settings.DATAFORMATS_ROOT, + settings.ALGORITHMS_ROOT, + settings.CACHE_ROOT, + ]: if os.path.exists(path): shutil.rmtree(path) + def login(self, username): + self.client.login(username=username, password=TEST_PWD) -#---------------------------------------------------------- + def login_johndoe(self): + self.login("johndoe") + def login_jackdoe(self): + self.login("jackdoe") -class ExperimentCreationAPI(ExperimentTestBase): +# ---------------------------------------------------------- + + +class ExperimentCreationAPI(ExperimentTestBase): def setUp(self): super(ExperimentCreationAPI, self).setUp() - self.url = reverse('api_experiments:list_create', args=['johndoe']) - + self.url = reverse("api_experiments:list_create", args=["johndoe"]) def test_no_access_for_anonymous_user(self): response = self.client.post(self.url) self.checkResponse(response, 403) - def test_bad_request_with_invalid_content_type(self): - self.client.login(username='johndoe', password='1234') - response = self.client.post(self.url, - content_type='image/png') - self.checkResponse(response, 415, content_type='application/json') - + self.login_johndoe() + response = self.client.post(self.url, content_type="image/png") + self.checkResponse(response, 415, content_type="application/json") def test_bad_request_with_unknown_toolchain(self): - self.client.login(username='johndoe', password='1234') - - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/private/unknown', - 'declaration': ExperimentTestBase.DECLARATION1, - }), content_type='application/json') + self.login_johndoe() - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + { + "toolchain": "johndoe/private/unknown", + "declaration": ExperimentTestBase.DECLARATION1, + } + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_without_declaration(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - }), content_type='application/json') - - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps({"toolchain": "johndoe/toolchain1/1"}), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_invalid_declaration(self): - self.client.login(username='johndoe', password='1234') - - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': {}, - }), content_type='application/json') + self.login_johndoe() - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps({"toolchain": "johndoe/toolchain1/1", "declaration": {}}), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unknown_global_environment_name(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() declaration = copy.deepcopy(ExperimentTestBase.DECLARATION1) - declaration['globals']['environment']['name'] = 'unknown' - - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': declaration, - }), content_type='application/json') + declaration["globals"]["environment"]["name"] = "unknown" - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + {"toolchain": "johndoe/toolchain1/1", "declaration": declaration} + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unknown_global_environment_version(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() declaration = copy.deepcopy(ExperimentTestBase.DECLARATION1) - declaration['globals']['environment']['version'] = 'unknown' + declaration["globals"]["environment"]["version"] = "unknown" - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': declaration, - }), content_type='application/json') - - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + {"toolchain": "johndoe/toolchain1/1", "declaration": declaration} + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unknown_algorithm_environment_name(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() declaration = copy.deepcopy(ExperimentTestBase.DECLARATION1) - declaration['blocks']['addition1']['environment'] = dict( - name='unknown', - version='1', + declaration["blocks"]["addition1"]["environment"] = dict( + name="unknown", version="1" ) - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': declaration, - }), content_type='application/json') - - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + {"toolchain": "johndoe/toolchain1/1", "declaration": declaration} + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unknown_algorithm_environment_version(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() declaration = copy.deepcopy(ExperimentTestBase.DECLARATION1) - declaration['blocks']['addition1']['environment'] = dict( - name='env1', - version='unknown', + declaration["blocks"]["addition1"]["environment"] = dict( + name="env1", version="unknown" ) - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': declaration, - }), content_type='application/json') - - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + {"toolchain": "johndoe/toolchain1/1", "declaration": declaration} + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unknown_analyzer_environment_name(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() declaration = copy.deepcopy(ExperimentTestBase.DECLARATION1) - declaration['analyzers']['analysis']['environment'] = dict( - name='unknown', - version='1', + declaration["analyzers"]["analysis"]["environment"] = dict( + name="unknown", version="1" ) - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': declaration, - }), content_type='application/json') - - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + {"toolchain": "johndoe/toolchain1/1", "declaration": declaration} + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unknown_analyzer_environment_version(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() declaration = copy.deepcopy(ExperimentTestBase.DECLARATION1) - declaration['analyzers']['analysis']['environment'] = dict( - name='env1', - version='unknown', + declaration["analyzers"]["analysis"]["environment"] = dict( + name="env1", version="unknown" ) - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': declaration, - }), content_type='application/json') - - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + {"toolchain": "johndoe/toolchain1/1", "declaration": declaration} + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unusable_global_environment(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() declaration = copy.deepcopy(ExperimentTestBase.DECLARATION1) - declaration['globals']['environment'] = dict( - name='private_env', - version='1.0' - ) - - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': declaration, - }), content_type='application/json') + declaration["globals"]["environment"] = dict(name="private_env", version="1.0") - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + {"toolchain": "johndoe/toolchain1/1", "declaration": declaration} + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unusable_algorithm_environment(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() declaration = copy.deepcopy(ExperimentTestBase.DECLARATION1) - declaration['blocks']['addition1']['environment'] = dict( - name='private_env', - version='1.0', + declaration["blocks"]["addition1"]["environment"] = dict( + name="private_env", version="1.0" ) - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': declaration, - }), content_type='application/json') - - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + {"toolchain": "johndoe/toolchain1/1", "declaration": declaration} + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unusable_analyzer_environment(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() declaration = copy.deepcopy(ExperimentTestBase.DECLARATION1) - declaration['analyzers']['analysis']['environment'] = dict( - name='private_env', - version='1.0', + declaration["analyzers"]["analysis"]["environment"] = dict( + name="private_env", version="1.0" ) - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': declaration, - }), content_type='application/json') - - self.checkResponse(response, 400, content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + {"toolchain": "johndoe/toolchain1/1", "declaration": declaration} + ), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_valid_experiment(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': ExperimentTestBase.DECLARATION1, - }), content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + { + "toolchain": "johndoe/toolchain1/1", + "declaration": ExperimentTestBase.DECLARATION1, + } + ), + content_type="application/json", + ) - data = self.checkResponse(response, 201, content_type='application/json') + data = self.checkResponse(response, 201, content_type="application/json") - url = reverse('api_experiments:object', args=['johndoe', 'johndoe', 'toolchain1', 1, 'to_remove']) + url = reverse( + "api_experiments:object", + args=["johndoe", "johndoe", "toolchain1", 1, "to_remove"], + ) url = url[:-10] - self.assertTrue(data['url'].startswith(url)) - self.assertTrue(data['name'].startswith('johndoe/johndoe/toolchain1/1/')) + self.assertTrue(data["url"].startswith(url)) + self.assertTrue(data["name"].startswith("johndoe/johndoe/toolchain1/1/")) experiments = Experiment.objects.all() self.assertEqual(experiments.count(), 1) experiment = experiments[0] - self.assertEqual(experiment.author.username, 'johndoe') + self.assertEqual(experiment.author.username, "johndoe") self.assertEqual(experiment.toolchain, self.toolchain1) self.assertTrue(experiment.creation_date is not None) self.assertTrue(experiment.start_date is None) @@ -709,33 +688,37 @@ class ExperimentCreationAPI(ExperimentTestBase): self.assertEqual(experiment.blocks.count(), 3) -#---------------------------------------------------------- +# ---------------------------------------------------------- class ExperimentDeletionAPI(ExperimentTestBase): - def setUp(self): super(ExperimentDeletionAPI, self).setUp() - self.url = reverse('api_experiments:list_create', args=['johndoe', ]) + self.url = reverse("api_experiments:list_create", args=["johndoe"]) def test_valid_experiment(self): - johndoe = User.objects.get(username='johndoe') - jackdoe = User.objects.create_user('jackdoe', 'jackdoe@test.org', '1234') - janedoe = User.objects.create_user('janedoe', 'janedoe@test.org', '1234') + johndoe = User.objects.get(username="johndoe") + jackdoe = User.objects.create_user("jackdoe", "jackdoe@test.org", TEST_PWD) + janedoe = User.objects.create_user("janedoe", "janedoe@test.org", TEST_PWD) - team = Team.objects.create(name='teamdoe', owner=johndoe) + team = Team.objects.create(name="teamdoe", owner=johndoe) team.members.add(janedoe, jackdoe) - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': ExperimentTestBase.DECLARATION1, - }), content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + { + "toolchain": "johndoe/toolchain1/1", + "declaration": ExperimentTestBase.DECLARATION1, + } + ), + content_type="application/json", + ) - data = self.checkResponse(response, 201, content_type='application/json') + self.checkResponse(response, 201, content_type="application/json") experiments = Experiment.objects.all() self.assertEqual(experiments.count(), 1) @@ -747,35 +730,43 @@ class ExperimentDeletionAPI(ExperimentTestBase): experiment.end_date = datetime.now() experiment.save() - url = reverse('api_experiments:share', args=['johndoe', 'johndoe', 'toolchain1', 1, experiment.name]) - response = self.client.post(url, json.dumps({ - 'teams': [team.name] - }), content_type='application/json') + url = reverse( + "api_experiments:share", + args=["johndoe", "johndoe", "toolchain1", 1, experiment.name], + ) + response = self.client.post( + url, json.dumps({"teams": [team.name]}), content_type="application/json" + ) - data = self.checkResponse(response, 200, content_type='application/json') + self.checkResponse(response, 200, content_type="application/json") experiment.refresh_from_db() self.assertTrue(experiment.modifiable()) self.assertTrue(experiment.deletable()) - url = reverse('api_experiments:object', args=['johndoe', 'johndoe', 'toolchain1', 1, experiment.name]) + url = reverse( + "api_experiments:object", + args=["johndoe", "johndoe", "toolchain1", 1, experiment.name], + ) response = self.client.delete(url) - data = self.checkResponse(response, 204) - + self.checkResponse(response, 204) def test_attested_experiment(self): - johndoe = User.objects.get(username='johndoe') + self.login_johndoe() - self.client.login(username='johndoe', password='1234') - - response = self.client.post(self.url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': ExperimentTestBase.DECLARATION1, - }), content_type='application/json') + response = self.client.post( + self.url, + json.dumps( + { + "toolchain": "johndoe/toolchain1/1", + "declaration": ExperimentTestBase.DECLARATION1, + } + ), + content_type="application/json", + ) - self.checkResponse(response, 201, content_type='application/json') + self.checkResponse(response, 201, content_type="application/json") experiments = Experiment.objects.all() self.assertEqual(experiments.count(), 1) @@ -786,13 +777,14 @@ class ExperimentDeletionAPI(ExperimentTestBase): experiment.end_date = datetime.now() experiment.save() - url = reverse('api_attestations:create') - response = self.client.post(url, - json.dumps({ - 'experiment': experiment.fullname(), - }), content_type='application/json') + url = reverse("api_attestations:create") + response = self.client.post( + url, + json.dumps({"experiment": experiment.fullname()}), + content_type="application/json", + ) - self.checkResponse(response, 201, content_type='application/json') + self.checkResponse(response, 201, content_type="application/json") # Must refresh the hard way, the attestation attribute is not part # of refresh_from_db @@ -801,166 +793,184 @@ class ExperimentDeletionAPI(ExperimentTestBase): self.assertFalse(experiment.modifiable()) self.assertFalse(experiment.deletable()) - url = reverse('api_experiments:object', args=['johndoe', 'johndoe', 'toolchain1', 1, experiment.name]) + url = reverse( + "api_experiments:object", + args=["johndoe", "johndoe", "toolchain1", 1, experiment.name], + ) response = self.client.delete(url) - data = self.checkResponse(response, 403) + self.checkResponse(response, 403) -#---------------------------------------------------------- +# ---------------------------------------------------------- -class ExperimentModificationAPI(ExperimentTestBase): +class ExperimentModificationAPI(ExperimentTestBase): def setUp(self): super(ExperimentModificationAPI, self).setUp() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - url = reverse('api_experiments:list_create', args=['johndoe']) - response = self.client.post(url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': ExperimentTestBase.DECLARATION1, - 'name': 'pending', - }), content_type='application/json') + url = reverse("api_experiments:list_create", args=["johndoe"]) + response = self.client.post( + url, + json.dumps( + { + "toolchain": "johndoe/toolchain1/1", + "declaration": ExperimentTestBase.DECLARATION1, + "name": "pending", + } + ), + content_type="application/json", + ) - self.checkResponse(response, 201, content_type='application/json') + self.checkResponse(response, 201, content_type="application/json") self.client.logout() - self.url = reverse('api_experiments:object', args=['johndoe', 'johndoe', 'toolchain1', 1, 'pending']) + self.url = reverse( + "api_experiments:object", + args=["johndoe", "johndoe", "toolchain1", 1, "pending"], + ) def test_no_access_for_anonymous_user(self): response = self.client.put(self.url) self.checkResponse(response, 403) - def test_no_access_for_other_user(self): - self.client.login(username='jackdoe', password='1234') + self.login_jackdoe() response = self.client.put(self.url) self.checkResponse(response, 403) - def test_bad_request_with_invalid_content_type(self): - self.client.login(username='johndoe', password='1234') - response = self.client.put(self.url, - content_type='image/png') - self.checkResponse(response, 400, content_type='application/json') - + self.login_johndoe() + response = self.client.put(self.url, content_type="image/png") + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_unknown_toolchain(self): - self.client.login(username='johndoe', password='1234') - - response = self.client.put(self.url, - json.dumps({ - 'toolchain': 'johndoe/private/unknown', - }), content_type='application/json') + self.login_johndoe() - self.checkResponse(response, 400, content_type='application/json') + response = self.client.put( + self.url, + json.dumps({"toolchain": "johndoe/private/unknown"}), + content_type="application/json", + ) + self.checkResponse(response, 400, content_type="application/json") def test_bad_request_with_invalid_declaration(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - response = self.client.put(self.url, - json.dumps({ - 'declaration': {}, - }), content_type='application/json') - - self.checkResponse(response, 400, content_type='application/json') + response = self.client.put( + self.url, json.dumps({"declaration": {}}), content_type="application/json" + ) + self.checkResponse(response, 400, content_type="application/json") def test_name_modification(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - response = self.client.put(self.url, - json.dumps({ - 'name': 'changed', - }), content_type='application/json') + response = self.client.put( + self.url, json.dumps({"name": "changed"}), content_type="application/json" + ) - data = self.checkResponse(response, 200, content_type='application/json') + data = self.checkResponse(response, 200, content_type="application/json") - url = reverse('api_experiments:object', args=['johndoe', 'johndoe', 'toolchain1', 1, 'changed']) - self.assertTrue(data['url'].endswith(url)) - self.assertEqual(data['name'], 'johndoe/johndoe/toolchain1/1/changed') + url = reverse( + "api_experiments:object", + args=["johndoe", "johndoe", "toolchain1", 1, "changed"], + ) + self.assertTrue(data["url"].endswith(url)) + self.assertEqual(data["name"], "johndoe/johndoe/toolchain1/1/changed") experiments = Experiment.objects.all() self.assertEqual(experiments.count(), 1) experiment = experiments[0] - self.assertEqual(experiment.name, 'changed') - + self.assertEqual(experiment.name, "changed") def test_description_modification(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - response = self.client.put(self.url, - json.dumps({ - 'short_description': 'short description', - 'description': 'long description', - }), content_type='application/json') + response = self.client.put( + self.url, + json.dumps( + { + "short_description": "short description", + "description": "long description", + } + ), + content_type="application/json", + ) - self.checkResponse(response, 200, content_type='application/json') + self.checkResponse(response, 200, content_type="application/json") experiments = Experiment.objects.all() self.assertEqual(experiments.count(), 1) experiment = experiments[0] - self.assertEqual(experiment.short_description, 'short description') - self.assertEqual(experiment.description, b'long description') + self.assertEqual(experiment.short_description, "short description") + self.assertEqual(experiment.description, b"long description") -#---------------------------------------------------------- +# ---------------------------------------------------------- class ExperimentStartingAPI(ExperimentTestBase): - def setUp(self): super(ExperimentStartingAPI, self).setUp() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - url = reverse('api_experiments:list_create', args=['johndoe']) - response = self.client.post(url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': ExperimentTestBase.DECLARATION1, - 'name': 'pending', - }), content_type='application/json') + url = reverse("api_experiments:list_create", args=["johndoe"]) + response = self.client.post( + url, + json.dumps( + { + "toolchain": "johndoe/toolchain1/1", + "declaration": ExperimentTestBase.DECLARATION1, + "name": "pending", + } + ), + content_type="application/json", + ) - self.checkResponse(response, 201, content_type='application/json') + self.checkResponse(response, 201, content_type="application/json") self.client.logout() - self.url = reverse('api_experiments:start', args=['johndoe', 'johndoe', 'toolchain1', 1, 'pending']) - + self.url = reverse( + "api_experiments:start", + args=["johndoe", "johndoe", "toolchain1", 1, "pending"], + ) def test_no_access_for_anonymous_user(self): response = self.client.post(self.url) self.checkResponse(response, 403) - def test_no_access_for_other_user(self): - self.client.login(username='jackdoe', password='1234') + self.login_jackdoe() response = self.client.post(self.url) self.checkResponse(response, 403) - def test_start_experiment(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() response = self.client.post(self.url) - data = self.checkResponse(response, 200, content_type='application/json') + data = self.checkResponse(response, 200, content_type="application/json") - url = reverse('api_experiments:object', args=['johndoe', 'johndoe', 'toolchain1', 1, 'pending']) - self.assertTrue(data['url'].endswith(url)) - self.assertEqual(data['name'], 'johndoe/johndoe/toolchain1/1/pending') + url = reverse( + "api_experiments:object", + args=["johndoe", "johndoe", "toolchain1", 1, "pending"], + ) + self.assertTrue(data["url"].endswith(url)) + self.assertEqual(data["name"], "johndoe/johndoe/toolchain1/1/pending") experiments = Experiment.objects.all() self.assertEqual(experiments.count(), 1) experiment = experiments[0] - self.assertEqual(experiment.author.username, 'johndoe') + self.assertEqual(experiment.author.username, "johndoe") self.assertEqual(experiment.toolchain, self.toolchain1) self.assertTrue(experiment.creation_date is not None) self.assertTrue(experiment.start_date is None) @@ -969,7 +979,7 @@ class ExperimentStartingAPI(ExperimentTestBase): self.assertEqual(experiment.blocks.count(), 3) - block = experiment.blocks.get(name='addition1') + block = experiment.blocks.get(name="addition1") self.assertEqual(Block.PENDING, block.status) self.assertFalse(block.analyzer) self.assertEqual(0, block.results.count()) @@ -980,7 +990,7 @@ class ExperimentStartingAPI(ExperimentTestBase): cached_file = hashes[0] self.assertEqual(CachedFile.NOT_CACHED, cached_file.status) - block = experiment.blocks.get(name='addition2') + block = experiment.blocks.get(name="addition2") self.assertEqual(Block.PENDING, block.status) self.assertFalse(block.analyzer) self.assertEqual(0, block.results.count()) @@ -991,7 +1001,7 @@ class ExperimentStartingAPI(ExperimentTestBase): cached_file = hashes[0] self.assertEqual(CachedFile.NOT_CACHED, cached_file.status) - block = experiment.blocks.get(name='analysis') + block = experiment.blocks.get(name="analysis") self.assertEqual(Block.PENDING, block.status) self.assertTrue(block.analyzer) self.assertEqual(0, block.results.count()) @@ -1002,13 +1012,12 @@ class ExperimentStartingAPI(ExperimentTestBase): cached_file = hashes[0] self.assertEqual(CachedFile.NOT_CACHED, cached_file.status) - def test_start_team_shared_experiment(self): - johndoe = User.objects.get(username='johndoe') - jackdoe = User.objects.create_user('jackdoe', 'jackdoe@test.org', '1234') - janedoe = User.objects.create_user('janedoe', 'janedoe@test.org', '1234') + johndoe = User.objects.get(username="johndoe") + jackdoe = User.objects.create_user("jackdoe", "jackdoe@test.org", TEST_PWD) + janedoe = User.objects.create_user("janedoe", "janedoe@test.org", TEST_PWD) - team = Team.objects.create(name='teamdoe', owner=johndoe) + team = Team.objects.create(name="teamdoe", owner=johndoe) team.members.add(janedoe, jackdoe) experiments = Experiment.objects.all() @@ -1018,27 +1027,29 @@ class ExperimentStartingAPI(ExperimentTestBase): experiment.end_date = datetime.now() experiment.save() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - url = reverse('api_experiments:share', args=['johndoe', 'johndoe', 'toolchain1', 1, experiment.name]) - response = self.client.post(url, json.dumps({ - 'teams': [team.name] - }), content_type='application/json') + url = reverse( + "api_experiments:share", + args=["johndoe", "johndoe", "toolchain1", 1, experiment.name], + ) + response = self.client.post( + url, json.dumps({"teams": [team.name]}), content_type="application/json" + ) - data = self.checkResponse(response, 200, content_type='application/json') + self.checkResponse(response, 200, content_type="application/json") self.client.logout() - self.client.login(username='jackdoe', password='1234') + self.login_jackdoe() response = self.client.post(self.url) - data = self.checkResponse(response, 200, content_type='application/json') - + self.checkResponse(response, 200, content_type="application/json") def test_start_user_shared_experiment(self): - johndoe = User.objects.get(username='johndoe') - janedoe = User.objects.create_user('janedoe', 'janedoe@test.org', '1234') + User.objects.get(username="johndoe") + janedoe = User.objects.create_user("janedoe", "janedoe@test.org", TEST_PWD) experiments = Experiment.objects.all() self.assertEqual(experiments.count(), 1) @@ -1047,148 +1058,164 @@ class ExperimentStartingAPI(ExperimentTestBase): experiment.end_date = datetime.now() experiment.save() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - url = reverse('api_experiments:share', args=['johndoe', 'johndoe', 'toolchain1', 1, experiment.name]) - response = self.client.post(url, json.dumps({ - 'users': [janedoe.username] - }), content_type='application/json') + url = reverse( + "api_experiments:share", + args=["johndoe", "johndoe", "toolchain1", 1, experiment.name], + ) + response = self.client.post( + url, + json.dumps({"users": [janedoe.username]}), + content_type="application/json", + ) - data = self.checkResponse(response, 200, content_type='application/json') + self.checkResponse(response, 200, content_type="application/json") self.client.logout() - self.client.login(username='janedoe', password='1234') + self.login("janedoe") response = self.client.post(self.url) - data = self.checkResponse(response, 200, content_type='application/json') - + self.checkResponse(response, 200, content_type="application/json") def test_start_experiment_with_private_database(self): - database = Database.objects.get(name='integers') + database = Database.objects.get(name="integers") database.sharing = Database.PRIVATE database.save() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() response = self.client.post(self.url) - self.checkResponse(response, 403, content_type='application/json') + self.checkResponse(response, 403, content_type="application/json") -#---------------------------------------------------------- +# ---------------------------------------------------------- class ResultsAPI(ExperimentTestBase): - def setUp(self): super(ResultsAPI, self).setUp() - User.objects.create_user('scheduler', 'scheduler@test.org', '1234') + User.objects.create_user("scheduler", "scheduler@test.org", TEST_PWD) - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - url = reverse('api_experiments:list_create', args=['johndoe']) - response = self.client.post(url, - json.dumps({ - 'toolchain': 'johndoe/toolchain1/1', - 'declaration': ExperimentTestBase.DECLARATION1, - 'name': 'experiment1', - }), content_type='application/json') + url = reverse("api_experiments:list_create", args=["johndoe"]) + response = self.client.post( + url, + json.dumps( + { + "toolchain": "johndoe/toolchain1/1", + "declaration": ExperimentTestBase.DECLARATION1, + "name": "experiment1", + } + ), + content_type="application/json", + ) - self.checkResponse(response, 201, content_type='application/json') + self.checkResponse(response, 201, content_type="application/json") - url = reverse('api_experiments:start', args=['johndoe', 'toolchain1', 1, 'experiment1']) + url = reverse( + "api_experiments:start", args=["johndoe", "toolchain1", 1, "experiment1"] + ) response = self.client.post(url) - self.checkResponse(response, 200, content_type='application/json') + self.checkResponse(response, 200, content_type="application/json") - url = reverse('api_experiments:list_create', args=['johndoe']) - response = self.client.post(url, - json.dumps({ - 'toolchain': 'johndoe/toolchain2/1', - 'declaration': ExperimentTestBase.DECLARATION2, - 'name': 'experiment2', - }), content_type='application/json') + url = reverse("api_experiments:list_create", args=["johndoe"]) + response = self.client.post( + url, + json.dumps( + { + "toolchain": "johndoe/toolchain2/1", + "declaration": ExperimentTestBase.DECLARATION2, + "name": "experiment2", + } + ), + content_type="application/json", + ) - self.checkResponse(response, 201, content_type='application/json') + self.checkResponse(response, 201, content_type="application/json") - url = reverse('api_experiments:start', args=['johndoe', 'toolchain2', 1, 'experiment2']) + url = reverse( + "api_experiments:start", args=["johndoe", "toolchain2", 1, "experiment2"] + ) response = self.client.post(url) - self.checkResponse(response, 200, content_type='application/json') + self.checkResponse(response, 200, content_type="application/json") self.client.logout() - self.experiment = Experiment.objects.get(author__username='johndoe', - toolchain__name='toolchain1', - name='experiment1', - ) - - self.experiment2 = Experiment.objects.get(author__username='johndoe', - toolchain__name='toolchain2', - name='experiment2', - ) + self.experiment = Experiment.objects.get( + author__username="johndoe", toolchain__name="toolchain1", name="experiment1" + ) - self.url = reverse('api_experiments:object', args=['johndoe', 'johndoe', 'toolchain1', 1, 'experiment1']) + self.experiment2 = Experiment.objects.get( + author__username="johndoe", toolchain__name="toolchain2", name="experiment2" + ) + self.url = reverse( + "api_experiments:object", + args=["johndoe", "johndoe", "toolchain1", 1, "experiment1"], + ) def test_no_retrieval_for_anonymous_user(self): response = self.client.get(self.url) self.checkResponse(response, 404) - def test_bad_retrieval_request_with_unknown_experiment(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - url = reverse('api_experiments:object', args=['johndoe', 'johndoe', 'toolchain1', 1, 'unknown']) + url = reverse( + "api_experiments:object", + args=["johndoe", "johndoe", "toolchain1", 1, "unknown"], + ) response = self.client.get(url) self.checkResponse(response, 404) - def test_bad_retrieval_request_with_unknown_attestation_number(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - url = reverse('api_experiments:attestations', args=[123456]) + url = reverse("api_experiments:attestations", args=[123456]) response = self.client.get(url) self.checkResponse(response, 404) - def test_retrieve_scheduled_experiment(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() response = self.client.get(self.url) - data = self.checkResponse(response, 200, content_type='application/json') + data = self.checkResponse(response, 200, content_type="application/json") - self.assertEqual(data['started'], False) - self.assertEqual(data['done'], False) - self.assertEqual(data['failed'], False) + self.assertEqual(data["started"], False) + self.assertEqual(data["done"], False) + self.assertEqual(data["failed"], False) - self.assertEqual(len(data['blocks_status']), 3) - self.assertEqual(data['blocks_status']['addition1'], 'pending') - self.assertEqual(data['blocks_status']['addition2'], 'pending') - self.assertEqual(data['blocks_status']['analysis'], 'pending') + self.assertEqual(len(data["blocks_status"]), 3) + self.assertEqual(data["blocks_status"]["addition1"], "pending") + self.assertEqual(data["blocks_status"]["addition2"], "pending") + self.assertEqual(data["blocks_status"]["analysis"], "pending") - self.assertEqual(len(data['results']), 1) - self.assertTrue('analysis' in data['results']) + self.assertEqual(len(data["results"]), 1) + self.assertTrue("analysis" in data["results"]) - result = data['results']['analysis'] - self.assertTrue('out_float' in result) - self.assertEqual(result['out_float']['type'], 'float32') - self.assertTrue(result['out_float']['value'] is None) - self.assertTrue('out_text' in result) - self.assertEqual(result['out_text']['type'], 'string') - self.assertTrue(result['out_text']['value'] is None) - - self.assertTrue(data['attestation'] is None) + result = data["results"]["analysis"] + self.assertTrue("out_float" in result) + self.assertEqual(result["out_float"]["type"], "float32") + self.assertTrue(result["out_float"]["value"] is None) + self.assertTrue("out_text" in result) + self.assertEqual(result["out_text"]["type"], "string") + self.assertTrue(result["out_text"]["value"] is None) + self.assertTrue(data["attestation"] is None) def test_retrieve_running_experiment(self): - block = self.experiment.blocks.get(name='addition1') + block = self.experiment.blocks.get(name="addition1") block.status = Block.PROCESSING block.save() @@ -1196,37 +1223,36 @@ class ResultsAPI(ExperimentTestBase): self.experiment.start_date = datetime.now() self.experiment.save() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() response = self.client.get(self.url) - data = self.checkResponse(response, 200, content_type='application/json') - - self.assertEqual(data['started'], True) - self.assertEqual(data['done'], False) - self.assertEqual(data['failed'], False) + data = self.checkResponse(response, 200, content_type="application/json") - self.assertEqual(len(data['blocks_status']), 3) - self.assertEqual(data['blocks_status']['addition1'], 'processing') - self.assertEqual(data['blocks_status']['addition2'], 'pending') - self.assertEqual(data['blocks_status']['analysis'], 'pending') + self.assertEqual(data["started"], True) + self.assertEqual(data["done"], False) + self.assertEqual(data["failed"], False) - self.assertEqual(len(data['results']), 1) - self.assertTrue('analysis' in data['results']) + self.assertEqual(len(data["blocks_status"]), 3) + self.assertEqual(data["blocks_status"]["addition1"], "processing") + self.assertEqual(data["blocks_status"]["addition2"], "pending") + self.assertEqual(data["blocks_status"]["analysis"], "pending") - result = data['results']['analysis'] - self.assertTrue('out_float' in result) - self.assertEqual(result['out_float']['type'], 'float32') - self.assertTrue(result['out_float']['value'] is None) - self.assertTrue('out_text' in result) - self.assertEqual(result['out_text']['type'], 'string') - self.assertTrue(result['out_text']['value'] is None) + self.assertEqual(len(data["results"]), 1) + self.assertTrue("analysis" in data["results"]) - self.assertTrue(data['attestation'] is None) + result = data["results"]["analysis"] + self.assertTrue("out_float" in result) + self.assertEqual(result["out_float"]["type"], "float32") + self.assertTrue(result["out_float"]["value"] is None) + self.assertTrue("out_text" in result) + self.assertEqual(result["out_text"]["type"], "string") + self.assertTrue(result["out_text"]["value"] is None) + self.assertTrue(data["attestation"] is None) def test_retrieve_done_experiment(self): - for name in ['addition1', 'addition2', 'analysis']: + for name in ["addition1", "addition2", "analysis"]: block = self.experiment.blocks.get(name=name) block.status = Block.DONE block.save() @@ -1236,33 +1262,32 @@ class ResultsAPI(ExperimentTestBase): self.experiment.end_date = datetime.now() self.experiment.save() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() response = self.client.get(self.url) - data = self.checkResponse(response, 200, content_type='application/json') - - self.assertEqual(data['started'], True) - self.assertEqual(data['done'], True) - self.assertEqual(data['failed'], False) + data = self.checkResponse(response, 200, content_type="application/json") - self.assertEqual(len(data['blocks_status']), 3) - self.assertEqual(data['blocks_status']['addition1'], 'generated') - self.assertEqual(data['blocks_status']['addition2'], 'generated') - self.assertEqual(data['blocks_status']['analysis'], 'generated') + self.assertEqual(data["started"], True) + self.assertEqual(data["done"], True) + self.assertEqual(data["failed"], False) - self.assertEqual(len(data['results']), 1) - self.assertTrue('analysis' in data['results']) + self.assertEqual(len(data["blocks_status"]), 3) + self.assertEqual(data["blocks_status"]["addition1"], "generated") + self.assertEqual(data["blocks_status"]["addition2"], "generated") + self.assertEqual(data["blocks_status"]["analysis"], "generated") - self.assertTrue(data['attestation'] is None) + self.assertEqual(len(data["results"]), 1) + self.assertTrue("analysis" in data["results"]) + self.assertTrue(data["attestation"] is None) def test_retrieve_failed_experiment(self): - block = self.experiment.blocks.get(name='addition1') + block = self.experiment.blocks.get(name="addition1") block.status = Block.PROCESSING block.save() - block = self.experiment.blocks.get(name='addition2') + block = self.experiment.blocks.get(name="addition2") self.experiment.start_date = datetime.now() self.experiment.end_date = datetime.now() @@ -1270,48 +1295,47 @@ class ResultsAPI(ExperimentTestBase): self.experiment.save() cached_file = block.first_cache() - cached_file.error_report = 'ERROR REPORT' + cached_file.error_report = "ERROR REPORT" cached_file.save() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() response = self.client.get(self.url) - data = self.checkResponse(response, 200, content_type='application/json') + data = self.checkResponse(response, 200, content_type="application/json") - self.assertTrue(data['started']) - self.assertTrue(data['done']) - self.assertTrue(data['failed']) + self.assertTrue(data["started"]) + self.assertTrue(data["done"]) + self.assertTrue(data["failed"]) - self.assertEqual(len(data['blocks_status']), 3) - self.assertEqual(data['blocks_status']['addition1'], 'processing') - self.assertEqual(data['blocks_status']['addition2'], 'pending') - self.assertEqual(data['blocks_status']['analysis'], 'pending') + self.assertEqual(len(data["blocks_status"]), 3) + self.assertEqual(data["blocks_status"]["addition1"], "processing") + self.assertEqual(data["blocks_status"]["addition2"], "pending") + self.assertEqual(data["blocks_status"]["analysis"], "pending") - self.assertEqual(len(data['results']), 1) - self.assertTrue('analysis' in data['results']) + self.assertEqual(len(data["results"]), 1) + self.assertTrue("analysis" in data["results"]) - result = data['results']['analysis'] + result = data["results"]["analysis"] - self.assertTrue('out_float' in result) - self.assertEqual(result['out_float']['type'], 'float32') - self.assertTrue(result['out_float']['value'] is None) - self.assertTrue('out_text' in result) - self.assertEqual(result['out_text']['type'], 'string') - self.assertTrue(result['out_text']['value'] is None) + self.assertTrue("out_float" in result) + self.assertEqual(result["out_float"]["type"], "float32") + self.assertTrue(result["out_float"]["value"] is None) + self.assertTrue("out_text" in result) + self.assertEqual(result["out_text"]["type"], "string") + self.assertTrue(result["out_text"]["value"] is None) - self.assertTrue(data['attestation'] is None) + self.assertTrue(data["attestation"] is None) - self.assertEqual(len(data['errors']), 1) - - error = data['errors'][0] - self.assertEqual(error['block'], 'addition2') - self.assertEqual(error['algorithm'], 'johndoe/sum/1') - self.assertEqual(error['details'], 'ERROR REPORT') + self.assertEqual(len(data["errors"]), 1) + error = data["errors"][0] + self.assertEqual(error["block"], "addition2") + self.assertEqual(error["algorithm"], "johndoe/sum/1") + self.assertEqual(error["details"], "ERROR REPORT") def test_retrieve_certified_experiment(self): - for name in ['addition1', 'addition2', 'analysis']: + for name in ["addition1", "addition2", "analysis"]: block = self.experiment.blocks.get(name=name) block.status = Block.DONE block.save() @@ -1321,232 +1345,234 @@ class ResultsAPI(ExperimentTestBase): self.experiment.end_date = datetime.now() self.experiment.save() - attestation = Attestation() - attestation.number = 123456 - attestation.experiment = self.experiment - attestation.toolchain = self.experiment.toolchain + attestation = Attestation() + attestation.number = 123456 + attestation.experiment = self.experiment + attestation.toolchain = self.experiment.toolchain attestation.creation_date = datetime.now() attestation.save() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() response = self.client.get(self.url) - data = self.checkResponse(response, 200, content_type='application/json') - - self.assertEqual(data['started'], True) - self.assertEqual(data['done'], True) - self.assertEqual(data['failed'], False) + data = self.checkResponse(response, 200, content_type="application/json") - self.assertEqual(len(data['blocks_status']), 3) - self.assertEqual(data['blocks_status']['addition1'], 'generated') - self.assertEqual(data['blocks_status']['addition2'], 'generated') - self.assertEqual(data['blocks_status']['analysis'], 'generated') + self.assertEqual(data["started"], True) + self.assertEqual(data["done"], True) + self.assertEqual(data["failed"], False) - self.assertEqual(len(data['results']), 1) - self.assertTrue('analysis' in data['results']) + self.assertEqual(len(data["blocks_status"]), 3) + self.assertEqual(data["blocks_status"]["addition1"], "generated") + self.assertEqual(data["blocks_status"]["addition2"], "generated") + self.assertEqual(data["blocks_status"]["analysis"], "generated") - self.assertEqual(data['attestation'], 123456) + self.assertEqual(len(data["results"]), 1) + self.assertTrue("analysis" in data["results"]) + self.assertEqual(data["attestation"], 123456) def test_retrieve_done_experiment_results_data(self): - analysis_block = self.experiment2.blocks.get(name='analysis') + analysis_block = self.experiment2.blocks.get(name="analysis") analysis_block.status = Block.DONE analysis_block.save() - db_result = Result() - db_result.cache = analysis_block.first_cache() - db_result.name = 'out_float' - db_result.type = 'float32' - db_result.primary = True - db_result.data_value = 1.0 + db_result = Result() + db_result.cache = analysis_block.first_cache() + db_result.name = "out_float" + db_result.type = "float32" + db_result.primary = True + db_result.data_value = 1.0 db_result.save() - db_result = Result() - db_result.cache = analysis_block.first_cache() - db_result.name = 'out_text' - db_result.type = 'string' - db_result.primary = True - db_result.data_value = "Hello world!\nTrain error: 0.2\nTest error: 0.3" + db_result = Result() + db_result.cache = analysis_block.first_cache() + db_result.name = "out_text" + db_result.type = "string" + db_result.primary = True + db_result.data_value = "Hello world!\nTrain error: 0.2\nTest error: 0.3" db_result.save() self.experiment2.start_date = datetime.now() self.experiment2.end_date = datetime.now() self.experiment2.save() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - url = reverse('api_experiments:object', args=['johndoe', 'johndoe', 'toolchain2', 1, 'experiment2']) + url = reverse( + "api_experiments:object", + args=["johndoe", "johndoe", "toolchain2", 1, "experiment2"], + ) response = self.client.get(url) - data = self.checkResponse(response, 200, content_type='application/json') - - self.assertEqual(len(data['results']), 1) - self.assertTrue('analysis' in data['results']) + data = self.checkResponse(response, 200, content_type="application/json") - result = data['results']['analysis'] + self.assertEqual(len(data["results"]), 1) + self.assertTrue("analysis" in data["results"]) - self.assertTrue('out_float' in result) - self.assertEqual(result['out_float']['type'], 'float32') - self.assertEqual(result['out_float']['value'], 1.0) - self.assertTrue('out_text' in result) - self.assertEqual(result['out_text']['type'], 'string') - self.assertEqual(result['out_text']['value'], "Hello world!\nTrain error: 0.2\nTest error: 0.3") + result = data["results"]["analysis"] + self.assertTrue("out_float" in result) + self.assertEqual(result["out_float"]["type"], "float32") + self.assertEqual(result["out_float"]["value"], 1.0) + self.assertTrue("out_text" in result) + self.assertEqual(result["out_text"]["type"], "string") + self.assertEqual( + result["out_text"]["value"], + "Hello world!\nTrain error: 0.2\nTest error: 0.3", + ) def test_retrieve_certified_experiment_results(self): - analysis_block = self.experiment2.blocks.get(name='analysis') + analysis_block = self.experiment2.blocks.get(name="analysis") analysis_block.status = Block.DONE analysis_block.save() - db_result = Result() - db_result.cache = analysis_block.first_cache() - db_result.name = 'out_float' - db_result.type = 'float32' - db_result.primary = True + db_result = Result() + db_result.cache = analysis_block.first_cache() + db_result.name = "out_float" + db_result.type = "float32" + db_result.primary = True db_result.data_value = 1.0 db_result.save() - db_result = Result() - db_result.cache = analysis_block.first_cache() - db_result.name = 'out_text' - db_result.type = 'string' - db_result.primary = True - db_result.data_value = "Hello world!\nTrain error: 0.2\nTest error: 0.3" + db_result = Result() + db_result.cache = analysis_block.first_cache() + db_result.name = "out_text" + db_result.type = "string" + db_result.primary = True + db_result.data_value = "Hello world!\nTrain error: 0.2\nTest error: 0.3" db_result.save() self.experiment2.start_date = datetime.now() self.experiment2.end_date = datetime.now() self.experiment2.save() - attestation = Attestation() - attestation.number = 123456 - attestation.experiment = self.experiment2 - attestation.toolchain = self.experiment2.toolchain + attestation = Attestation() + attestation.number = 123456 + attestation.experiment = self.experiment2 + attestation.toolchain = self.experiment2.toolchain attestation.creation_date = datetime.now() attestation.save() - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - - url = reverse('api_experiments:attestations', args=[123456]) + url = reverse("api_experiments:attestations", args=[123456]) response = self.client.get(url) - data = self.checkResponse(response, 200, content_type='application/json') - - self.assertEqual(len(data['results']), 1) - self.assertTrue('analysis' in data['results']) + data = self.checkResponse(response, 200, content_type="application/json") - result = data['results']['analysis'] + self.assertEqual(len(data["results"]), 1) + self.assertTrue("analysis" in data["results"]) - self.assertTrue('out_float' in result) - self.assertEqual(result['out_float']['type'], 'float32') - self.assertEqual(result['out_float']['value'], 1.0) - self.assertTrue('out_text' in result) - self.assertEqual(result['out_text']['type'], 'string') - self.assertEqual(result['out_text']['value'], "Hello world!\nTrain error: 0.2\nTest error: 0.3") + result = data["results"]["analysis"] + self.assertTrue("out_float" in result) + self.assertEqual(result["out_float"]["type"], "float32") + self.assertEqual(result["out_float"]["value"], 1.0) + self.assertTrue("out_text" in result) + self.assertEqual(result["out_text"]["type"], "string") + self.assertEqual( + result["out_text"]["value"], + "Hello world!\nTrain error: 0.2\nTest error: 0.3", + ) def test_no_started_field(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - response = self.client.get(self.url + '?fields=done,blocks_status,results') + response = self.client.get(self.url + "?fields=done,blocks_status,results") - data = self.checkResponse(response, 200, content_type='application/json') + data = self.checkResponse(response, 200, content_type="application/json") - self.assertFalse('started' in data) + self.assertFalse("started" in data) - self.assertEqual(data['done'], False) + self.assertEqual(data["done"], False) - self.assertEqual(len(data['blocks_status']), 3) - self.assertEqual(data['blocks_status']['addition1'], 'pending') - self.assertEqual(data['blocks_status']['addition2'], 'pending') - self.assertEqual(data['blocks_status']['analysis'], 'pending') + self.assertEqual(len(data["blocks_status"]), 3) + self.assertEqual(data["blocks_status"]["addition1"], "pending") + self.assertEqual(data["blocks_status"]["addition2"], "pending") + self.assertEqual(data["blocks_status"]["analysis"], "pending") - self.assertEqual(len(data['results']), 1) - self.assertTrue('analysis' in data['results']) - - result = data['results']['analysis'] - self.assertTrue('out_float' in result) - self.assertTrue('out_text' in result) + self.assertEqual(len(data["results"]), 1) + self.assertTrue("analysis" in data["results"]) + result = data["results"]["analysis"] + self.assertTrue("out_float" in result) + self.assertTrue("out_text" in result) def test_no_done_field(self): - self.client.login(username='johndoe', password='1234') - - response = self.client.get(self.url + '?fields=started,blocks_status,results') + self.login_johndoe() - data = self.checkResponse(response, 200, content_type='application/json') + response = self.client.get(self.url + "?fields=started,blocks_status,results") - self.assertFalse('done' in data) + data = self.checkResponse(response, 200, content_type="application/json") - self.assertEqual(data['started'], False) + self.assertFalse("done" in data) - self.assertEqual(len(data['blocks_status']), 3) - self.assertEqual(data['blocks_status']['addition1'], 'pending') - self.assertEqual(data['blocks_status']['addition2'], 'pending') - self.assertEqual(data['blocks_status']['analysis'], 'pending') + self.assertEqual(data["started"], False) - self.assertEqual(len(data['results']), 1) + self.assertEqual(len(data["blocks_status"]), 3) + self.assertEqual(data["blocks_status"]["addition1"], "pending") + self.assertEqual(data["blocks_status"]["addition2"], "pending") + self.assertEqual(data["blocks_status"]["analysis"], "pending") - result = data['results']['analysis'] - self.assertTrue('out_float' in result) - self.assertTrue('out_text' in result) + self.assertEqual(len(data["results"]), 1) + result = data["results"]["analysis"] + self.assertTrue("out_float" in result) + self.assertTrue("out_text" in result) def test_no_blocks_status_field(self): - self.client.login(username='johndoe', password='1234') - - response = self.client.get(self.url + '?fields=done,started,results') + self.login_johndoe() - data = self.checkResponse(response, 200, content_type='application/json') + response = self.client.get(self.url + "?fields=done,started,results") - self.assertFalse('blocks_status' in data) + data = self.checkResponse(response, 200, content_type="application/json") - self.assertEqual(data['started'], False) - self.assertEqual(data['done'], False) + self.assertFalse("blocks_status" in data) - self.assertEqual(len(data['results']), 1) - self.assertTrue('analysis' in data['results']) + self.assertEqual(data["started"], False) + self.assertEqual(data["done"], False) - result = data['results']['analysis'] - self.assertTrue('out_float' in result) - self.assertTrue('out_text' in result) + self.assertEqual(len(data["results"]), 1) + self.assertTrue("analysis" in data["results"]) + result = data["results"]["analysis"] + self.assertTrue("out_float" in result) + self.assertTrue("out_text" in result) def test_no_results_field(self): - self.client.login(username='johndoe', password='1234') + self.login_johndoe() - response = self.client.get(self.url + '?fields=done,started,blocks_status') + response = self.client.get(self.url + "?fields=done,started,blocks_status") - data = self.checkResponse(response, 200, content_type='application/json') + data = self.checkResponse(response, 200, content_type="application/json") - self.assertFalse('results' in data) + self.assertFalse("results" in data) - self.assertEqual(data['started'], False) - self.assertEqual(data['done'], False) + self.assertEqual(data["started"], False) + self.assertEqual(data["done"], False) - self.assertEqual(len(data['blocks_status']), 3) - self.assertEqual(data['blocks_status']['addition1'], 'pending') - self.assertEqual(data['blocks_status']['addition2'], 'pending') - self.assertEqual(data['blocks_status']['analysis'], 'pending') + self.assertEqual(len(data["blocks_status"]), 3) + self.assertEqual(data["blocks_status"]["addition1"], "pending") + self.assertEqual(data["blocks_status"]["addition2"], "pending") + self.assertEqual(data["blocks_status"]["analysis"], "pending") -#---------------------------------------------------------- +# ---------------------------------------------------------- class TeamDeletionPropagation(ExperimentTestBase): def setUp(self): super(TeamDeletionPropagation, self).setUp() - user1 = User.objects.get(username='johndoe') - user2 = User.objects.create_user('jackdoe', 'jackdoe@test.org', '1234') - user3 = User.objects.create_user('janedoe', 'janedoe@test.org', '1234') + user1 = User.objects.get(username="johndoe") + user2 = User.objects.create_user("jackdoe", "jackdoe@test.org", TEST_PWD) + user3 = User.objects.create_user("janedoe", "janedoe@test.org", TEST_PWD) - team1 = Team.objects.create(name='teamdoe', owner=user1) - team2 = Team.objects.create(name='teamdoe2', owner=user1) - Team.objects.create(name='teamdoe3', owner=user1) - Team.objects.create(name='teamdoe4', owner=user1) + team1 = Team.objects.create(name="teamdoe", owner=user1) + team2 = Team.objects.create(name="teamdoe2", owner=user1) + Team.objects.create(name="teamdoe3", owner=user1) + Team.objects.create(name="teamdoe4", owner=user1) team1.members.add(user2) team2.members.add(user3) @@ -1556,51 +1582,49 @@ class TeamDeletionPropagation(ExperimentTestBase): self.assertEqual(Toolchain.objects.for_user(user).count(), toolchains) self.assertEqual(Algorithm.objects.for_user(user).count(), algorithms) - def test_propagation(self): - jackdoe = User.objects.get(username='jackdoe') - johndoe = User.objects.get(username='johndoe') - janedoe = User.objects.get(username='janedoe') + jackdoe = User.objects.get(username="jackdoe") + johndoe = User.objects.get(username="johndoe") + janedoe = User.objects.get(username="janedoe") - team_to_delete = Team.objects.create(name='team_to_delete', owner=johndoe) + team_to_delete = Team.objects.create(name="team_to_delete", owner=johndoe) team_to_delete.members.add(janedoe, jackdoe) self.check_shared_objects(janedoe, 0, 0, 0) self.check_shared_objects(jackdoe, 0, 0, 0) - db_toolchain = Toolchain.objects.get(author=johndoe, name='toolchain1') + db_toolchain = Toolchain.objects.get(author=johndoe, name="toolchain1") (experiment, toolchain, errors) = Experiment.objects.create_experiment( author=johndoe, toolchain=db_toolchain, - name='john_experiment1', - short_description='john_experiment1', - description='description', - declaration=ExperimentTestBase.DECLARATION1 + name="john_experiment1", + short_description="john_experiment1", + description="description", + declaration=ExperimentTestBase.DECLARATION1, ) - assert experiment, errors + self.assertIsNotNone(experiment, errors) algorithms_infos = {} for algorithm in experiment.referenced_algorithms.filter(author=johndoe): - algorithms_infos[algorithm.fullname()] = { 'opensource': True } + algorithms_infos[algorithm.fullname()] = {"opensource": True} experiment.share(teams=[team_to_delete], algorithms_infos=algorithms_infos) self.check_shared_objects(janedoe, 1, 1, 2) self.check_shared_objects(jackdoe, 1, 1, 2) - experiment, toolchain, errors = Experiment.objects.create_experiment( author=janedoe, toolchain=db_toolchain, - name='jane_experiment1', - short_description='jane_experiment1', - description='description', - declaration=ExperimentTestBase.DECLARATION1 + name="jane_experiment1", + short_description="jane_experiment1", + description="description", + declaration=ExperimentTestBase.DECLARATION1, ) - assert experiment, errors + self.assertIsNotNone(experiment, errors) self.check_shared_objects(janedoe, 2, 1, 2) self.check_shared_objects(jackdoe, 1, 1, 2)