def get(self):
        param = self.request.matchdict.get('param')

        if param in self.valid_params:
            try:
                data = str_to_obj(self.db.get(param))
            except ValueError:
                data = None
                logger.warn('Cache is empty!',
                            extra=journal_context({'MESSAGE_ID': API_INFO},
                                                  {}))

            if data:
                response = Response(body=json.dumps(data),
                                    content_type='application/json',
                                    status=200)
            else:
                file_path = os.path.join(self.DATA_PATH,
                                         '{}.json'.format(param))
                response = FileResponse(path=file_path,
                                        request=self.request,
                                        content_type='application/json')
        else:
            msg = 'URL parameter must be "inn.json", "atc.json", "inn2atc.json" or "atc2inn.json"'
            data = {
                'success': False,
                'error': True,
                'message':
                'URL parameter "{}" not valid. {}'.format(param, msg)
            }
            response = Response(body=json.dumps(data),
                                content_type='application/json',
                                status=400)

        return response
Beispiel #2
0
    def test_str_to_obj(self):
        self.assertEqual(str_to_obj('\'test\''), 'test')
        self.assertEqual(str_to_obj('{1: 1}'), {1: 1})
        self.assertEqual(str_to_obj('''{'test': 'test'}'''), {'test': 'test'})
        self.assertEqual(str_to_obj('[1, 2, 3]'), [1, 2, 3])

        with self.assertRaises(SyntaxError):
            str_to_obj('')

        with self.assertRaises(TypeError):
            str_to_obj()
    def test_api_with_valid_params(self):
        self.app.authorization = ('Basic', ('brokername', 'brokername'))
        request_path = '{}/registry/'.format(ROUTE_PREFIX)

        # get ATC data without cache
        response = self.app.get(request_path + 'atc.json', status=200)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content_type, 'application/json')
        data = response.json
        self.assertTrue(any([True for i in INITIAL_ATC_KEYS_DATA if i in data.get('data')]))
        self.assertFalse(self.db.get('atc'))

        # get ATC data with cache
        services_not_available = event.Event()
        services_not_available.set()

        json_former = JsonFormer(
            self.db, delay=config.get('delay'),
            json_files_delay=config.get('json_files_delay'),
            cache_monitoring_delay=config.get('cache_monitoring_delay'),
            services_not_available=services_not_available
        )
        json_former._update_cache('atc')

        response = self.app.get(request_path + 'atc.json', status=200)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content_type, 'application/json')
        data = response.json
        self.assertTrue(self.db.get('atc'))
        self.assertTrue(any([True for i in INITIAL_ATC_KEYS_DATA if i in data.get('data')]))
        self.assertEqual(data, str_to_obj(self.db.get('atc')))

        # get INN data without cache
        response = self.app.get(request_path + 'inn.json', status=200)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content_type, 'application/json')
        data = response.json
        self.assertTrue(any([True for i in INITIAL_INN_KEYS_DATA if i in data.get('data')]))
        self.assertFalse(self.db.get('inn'))

        # get INN data with cache
        json_former._update_cache('inn')

        response = self.app.get(request_path + 'inn.json', status=200)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content_type, 'application/json')
        data = response.json
        self.assertTrue(self.db.get('inn'))
        self.assertTrue(any([True for i in INITIAL_INN_KEYS_DATA if i in data.get('data')]))
        self.assertEqual(data, str_to_obj(self.db.get('inn')))

        # get INN2ATC data without cache
        response = self.app.get(request_path + 'inn2atc.json', status=200)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content_type, 'application/json')
        data = response.json
        self.assertTrue(any([True for i in INITIAL_INN_KEYS_DATA if i in data.get('data')]))
        self.assertFalse(self.db.get('inn2atc'))

        # get INN2ATC data with cache
        json_former._update_cache('inn2atc')

        response = self.app.get(request_path + 'inn2atc.json', status=200)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content_type, 'application/json')
        data = response.json
        self.assertTrue(self.db.get('inn2atc'))
        self.assertTrue(any([True for i in INITIAL_INN_KEYS_DATA if i in data.get('data')]))
        self.assertEqual(data, str_to_obj(self.db.get('inn2atc')))

        # get ATC2INN data without cache
        response = self.app.get(request_path + 'atc2inn.json', status=200)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content_type, 'application/json')
        data = response.json
        self.assertTrue(any([True for i in INITIAL_ATC_KEYS_DATA if i in data.get('data')]))
        self.assertFalse(self.db.get('atc2inn'))

        # get ATC2INN data with cache
        json_former._update_cache('atc2inn')

        response = self.app.get(request_path + 'atc2inn.json', status=200)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content_type, 'application/json')
        data = response.json
        self.assertTrue(self.db.get('atc2inn'))
        self.assertTrue(any([True for i in INITIAL_ATC_KEYS_DATA if i in data.get('data')]))
        self.assertEqual(data, str_to_obj(self.db.get('atc2inn')))
