"""Test handler util.""" import codecs import copy import json import application from territories import handlers from territories.constants import header from territories.util.handler_util import add_pagination from territories.validation import json_schema def test_header_validation_success(db_fixture, header_fixture, context): """Test success response.""" with context, application.app.test_request_context(headers=header_fixture): response = handlers.get_standards() assert response def test_header_validation_fail(db_fixture, header_fixture, context): """Test validation error response when Correlation-Id header missing.""" mutated_header = copy.copy(header_fixture) mutated_header.pop(header.CORRELATION_ID) with context, application.app.test_request_context(headers=mutated_header): response = handlers.get_standards() assert response.status_code == 400 data = json.loads(codecs.utf_8_decode(response.data)[0]) assert data['code'] == json_schema.VALIDATION_ERROR assert header.CORRELATION_ID in data['message'] def test_pagination(): """Test pagination.""" test_records = [{'k': v} for v in range(30)] offset = 0 limit = 10 paginated = add_pagination(test_records, offset, limit) assert len(paginated['items']) == limit assert len(test_records) == paginated['pagination']['total_records'] assert paginated['pagination']['offset'] == offset assert paginated['pagination']['limit'] == limit