Esempio n. 1
0
    def test_suggestions(self):
        # Create and index a manifest.
        v_id = str(uuid.uuid4())
        v_url = "http://localhost:8888/misirlou/tests/fixtures/manifest.json"
        with open("misirlou/tests/fixtures/manifest.json") as f:
            w_valid = ManifestImporter(v_url, v_id, prefetched_data=f.read())
        w_valid.create()
        self.solr_con.commit()

        # Make sure suggester is built.
        url = settings.SOLR_TEST + "suggest/?suggest.build=true&suggest.reload=true"
        requests.get(url)

        # Some queries with known responses given our test manifest.
        resp = self.client.get("/suggest/?q=serm")
        expected = {'suggestions': ['<b>serm</b>on', '<b>serm</b>ons']}
        self.assertDictEqual(resp.data, expected)

        resp = self.client.get("/suggest/?q=dest")
        expected = {'suggestions': ['<b>dest</b>ine']}
        self.assertDictEqual(resp.data, expected)

        # A query which should have no suggestions, as it is a complete word.
        resp = self.client.get("/suggest/?q=dimanches")
        expected = {'suggestions': []}
        self.assertDictEqual(resp.data, expected)

        # Forgetting the query parameter is handled silently.
        resp = self.client.get("/suggest/")
        expected = {'suggestions': []}
        self.assertDictEqual(resp.data, expected)
Esempio n. 2
0
    def _retrieve_remote_manifest(self):
        """Test the ability to fetch this manifest from the remote.

        An SHA1 hash is computed and stored in self.remote_hash.
        The manifest at the location is stored as self.remote_json.
        """
        remote_url = self.local_json['@id']

        if not remote_url.startswith('https'):
            self._handle_err("HTTPS_STORED")

        resp = None
        try:
            resp = requests.get(remote_url, timeout=20)
        except requests.exceptions.SSLError:
            self._handle_err("MANIFEST_SSL_FAILURE")
        if not resp:
            try:
                resp = requests.get(remote_url, verify=False, timeout=20)
            except requests.exceptions.Timeout:
                self._handle_err("TIMEOUT_REMOTE_RETRIEVAL")

        if (resp.status_code < 200 or resp.status_code >= 400) and self.RAISE_FAILED_REMOTE_RETRIEVAL:
            self._handle_err("FAILED_REMOTE_RETRIEVAL")

        self.remote_hash = ManifestImporter.generate_manifest_hash(resp.text)
        self.remote_json = json.loads(resp.text)
Esempio n. 3
0
    def test_results_search(self):

        # Create and index a manifest.
        v_id = str(uuid.uuid4())
        v_url = "http://*****:*****@id': 'http://testserver/?q=Maria&format=json',
                'results': [{
                    'label': ['Luzern, Zentral- und Hochschulbibliothek, KB 35 4°'],
                    'logo': None,
                    'description': ['In addition to sermons and sermon-related material pertaining to Sundays, saints’ days and feast-days dedicated to Mary, the manuscript contains part of S. Bonaventure’s (1221-1274) commentary on the four books of the Sentences of Peter Lombard, and the treatise De arca Noe by Marquard of Lindau (d. 1392). '],
                    'thumbnail': {
                        'height': 6496,
                        'format': 'image/jpeg',
                        '@type': 'dctypes:Image',
                        'width': 4872,
                        'service': {
                            'profile': 'http://library.stanford.edu/iiif/image-api/compliance.html#level1',
                            '@context': 'http://iiif.io/api/image/2/context.json',
                            '@id': 'http://www.e-codices.unifr.ch/loris/zhl/zhl-0035-4/zhl-0035-4_e001.jp2'},
                        '@id': 'http://www.e-codices.unifr.ch/loris/zhl/zhl-0035-4/zhl-0035-4_e001.jp2/full/full/0/default.jpg'},
                    'local_id': '183072c6-2eba-4faf-af1c-87f77b7aa229',
                    'hits': [{
                        'field': 'description_txt_it',
                        'parsed': [' dedicate a ', 'Maria', ', il manoscritto contiene parti del commento di s. Bonaventura (1221-1274) al libro']}],
                    '@id': 'http://localhost:8888/misirlou/tests/fixtures/manifest.json',
                    'attribution': ['e-codices - Virtual Manuscript Library of Switzerland']}],
                'num_found': 1},
            'routes': {
                'manifests': 'http://testserver/manifests/?format=json'}}
        self.solr_con.delete_all()
        self.solr_con.commit()
