"""Post validation tests.""" import json import unittest import application from utils import db_ops SCHEMA_VALIDATION_ERROR = 'schema validation error' NONEXISTENT_ID_ERROR = 'id(s) not found in database' class TestPostValidation(unittest.TestCase): """Test validation of POST /manual_adjustment input json.""" def setUp(self): """Test setup procedure.""" self.app = application.app.test_client() # valid request json: self.request_json = { 'amount': 234.56, 'category_id': 0, 'comment': 'test comment', 'adjust_for_period_id': 156, 'apply_to_period_id': 156, 'parent_id': 16031, 'parent_type': 'vendor', 'created_by': 261, 'currencies_id': 1, 'amount_in_original_currency': 456.78} self.headers = [('Content-Type', 'application/json')] self.nonnumerics = ('abc', None) self.nonints = ('abc', 1.1, None) self.nonstrings = (1, 1.1, None) self.empty_strings = ('', None) db_ops.create_tables() db_ops.seed_art_relations() def tearDown(self): """Test teardown procedure.""" self.request_json = {} db_ops.create_tables() def test_nonnumeric_amount_fail(self): """Assert that nonnumeric amount raises error.""" for nonnumeric in self.nonnumerics: self.request_json['amount'] = nonnumeric res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('amount', json_response['error_detail']) def test_amount_missing_fail(self): """Assert that missing amount raises error.""" self.request_json.pop('amount') res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('amount', json_response['error_detail']) def test_nonint_category_id_fail(self): """Assert that non-integer category_id raises error.""" for nonint in self.nonints: self.request_json['category_id'] = nonint res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('category_id', json_response['error_detail']) def test_category_id_missing_fail(self): """Assert that missing category_id raises error.""" self.request_json.pop('category_id') res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('category_id', json_response['error_detail']) def test_nonstring_comment_fail(self): """Assert that non-string comment raises error.""" for nonstring in self.nonstrings: self.request_json['comment'] = nonstring res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('comment', json_response['error_detail']) def test_empty_comment_fail(self): """Assert that empty comment raises error.""" for empty_string in self.empty_strings: self.request_json['comment'] = empty_string res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('comment', json_response['error_detail']) def test_comment_missing_fail(self): """Assert that missing comment raises error.""" self.request_json.pop('comment') res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('comment', json_response['error_detail']) def test_nonint_adjust_for_period_id_fail(self): """Assert that non-integer adjust_for_period_id raises error.""" for nonint in self.nonints: self.request_json['adjust_for_period_id'] = nonint res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn( 'adjust_for_period_id', json_response['error_detail']) def test_adjust_for_period_id_missing_fail(self): """Assert that missing adjust_for_period_id raises error.""" self.request_json.pop('adjust_for_period_id') res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('adjust_for_period_id', json_response['error_detail']) def test_nonint_apply_to_period_id_fail(self): """Assert that non-integer apply_to_period_id raises error.""" for nonint in self.nonints: self.request_json['apply_to_period_id'] = nonint res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('apply_to_period_id', json_response['error_detail']) def test_apply_to_period_id_missing_fail(self): """Assert that missing apply_to_period_id raises error.""" self.request_json.pop('apply_to_period_id') res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('apply_to_period_id', json_response['error_detail']) def test_nonint_parent_id_fail(self): """Assert that non-integer parent_id raises error.""" for nonint in self.nonints: self.request_json['parent_id'] = nonint res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('parent_id', json_response['error_detail']) def test_parent_id_missing_fail(self): """Assert that missing parent_id raises error.""" self.request_json.pop('parent_id') res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('parent_id', json_response['error_detail']) def test_nonstring_parent_type_fail(self): """Assert that non-string parent_type raises error.""" for nonstring in self.nonstrings: self.request_json['parent_type'] = nonstring res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('parent_type', json_response['error_detail']) def test_illegal_parent_type_fail(self): """Assert that illegal parent_type (not 'vendor') raises error.""" self.request_json['parent_type'] = 'vwjwtoeelzoxllytsmpx' res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('parent_type', json_response['error_detail']) def test_parent_type_missing_fail(self): """Assert that missing parent_type raises error.""" self.request_json.pop('parent_type') res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('parent_type', json_response['error_detail']) def test_nonint_created_by_fail(self): """Assert that non-integer created_by raises error.""" for nonint in self.nonints: self.request_json['created_by'] = nonint res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('created_by', json_response['error_detail']) def test_created_by_missing_fail(self): """Assert that missing created_by raises error.""" self.request_json.pop('created_by') res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('created_by', json_response['error_detail']) def test_nonint_currencies_id_by_fail(self): """Assert that non-integer currencies_id raises error.""" for nonint in self.nonints: self.request_json['currencies_id'] = nonint res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('currencies_id', json_response['error_detail']) def test_nonnumeric_amount_in_original_currency_fail(self): """Assert that nonnumeric amount_in_original_currency raises error.""" for nonnumeric in self.nonnumerics: self.request_json['amount_in_original_currency'] = nonnumeric res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn( 'amount_in_original_currency', json_response['error_detail']) def test_parent_id_not_in_db(self): """Assert that nonexistent parent_id raises error.""" nonexistent_parent_id = 7023302194518892 self.request_json['parent_id'] = nonexistent_parent_id res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], NONEXISTENT_ID_ERROR) self.assertIn( 'missing parent_id={}'.format(nonexistent_parent_id), json_response['error_detail']) def test_adjust_for_period_id_not_in_db(self): """Assert that nonexistent adjust_for_period_id raises error.""" nonexistent_adjust_for_period_id = 7023302194518892 self.request_json['adjust_for_period_id'] = \ nonexistent_adjust_for_period_id res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], NONEXISTENT_ID_ERROR) self.assertIn( 'missing adjust_for_period_id={}'.format( nonexistent_adjust_for_period_id), json_response['error_detail']) def test_apply_to_period_id_not_in_db(self): """Assert that nonexistent apply_to_period_id raises error.""" nonexistent_apply_to_period_id = 7023302194518892 self.request_json['apply_to_period_id'] = \ nonexistent_apply_to_period_id res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], NONEXISTENT_ID_ERROR) self.assertIn( 'missing apply_to_period_id={}'.format( nonexistent_apply_to_period_id), json_response['error_detail']) def test_category_id_not_in_db(self): """Assert that nonexistent category_id raises error.""" nonexistent_category_id = 7023302194518892 self.request_json['category_id'] = nonexistent_category_id res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], NONEXISTENT_ID_ERROR) self.assertIn( 'missing category_id={}'.format(nonexistent_category_id), json_response['error_detail']) def test_created_by_not_in_db(self): """Assert that nonexistent created_by raises error.""" nonexistent_created_by = 7023302194518892 self.request_json['created_by'] = nonexistent_created_by res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], NONEXISTENT_ID_ERROR) self.assertIn( 'missing created_by={}'.format(nonexistent_created_by), json_response['error_detail']) def test_currencies_id_not_in_db(self): """Assert that nonexistent currencies_id raises error.""" nonexistent_currencies_id = 7023302194518892 self.request_json['currencies_id'] = nonexistent_currencies_id res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], NONEXISTENT_ID_ERROR) self.assertIn( 'missing currencies_id={}'.format(nonexistent_currencies_id), json_response['error_detail']) def test_additional_properties_fail(self): """Assert that unexpected field in post json raises error.""" self.request_json['extraneous_property'] = 1 res = self.app.post( '/manual_adjustment', data=json.dumps(self.request_json), headers=self.headers) self.assertEqual(res.status_code, 400) json_response = json.loads(res.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn('extraneous_property', json_response['error_detail'])