def add_new_type(db_path, json_path): engine = create_engine('sqlite:///%s' % db_path, echo=False) if not os.path.exists(db_path): Base.metadata.create_all(engine) service = DbService(engine) with open(json_path) as f: obj = json.load(f) service.add_type(obj)
def setUp(self): engine = create_engine('sqlite:///:memory:') Base.metadata.create_all(engine) self._service = DbService(engine) app.config['BACKEND'] = self._service self.app = app.test_client()
def _basic_add_type(self): insurance_type = { 'name': 'Stolen car', 'description': 'Insurance for when a car is stolen', 'attributes': [ { 'name': 'Start date', 'description': 'When the insurance begins to take effect', 'type': 'date', }, { 'name': 'End date', 'description': 'When the insurance finishes', 'type': 'date', }, { 'name': 'Value', 'description': 'How much to pay in the case of the car is stolen', 'type': 'numeric', }, { 'name': 'Communication days', 'description': 'The maximal number of days the client has to communicate the theft', 'type': 'int', }, { 'name': 'Customer history', 'description': 'Has the client being stolen before?', 'type': 'enum', 'allowed_values': ['Never stolen', 'Stolen once', 'Stolen multiple times'], }, ] } json_path = os.path.join(self.temp_dir, 'insurance.json') with open(json_path, 'w') as f: json.dump(insurance_type, f) add_new_type(self.db_path, json_path) engine = create_engine('sqlite:///%s' % self.db_path, echo=False) service = DbService(engine) types = service.get_types() assert len(types) == 1 result_type = types[0] result_type.pop('id') for attr in result_type['attributes']: attr.pop('id') assert result_type == insurance_type
class TestServer(unittest.TestCase): def setUp(self): engine = create_engine('sqlite:///:memory:') Base.metadata.create_all(engine) self._service = DbService(engine) app.config['BACKEND'] = self._service self.app = app.test_client() def test_search_no_types(self): response = self.app.get('/search/types/') self.assertEqual(response.status_code, 200) self.assertEqual(response.headers['Content-type'], _JSON_CONTENT_TYPE) data = from_json(response) self.assertEqual(data, { 'total': 0, 'types': [], }) def test_search_with_types(self): expected = [] for i in range(3): risk_type = deepcopy(_full_insurance) # this will make sure names are not being inserted in order risk_type['name'] = '%s - %s' % (9 - i, risk_type['name']) new_id = self._service.add_type(risk_type) expected.append(self._service.get_type(new_id)) expected.sort(key=lambda x: x['name']) response = self.app.get('/search/types/') self.assertEqual(response.status_code, 200) self.assertEqual(response.headers['Content-type'], _JSON_CONTENT_TYPE) data = from_json(response) self.assertEqual(data, { 'total': 3, 'types': expected, }) def test_get_type(self): risk_type = deepcopy(_full_insurance) new_id = self._service.add_type(risk_type) expected = self._service.get_type(new_id) response = self.app.get('/types/%s/' % new_id) self.assertEqual(response.status_code, 200) self.assertEqual(response.headers['Content-type'], _JSON_CONTENT_TYPE) data = from_json(response) self.assertEqual(data, expected) def test_get_type_not_available(self): for key in [1, 'a']: response = self.app.get('/types/%s/' % key) self.assertEqual(response.status_code, 404, '%s - should return error 400' % key) self.assertEqual(response.headers['Content-type'], _JSON_CONTENT_TYPE)
def setUp(self): self.engine = create_engine('sqlite:///:memory:', echo=False) Base.metadata.create_all(self.engine) self.db_service = DbService(self.engine) self.Session = sessionmaker(bind=self.engine)
class TestDbService(unittest.TestCase): def setUp(self): self.engine = create_engine('sqlite:///:memory:', echo=False) Base.metadata.create_all(self.engine) self.db_service = DbService(self.engine) self.Session = sessionmaker(bind=self.engine) def test_add_type(self): insurance_type = deepcopy(full_insurance) new_id = self.db_service.add_type(insurance_type) session = self.Session() added_type = session.query(RiskType).filter_by(name='Stolen car').first() assert added_type.id == new_id assert added_type.name == insurance_type['name'] assert added_type.description == insurance_type['description'] def assert_attribute(attribute, dict_repr): assert attribute.name == dict_repr['name'] assert attribute.description == dict_repr.get('description') assert attribute.data_type == str2enum(dict_repr['type']) if attribute.data_type == AttributeDataType.ENUM: assert [v.value for v in attribute.allowed_values] == dict_repr.get('allowed_values') model_attributes = added_type.attributes dict_attributes = insurance_type['attributes'] assert_attribute(model_attributes[0], dict_attributes[0]) assert_attribute(model_attributes[1], dict_attributes[1]) assert_attribute(model_attributes[2], dict_attributes[2]) assert_attribute(model_attributes[3], dict_attributes[3]) assert_attribute(model_attributes[4], dict_attributes[4]) assert_attribute(model_attributes[5], dict_attributes[5]) def test_get_type(self): insurance_type = deepcopy(full_insurance) new_id = self.db_service.add_type(insurance_type) insurance_type['id'] = new_id result_type = self.db_service.get_type(new_id) for attribute in result_type['attributes']: # if there is no id, we'll get an attribute error attribute.pop('id') assert result_type == insurance_type def test_get_type_invalid_id(self): assert self.db_service.get_type(12345) is None def test_get_types(self): insurance_type = deepcopy(full_insurance) self.db_service.add_type(insurance_type) self.db_service.add_type(insurance_type) result_types = self.db_service.get_types() assert len(result_types) == 2 for result_type in result_types: result_type.pop('id') for attr in result_type['attributes']: attr.pop('id') assert result_type == insurance_type def test_get_types_ordered(self): def new_insurance(name): insurance_type = deepcopy(full_insurance) insurance_type['name'] = name return insurance_type self.db_service.add_type(new_insurance('c')) self.db_service.add_type(new_insurance('B')) self.db_service.add_type(new_insurance('a')) result_types = self.db_service.get_types(order_by_name=True) assert ['a', 'B', 'c'] == [r['name'] for r in result_types]
def _db(): if 'BACKEND' not in app.config: db_path = os.environ['SQLITE_PATH'] engine = create_engine('sqlite:///%s' % db_path) app.config['BACKEND'] = DbService(engine) return app.config['BACKEND']