Esempio n. 4
0
    def test_get(self):
        # Create and index a manifest.
        v_id = str(uuid.uuid4())
        v_url = "http://localhost:8888/misirlou/tests/fixtures/manifest.json"

        with open("misirlou/tests/fixtures/manifest.json") as f:
            w_valid = ManifestImporter(v_url, v_id, prefetched_data=f.read())

        w_valid.create()
        w_valid.db_rep.is_valid = True
        w_valid.db_rep.save()
        w_valid.db_rep._update_solr_validation()
        self.solr_con.commit()

        with open("misirlou/tests/fixtures/recent_manifests.json") as f:
            expected = json.load(f)

        actual = self.client.get("/manifests/recent/").json()

        for result in itertools.chain(expected["results"], actual["results"]):
            del result["local_id"]

        self.assertDictEqual(actual, expected)
Esempio n. 5
0
 def setUp(self):
     self.v_id = str(uuid.uuid4())
     self.v_url = "http://localhost:8888/misirlou/tests/fixtures/manifest.json"
     with open("misirlou/tests/fixtures/manifest.json") as f:
         self.w_valid = ManifestImporter(self.v_url, self.v_id, prefetched_data=f.read())
Esempio n. 6
0
class ManifestImporterTestCase(MisirlouTestSetup):
    def setUp(self):
        self.v_id = str(uuid.uuid4())
        self.v_url = "http://*****:*****@language': "en", "@value": "one"},
                                        {'@language': "fr", "@value": "deux"}],
            'multiple-unknown-no-en': [{'@language': "de", "@value": "eins"},
                                       {'@language': "it", "@value": "dos"}],
        }
        self.w_valid.doc = {'metadata': []}
        for k, v in metadata.items():
            self.w_valid._add_metadata(k, v)

        #  Needed as hashed keys may appear in any order.
        self.w_valid.doc['metadata'] = sorted(self.w_valid.doc['metadata'])

        correct = {'metadata': ['one', 'one two'],
                   'metadata_txt_de': ['eins'],
                   'metadata_txt_fr': ['deux'],
                   'metadata_txt_en': ['one'],
                   'metadata_txt_it': ['dos']}
        self.assertDictEqual(self.w_valid.doc, correct)

    def test_add_metadata_known_keys(self):
        """Test parsing metadata with known keys.

        When keys are known, the parser should add them as independent keys
        in the document, preserve language data, and set defaults when
        English values are found.
        """

        metadata = {
            'title': "The title",
            'author': [{'@language': "en", "@value": "The Author"},
                       {'@language': "fr", "@value": "Le Author"}],
            'location': [{'@language': 'fr', "@value": "Le Monde"}],
            'period': "Around 14th Century"
        }
        self.w_valid.doc = {'metadata': []}
        for k, v in metadata.items():
            self.w_valid._add_metadata(k, v)

        correct = {'title': 'The title',
                   'author': 'The Author',
                   'author_txt_fr': 'Le Author',
                   'location_txt_fr': 'Le Monde',
                   'date': 'Around 14th Century',
                   'metadata': []}

        self.assertDictEqual(self.w_valid.doc, correct)

    def test_label_normalizer(self):
        """Test behaviour of label normalizer (choosing a label).

        Label normalizer will compare labels against the map defined in
        setings.py. The goal is to match similar words to one indexed field
        (e.g. 'period' -> 'date', 'Title(s)' -> 'title'"

        It should follow this priority in choosing a label

            1) An english label that can be normalized
            2) Any label that can be normalized
            3) Return None (as signifier of no good choices)
        """

        # An english, normalizable label gets priority.
        eng_priority = [{"@language": "fr", "@value": "date"},
                        {"@language": "en", "@value": "title(s)"}]
        l = self.w_valid._meta_label_normalizer(eng_priority)
        self.assertEqual(l, "title")

        # A normalizable label get's priority over an unknown english label.
        norm_priority = [{"@language": "fr", "@value": "publication date"},
                        {"@language": "en", "@value": "Unknown"}]
        l = self.w_valid._meta_label_normalizer(norm_priority)
        self.assertEqual(l, 'date')

        # With no normalization possible, return None.
        no_eng = [{"@language": "fr", "@value": "je ne sais pas"},
                 {"@language": "it", "@value": "unknown"}]
        l = self.w_valid._meta_label_normalizer(no_eng)
        self.assertEqual(l, None)

        # If label is a normalizable string, get it's normalization
        str_test = "publication date"
        l = self.w_valid._meta_label_normalizer(str_test)
        self.assertEqual(l, "date")

        # If label is unknown string, return nothing.
        str_test = "Unknown"
        l = self.w_valid._meta_label_normalizer(str_test)
        self.assertEqual(l, None)

        # Lack of a label returns None
        l = self.w_valid._meta_label_normalizer(None)
        self.assertEqual(l, None)