コード例 #1
0
def test_get_data_for_image_returns_response_json_with_additional_images(
        monkeypatch):
    with open(os.path.join(RESOURCES, "sample_response.json")) as f:
        actual_response_json = json.load(f)

    def mock_get_response_json(query_params, retries=0):
        return actual_response_json

    monkeypatch.setattr(mma, "_get_response_json", mock_get_response_json)
    with open(os.path.join(RESOURCES,
                           "sample_additional_image_data.json")) as f:
        image_data = json.load(f)

    r = requests.Response()
    r.status_code = 200
    r.json = MagicMock(return_value=image_data)
    with patch.object(mma.image_store, "add_item",
                      return_value=image_data) as mock_add:
        mma._get_data_for_image(45734)

    mock_add.assert_called_with(
        creator="Kiyohara Yukinobu",
        foreign_identifier="45734-DP251120",
        foreign_landing_url=(
            "https://wwwstg.metmuseum.org/art/collection/search/45734"),
        image_url=(
            "https://images.metmuseum.org/CRDImages/as/original/DP251120.jpg"),
        license_="cc0",
        license_version="1.0",
        meta_data={
            "accession_number":
            "36.100.45",
            "classification":
            "Paintings",
            "culture":
            "Japan",
            "date":
            "late 17th century",
            "medium":
            "Hanging scroll; ink and color on silk",
            "credit_line":
            ("The Howard Mansfield Collection, Purchase, Rogers Fund, 1936"),
        },
        thumbnail_url=None,
        title="Quail and Millet",
    )

    assert mock_add.call_count == 3
コード例 #2
0
def test_get_data_for_image_with_none_response():
    with patch.object(mma.delayed_requester, "get",
                      return_value=None) as mock_get:
        with pytest.raises(Exception):
            assert mma._get_data_for_image(10)

    assert mock_get.call_count == 6
コード例 #3
0
def test_get_data_for_image_returns_response_json_when_all_ok(monkeypatch):
    with open(
            os.path.join(RESOURCES,
                         "sample_response_without_additional.json")) as f:
        actual_response_json = json.load(f)

    def mock_get_response_json(query_params, retries=0):
        return actual_response_json

    monkeypatch.setattr(mma, "_get_response_json", mock_get_response_json)
    with open(os.path.join(RESOURCES,
                           "sample_additional_image_data.json")) as f:
        image_data = json.load(f)

    r = requests.Response()
    r.status_code = 200
    r.json = MagicMock(return_value=image_data)
    with patch.object(mma.image_store, "add_item",
                      return_value=image_data) as mock_add:
        mma._get_data_for_image(45733)

    mock_add.assert_called_with(
        creator="",
        foreign_identifier="45733-79_2_414b_S1_sf",
        foreign_landing_url=(
            "https://www.metmuseum.org/art/collection/search/47533"),
        image_url=(
            "https://images.metmuseum.org/CRDImages/as/original/79_2_414b_S1"
            "_sf.jpg"),
        license_="cc0",
        license_version="1.0",
        meta_data={
            "accession_number": "79.2.414b",
            "classification": "Ceramics",
            "culture": "China",
            "date": "",
            "medium": "Porcelain painted in underglaze blue",
            "credit_line": "Purchase by subscription, 1879",
        },
        thumbnail_url=(
            "https://images.metmuseum.org/CRDImages/as/web-large/79_2_414b_S1"
            "_sf.jpg"),
        title="Cover",
    )

    assert mock_add.call_count == 1
コード例 #4
0
def test_get_data_for_image_returns_response_json_when_all_ok(monkeypatch):
    with open(
        os.path.join(RESOURCES, 'sample_response_without_additional.json')
    ) as f:
        actual_response_json = json.load(f)

    def mock_get_response_json(query_params, retries=0):
        return actual_response_json

    monkeypatch.setattr(mma, '_get_response_json', mock_get_response_json)
    with open(
        os.path.join(RESOURCES, 'sample_additional_image_data.json')
    ) as f:
        image_data = json.load(f)

    r = requests.Response()
    r.status_code = 200
    r.json = MagicMock(return_value=image_data)
    with patch.object(
        mma.image_store,
        'add_item',
        return_value=image_data
    ) as mock_add:
        mma._get_data_for_image(45733)

    mock_add.assert_called_with(
        creator='',
        foreign_identifier=45733,
        foreign_landing_url='https://www.metmuseum.org/art/collection/search/47533',
        image_url='https://images.metmuseum.org/CRDImages/as/original/79_2_414b_S1_sf.jpg',
        license_='cc0',
        license_version='1.0',
        meta_data={
            'accession_number': '79.2.414b',
            'classification': 'Ceramics',
            'culture': 'China',
            'date': '',
            'medium': 'Porcelain painted in underglaze blue',
            'credit_line': 'Purchase by subscription, 1879'
        },
        thumbnail_url='https://images.metmuseum.org/CRDImages/as/web-large/79_2_414b_S1_sf.jpg',
        title='Cover'
    )

    assert mock_add.call_count == 1
コード例 #5
0
def test_get_data_for_image_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_data_for_image(10)

    assert mock_get.call_count == 6