def test_put_with_mock_db(self): """ Testing the put request to /api/departments/<id> with mock db """ # create department department = Department(name='Test Department1', description='Test Description1') # pylint: disable=no-member db.session.add(department) db.session.commit() # update department with patch('department_app.service.department_service.get_department_by_id') \ as mocked_query, \ patch('department_app.db.session.add', autospec=True) as mock_session_add, \ patch('department_app.db.session.commit', autospec=True) as mock_session_commit: mocked_query.return_value = FakeDepartment() client = create_app().test_client() url = '/api/departments/1' data = { 'name': 'Update Test Department1', 'description': 'Update Test Description1' } client.put(url, data=json.dumps(data), content_type='application/json') mock_session_add.assert_called_once() mock_session_commit.assert_called_once()
def test_get(self): """ Testing the get request to /api/employees. It should return the status code 200 """ client = create_app().test_client() response = client.get('/api/employees') assert response.status_code == http.HTTPStatus.OK
def test_get_employees_born_between(self): """ Testing the get request to /api/employees?start_date='start_date'&end_date='end_date' It should return the status code 200 """ client = create_app().test_client() url = '/api/employees?start_date=\'12/13/1971\'&end_date=\'12/13/1991\'' response = client.get(url) assert response.status_code == http.HTTPStatus.OK
def test_get_department(self): """ Testing the get request to /api/departments/<id> It should return the status code 200 """ client = create_app().test_client() url = '/api/departments/2' response = client.get(url) assert response.status_code == http.HTTPStatus.OK
def setUp(self): """ Execute before every test case """ self.app = create_app() self.app.config.from_object(TestingConfig) self.app_context = self.app.app_context() self.app_context.push() db.create_all()
def test_get_employees_born_on(self): """ Testing the get request to /api/employees?date='date' where date is a birthday of existing employee. It should return the status code 200 """ client = create_app().test_client() url = '/api/employees?date=\'10/06/1998\'' response = client.get(url) assert response.status_code == http.HTTPStatus.OK
def test_get_with_mock_db(self, mock_db_call): """ Testing the get request to /api/departments with mock db. It should return the status code 200 and an empty list """ client = create_app().test_client() response = client.get('/api/departments') mock_db_call.assert_called_once() assert response.status_code == http.HTTPStatus.OK assert len(response.json) == 0
def test_delete(self): """ Testing the delete request to /api/departments/<id> It should return the status code 204 (NO_CONTENT) """ department = Department(name='Test Department1', description='Test Description1') # pylint: disable=no-member db.session.add(department) db.session.commit() client = create_app().test_client() url = '/api/departments/1' response = client.delete(url) assert response.status_code == http.HTTPStatus.NO_CONTENT
def test_post(self): """ Testing the post request to /api/departments. It should return the status code 201 """ client = create_app().test_client() # data should be changed before calling the test data = { 'name': 'Test Department_', 'description': 'Test Description_' } response = client.post('/api/departments', data=json.dumps(data), content_type='application/json') assert response.status_code == http.HTTPStatus.CREATED
def app(): """ Pytest fixture to make app, application test context and db populated with test data available in tests. """ _app = create_app(config_class=TestConfig) ctx = _app.test_request_context() ctx.push() _app.testing = True with _app.app_context(): db.create_all() populate_db() yield _app ctx.pop()
def test_post_with_mock_db(self): """ Testing the post request to /api/departments with mock db """ with patch('department_app.db.session.add', autospec=True) as mock_session_add, \ patch('department_app.db.session.commit', autospec=True) as mock_session_commit: client = create_app().test_client() data = { 'name': 'Test Department1', 'description': 'Test Description1' } client.post('/api/departments', data=json.dumps(data), content_type='application/json') mock_session_add.assert_called_once() mock_session_commit.assert_called_once()
def test_client(): """Create an instance of an app. Add it to context. Run tests and then delete app from context. """ # Create app with test configuration. app = create_app("testconfig.py") # Test client provided by Flask testing_client = app.test_client() # Establish an app context. ctx = app.app_context() ctx.push() yield testing_client # Remove app context. ctx.pop()
def test_put(self): """ Testing the put request to /api/departments/<id> It should return the status code 200 """ department = Department(name='Test Department1', description='Test Description1') # pylint: disable=no-member db.session.add(department) db.session.commit() client = create_app().test_client() url = '/api/departments/1' data = { 'name': 'Update Test Department1', 'description': 'Update Test Description1' } response = client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == http.HTTPStatus.OK
def test_put(self): """ Testing the put request to /api/employees/<id> . It should return the status code 200 """ client = create_app().test_client() url = '/api/employees/6' data = { 'username': '******', 'email': '*****@*****.**', 'first_name': 'test_persona', 'last_name': 'test_persona', 'department_id': 3, 'salary': '1700', 'birthday': '10/06/1998' } response = client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == http.HTTPStatus.OK
def test_post(self): """ Testing the post request to /api/employees. It should return the status code 201 """ client = create_app().test_client() # data should be changed before calling the test data = { 'username': '******', 'email': '*****@*****.**', 'first_name': 'new_test', 'last_name': 'new_test', 'password': '******', 'department_id': 3, 'salary': '700', 'birthday': '10/06/1998' } response = client.post('/api/employees', data=json.dumps(data), content_type='application/json') assert response.status_code == http.HTTPStatus.CREATED
def test_post_with_mock_db(self): """ Testing the post request to /api/employees with mock db """ with patch('department_app.db.session.add', autospec=True) as mock_session_add, \ patch('department_app.db.session.commit', autospec=True) as mock_session_commit: client = create_app().test_client() data = { 'username': '******', 'email': '*****@*****.**', 'first_name': 'cool', 'last_name': 'test', 'password': '******', 'department_id': 3, 'salary': '700', 'birthday': '10/06/1998' } client.post('/api/employees', data=json.dumps(data), content_type='application/json') mock_session_add.assert_called_once() mock_session_commit.assert_called_once()
def setUp(self): self.app = create_app(TestConfig) self.app_context = self.app.app_context() self.app_context.push() self.client = self.app.test_client() db.create_all()
# -*- coding: utf-8 -*- """ run.py ~~~~~~ Module to run the application. """ from department_app import create_app app = create_app("config.py") if __name__ == "__main__": app.run(debug=True)
) e_4 = Employee( name="Employee 4", date_of_birth=date(year=1994, month=4, day=4), salary=2000, department_id=2, ) e_5 = Employee( name="Employee 5", date_of_birth=date(year=1995, month=5, day=5), salary=2000, department_id=2, ) db.session.add(d_1) db.session.add(d_2) db.session.add(e_1) db.session.add(e_2) db.session.add(e_3) db.session.add(e_4) db.session.add(e_5) db.session.commit() if __name__ == "__main__": app = create_app() with app.app_context(): populate_db()