def test_prop_doesnt_exist():
    """Should return original JSON when prop doesn't exist."""
    x = {"aaa": "BBB"}
    r, c = _get_server_response(json.dumps(x), 'aaa%2Fddd')
    pinfo(x,r,c)
    print_error_log()
    assert_same_jsons(c, x)
Esempio n. 2
0
def test_setting_empty_type_from_format():
    "Should set empty type according to format field according to type mapping."

    DATA = [
        {"in": {"format": "audio/mp3"}, "out": {"type": "sound"}},
        {"in": {"format": "image/jpg"}, "out": {"type": "image"}},
        {"in": {"format": "video/mpeg"}, "out": {"type": "moving image"}},
        {"in": {"format": "text/calendar"}, "out": {"type": "text"}},
        {"in": {"format": "audio"}, "out": {"format": "audio"}},
        {"in": {"format": "something strange"}, "out": {"format": "something strange"}},
    ]

    FORMATS = ["audio"]
    EXPECTED_TYPES = []
    INPUT = {"sourceResource": {"format": "FORMAT", "type": ""}}
    EXPECTED = {"sourceResource": {"format": "FORMAT", "type": "TYPE"}}

    for d in DATA:
        INPUT["sourceResource"] = d["in"]
        js = json.dumps(INPUT)
        url = server() + "enrich-format?prop=sourceResource/format"

        resp, content = H.request(url, "POST", body=js)
        # pinfo(content)
        pinfo(INPUT)

        assert str(resp.status).startswith("2")

        EXPECTED["sourceResource"] = d["out"]
        assert_same_jsons(EXPECTED, content)
def test_prop_doesnt_exist():
    """Should return original JSON when prop doesn't exist."""
    x = {"aaa": "BBB"}
    r, c = _get_server_response(json.dumps(x), 'aaa%2Fddd')
    pinfo(x, r, c)
    print_error_log()
    assert_same_jsons(c, x)
Esempio n. 4
0
def test_populating_publisher_field():
    INPUT = {
            "freetext": {
                "publisher": [
                    {
                        "#text": "Glossary of Coins and Currency Terms",
                        "@label": "Publication title"
                        },
                    {
                        "#text": "http://americanhistory.si.edu/coins/glossary.cfm",
                        "@label": "Publication URL"
                        },
                    {
                        "#text": "xx",
                        "@label": "Publisher"
                        }
                    ],
                }
            }
    EXPECTED_PUBLISHER = {
            "publisher": ["xx"]
    }
    resp, content = _get_server_response(INPUT)
    print_error_log()
    assert resp["status"].startswith("2")
    CONTENT = json.loads(content)
    pinfo(content)

    assert_same_jsons(EXPECTED_PUBLISHER, CONTENT["sourceResource"])
Esempio n. 5
0
def test_substitute_with_list_of_dictionaries():
    """
    Should convert all dicts in a list.
    """
    data = {
                "xxx": "yyy",
                "aaa": {
                    "bbb": "ccc",
                    "xxx": [
                        {"eee": "aaa"},
                        {"xxx": "eee"},
                        {"eee": "bbb"}
                    ]
                }
    }

    INPUT = json.dumps(data)
    data["aaa"]["xxx"] = [
                        {"eee": "AAA222"},
                        {"xxx": "eee"},
                        {"eee": "BBB222"},
    ]

    EXPECTED_OUTPUT = json.dumps(data)
    resp, content = _get_server_response(INPUT, "aaa/xxx/eee", "aaa/xxx/eee", "test2")
    print_error_log()
    pinfo(resp, content)

    assert resp.status == 200
    assert_same_jsons(content, EXPECTED_OUTPUT)
Esempio n. 6
0
def test_missing_value_in_dict():
    """Should do nothing when there is no such value in dictionary."""
    INPUT = {"a": "b"}
    resp, content = _get_server_response(json.dumps(INPUT), "a", "x", "test")
    print_error_log()
    pinfo(resp, content)

    assert resp.status == 200
    assert_same_jsons(content, INPUT)
def test_missing_value_in_dict():
    """Should do nothing when there is no such value in dictionary."""
    INPUT = {"a": "b"}
    resp, content = _get_server_response(json.dumps(INPUT), "a", "x", "test")
    print_error_log()
    pinfo(resp, content)

    assert resp.status == 200
    assert_same_jsons(content, INPUT)
