def test_that_only_locations_with_data_show_up(self):
        url = "/api/tests/preview/locations"
        gen_adult_record()
        gen_adult_record(cycle="cycle2")
        gen_adult_record(name="loc2", cycle="cycle2")
        gen_adult_record(name="loc3", cycle="cycle3", existing=None, new=None)
        params = DefinitionFactory().initial().get()
        response = self.app.post_json(url, user="******", params=params)
        json_response = response.content.decode("utf8")
        self.assertEqual(200, response.status_code)
        locations = loads(json_response).get("locations", [])
        self.assertEqual(2, len(locations))
        assert_that(
            locations,
            has_item(
                has_entries({
                    "name": equal_to("loc1"),
                    "district": equal_to("dis1"),
                    "cycles": equal_to(["cycle1", "cycle2"]),
                })),
        )

        assert_that(
            locations,
            has_item(
                has_entries({
                    "name": equal_to("loc2"),
                    "district": equal_to("dis1"),
                    "cycles": equal_to(["cycle2"]),
                })),
        )
 def test_result_with_adult_records(self):
     gen_adult_record()
     params = DefinitionFactory().sampled().are_equal().get()
     response = self.app.post_json(self.url,
                                   user="******",
                                   params=params,
                                   expect_errors=True)
     json_response = loads(response.content.decode("utf8"))
     self.assertEqual(200, response.status_code)
     assert_that(
         json_response,
         has_entry(
             equal_to("groups"),
             has_item(
                 has_entries({
                     "name":
                     equal_to("G1"),
                     "aggregation":
                     "SUM",
                     "values":
                     equal_to([["form_a", 1.0, 12.0]]),
                     "factored_values":
                     equal_to([["form_a", 1.0, 12.0]]),
                     "headers":
                     equal_to(["new", "existing"]),
                     "result":
                     equal_to(13.0),
                 })),
         ),
     )
    def test_result_with_consumption_records(self):
        gen_consumption_record(opening_balance=10, closing_balance=20)
        params = DefinitionFactory().sampled().are_equal().fields(
            "opening_balance",
            "closing_balance").model("Consumption").factors(form_a=10).get()
        response = self.app.post_json(self.url,
                                      user="******",
                                      params=params,
                                      expect_errors=True)
        json_response = loads(response.content.decode("utf8"))

        self.assertEqual(200, response.status_code)
        assert_that(
            json_response,
            has_entry(
                equal_to("groups"),
                has_item(
                    has_entries({
                        "name":
                        equal_to("G1"),
                        "aggregation":
                        "SUM",
                        "values":
                        equal_to([["form_a", 10.0, 20.0]]),
                        "factored_values":
                        equal_to([["form_a", 100.0, 200.0]]),
                        "headers":
                        equal_to(["opening_balance", "closing_balance"]),
                        "result":
                        equal_to(300.0),
                    })),
            ),
        )
 def test_get_preview_formulations_for_other_test(self):
     definition = Definition.from_dict(DefinitionFactory().initial().get())
     group = DefinitionGroup.from_dict({
         "model": {
             "id": "Adult"
         },
         "selected_formulations": ["a", "b"]
     })
     check = DBBasedCheckPreview(definition=definition)
     formulations = check.get_preview_formulations(group)
     self.assertEqual(len(formulations), 2)
     self.assertEqual(formulations, ["a", "b"])
 def test_that_models_can_be_overridden(self):
     gen_paed_record(formulation="form_tra")
     gen_paed_record(formulation="form_trb")
     gen_adult_record(formulation="form_tra", new=300, existing=300)
     definition_builder = DefinitionFactory().traced(
         tracer={
             "slug": "trace1",
             "patient_formulations": ["form_tra", "form_trb"]
         })
     definition_builder.model("Paed").model_overrides(
         {"trace1": {
             "id": "Adult",
             "formulations": ["form_tra"]
         }})
     definition_builder.tracing_formulations("patient_formulations",
                                             "form_tra", "form_trb")
     definition_builder.are_equal()
     definition = definition_builder.get()
     response = self.app.post_json(self.url,
                                   user="******",
                                   params=definition,
                                   expect_errors=True)
     json_response = loads(response.content.decode("utf8"))
     self.assertEqual(200, response.status_code)
     assert_that(
         json_response,
         has_entry(
             equal_to("groups"),
             has_item(
                 has_entries({
                     "name":
                     equal_to("G1"),
                     "aggregation":
                     "SUM",
                     "values":
                     equal_to([["form_tra", 300.0, 300.0]]),
                     "factored_values":
                     equal_to([["form_tra", 300.0, 300.0]]),
                     "headers":
                     equal_to(["new", "existing"]),
                     "result":
                     equal_to(600.0),
                 })),
         ),
     )
 def test_that_a_sample_is_required(self):
     response = self.app.post_json(
         self.url,
         user="******",
         params=DefinitionFactory().initial().get(),
         expect_errors=True,
     )
     json_response = loads(response.content.decode("utf8"))
     self.assertEqual(400, response.status_code)
     assert_that(
         json_response,
         has_entry(equal_to("sample"),
                   has_item(equal_to("This field is required."))),
     )
    def test_two_facility_with_tracing_type(self):
        gen_consumption_record(formulation="form_tra",
                               opening_balance=10,
                               closing_balance=20)
        params = DefinitionFactory().traced(
            tracer={
                "slug": "trace1",
                "formulations": ["form_tra", "form_trb"]
            }).model("Consumption").tracing_formulations(
                "consumption_formulations", "form_tra",
                "form_trb").fields("opening_balance",
                                   "closing_balance").are_equal().get()
        response = self.app.post_json(self.url,
                                      user="******",
                                      params=params,
                                      expect_errors=True)
        json_response = loads(response.content.decode("utf8"))

        self.assertEqual(200, response.status_code)
        assert_that(
            json_response,
            has_entry(
                equal_to("groups"),
                has_item(
                    has_entries({
                        "name":
                        equal_to("G1"),
                        "aggregation":
                        "SUM",
                        "values":
                        equal_to([["form_tra", 10.0, 20.0]]),
                        "factored_values":
                        equal_to([["form_tra", 10.0, 20.0]]),
                        "headers":
                        equal_to(["opening_balance", "closing_balance"]),
                        "result":
                        equal_to(30.0),
                    })),
            ),
        )
 def test_get_preview_formulations_for_traced_test(self):
     definition = Definition.from_dict(DefinitionFactory().traced().get())
     tracing_formulations = [
         tf.as_dict_obj() for tf in TracingFormulations.objects.all()
     ]
     model = {"id": "Adult", "tracingFormulations": tracing_formulations}
     group = DefinitionGroup.from_dict({"model": model})
     check = DBBasedCheckPreview(definition=definition)
     formulations = check.get_preview_formulations(group)
     self.assertEqual(len(formulations), 7)
     self.assertEqual(
         formulations,
         [
             u"TDF/3TC/EFV (PMTCT)",
             u"TDF/3TC/EFV (ADULT)",
             u"ABC/3TC/LPV/r",
             u"ABC/3TC/EFV",
             u"ABC/3TC/NVP",
             u"ABC/3TC/EFV",
             u"AZT/3TC/EFV",
         ],
     )
    def test_no_negatives(self):
        gen_consumption_record(formulation="form1",
                               opening_balance=10,
                               closing_balance=20)
        gen_consumption_record(formulation="form2",
                               opening_balance=40,
                               closing_balance=30)
        definition_builder = DefinitionFactory().sampled().formulations(
            "form1", "form2")
        definition_builder.fields("opening_balance", "closing_balance")
        definition_builder.model("Consumption")
        definition_builder.aggregation("VALUE")
        definition_builder.has_no_negatives()
        response = self.app.post_json(
            self.url,
            user="******",
            params=definition_builder.get(),
            expect_errors=True,
        )
        json_response = loads(response.content.decode("utf8"))

        self.assertEqual(200, response.status_code)
        # assert_that(json_response, has_entry(equal_to("groups"), has_item(
        #     has_entries(
        #         {
        #             "name": equal_to('G1'),
        #             "aggregation": "VALUES",
        #             "values": equal_to([['form1', 10.0, 20.0], ['form2', 40.0, 30.0]]),
        #             "headers": equal_to(['opening_balance', 'closing_balance']),
        #             "result": equal_to(100.0)
        #         }
        #
        #     )
        # )))
        assert_that(
            json_response,
            has_entry(
                equal_to("groups"),
                has_item(
                    has_entries({
                        "name":
                        equal_to("G2"),
                        "aggregation":
                        "VALUE",
                        "values":
                        equal_to([["form1", 10.0, 20.0], ["form2", 40.0,
                                                          30.0]]),
                        "headers":
                        equal_to(["opening_balance", "closing_balance"]),
                        "result":
                        equal_to([10.0, 20.0, 40.0, 30.0]),
                    })),
            ),
        )

        assert_that(
            json_response,
            has_entry(equal_to("result"),
                      has_entries({"DEFAULT": equal_to("YES")})),
        )
    def test_less_than(self):
        gen_consumption_record(formulation="form1",
                               opening_balance=10,
                               closing_balance=20)
        gen_consumption_record(formulation="form2",
                               opening_balance=40,
                               closing_balance=30)
        definition_builder = DefinitionFactory().sampled().formulations(
            "form1", "form2")
        definition_builder.fields("opening_balance", "closing_balance")
        definition_builder.model("Consumption")
        definition_builder.percentage_variance_is_less_than(50)
        response = self.app.post_json(
            self.url,
            user="******",
            params=definition_builder.get(),
            expect_errors=True,
        )
        json_response = loads(response.content.decode("utf8"))

        self.assertEqual(200, response.status_code)
        assert_that(
            json_response,
            has_entry(
                equal_to("groups"),
                has_item(
                    has_entries({
                        "name":
                        equal_to("G1"),
                        "aggregation":
                        "SUM",
                        "values":
                        equal_to([["form1", 10.0, 20.0], ["form2", 40.0,
                                                          30.0]]),
                        "headers":
                        equal_to(["opening_balance", "closing_balance"]),
                        "result":
                        equal_to(100.0),
                    })),
            ),
        )
        assert_that(
            json_response,
            has_entry(
                equal_to("groups"),
                has_item(
                    has_entries({
                        "name":
                        equal_to("G2"),
                        "aggregation":
                        "SUM",
                        "values":
                        equal_to([["form1", 10.0, 20.0], ["form2", 40.0,
                                                          30.0]]),
                        "headers":
                        equal_to(["opening_balance", "closing_balance"]),
                        "result":
                        equal_to(100.0),
                    })),
            ),
        )

        assert_that(
            json_response,
            has_entry(equal_to("result"),
                      has_entries({"DEFAULT": equal_to("YES")})),
        )
Esempio n. 11
0
 def test_can_get_check_by_type(self):
     definition = DefinitionFactory().initial().getDef()
     check = get_check(definition)
     self.assertIsInstance(check, UserDefinedFacilityCheck)
Esempio n. 12
0
 def test_can_get_check_by_class_name(self):
     definition = DefinitionFactory().based_on_class(
         "dashboard.checks.legacy.blanks.MultipleCheck"
     ).getDef()
     check = get_check(definition)
     self.assertIsInstance(check, MultipleCheck)
 def test_get_result_key_should_return_key_for_tracer(self):
     definition = Definition.from_dict(DefinitionFactory().initial().get())
     check = DBBasedCheckPreview(definition=definition)
     key = check.get_result_key(Tracer.F1())
     self.assertEqual(key, "tdf3tcefv-adult")
 def test_get_result_key_should_return_default_if_no_tracer_is_given(self):
     definition = Definition.from_dict(DefinitionFactory().initial().get())
     check = DBBasedCheckPreview(definition=definition)
     key = check.get_result_key(None)
     self.assertEqual(key, "DEFAULT")