def test_records_are_given___records_are_written_to_oasis_keys_files_correctly(self, successes, nonsuccesses):

        oasis_keys_file_to_record_metadict = {
            'LocID': 'id',
            'PerilID': 'peril_id',
            'CoverageID': 'coverage',
            'AreaPerilID': 'area_peril_id',
            'VulnerabilityID': 'vulnerability_id'
        }
        oasis_keys_error_file_to_record_metadict = {
            'LocID': 'id',
            'PerilID': 'peril_id',
            'CoverageID': 'coverage',
            'Message': 'message'
        }

        with TemporaryDirectory() as d:
            keys_file_path = os.path.join(d, 'keys.csv')
            keys_error_file_path = os.path.join(d, 'keys-errors.csv')

            _, successes_count = OasisKeysLookupFactory.write_oasis_keys_file(successes, keys_file_path)
            _, nonsuccesses_count = OasisKeysLookupFactory.write_oasis_keys_error_file(nonsuccesses, keys_error_file_path)

            with io.open(keys_file_path, 'r', encoding='utf-8') as f1, io.open(keys_error_file_path, 'r', encoding='utf-8') as f2:
                written_successes = [dict((oasis_keys_file_to_record_metadict[k], r[k]) for k in r) for r in pd.read_csv(f1).T.to_dict().values()]
                written_nonsuccesses = [dict((oasis_keys_error_file_to_record_metadict[k], r[k]) for k in r) for r in pd.read_csv(f2).T.to_dict().values()]

            success_matches = list(filter(lambda r: (r['id'] == ws['id'] for ws in written_successes), successes))
            nonsuccess_matches = list(filter(lambda r: (r['id'] == ws['id'] for ws in written_nonsuccesses), nonsuccesses))

            self.assertEqual(successes_count, len(successes))
            self.assertEqual(success_matches, successes)

            self.assertEqual(nonsuccesses_count, len(nonsuccesses))
            self.assertEqual(nonsuccess_matches, nonsuccesses)
