"""Presentation tests.""" import json import unittest from _pytest.monkeypatch import MonkeyPatch from manualadjustment import presentation as pres from manualadjustment.logic.result import Result from manualadjustment.models.manual_adjustment import ManualAdjustment from tests.fixtures import flask_global class TestPresentationLayer(unittest.TestCase): """Presentation Layer test class.""" def setUp(self): """Test setup procedure.""" self.monkeypatch = MonkeyPatch() flask_global.mock(self.monkeypatch) def tearDown(self): """Test teardown procedure.""" self.monkeypatch.undo() self.monkeypatch = None def adjustment_model(self): """Sample model object of an adjustment.""" model = ManualAdjustment() model.id = 123 model.amount = 456.23 model.adjust_for_period_id = 183 model.adjust_for_quarter = 2 model.adjust_for_year = 2014 model.amount_in_original_currency = 123.45 model.apply_to_period_id = 172 model.apply_to_quarter = 3 model.apply_to_year = 2014 model.attachment_location = 'over-here/blarf/thing.csv' model.category = 'this is garbage' model.category_id = 53 model.comment = 'manual adjustments rawk bro' model.created_by = 889 model.currencies_id = 1 model.date_added = '2015-06-23 00:11:22' model.parent_id = 7123 model.parent_type = 'vendor' model.updated_timestamp = '2015-06-23 11:22:33' return model def successful_adj_result(self, status=200): """Sample result object of a successful adjustment.""" model = self.adjustment_model() return Result(data=model, status=status) def test_get_manual_adj_response(self): """Verify returned json contains expected fields and datatypes.""" result = Result( data={ 'manual_adjustments': [], 'page_count': 1, 'page_offset': 0, 'page_limit': 1}) json_str, status = pres.get_manual_adj_response(result) json_obj = json.loads(json_str) self.assertIsInstance(json_obj, dict) self.assertIn('manual_adjustments', json_obj) self.assertIn('pagination', json_obj) self.assertIsInstance(json_obj['manual_adjustments'], list) self.assertIsInstance(json_obj['pagination'], dict) self.assertEqual(status, 200) def test_error_response(self): """Verify that error response is in expected format.""" dummy_res = Result() json_str, _ = pres.error_response(dummy_res) json_obj = json.loads(json_str) self.assertIsInstance(json_obj, dict) self.assertIn('error', json_obj) self.assertIn('error_detail', json_obj) def test_health_response(self): """Test health check response.""" assert pres.health_response('test') == ('test', 200) def test_manual_adj_response(self): """Test manual adjustment response.""" result = self.successful_adj_result() model = result.data json_str, status = pres.manual_adj_response(result) json_obj = json.loads(json_str) expected_obj = { 'id': model.id, 'amount': model.amount, 'adjust_for_period_id': model.adjust_for_period_id, 'amount_in_original_currency': model.amount_in_original_currency, 'apply_to_period_id': model.apply_to_period_id, 'attachment_location': model.attachment_location, 'category_id': model.category_id, 'comment': model.comment, 'created_by': model.created_by, 'currencies_id': model.currencies_id, 'date_added': model.date_added, 'parent_id': model.parent_id, 'parent_type': model.parent_type, 'updated_timestamp': model.updated_timestamp} self.assertEqual(json_obj, expected_obj) self.assertEqual(status, result.status) class TestPresentationDict(TestPresentationLayer): """Presentation test class for adjustment dict serializer.""" def adjustment_model(self): """Sample adjustment model.""" model = super().adjustment_model() model.release_manual_adjustments = [] return model def adjustment_dict(self): """Sample adjustment model serialized into a dict.""" model = { 'id': 123, 'amount': 456.23, 'adjust_for_period_id': 183, 'adjust_for_quarter': 2, 'adjust_for_year': 2014, 'amount_in_original_currency': 123.45, 'apply_to_period_id': 172, 'apply_to_quarter': 3, 'apply_to_year': 2014, 'attachment_location': 'over-here/blarf/thing.csv', 'category': 'this is garbage', 'category_id': 53, 'comment': 'manual adjustments rawk bro', 'created_by': 889, 'currencies_id': 1, 'date_added': '2015-06-23 00:11:22', 'parent_id': 7123, 'parent_type': 'vendor', 'updated_timestamp': '2015-06-23 11:22:33', 'release_manual_adjustments': [ ] } return model def test_adjustment_dict(self): """Test manual adjustment dict serialization.""" model = self.adjustment_model() expected = self.adjustment_dict() results = pres.adjustment_dict(model) assert results == expected