"""Tests for model_utils module.""" from copy import deepcopy import pytest from conflict_manager.utils import model_utils def test_query_results_to_dict(mocker): """Test convert query results to dict.""" class _KeyedList(list): """Behaves similiarly enough to ResultProxy for purposes of testing.""" def keys(self): return ['a', 'b'] expected_result = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}] query_results = _KeyedList(deepcopy(expected_result)) dict_results = model_utils.query_results_to_dict(query_results) assert not isinstance(dict_results, _KeyedList) assert dict_results == expected_result @pytest.fixture def results(): """Fixture for results list.""" return [{} for x in range(1, 12)] @pytest.mark.parametrize('offset', [0, 50]) def test_get_total_records_less_than_limit(results, offset): """Test getting total records when below limit.""" total_records = model_utils.get_total_records( results, offset=offset, limit=50) assert total_records == len(results) + offset def test_get_total_records_no_results(): """Test getting total records when no results and on first page.""" total_records = model_utils.get_total_records([], offset=0, limit=50) assert total_records == 0 def test_get_total_records_indetriminate(results): """Test getting total records when results and at limit.""" # pytest.mark.parametrize with fixtures is not supported test_cases = [ 0, len(results) ] for offset in test_cases: total_records = model_utils.get_total_records( results, offset=offset, limit=len(results)) assert total_records is None def test_validate_args(query_args): """Test the query validation util correctly allows good query args.""" assert model_utils._validate_query_args(query_args) def test_validate_args_fail(query_args_bad): """Test the query validation util correctly forbids bad query args.""" assert not model_utils._validate_query_args(query_args_bad)