Ejemplo n.º 1
0
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")
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
 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")
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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)