Example #1
0
    def test_update_author_from_remote(self):
        """trigger the function that looks up the remote data"""
        author = models.Author.objects.create(name="Test",
                                              openlibrary_key="OL123A")
        # pylint: disable=attribute-defined-outside-init
        self.connector.author_mappings = [
            Mapping("id"),
            Mapping("name"),
            Mapping("isni"),
        ]

        responses.add(
            responses.GET,
            "https://openlibrary.org/authors/OL123A",
            json={
                "id": "https://www.example.com/author",
                "name": "Beep",
                "isni": "hi"
            },
        )

        self.connector.update_author_from_remote(author)

        author.refresh_from_db()
        self.assertEqual(author.name, "Test")
        self.assertEqual(author.isni, "hi")
    def setUp(self):
        self.book = models.Edition.objects.create(title='Example Edition')

        models.Connector.objects.create(
            identifier='example.com',
            connector_file='openlibrary',
            base_url='https://example.com',
            books_url='https:/example.com',
            covers_url='https://example.com',
            search_url='https://example.com/search?q=',
        )
        self.connector = Connector('example.com')

        self.data = {
            'title': 'Unused title',
            'ASIN': 'A00BLAH',
            'isbn_10': '1234567890',
            'isbn_13': 'blahhh',
            'blah': 'bip',
            'format': 'hardcover',
            'series': ['one', 'two'],
        }
        self.connector.key_mappings = [
            Mapping('isbn_10', model=models.Edition),
            Mapping('isbn_13'),
            Mapping('lccn', model=models.Work),
            Mapping('asin', remote_field='ASIN'),
        ]
Example #3
0
    def setUp(self):
        ''' we need an example connector '''
        self.connector_info = models.Connector.objects.create(
            identifier='example.com',
            connector_file='openlibrary',
            base_url='https://example.com',
            books_url='https://example.com/books',
            covers_url='https://example.com/covers',
            search_url='https://example.com/search?q=',
        )
        work_data = {
            'id': 'abc1',
            'title': 'Test work',
            'type': 'work',
            'openlibraryKey': 'OL1234W',
        }
        self.work_data = work_data
        edition_data = {
            'id': 'abc2',
            'title': 'Test edition',
            'type': 'edition',
            'openlibraryKey': 'OL1234M',
        }
        self.edition_data = edition_data

        class TestConnector(abstract_connector.AbstractConnector):
            ''' nothing added here '''
            def format_search_result(self, search_result):
                return search_result

            def parse_search_data(self, data):
                return data

            def is_work_data(self, data):
                return data['type'] == 'work'

            def get_edition_from_work_data(self, data):
                return edition_data

            def get_work_from_edition_data(self, data):
                return work_data

            def get_authors_from_data(self, data):
                return []

            def expand_book_data(self, book):
                pass

        self.connector = TestConnector('example.com')
        self.connector.book_mappings = [
            Mapping('id'),
            Mapping('title'),
            Mapping('openlibraryKey'),
        ]

        self.book = models.Edition.objects.create(
            title='Test Book',
            remote_id='https://example.com/book/1234',
            openlibrary_key='OL1234M')
Example #4
0
    def test_get_or_create_author(self):
        """load an author"""
        self.connector.author_mappings = [
            Mapping("id"),
            Mapping("name"),
        ]

        responses.add(
            responses.GET,
            "https://www.example.com/author",
            json={"id": "https://www.example.com/author", "name": "Test Author"},
        )
        result = self.connector.get_or_create_author("https://www.example.com/author")
        self.assertIsInstance(result, models.Author)
        self.assertEqual(result.name, "Test Author")
        self.assertEqual(result.origin_id, "https://www.example.com/author")
 def test_create_mapping_with_formatter(self):
     ''' a function is provided to modify the data '''
     formatter = lambda x: 'aa' + x
     mapping = Mapping('isbn', formatter=formatter)
     self.assertEqual(mapping.local_field, 'isbn')
     self.assertEqual(mapping.remote_field, 'isbn')
     self.assertEqual(mapping.formatter, formatter)
     self.assertEqual(mapping.formatter('bb'), 'aabb')
