def test_pck_transformer_total_playback_qss_missing_data_from_mapping(self):
        """
        For QSS (Stocks), downstream needs to calculate the start and end of period totals.
        It does this with a mapping in the pck_transformer.  If a new formtype is added but it's not
        added to the mapping or a 'start' or 'end' key isn't present then a KeyError exception is thrown.
        """

        scenarios = ["9999", "0033"]
        for form_type in scenarios:
            survey = {'survey_id': "017"}
            response = {
                "collection": {
                    "instrument_id": form_type
                },
                "data": {
                    "15": "Yes",
                    "139": "7300",
                    "140": "7680",
                    "144": "2000",
                    "145": "2205",
                    "146": "A lot of changes.",
                    "149": "1800",
                    "150": "12205",
                }
            }

            pck_transformer = PCKTransformer(survey, response)
            pck_transformer.qss_questions = {
                "0033": {
                    "end": ['140', '145', '150']
                }
            }
            with self.assertRaises(KeyError):
                pck_transformer.calculate_total_playback()
    def test_pck_transformer_calculates_total_playback_qss():
        """
        For QSS (Stocks), downstream needs to calculate the start and end of period totals.
        The fields that are added together are defined in a dictionary in the pck_transformer
        """
        scenarios = ["0001", "0002"]
        for form_type in scenarios:
            survey = {'survey_id': "017"}
            response = {
                "collection": {
                    "instrument_id": form_type
                },
                "data": {
                    "15": "Yes",
                    "139": "7300",
                    "140": "7680",
                    "144": "2000",
                    "145": "2205",
                    "146": "A lot of changes.",
                    "149": "1800",
                    "150": "12205",
                }
            }

            pck_transformer = PCKTransformer(survey, response)
            pck_transformer.calculate_total_playback()

            assert pck_transformer.data == {
                '15': "Yes",
                '65': '11100',
                '66': '22090',
                '139': '7300',
                '140': '7680',
                '144': '2000',
                '145': '2205',
                '146': 'A lot of changes.',
                '149': '1800',
                '150': '12205'
            }
    def test_pck_transformer_calculates_total_playback_qcas(self):
        """
        For QCAS, downstream needs the calculated values for both acquisitions
        and proceeds from disposals to be sent in the PCK.
        """
        survey = {'survey_id': '019'}
        response = {
            "collection": {
                "instrument_id": "0020"
            },
            "data": {
                "11": "03/07/2018",
                "12": "01/10/2018",
                "146": "A lot of changes.",

                # Disposals
                "689": "499",
                "696": "500",
                "704": "12345.67",
                "708": "12345500",
                "710": "-499",
                "712": "-12345.67",

                # Construction
                "681": "1000",

                # Acquisitions
                "688": "1500",
                "695": "1500",
                "703": "1500",
                "707": "1500",
                "709": "1500",
                "711": "1500",

                # Mineral
                "697": "-1500",

                "146a": "Yes",
                "146b": "Start or end of a long term project",
                "146c": "Site changes, for example, openings, closures, refurbishments or upgrades",
                "146d": "End of accounting period or financial year",
                "146e": "Normal movement for time of year",
                "146f": "Change of business structure, merger, or takeover",
                "146g": "One off or unusual investment",
                "146h": "Introduction / removal of new legislation / incentive",
                "146i": "Availability of credit",
                "146j": "Overspend during the previous quarter",
                "146k": "Other",
                "d12": "Yes",
                "d681": "Yes"
            }
        }

        pck_transformer = PCKTransformer(survey, response)

        pck_transformer.round_numeric_values()

        assert pck_transformer.data['689'] == '0'
        assert pck_transformer.data['696'] == '1'
        assert pck_transformer.data['704'] == '12'
        assert pck_transformer.data['708'] == '12346'
        assert pck_transformer.data['710'] == '-0'
        assert pck_transformer.data['712'] == '-12'

        pck_transformer.calculate_total_playback()

        # Total value of acquisitions questions for only machinery and equipments section
        assert pck_transformer.data['714'] == '12'

        # Total value of disposals questions for only machinery and equipments section
        assert pck_transformer.data['715'] == '12347'

        # Total value of all acquisitions questions
        assert pck_transformer.data['692'] == '11'

        # Total value of all disposals questions (same as '715' since constructions section and minerals sections does not have disposals question)
        assert pck_transformer.data['693'] == '12347'
    def test_pck_transformer_calculates_total_playback_qcas(self):
        """
        For QCAS, downstream needs the calculated values for both acquisitions
        and proceeds from disposals to be sent in the PCK.
        """
        survey = {'survey_id': '019'}
        response = {
            "collection": {
                "instrument_id": "0020"
            },
            "data": {
                "11": "03/07/2018",
                "12": "01/10/2018",
                "146": "A lot of changes.",

                # Disposals
                "689": "499",
                "696": "500",
                "704": "12345.67",
                "708": "12345500",
                "710": "-499",
                "712": "-12345.67",

                # Construction
                "681": "1000",

                # Acquisitions
                "688": "1500",
                "695": "1500",
                "703": "1500",
                "707": "1500",
                "709": "1500",
                "711": "1500",

                # Mineral
                "697": "-1500",

                "146a": "Yes",
                "146b": "Start or end of a long term project",
                "146c": "Site changes, for example, openings, closures, refurbishments or upgrades",
                "146d": "End of accounting period or financial year",
                "146e": "Normal movement for time of year",
                "146f": "Change of business structure, merger, or takeover",
                "146g": "One off or unusual investment",
                "146h": "Introduction / removal of new legislation / incentive",
                "146i": "Availability of credit",
                "146j": "Overspend during the previous quarter",
                "146k": "Other",
                "d12": "Yes",
                "d681": "Yes"
            }
        }

        pck_transformer = PCKTransformer(survey, response)

        pck_transformer.round_numeric_values()

        assert pck_transformer.data['689'] == '0'
        assert pck_transformer.data['696'] == '1'
        assert pck_transformer.data['704'] == '12'
        assert pck_transformer.data['708'] == '12346'
        assert pck_transformer.data['710'] == '-0'
        assert pck_transformer.data['712'] == '-12'

        pck_transformer.calculate_total_playback()

        # Total value of acquisitions questions for only machinery and equipments section
        assert pck_transformer.data['714'] == '12'

        # Total value of disposals questions for only machinery and equipments section
        assert pck_transformer.data['715'] == '12347'

        # Total value of all acquisitions questions
        assert pck_transformer.data['692'] == '11'

        # Total value of all disposals questions (same as '715' since constructions section and minerals sections does not have disposals question)
        assert pck_transformer.data['693'] == '12347'