Пример #2
0
    def action(self, args):
        """
        Generates a new a model testing dockerfile from the supplied template

        :param args: The arguments from the command line
        :type args: Namespace
        """
        dockerfile_src = os.path.join(os.path.dirname(__file__),
                                      os.path.pardir, '_data',
                                      'Dockerfile.model_api_tester')

        version_file = args.model_version_file or os.path.join(
            args.model_data_directory, 'ModelVersion.csv')
        version_info = OasisKeysLookupFactory.get_model_info(version_file)

        dockerfile_dst = os.path.join(
            args.model_data_directory,
            'Dockerfile.{}_{}_model_api_tester'.format(
                version_info['supplier_id'].lower(),
                version_info['model_id'].lower()),
        )

        replace_in_file(dockerfile_src, dockerfile_dst,
                        ['%{}%'.format(s) for s in self.var_names], [
                            __version__,
                            args.api_server_url,
                            version_info['supplier_id'],
                            version_info['model_id'],
                            version_info['model_version_id'],
                            args.model_data_directory,
                        ])

        self.logger.info('File created at {}.'.format(dockerfile_dst))
        return 0
    def test_entries_are_dictionaries_success_only_is_false___all_entries_are_included(self, data):
        with patch('oasislmf.keys.lookup.OasisKeysLookupFactory.get_model_exposures'):
            self.create_fake_lookup(return_value=data)

            res = list(OasisKeysLookupFactory.get_keys(lookup=self.lookup_instance, model_exposures_file_path='path', success_only=False))

            self.assertEqual(res, data)
    def test_records_are_given___records_are_written_to_json_keys_files_correctly(self, successes, nonsuccesses):

        with TemporaryDirectory() as d:
            keys_file_path = os.path.join(d, 'keys.json')
            keys_error_file_path = os.path.join(d, 'keys-errors.json')

            _, successes_count = OasisKeysLookupFactory.write_json_keys_file(successes, keys_file_path)
            _, nonsuccesses_count = OasisKeysLookupFactory.write_json_keys_file(nonsuccesses, keys_error_file_path)

            with io.open(keys_file_path, 'r', encoding='utf-8') as f1, io.open(keys_error_file_path, 'r', encoding='utf-8') as f2:
                written_successes = json.load(f1)
                written_nonsuccesses = json.load(f2)

            self.assertEqual(successes_count, len(successes))
            self.assertEqual(written_successes, successes)

            self.assertEqual(nonsuccesses_count, len(nonsuccesses))
            self.assertEqual(written_nonsuccesses, nonsuccesses)
    def test_produced_keys_are_passed_to_write_oasis_keys_file(self, data):
        with TemporaryDirectory() as d,\
             patch('oasislmf.keys.lookup.OasisKeysLookupFactory.get_keys', Mock(return_value=(r for r in data))) as get_keys_mock,\
             patch('oasislmf.keys.lookup.OasisKeysLookupFactory.write_oasis_keys_file') as write_oasis_keys_file_mock:

            keys_file_path = os.path.join(d, 'piwind-keys.csv')
            OasisKeysLookupFactory.save_keys(
                lookup=self.create_fake_lookup(),
                keys_file_path=keys_file_path,
                model_exposures=json.dumps(data)
            )

            get_keys_mock.assert_called_once_with(
                lookup=self.lookup_instance,
                model_exposures=json.dumps(data),
                model_exposures_file_path=None,
                success_only=True
            )
            write_oasis_keys_file_mock.assert_called_once_with(data, keys_file_path)
    def test_exposures_string_is_provided___file_content_is_loaded(self, data):
        stream = StringIO()
        data = [('first', 'second')] + data

        csv.writer(stream).writerows(data)

        res = OasisKeysLookupFactory.get_model_exposures(model_exposures=stream.getvalue())
        res = [tuple(res)] + [tuple(res.iloc[i]) for i in range(len(res))]

        self.assertEqual(res, data)
    def test_file_is_provided___file_content_is_loaded(self, data):
        data = [('first', 'second')] + data

        with NamedTemporaryFile('w') as f:
            csv.writer(f).writerows(data)
            f.flush()

            res = OasisKeysLookupFactory.get_model_exposures(model_exposures_file_path=f.name)
            res = [tuple(res)] + [tuple(res.iloc[i]) for i in range(len(res))]

            self.assertEqual(res, data)
    def test_keys_path_is_not_supplied___correct_instance_is_created_with_correct_model_info(self, supplier, model, version):
        with TemporaryDirectory() as d:
            version_path = os.path.join(d, 'version.csv')
            self.write_version_file(supplier, model, version, version_path)

            module_path = os.path.join(d, '{}_lookup.py'.format(model))
            self.write_py_module(model, module_path)

            _, instance = OasisKeysLookupFactory.create(
                model_version_file_path=version_path,
                lookup_package_path=module_path,
            )

            self.assertEqual(type(instance).__name__, '{}KeysLookup'.format(model))
            self.assertEqual(instance.supplier, supplier)
            self.assertEqual(instance.model_name, model)
            self.assertEqual(instance.model_version, version)
            self.assertEqual(instance.keys_data_directory, os.path.join(os.sep, 'var', 'oasis', 'keys_data'))
 def test_no_model_exposures_are_provided___oasis_exception_is_raised(self):
     with self.assertRaises(OasisException):
         list(OasisKeysLookupFactory.get_keys(self.create_fake_lookup()))
    def test_model_exposures_are_provided___exposures_are_passed_to_get_model_exposures_result_is_passed_to_lookup_process_locations(self, exposures, result):
        with patch('oasislmf.keys.lookup.OasisKeysLookupFactory.get_model_exposures', Mock(return_value=result)):
            list(OasisKeysLookupFactory.get_keys(self.create_fake_lookup(), model_exposures=exposures))

            OasisKeysLookupFactory.get_model_exposures.assert_called_once_with(model_exposures=exposures, model_exposures_file_path=None)
            self.lookup_instance.process_locations.assert_called_once_with(result)
 def test_no_file_or_exposures_are_provided___oasis_exception_is_raised(self):
     with self.assertRaises(OasisException):
         OasisKeysLookupFactory.get_model_exposures()