Example #6
0
 def test_create_mapping_with_formatter(self):
     """ a function is provided to modify the data """
     formatter = lambda x: "aa" + x
     mapping = Mapping("isbn", formatter=formatter)
     self.assertEqual(mapping.local_field, "isbn")
     self.assertEqual(mapping.remote_field, "isbn")
     self.assertEqual(mapping.formatter, formatter)
     self.assertEqual(mapping.formatter("bb"), "aabb")
 def test_create_mapping_with_formatter(self):
     formatter = lambda x: 'aa' + x
     mapping = Mapping('isbn', formatter=formatter)
     self.assertEqual(mapping.local_field, 'isbn')
     self.assertEqual(mapping.remote_field, 'isbn')
     self.assertEqual(mapping.formatter, formatter)
     self.assertEqual(mapping.model, None)
     self.assertEqual(mapping.formatter('bb'), 'aabb')
 def test_create_mapping_with_remote(self):
     ''' the remote field is different than the local field '''
     mapping = Mapping('isbn', remote_field='isbn13')
     self.assertEqual(mapping.local_field, 'isbn')
     self.assertEqual(mapping.remote_field, 'isbn13')
     self.assertEqual(mapping.formatter('bb'), 'bb')
 def test_create_mapping(self):
     ''' maps remote fields for book data to bookwyrm activitypub fields '''
     mapping = Mapping('isbn')
     self.assertEqual(mapping.local_field, 'isbn')
     self.assertEqual(mapping.remote_field, 'isbn')
     self.assertEqual(mapping.formatter('bb'), 'bb')
Example #10
0
 def test_create_mapping_with_remote(self):
     """ the remote field is different than the local field """
     mapping = Mapping("isbn", remote_field="isbn13")
     self.assertEqual(mapping.local_field, "isbn")
     self.assertEqual(mapping.remote_field, "isbn13")
     self.assertEqual(mapping.formatter("bb"), "bb")
Example #11
0
 def test_create_mapping(self):
     """ maps remote fields for book data to bookwyrm activitypub fields """
     mapping = Mapping("isbn")
     self.assertEqual(mapping.local_field, "isbn")
     self.assertEqual(mapping.remote_field, "isbn")
     self.assertEqual(mapping.formatter("bb"), "bb")
 def test_create_mapping_with_remote(self):
     mapping = Mapping('isbn', remote_field='isbn13')
     self.assertEqual(mapping.local_field, 'isbn')
     self.assertEqual(mapping.remote_field, 'isbn13')
     self.assertEqual(mapping.model, None)
     self.assertEqual(mapping.formatter('bb'), 'bb')
Example #13
0
    def setUp(self):
        """ we need an example connector """
        self.connector_info = models.Connector.objects.create(
            identifier="example.com",
            connector_file="openlibrary",
            base_url="https://example.com",
            books_url="https://example.com/books",
            covers_url="https://example.com/covers",
            search_url="https://example.com/search?q=",
        )
        work_data = {
            "id": "abc1",
            "title": "Test work",
            "type": "work",
            "openlibraryKey": "OL1234W",
        }
        self.work_data = work_data
        edition_data = {
            "id": "abc2",
            "title": "Test edition",
            "type": "edition",
            "openlibraryKey": "OL1234M",
        }
        self.edition_data = edition_data

        class TestConnector(abstract_connector.AbstractConnector):
            """ nothing added here """
            def format_search_result(self, search_result):
                return search_result

            def parse_search_data(self, data):
                return data

            def format_isbn_search_result(self, search_result):
                return search_result

            def parse_isbn_search_data(self, data):
                return data

            def is_work_data(self, data):
                return data["type"] == "work"

            def get_edition_from_work_data(self, data):
                return edition_data

            def get_work_from_edition_data(self, data):
                return work_data

            def get_authors_from_data(self, data):
                return []

            def expand_book_data(self, book):
                pass

        self.connector = TestConnector("example.com")
        self.connector.book_mappings = [
            Mapping("id"),
            Mapping("title"),
            Mapping("openlibraryKey"),
        ]

        self.book = models.Edition.objects.create(
            title="Test Book",
            remote_id="https://example.com/book/1234",
            openlibrary_key="OL1234M",
        )