Esempio n. 8
0
def test_substitution_with_missing_key():
    """
    Should return the same JSON when the key is missing from substitution.
    """
    INPUT = '{"aaa":"bbb"}'
    resp, content = _get_server_response(INPUT, "bbb", "bbb", "test")

    print_error_log()
    pinfo(content)
    assert resp.status == 200
    assert_same_jsons(INPUT, content)
def test_substitution_with_missing_key():
    """
    Should return the same JSON when the key is missing from substitution.
    """
    INPUT = '{"aaa":"bbb"}'
    resp, content = _get_server_response(INPUT, "bbb", "bbb", "test")

    print_error_log()
    pinfo(content)
    assert resp.status == 200
    assert_same_jsons(INPUT, content)
Esempio n. 10
0
def test_setting_missing_type_for_missing_format():
    "Should not change anything."
    INPUT = {"format": None, "type": ""}
    js = json.dumps(INPUT)

    url = server() + "enrich-format?prop=format&type_field=type"

    resp, content = H.request(url, "POST", body=js)
    pinfo(content)
    assert str(resp.status).startswith("2")
    result = json.loads(content)
    assert_same_jsons(INPUT, content)
Esempio n. 11
0
def test_populating_data_provider_field():
    INPUT = {
            "descriptiveNonRepeating": {
                "data_source": "Smithsonian Institution Archives",
                }
            }
    resp, content = _get_server_response(INPUT)
    pinfo(content)
    print_error_log()
    assert resp["status"].startswith("2")
    CONTENT = json.loads(content)
    assert CONTENT["dataProvider"] == "Smithsonian Institution Archives"
Esempio n. 12
0
def test_setting_missing_type_for_missing_format():
    "Should not change anything."
    INPUT = {"format": None, "type": ""}
    js = json.dumps(INPUT)

    url = server() + "enrich-format?prop=format&type_field=type"

    resp, content = H.request(url, "POST", body=js)
    pinfo(content)
    assert str(resp.status).startswith("2")
    result = json.loads(content)
    assert_same_jsons(INPUT, content)
Esempio n. 13
0
def test_setting_missing_type_from_format():
    "Should set type according to format field."
    INPUT = {"sourceResource": {"format": "image/jpg"}}
    EXPECTED = {"sourceResource": {"type": "image"}}
    js = json.dumps(INPUT)

    url = server() + "enrich-format?prop=sourceResource/format&type_field=sourceResource/type"

    resp, content = H.request(url, "POST", body=js)
    pinfo(content)
    assert str(resp.status).startswith("2")
    result = json.loads(content)
    assert_same_jsons(EXPECTED, content)
Esempio n. 14
0
def test_enrich_date_date_parse_format_yyyy_mm_dd():
    """Correctly transform a date of format YYYY-MM-DD"""
    INPUT = {"date": "1928-05-20"}
    EXPECTED = {"date": {"begin": u"1928-05-20", "end": u"1928-05-20", "displayDate": "1928-05-20"}}

    url = server() + "enrich_earliest_date?prop=date"

    resp, content = H.request(url, "POST", body=json.dumps(INPUT))
    assert str(resp.status).startswith("2")

    pinfo(resp, content)
    result = json.loads(content)
    assert result["date"] == EXPECTED["date"]
Esempio n. 15
0
def test_dc_data_provider():
    """Should convert data providers for Digital Commonwealth."""
    data = [
        ("Beverly High School", "Beverly High School Digital Collection"),
        ("Brookline Public Library", "Brookline Photograph Collection"),
        ("C/W MARS", "Amherst Collection"),
        ("Essex Agricultural and Technical High School", "Essex Aggie Digital Collection"),
        ("Framingham State University", "Henry Whittemore Library"),
    ]
    for d in data:
        INPUT = {"a": d[1]}
        EXPECTED_OUTPUT = {"a": d[1], "b": d[0]}
        resp, content = _get_server_response(json.dumps(INPUT), "a", "b", "dc_data_provider")
        print_error_log()
        pinfo(resp, content)

        assert resp.status == 200
        assert_same_jsons(content, EXPECTED_OUTPUT)
Esempio n. 16
0
def test_setting_missing_type_from_format():
    "Should set type according to format field."
    INPUT = {
        "sourceResource": {
            "format": "image/jpg",
        }
    }
    EXPECTED = {"sourceResource": {"type": "image"}}
    js = json.dumps(INPUT)

    url = server(
    ) + "enrich-format?prop=sourceResource/format&type_field=sourceResource/type"

    resp, content = H.request(url, "POST", body=js)
    pinfo(content)
    assert str(resp.status).startswith("2")
    result = json.loads(content)
    assert_same_jsons(EXPECTED, content)
