class UpdateScenarioViewTest(TestCase): def setUp(self): self.scenario = ScenarioFactory(xml=get_xml("default.xml")) self.client.login(username=self.scenario.user.username, password="******") self.url = reverse("ts_om.scenario.update") # self.data = {"xml": get_xml("default.xml")} self.data = { "scenario_id": self.scenario.id, "xml": "", "name": "Updated scenario name", "description": "New Description" } def test(self): response = self.client.post(self.url, data=self.data) self.assertEqual(response.status_code, 200) self.scenario.refresh_from_db() self.assertEqual(self.scenario.name, "Updated scenario name") self.assertEqual(self.scenario.description, "New Description") self.assertEqual(self.scenario.xml, "") def test_no_permission(self): user = UserFactory() client = Client() client.login(username=user.username, password="******") response = client.post(self.url, data=self.data) self.assertEqual(response.status_code, 403) self.scenario.refresh_from_db() self.assertNotEqual(self.scenario.name, "Updated scenario name") self.assertNotEqual(self.scenario.description, "New Description") self.assertNotEqual(self.scenario.xml, "")
def setUp(self): self.scenario = ScenarioFactory() self.user = self.scenario.user self.user.set_password("1") self.user.save() self.url = reverse("ts_om.download", kwargs={"scenario_id": self.scenario.id})
def test_xml_name_property_set(self): scenario = ScenarioFactory(xml=""" <om:scenario xmlns:om="http://openmalaria.org/schema/scenario_32" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Test" schemaVersion="32" xsi:schemaLocation="http://openmalaria.org/schema/scenario_32 scenario_32.xsd"> </om:scenario>""") self.assertEqual(scenario.xml_name, "Test") scenario.xml_name = "New Name" self.assertEqual(scenario.xml_name, "New Name") self.assertIn("name=\"New Name\"", scenario.xml)
def setUp(self): self.scenario = ScenarioFactory() self.url = reverse("ts_om.summary", kwargs={"scenario_id": self.scenario.id}) self.client.login(username=self.scenario.user.username, password="******") self.data = { "name": "Scenario", "desc": "Scenario Description", "xml": "mmm" }
class DownloadScenarioXmlViewTest(TestCase): def setUp(self): self.scenario = ScenarioFactory() self.user = self.scenario.user self.user.set_password("1") self.user.save() self.url = reverse("ts_om.download", kwargs={"scenario_id": self.scenario.id}) def test_anonymous(self): client = Client() response = client.get(self.url) self.assertEqual(response.status_code, 302) self.assertIn("login", response.url) def test_success(self): client = Client() client.login(username=self.user.username, password="******") response = client.get(self.url) self.assertEqual(response.status_code, 200) self.assertIn( '<demography maximumAgeYrs="90" name="Rachuonyo" popSize="100">', response.content) def test_wrong_user(self): client = Client() user = UserFactory() client.login(username=user.username, password="******") response = client.get(self.url) self.assertEqual(response.status_code, 404) def test_no_scenario(self): client = Client() client.login(username=self.user.username, password="******") url = reverse("ts_om.download", kwargs={"scenario_id": 1000000}) response = client.get(url) self.assertEqual(response.status_code, 404) def test_incorrect_xml(self): client = Client() self.scenario.xml = "123" self.scenario.save() client.login(username=self.user.username, password="******") response = client.get(self.url) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, "123") def test_pretty_print(self): client = Client() self.scenario.xml = "<xml><test></test></xml>" self.scenario.save() client.login(username=self.user.username, password="******") response = client.get(self.url) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, "<xml>\n <test/>\n</xml>\n")
def setUp(self): self.scenario = ScenarioFactory(xml=get_xml("default.xml")) self.client.login(username=self.scenario.user.username, password="******") self.url = reverse("ts_om.scenario.update") # self.data = {"xml": get_xml("default.xml")} self.data = { "scenario_id": self.scenario.id, "xml": "", "name": "Updated scenario name", "description": "New Description" }
def setUp(self): self.simulation = Simulation.objects.create(status=Simulation.COMPLETE) self.simulation.set_input_file(get_xml("scenario.xml")) self.simulation.set_output_file(get_xml("output.txt")) self.simulation.set_ctsout_file(get_xml("ctsout.txt")) self.simulation.set_model_stdout(get_xml("model_stdout_stderr.txt")) self.scenario = ScenarioFactory(xml=get_xml("scenario.xml"), new_simulation=self.simulation) self.user = self.scenario.user self.client.login(username=self.user, password="******") self.url = reverse("ts_om_viz.get_cts_data", kwargs={ "sim_id": self.scenario.new_simulation.id, "measure_name": "simulated EIR" })
def test_xml_name_property_get_4(self): scenario = ScenarioFactory(xml=""" <om:scenario xmlns:om="http://openmalaria.org/schema/scenario_32" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Default scenario" schemaVersion="32" xsi:schemaLocation="http://openmalaria.org/schema/scenario_32 scenario_32.xsd"> </om:scenario>""") self.assertEqual(scenario.xml_name, "Default scenario")
def setUp(self): self.scenario = ScenarioFactory() self.client.login(username=self.scenario.user.username, password="******") self.url = reverse("ts_om.monitoring.update.form", kwargs={"scenario_id": self.scenario.id}) self.data = { "xml": get_xml("group 8 baseline p2.xml"), "start_date": 1, }
class DeleteScenarioViewTest(TestCase): def setUp(self): self.user = UserFactory() self.scenario = ScenarioFactory(user=self.user) self.client.login(username=self.user.username, password="******") self.url = reverse("ts_om.delete", kwargs={"scenario_id": self.scenario.id}) def test_as_anonymous(self): client = Client() response = client.get(self.url) self.assertEqual(response.status_code, 302) self.scenario.refresh_from_db() self.assertEqual(self.scenario.deleted, False) def test_no_permission(self): user = UserFactory() client = Client() client.login(username=user.username, password="******") response = client.get(self.url) self.assertEqual(response.status_code, 403) self.scenario.refresh_from_db() self.assertEqual(self.scenario.deleted, False) def test_success(self): response = self.client.get(self.url) self.assertEqual(response.status_code, 302) self.assertEqual(self.client.session["notifications"][0]["type"], SUCCESS) self.assertEqual(response.url, reverse("ts_om.list")) self.scenario.refresh_from_db() self.assertEqual(self.scenario.deleted, True) def test_success_undelete(self): self.scenario.deleted = True self.scenario.save() response = self.client.get(self.url) self.assertEqual(response.status_code, 302) self.assertEqual(self.client.session["notifications"][0]["type"], SUCCESS) self.assertEqual(response.url, reverse("ts_om.list")) self.scenario.refresh_from_db() self.assertEqual(self.scenario.deleted, False)
def create_scenario_from_directory(user, directory): simulation = Simulation.objects.create(status=Simulation.COMPLETE) simulation.set_input_file(get_xml(os.path.join(directory, "scenario.xml"))) simulation.set_output_file(get_xml(os.path.join(directory, "output.txt"))) simulation.set_ctsout_file(get_xml(os.path.join(directory, "ctsout.txt"))) simulation.set_model_stdout( get_xml(os.path.join(directory, "model_stdout_stderr.txt"))) scenario = ScenarioFactory(user=user, xml=get_xml("scenario.xml"), new_simulation=simulation) return scenario
def test_empty_simulation(self): simulation = Simulation.objects.create() scenario = ScenarioFactory(user=self.user, xml="", new_simulation=simulation) url = reverse("ts_om_viz.SimulationView", kwargs={"id": simulation.id}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertIn("Error processing input or output files", response.content) self.assertEqual(response.context["simulation"], simulation) self.assertEqual(response.context["output_txt_filename"], None) self.assertEqual(response.context["ctsout_txt_filename"], None) self.assertNotIn("xml_filename", response.context) self.assertNotIn("model_stdout", response.context)
class SubmitScenariosViewTest(TestCase): def setUp(self): self.scenario = ScenarioFactory() self.user = self.scenario.user self.user.set_password("1") self.user.save() @patch("website.apps.ts_om.views.submit_scenarios_view.submit") def test_anonymous(self, submit_func): client = Client() data = {"scenario_ids": json.dumps([self.scenario.id])} response = client.post(reverse("ts_om.submit"), data=data) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) self.assertEqual(response_json["ok"], False) self.scenario.refresh_from_db() self.assertEqual(submit_func.called, False) @patch("website.apps.ts_om.views.submit_scenarios_view.submit") @patch("website.apps.ts_om.views.submit_scenarios_view.rest_validate") def test_success(self, rest_validate_func, submit_func): rest_validate_func.return_value = json.dumps({'result': 0}) client = Client() client.login(username=self.user.username, password="******") data = {"scenario_ids": json.dumps([self.scenario.id])} response = client.post(reverse("ts_om.submit"), data=data) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) self.assertEqual(response_json["ok"], True) self.scenario.refresh_from_db() self.assertEqual(rest_validate_func.called, True) self.assertEqual(submit_func.called, True) @patch("website.apps.ts_om.views.submit_scenarios_view.submit") @patch("website.apps.ts_om.views.submit_scenarios_view.rest_validate") def test_validation_fails(self, rest_validate_func, submit_func): rest_validate_func.return_value = json.dumps({'result': 66}) client = Client() client.login(username=self.user.username, password="******") data = {"scenario_ids": json.dumps([self.scenario.id])} response = client.post(reverse("ts_om.submit"), data=data) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) # self.assertEqual(response_json["ok"], False) self.scenario.refresh_from_db() self.assertEqual(rest_validate_func.called, True) self.assertEqual(submit_func.called, False)
def setUp(self): self.scenario = ScenarioFactory(xml=get_xml("default.xml")) self.client.login(username=self.scenario.user.username, password="******") self.url = reverse("ts_om.status") # self.data = {"xml": get_xml("default.xml")} self.data = {"scenario_id": self.scenario.id}
class ScenarioSummaryViewTest(TestCase): def setUp(self): self.scenario = ScenarioFactory() self.url = reverse("ts_om.summary", kwargs={"scenario_id": self.scenario.id}) self.client.login(username=self.scenario.user.username, password="******") self.data = { "name": "Scenario", "desc": "Scenario Description", "xml": "mmm" } def test_get_as_anonymous(self): client = Client() response = client.get(self.url) self.assertEqual(response.status_code, 302) self.assertIn(reverse("login") + "?next=" + self.url, response.url) def test_get_as_different_user(self): client = Client() user = UserFactory() client.login(username=user.username, password="******") response = client.get(self.url) self.assertEqual(response.status_code, 403) def test_success_get(self): response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["scenario"], self.scenario) self.assertEqual( ['arabiensis', 'funestus', 'minor', 'gambiae'], [vector.mosquito for vector in response.context["vectors"]]) def test_get_broken_xml(self): self.scenario.xml = "<" self.scenario.save() response = self.client.get(self.url) self.assertEqual(response.status_code, 200) def test_post_as_anonymous(self): client = Client() response = client.post(self.url, data={}) self.assertEqual(response.status_code, 302) self.assertIn(reverse("login") + "?next=" + self.url, response.url) def test_post_as_different_user(self): client = Client() user = UserFactory() client.login(username=user.username, password="******") response = client.post(self.url, data={}) self.assertEqual(response.status_code, 403) def test_post_broken_xml(self): response = self.client.post(self.url, data=self.data) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, self.url) self.assertEqual(self.client.session["notifications"][0]["type"], DANGER) def test_post_success(self): self.data['xml'] = get_xml() response = self.client.post(self.url, data=self.data) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, reverse('ts_om.list')) self.assertEqual(self.client.session["notifications"][0]["type"], SUCCESS) @patch("website.apps.ts_om_edit.views.ScenarioSummaryView.submit.submit") def test_post_save_and_run_failed(self, submit_func): submit_func.return_value = None self.data['xml'] = get_xml() self.data['submit_run'] = "Save and Run" response = self.client.post(self.url, data=self.data) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, reverse('ts_om.list')) self.assertEqual(len(self.client.session["notifications"]), 2) # print(self.client.session["notifications"][0]["message"]) # print(self.client.session["notifications"][1]["message"]) self.assertEqual(self.client.session["notifications"][0]["type"], DANGER) # Can't submit the simulation self.assertEqual(self.client.session["notifications"][1]["type"], SUCCESS) # Scenario saved successfully self.assertEqual(submit_func.called, True) @patch("website.apps.ts_om_edit.views.ScenarioSummaryView.submit.submit") def test_post_save_and_run_success(self, submit_func): simulation = SimulationFactory() submit_func.return_value = simulation self.data['xml'] = get_xml() self.data['submit_run'] = "Save and Run" response = self.client.post(self.url, data=self.data) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, reverse('ts_om.list')) self.assertEqual(len(self.client.session["notifications"]), 2) # print(self.client.session["notifications"][0]["message"]) # print(self.client.session["notifications"][1]["message"]) self.assertEqual(self.client.session["notifications"][0]["type"], SUCCESS) # Submitted successfully self.assertEqual(self.client.session["notifications"][1]["type"], SUCCESS) # Scenario saved successfully self.assertEqual(submit_func.called, True)
class GetCtsDataViewTest(TestCase): def setUp(self): self.simulation = Simulation.objects.create(status=Simulation.COMPLETE) self.simulation.set_input_file(get_xml("scenario.xml")) self.simulation.set_output_file(get_xml("output.txt")) self.simulation.set_ctsout_file(get_xml("ctsout.txt")) self.simulation.set_model_stdout(get_xml("model_stdout_stderr.txt")) self.scenario = ScenarioFactory(xml=get_xml("scenario.xml"), new_simulation=self.simulation) self.user = self.scenario.user self.client.login(username=self.user, password="******") self.url = reverse("ts_om_viz.get_survey_data", kwargs={ "sim_id": self.scenario.new_simulation.id, "measure_id": nSevere, "bin_number": 1 }) def test_simulation_no_input_file(self): simulation = Simulation.objects.create(status=Simulation.COMPLETE) self.scenario.new_simulation = simulation self.scenario.save() url = reverse("ts_om_viz.get_survey_data", kwargs={ "sim_id": simulation.id, "measure_id": nSevere, "bin_number": 1 }) self.assertRaises(TypeError, self.client.get, url) def test_unknown_measure(self): url = reverse("ts_om_viz.get_survey_data", kwargs={ "sim_id": self.scenario.new_simulation.id, "measure_id": 100000, "bin_number": 1 }) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_success(self): response = self.client.get(self.url) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content["sim_id"], self.scenario.new_simulation.id) self.assertEqual(json_content["data"][0], [10.082191780821917, 1.0]) self.assertEqual(json_content["data"][-1], [30.0, 0.0]) self.assertEqual(json_content["measure_name"], "nSevere(0.0 - 90)") self.assertIn("description", json_content) def test_success_specifies_name(self): scenario = create_scenario_from_directory(user=self.user, directory="gambiae") url = reverse("ts_om_viz.get_survey_data", kwargs={ "sim_id": scenario.new_simulation.id, "measure_id": Vector_Nv0, "bin_number": "gambiae" }) response = self.client.get(url) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content["sim_id"], scenario.new_simulation.id) self.assertEqual(json_content["data"][0], [2.0, 14.6554]) self.assertEqual(json_content["data"][-1], [10.0, 14.6554]) self.assertEqual(json_content["measure_name"], "Vector_Nv0(gambiae)") self.assertIn("description", json_content) def test_success_no_ctsout_file(self): self.simulation.ctsout_file = None self.simulation.model_stdout = None self.simulation.save() response = self.client.get(self.url) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content["sim_id"], self.scenario.new_simulation.id) self.assertEqual(json_content["data"][0], [10.082191780821917, 1.0]) self.assertEqual(json_content["data"][-1], [30.0, 0.0]) self.assertEqual(json_content["measure_name"], "nSevere(0.0 - 90)") self.assertIn("description", json_content) def test_anonymous_user(self): client = Client() response = client.get(self.url) self.assertEqual(response.status_code, 403) def test_different_user(self): client = Client() user = UserFactory() client.login(username=user.username, password="******") response = client.get(self.url) self.assertEqual(response.status_code, 403)
def setUp(self): self.scenario = ScenarioFactory()
def test_output_file_property_3(self): scenario = ScenarioFactory(xml=get_xml("tororo.xml")) scenario.new_simulation = Simulation.objects.create() scenario.save() scenario.new_simulation.set_model_stdout("123") self.assertEqual(scenario.output_file.read(), "123")
def test_output_file_property_2(self): scenario = ScenarioFactory(xml=get_xml("tororo.xml")) scenario.new_simulation = Simulation.objects.create() scenario.save() self.assertEqual(scenario.output_file, None)
def test_output_file_property_1(self): scenario = ScenarioFactory(xml=get_xml("tororo.xml")) self.assertEqual(scenario.output_file, None)
def setUp(self): self.scenario = ScenarioFactory() self.user = self.scenario.user self.user.set_password("1") self.user.save()
def test_xml_name_property_get_2(self): scenario = ScenarioFactory(xml="<aaaa") self.assertEqual(scenario.xml_name, "Invalid xml document")
def test_xml_name_property_get_3(self): scenario = ScenarioFactory(xml="<xml></xml>") self.assertEqual(scenario.xml_name, "Unnamed scenario")
def setUp(self): self.user = UserFactory() self.scenario = ScenarioFactory(user=self.user) self.client.login(username=self.user.username, password="******") self.url = reverse("ts_om.delete", kwargs={"scenario_id": self.scenario.id})
def test_demography_property_1(self): scenario = ScenarioFactory(xml=get_xml("tororo.xml")) self.assertEqual(scenario.demography.name, "Tororo")
def test_demography_property_2(self): scenario = ScenarioFactory(xml="xml") self.assertEqual(scenario.demography, "no_name")
class GetCtsDataViewTest(TestCase): def setUp(self): self.simulation = Simulation.objects.create(status=Simulation.COMPLETE) self.simulation.set_input_file(get_xml("scenario.xml")) self.simulation.set_output_file(get_xml("output.txt")) self.simulation.set_ctsout_file(get_xml("ctsout.txt")) self.simulation.set_model_stdout(get_xml("model_stdout_stderr.txt")) self.scenario = ScenarioFactory(xml=get_xml("scenario.xml"), new_simulation=self.simulation) self.user = self.scenario.user self.client.login(username=self.user, password="******") self.url = reverse("ts_om_viz.get_cts_data", kwargs={ "sim_id": self.scenario.new_simulation.id, "measure_name": "simulated EIR" }) def test_simulation_no_input_file(self): simulation = Simulation.objects.create(status=Simulation.COMPLETE) self.scenario.new_simulation = simulation self.scenario.save() url = reverse("ts_om_viz.get_cts_data", kwargs={ "sim_id": self.scenario.new_simulation.id, "measure_name": "simulated EIR" }) self.assertRaises(TypeError, self.client.get, url) def test_unknown_measure(self): url = reverse("ts_om_viz.get_cts_data", kwargs={ "sim_id": self.scenario.new_simulation.id, "measure_name": "simulated EIR1" }) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_success(self): response = self.client.get(self.url) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content["sim_id"], self.scenario.new_simulation.id) self.assertEqual(json_content["data"][0], 0.00223318) self.assertEqual(json_content["data"][-1], 0.00290952) self.assertEqual(json_content["measure_name"], "simulated EIR") self.assertIn("description", json_content) def test_success_no_output_file(self): self.simulation.output_file = None self.simulation.model_stdout = None self.simulation.save() response = self.client.get(self.url) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content["sim_id"], self.scenario.new_simulation.id) self.assertEqual(json_content["data"][0], 0.00223318) self.assertEqual(json_content["data"][-1], 0.00290952) self.assertEqual(json_content["measure_name"], "simulated EIR") self.assertIn("description", json_content) def test_anonymous_user(self): client = Client() response = client.get(self.url) self.assertEqual(response.status_code, 403) def test_different_user(self): client = Client() user = UserFactory() print user.username print self.user.username client.login(username=user.username, password="******") response = client.get(self.url) self.assertEqual(response.status_code, 403)