def _group_values_from_db(self, group, cycle, sample_location, tracer): model = group.model.as_model() if group.has_overrides: model_id = group.sample_formulation_model_overrides.get( tracer.key, {}).get("id", None) if model_id: model = group.model.as_model(model_id) if model: formulations = self.get_formulations(group, tracer) db_records = model.objects.filter( name=sample_location["name"], cycle=parse_cycle(cycle, group), district=sample_location["district"], formulation__in=formulations, ).values_list("formulation", *group.selected_fields) records = [ DataRecord(formulation=r[0], fields=group.selected_fields, values=r[1:]) for r in db_records ] factored_values = get_factored_records(group.factors, records) return GroupResult( group=group, values=records, factored_records=factored_values, tracer=tracer, aggregate=self.aggregate_values(group, factored_values), )
def _map(item): values = [ v for i, v in enumerate(pick(attr.asdict(item), fields).values()) ] return DataRecord(formulation=item.formulation, fields=fields, values=values)
def test_get_factored_values_when_factors_set(self): result = get_factored_records( {}, [ DataRecord.from_list(["formA", 1, 3]), DataRecord.from_list(["formB", 2, 3]), DataRecord.from_list(["formA", 0, 0]), ], ) self.assertEqual( result, [ DataRecord.from_list(["formA", 1, 3]), DataRecord.from_list(["formB", 2, 3]), DataRecord.from_list(["formA", 0, 0]), ], )
def test_get_factored_values_when_some_fields_are_missing(self): result = get_factored_records( {"formA": 2}, [ DataRecord.from_list(["formA", 1, 3]), DataRecord.from_list(["formB", 2, 3]), DataRecord.from_list(["formA", 0, 0]), ], ) self.assertEqual( result, [ DataRecord.from_list(["formA", 2, 6]), DataRecord.from_list(["formB", 2, 3]), DataRecord.from_list(["formA", 0, 0]), ], )
def test_get_factored_values_when_factors_set_to_decimals(self): result = get_factored_records( { "formA": 0.1, "formB": 2.5 }, [ DataRecord.from_list(["formA", 1, 3]), DataRecord.from_list(["formB", 2, 3]), DataRecord.from_list(["formA", 0, 0]), ], ) self.assertEqual( result, [ DataRecord.from_list(["formA", 0.1, 3 * 0.1]), DataRecord.from_list(["formB", 5.0, 7.5]), DataRecord.from_list(["formA", 0, 0]), ], )
def test_get_factored_values(self): result = get_factored_records( { "formA": 2, "formB": 1 }, [ DataRecord.from_list(["formA", 1, 3]), DataRecord.from_list(["formB", 2, 3]), DataRecord.from_list(["formA", 0, 0]), ], ) self.assertEqual( result, [ DataRecord.from_list(["formA", 2.0, 6.0]), DataRecord.from_list(["formB", 2.0, 3.0]), DataRecord.from_list(["formA", 0.0, 0.0]), ], )
def test_get_factored_values_when_factors_set_to_strings(self): result = get_factored_records( { "formA": "Asd", "existing": "a" }, [ DataRecord.from_list(["formA", 1, 3]), DataRecord.from_list(["formB", 2, 3]), DataRecord.from_list(["formA", 0, 0]), ], ) self.assertEqual( result, [ DataRecord.from_list(["formA", 1, 3]), DataRecord.from_list(["formB", 2, 3]), DataRecord.from_list(["formA", 0, 0]), ], )
) from dashboard.checks.entities import ( GroupResult, DefinitionGroup, GroupModel, DefinitionOption, DataRecord, ) from dashboard.checks.tracer import Tracer current_cycle = DefinitionOption(id="Current", name="Current Cycle") previous_cycle = DefinitionOption(id="Previous", name="Previous Cycle") sum_comparison = DefinitionOption(id="SUM", name="SUM") r1 = DataRecord( formulation= "Tenofovir/Lamivudine/Efavirenz (TDF/3TC/EFV) 300mg/300mg/600mg[Pack 30]", values=[50.0], fields=["consumption"], ) r2 = DataRecord( formulation= "Tenofovir/Lamivudine/Efavirenz (TDF/3TC/EFV) 300mg/300mg/600mg[Pack 30]", values=[0], fields=["consumption"], ) tracer = Tracer( key=u"tdf3tcefv-adult", consumption_formulations=[ u"Tenofovir/Lamivudine/Efavirenz (TDF/3TC/EFV) 300mg/300mg/600mg[Pack 30]" ], patient_formulations=[u"TDF/3TC/EFV (PMTCT)", u"TDF/3TC/EFV (ADULT)"], extras=None,
def test_get_factored_values_when_factors_set_to_decimals_d(self): result = get_factored_records({ "formA": 0.1, "formB": 2.5 }, [DataRecord.from_list(["formA", 1, None])]) self.assertEqual(result, [DataRecord.from_list(["formA", 0.1, None])])