Esempio n. 17
0
def test_enrich_type_cleanup():
    "Test type normalization"
    INPUT = {
        "type": ["Still Images", "Moving Images", "Moving Image", "Text", "Statue"]
    }
    EXPECTED = {
        u'type': ["image", "moving image", "moving image", "text"],
        u'TBD_physicalformat': ["Statue"]
    }

    url = server() + "enrich-type?prop=type&format_field=TBD_physicalformat"

    resp, content = H.request(url, "POST", body=json.dumps(INPUT))
    print_error_log()
    assert str(resp.status).startswith("2")
    result = json.loads(content)
    pinfo(content)
    assert result['type'] == EXPECTED['type']
Esempio n. 18
0
def test_enrich_type_cleanup():
    "Test type normalization"
    INPUT = {
        "type":
        ["Still Images", "Moving Images", "Moving Image", "Text", "Statue"]
    }
    EXPECTED = {
        u'type': ["image", "moving image", "moving image", "text"],
        u'TBD_physicalformat': ["Statue"]
    }

    url = server() + "enrich-type?prop=type&format_field=TBD_physicalformat"

    resp, content = H.request(url, "POST", body=json.dumps(INPUT))
    print_error_log()
    assert str(resp.status).startswith("2")
    result = json.loads(content)
    pinfo(content)
    assert result['type'] == EXPECTED['type']
Esempio n. 19
0
def test_enrich_date_date_parse_format_yyyy_mm_dd():
    """Correctly transform a date of format YYYY-MM-DD"""
    INPUT = {"date": "1928-05-20"}
    EXPECTED = {
        'date': {
            'begin': u'1928-05-20',
            'end': u'1928-05-20',
            'displayDate': '1928-05-20'
        }
    }

    url = server() + "enrich_earliest_date?prop=date"

    resp, content = H.request(url, "POST", body=json.dumps(INPUT))
    assert str(resp.status).startswith("2")

    pinfo(resp, content)
    result = json.loads(content)
    assert result['date'] == EXPECTED['date']
def test_enrich_date_date_parse_format_yyyy_00_00():
    """Strip off bogus MM-DD from YYYY-00-00"""
    INPUT = {"date": "1990-00-00"}
    EXPECTED = {
        'date': [{
            'begin': u'1990',
            'end': u'1990',
            'displayDate': '1990'
        }]
    }

    url = server() + "enrich_earliest_date?prop=date"

    resp, content = H.request(url, "POST", body=json.dumps(INPUT))
    assert str(resp.status).startswith("2")

    pinfo(resp, content)
    result = json.loads(content)
    assert result['date'] == EXPECTED['date']
Esempio n. 21
0
def test_dc_data_provider():
    """Should convert data providers for Digital Commonwealth."""
    data = [
        ("Beverly High School", "Beverly High School Digital Collection"),
        ("Brookline Public Library", "Brookline Photograph Collection"),
        ("C/W MARS", "Amherst Collection"),
        ("Essex Agricultural and Technical High School",
         "Essex Aggie Digital Collection"),
        ("Framingham State University", "Henry Whittemore Library"),
    ]
    for d in data:
        INPUT = {"a": d[1]}
        EXPECTED_OUTPUT = {"a": d[1], "b": d[0]}
        resp, content = _get_server_response(json.dumps(INPUT), "a", "b",
                                             "dc_data_provider")
        print_error_log()
        pinfo(resp, content)

        assert resp.status == 200
        assert_same_jsons(content, EXPECTED_OUTPUT)
def test_substitute_with_list_of_dictionaries():
    """
    Should convert all dicts in a list.
    """
    data = {
        "xxx": "yyy",
        "aaa": {
            "bbb": "ccc",
            "xxx": [{
                "eee": "aaa"
            }, {
                "xxx": "eee"
            }, {
                "eee": "bbb"
            }]
        }
    }

    INPUT = json.dumps(data)
    data["aaa"]["xxx"] = [
        {
            "eee": "AAA222"
        },
        {
            "xxx": "eee"
        },
        {
            "eee": "BBB222"
        },
    ]

    EXPECTED_OUTPUT = json.dumps(data)
    resp, content = _get_server_response(INPUT, "aaa/xxx/eee", "aaa/xxx/eee",
                                         "test2")
    print_error_log()
    pinfo(resp, content)

    assert resp.status == 200
    assert_same_jsons(content, EXPECTED_OUTPUT)
