def test_create_meta_data(): exact_response = { 'accessionNumber': '36.100.45', 'classification': 'Paintings', 'creditLine': ( 'The Howard Mansfield Collection, Purchase, Rogers Fund, 1936' ), 'culture': 'Japan', 'objectDate': 'late 17th century', 'medium': 'Hanging scroll; ink and color on silk', } exact_meta_data = { 'accession_number': '36.100.45', 'classification': 'Paintings', 'credit_line': ( 'The Howard Mansfield Collection, Purchase, Rogers Fund, 1936' ), 'culture': 'Japan', 'date': 'late 17th century', 'medium': 'Hanging scroll; ink and color on silk', } r = requests.Response() r.status_code = 200 r.json = MagicMock(return_value=exact_response) with patch.object( mma.delayed_requester, 'get', return_value=r ): response = mma._get_response_json(None, '', retries=2) meta_data = mma._create_meta_data(response) assert exact_meta_data == meta_data
def test_get_response_json_retries_with_none_response(): with patch.object( mma.delayed_requester, 'get', return_value=None ) as mock_get: with pytest.raises(Exception): assert mma._get_response_json(None, '', retries=2) assert mock_get.call_count == 3
def test_get_response_json_retries_with_non_ok(): r = requests.Response() r.status_code = 504 r.json = MagicMock(return_value={}) with patch.object( mma.delayed_requester, 'get', return_value=r ) as mock_get: with pytest.raises(Exception): assert mma._get_response_json(None, '', retries=2) assert mock_get.call_count == 3
def test_get_response_json(): expect_json = {"it": "works"} endpoint = "https://abc.com" query_params = {"a": "b"} retries = 2 with patch.object(mma.delayed_requester, "get_response_json", return_value=expect_json) as mock_get_response_json: r_json = mma._get_response_json(query_params, endpoint, retries=retries) assert r_json == expect_json mock_get_response_json.assert_called_once_with(endpoint, query_params=query_params, retries=retries)
def test_get_response_json_returns_response_json_when_all_ok(): expect_response_json = { 'accessionNumber': '36.100.45', 'artistAlphaSort': 'Kiyohara Yukinobu', 'artistBeginDate': '1643' } r = requests.Response() r.status_code = 200 r.json = MagicMock(return_value=expect_response_json) with patch.object( mma.delayed_requester, 'get', return_value=r ) as mock_get: actual_response_json = mma._get_response_json(None, '', retries=2) assert mock_get.call_count == 1 assert actual_response_json == expect_response_json
def test_create_meta_data(): exact_response = { "accessionNumber": "36.100.45", "classification": "Paintings", "creditLine": ("The Howard Mansfield Collection, Purchase, Rogers Fund, 1936"), "culture": "Japan", "objectDate": "late 17th century", "medium": "Hanging scroll; ink and color on silk", } exact_meta_data = { "accession_number": "36.100.45", "classification": "Paintings", "credit_line": ("The Howard Mansfield Collection, Purchase, Rogers Fund, 1936"), "culture": "Japan", "date": "late 17th century", "medium": "Hanging scroll; ink and color on silk", } r = requests.Response() r.status_code = 200 r.json = MagicMock(return_value=exact_response) with patch.object(mma.delayed_requester, "get", return_value=r): response = mma._get_response_json(None, "", retries=2) meta_data = mma._create_meta_data(response) assert exact_meta_data == meta_data