Beispiel #4
0
    def test_update_json(self):
        self.worker = JsonFormer(self.db, config.get('delay'),
                                 config.get('json_files_delay'),
                                 config.get('cache_monitoring_delay'),
                                 config.get('services_not_available'))

        self.worker.DATA_PATH = self.DATA_PATH
        self.worker.registry_xml = os.path.join(self.worker.DATA_PATH,
                                                'registry.xml')
        self.worker.inn_json = os.path.join(self.worker.DATA_PATH, 'inn.json')
        self.worker.atc_json = os.path.join(self.worker.DATA_PATH, 'atc.json')
        self.worker.inn2atc_json = os.path.join(self.worker.DATA_PATH,
                                                'inn2atc.json')
        self.worker.atc2inn_json = os.path.join(self.worker.DATA_PATH,
                                                'atc2inn.json')

        # updates json`s with empty registry
        self.assertTrue(
            file_exists(os.path.join(self.worker.DATA_PATH, 'registry.xml')))
        self.assertTrue(
            file_is_empty(os.path.join(self.worker.DATA_PATH, 'registry.xml')))

        json_names = ['mnn', 'atc1', 'inn2atc', 'atc2inn']
        for name in json_names:
            self.assertEqual(self.worker.update_json(name), None)

        # updates json`s with registry with invalid data
        with open(os.path.join(self.BASE_DIR, 'test_registry.xml'), 'r') as f:
            xml = f.read()

        with open(os.path.join(self.worker.DATA_PATH, 'registry.xml'),
                  'w') as f:
            f.write(xml)

        self.assertFalse(
            file_is_empty(os.path.join(self.worker.DATA_PATH, 'registry.xml')))

        # update json`s with invalid xml tag
        self.assertEqual(self.worker.update_json('xxx'), None)

        # update inn json with valid xml tag
        self.assertEqual(file_exists(self.worker.inn_json), False)
        self.assertEqual(file_exists(self.worker.atc_json), False)

        create_file(os.path.join(self.worker.DATA_PATH, 'inn.json'))
        create_file(os.path.join(self.worker.DATA_PATH, 'atc.json'))
        create_file(os.path.join(self.worker.DATA_PATH, 'inn2atc.json'))
        create_file(os.path.join(self.worker.DATA_PATH, 'atc2inn.json'))

        self.assertEqual(file_exists(self.worker.inn_json), True)
        self.assertEqual(file_is_empty(self.worker.inn_json), True)

        self.assertEqual(file_exists(self.worker.atc_json), True)
        self.assertEqual(file_is_empty(self.worker.atc_json), True)

        self.assertFalse(file_is_empty(self.worker.registry_xml))

        self.worker.update_json('mnn')
        self.assertFalse(file_is_empty(self.worker.inn_json))

        self.worker.update_json('atc1')
        self.assertFalse(file_is_empty(self.worker.atc_json))

        self.worker.update_json('inn2atc')
        self.assertFalse(file_is_empty(self.worker.inn2atc_json))

        self.worker.update_json('atc2inn')
        self.assertFalse(file_is_empty(self.worker.atc2inn_json))

        # check cache
        cache = self.db.get('inn')
        self.assertEqual(
            str_to_obj(cache).get('data'), {u'methyluracil': u'Methyluracil'})

        cache = self.db.get('atc')
        self.assertEqual(str_to_obj(cache).get('data'), {})

        cache = self.db.get('inn2atc')
        self.assertIn(u'methyluracil', str_to_obj(cache).get('data'))

        if file_is_empty(self.worker.atc2inn_json):
            cache = self.db.get('atc2inn')
            self.assertEqual(str_to_obj(cache).get('data'), {})
        else:
            cache = self.db.get('atc2inn')
            with open(self.worker.atc2inn_json) as f:
                data = f.read()

            if str_to_obj(data).get('data'):
                self.assertEqual(
                    str_to_obj(data).get('data'),
                    str_to_obj(cache).get('data'))
            else:
                self.assertEqual(str_to_obj(data).get('data'), dict())

        self.db.flushall()
        self.assertEqual(self.db.has('inn'), False)

        self.worker._update_cache('inn')
        self.assertEqual(self.db.has('inn'), True)