Esempio n. 23
0
def test_populating_title():
    LABELS = (True, "Title"), (True, "title"), (False, "aaa"), (False, "bbb")
    INPUT = {
            "descriptiveNonRepeating": {
                "title": {
                    "@label": "",
                    "#text": "tt",
                }
            }
    }

    for d in LABELS:
        INPUT["descriptiveNonRepeating"]["title"]["@label"] = d[1]
        resp, content = _get_server_response(INPUT)
        #print_error_log()
        pinfo(resp, content)
        assert resp["status"].startswith("2")
        CONTENT = json.loads(content)
        if d[0]:
            assert_same_jsons({"title": "tt"}, CONTENT["sourceResource"])
        else:
            assert "sourceResource" not in CONTENT
Esempio n. 24
0
def test_setting_empty_type_from_format():
    "Should set empty type according to format field according to type mapping."

    DATA = [{
        "in": {
            "format": "audio/mp3"
        },
        "out": {
            "type": "sound"
        }
    }, {
        "in": {
            "format": "image/jpg"
        },
        "out": {
            "type": "image"
        }
    }, {
        "in": {
            "format": "video/mpeg"
        },
        "out": {
            "type": "moving image"
        }
    }, {
        "in": {
            "format": "text/calendar"
        },
        "out": {
            "type": "text"
        }
    }, {
        "in": {
            "format": "audio"
        },
        "out": {
            "format": "audio"
        }
    }, {
        "in": {
            "format": "something strange"
        },
        "out": {
            "format": "something strange"
        }
    }]

    FORMATS = ["audio"]
    EXPECTED_TYPES = []
    INPUT = {"sourceResource": {"format": "FORMAT", "type": ""}}
    EXPECTED = {"sourceResource": {"format": "FORMAT", "type": "TYPE"}}

    for d in DATA:
        INPUT["sourceResource"] = d["in"]
        js = json.dumps(INPUT)
        url = server() + "enrich-format?prop=sourceResource/format"

        resp, content = H.request(url, "POST", body=js)
        #pinfo(content)
        pinfo(INPUT)

        assert str(resp.status).startswith("2")

        EXPECTED["sourceResource"] = d["out"]
        assert_same_jsons(EXPECTED, content)
Esempio n. 25
0
def test_populating_collection_name():
    INPUT = {
            "collection": {
                "@id": "http://dp.la/api/collections/smithsonian--nmafa_files_1964-2008_[national_museum_of_african_art_u.s._office_of_the_director]",
                "title": "nmafa_files_1964-2008_[national_museum_of_african_art_u.s._office_of_the_director]"
                },
            "freetext": {
                "physicalDescription": {
                    "#text": "4 cu. ft. (4 record storage boxes)",
                    "@label": "Physical description"
                    },
                "name": [
                    {
                        "#text": "National Museum of African Art (U.S.) Office of the Director",
                        "@label": "Creator"
                        },
                    {
                        "#text": "Fiske, Patricia L",
                        "@label": "Subject"
                        },
                    {
                        "#text": "Patton, Sharon F",
                        "@label": "Subject"
                        },
                    {
                        "#text": "Robbins, Warren M",
                        "@label": "Subject"
                        },
                    {
                        "#text": "Walker, Roslyn A",
                        "@label": "Subject"
                        },
                    {
                        "#text": "Williams, Sylvia H",
                        "@label": "Subject"
                        },
                    {
                        "#text": "Reinhardt, John E",
                        "@label": "Subject"
                        },
                    {
                        "#text": "Museum of African Art (U.S.)",
                        "@label": "Subject"
                        },
                    {
                        "#text": "Quadrangle Complex Quadrangle Museum Project",
                        "@label": "Subject"
                        }
                    ],
                "setName": {
                    "#text": "NMAfA Files 1964-2008 [National Museum of African Art (U.S.) Office of the Director]",
                    "@label": "See more items in"
                    },
                }
            }
    EXPECTED_COLLECTION = {
            "@id": "http://dp.la/api/collections/smithsonian--nmafa_files_1964-2008_[national_museum_of_african_art_u.s._office_of_the_director]",
            "title": "NMAfA Files 1964-2008 [National Museum of African Art (U.S.) Office of the Director]",
            }
    resp, content = _get_server_response(INPUT)
    assert resp["status"].startswith("2")
    CONTENT = json.loads(content)
    pinfo(content,CONTENT["sourceResource"]["collection"])
    assert_same_jsons(EXPECTED_COLLECTION, CONTENT["sourceResource"]["collection"])