def setUpClass(cls):
     factory = DataFactory()
     cls.months = factory.create_months("2019-01-01", 3)
     # This practice won't do any prescribing but it will have practice
     # statistics so it should still show up in our data
     cls.non_prescribing_practice = factory.create_practice()
     # Create some practices that prescribe during the period
     cls.prescribing_practices = factory.create_practices(3)
     # Create some presentations and some prescribing
     cls.presentations = factory.create_presentations(4)
     cls.prescribing = factory.create_prescribing(
         cls.presentations, cls.prescribing_practices, cls.months
     )
     # Create practices statistics for all active practices
     cls.active_practices = cls.prescribing_practices + [
         cls.non_prescribing_practice
     ]
     cls.practice_statistics = factory.create_practice_statistics(
         cls.active_practices, cls.months
     )
     # Create a presentation which changes its BNF code and create some
     # prescribing with both old and new codes
     cls.presentation_to_update = factory.create_presentation()
     cls.updated_presentation = factory.update_bnf_code(cls.presentation_to_update)
     cls.prescribing_with_old_code = factory.create_prescribing(
         [cls.presentation_to_update], cls.active_practices, cls.months
     )
     cls.prescribing_with_new_code = factory.create_prescribing(
         [cls.updated_presentation], cls.active_practices, cls.months
     )
     # Create a presenation without creating any prescribing for it so we can
     # check it doesn't appear in the final file
     cls.presentation_without_prescribing = factory.create_presentation()
     # We deliberately import data for fewer months than we've created so we
     # can test that only the right data is included
     cls.months_to_import = cls.months[1:]
     # This practice only has data for the month we don't import, so it
     # shouldn't show up at all in our data
     cls.closed_practice = factory.create_practice()
     factory.create_prescription(
         cls.presentations[0], cls.closed_practice, cls.months[0]
     )
     factory.create_statistics_for_one_practice_and_month(
         cls.closed_practice, cls.months[0]
     )
     cls.data_factory = factory
     # The format of `end_date` only uses year and month
     cls.end_date = max(cls.months_to_import)[:7]
     cls.number_of_months = len(cls.months_to_import)
     cls.create_matrixstore(factory, cls.end_date, cls.number_of_months)
示例#2
0
    def setUpTestData(cls):
        # Create some sets of substitutable presentations, which each consist
        # of a generic plus some branded equivalents
        substitution_sets = []
        for i in range(4):
            generic_code = invent_generic_bnf_code(i)
            brands = invent_brands_from_generic_bnf_code(generic_code)
            substitution_sets.append([generic_code] + brands)

        # Create some practices and some prescribing for these presentations
        factory = DataFactory()
        factory.create_months("2020-01-01", 2)
        factory.create_practices(10)
        for bnf_codes in substitution_sets:
            for bnf_code in bnf_codes:
                factory.create_presentation(bnf_code=bnf_code)
        factory.create_prescribing(factory.presentations, factory.practices,
                                   factory.months)

        # The DataFactory creates data that can be written to the MatrixStore
        # but doesn't automatically create anything in the database, so we do
        # that manually here
        ccg = PCT.objects.create(name="CCG1", code="ABC", org_type="CCG")
        for practice in factory.practices:
            Practice.objects.create(name=practice["name"],
                                    code=practice["code"],
                                    setting=4,
                                    ccg=ccg)
        for presentation in factory.presentations:
            Presentation.objects.create(bnf_code=presentation["bnf_code"],
                                        name=presentation["name"])

        cls.substitution_sets = substitution_sets
        cls.factory = factory
        cls._remove_patch = patch_global_matrixstore(
            matrixstore_from_data_factory(factory))
        # Clear the cache on this memoized function
        get_substitution_sets.cache_clear()