"""Put manual adjustment validation tests.""" import json import unittest import boto3 from moto import mock_aws import application from manualadjustment import config from manualadjustment.models.manual_adjustment import ManualAdjustment from utils import db_ops SCHEMA_VALIDATION_ERROR = 'schema validation error' ID_NOT_FOUND_ERROR = 'id(s) not found in database' class TestPostValidation(unittest.TestCase): """Test validation of PUT /manual_adjustment input json.""" def setUp(self): """Test setup procedure.""" self.app = application.app.test_client() self.headers = [('Content-Type', 'application/json')] db_ops.create_tables() db_ops.seed_art_relations() self.adjustment = db_ops.ar_session.query(ManualAdjustment).first() self.non_existent_id = 1234567890 def tearDown(self): """Test teardown procedure.""" self.test_json = {} db_ops.create_tables() def request(self, request_json): """Test client based request.""" return self.app.put( '/manual_adjustment/{}'.format(self.adjustment.id), data=json.dumps(request_json), headers=self.headers) def assert_field_error(self, response, field_name): """Assert json error for a response object.""" json_response = json.loads(response.data.decode('utf-8')) self.assertEqual(json_response['error'], SCHEMA_VALIDATION_ERROR) self.assertIn(field_name, json_response['error_detail']) def assert_bad_id(self, field_name): """Assert bad id request for a field name.""" response = self.request({field_name: self.non_existent_id}) json_response = json.loads(response.data.decode('utf-8')) self.assertEqual(json_response['error'], ID_NOT_FOUND_ERROR) expected_detail = '{}={};'.format(field_name, self.non_existent_id) self.assertIn(expected_detail, json_response['error_detail']) def test_nonnumeric_amount_fail(self): """Assert that nonnumeric amount raises error.""" response = self.request({'amount': 'a bajillion'}) self.assertEqual(response.status_code, 400) self.assert_field_error(response, 'amount') def test_non_int_category_id_fail(self): """Assert that non-integer category_id raises error.""" response = self.request({'category_id': 'complete fraud'}) self.assertEqual(response.status_code, 400) self.assert_field_error(response, 'category_id') @mock_aws def test_comment_success(self): """Assert that a valid comment is accepted.""" sqs_conn = boto3.client('sqs', region_name=config.AWS_REGION) sqs_conn.create_queue(QueueName=config.queue_name) response = self.request({'comment': 'i heart manual adjustments zomg'}) self.assertEqual(response.status_code, 200) def test_non_int_adjust_for_period_fail(self): """Assert that non-numeric adjust_for_period_id raises error.""" response = self.request({'adjust_for_period_id': 'last month'}) self.assertEqual(response.status_code, 400) self.assert_field_error(response, 'adjust_for_period_id') def test_non_int_parent_id_fail(self): """Assert that non-numeric parent_id raises error.""" response = self.request({'parent_id': 'mom?'}) self.assertEqual(response.status_code, 400) self.assert_field_error(response, 'parent_id') def test_non_vendor_parent_type_fail(self): """Assert that parent_type other than vendor raises error.""" response = self.request({'parent_type': 'negligent'}) self.assertEqual(response.status_code, 400) self.assert_field_error(response, 'parent_type') def test_non_int_created_by_fail(self): """Assert that non-int created_by raises error.""" response = self.request({'created_by': 'an insane person'}) self.assertEqual(response.status_code, 400) self.assert_field_error(response, 'created_by') def test_non_int_currencies_id_fail(self): """Assert that non-int currencies_id raises error.""" response = self.request({'currencies_id': 'Kongbucks'}) self.assertEqual(response.status_code, 400) self.assert_field_error(response, 'currencies_id') def test_non_numeric_amount_in_original_currency_fail(self): """Assert that non-numeric amount_in_original_currency raises error.""" response = self.request({'amount_in_original_currency': 'nada'}) self.assertEqual(response.status_code, 400) self.assert_field_error(response, 'amount_in_original_currency') def test_non_existent_parent_id(self): """Test bad id for field parent_id.""" self.assert_bad_id('parent_id') def test_non_existent_category_id(self): """Test bad id for field category_id.""" self.assert_bad_id('category_id') def test_non_existent_adjust_for_period_id(self): """Test bad id for field adjust_for_period_id.""" self.assert_bad_id('adjust_for_period_id') def test_non_existent_apply_to_period_id(self): """Test bad id for field apply_to_period_id.""" self.assert_bad_id('apply_to_period_id') def test_non_existent_created_by(self): """Test bad id for field created_by.""" self.assert_bad_id('created_by') def test_non_existent_currencies_id(self): """Test bad id for field currencies_id.""" self.assert_bad_id('currencies_id') def test_additional_properties_fail(self): """Assert that unexpected field in put json raises error.""" response = self.request({'extraneous_field': 1}) self.assertEqual(response.status_code, 400) self.assert_field_error(response, 'extraneous_field')