Test fixes

This commit is contained in:
Mouse Reeve 2021-02-17 08:35:17 -08:00
parent 7b27f98e20
commit cbf5479253
4 changed files with 42 additions and 20 deletions

View file

@ -92,11 +92,26 @@ class Openlibrary(TestCase):
responses.add(
responses.GET,
'https://openlibrary.org/authors/OL382982A',
json={'hi': 'there'},
json={
"name": "George Elliott",
"personal_name": "George Elliott",
"last_modified": {
"type": "/type/datetime",
"value": "2008-08-31 10:09:33.413686"
},
"key": "/authors/OL453734A",
"type": {
"key": "/type/author"
},
"id": 1259965,
"revision": 2
},
status=200)
results = self.connector.get_authors_from_data(self.work_data)
for result in results:
self.assertIsInstance(result, models.Author)
result = list(results)[0]
self.assertIsInstance(result, models.Author)
self.assertEqual(result.name, 'George Elliott')
self.assertEqual(result.openlibrary_key, 'OL453734A')
def test_get_cover_url(self):
@ -201,8 +216,11 @@ class Openlibrary(TestCase):
'https://openlibrary.org/authors/OL382982A',
json={'hi': 'there'},
status=200)
result = self.connector.create_edition_from_data(
work, self.edition_data)
with patch('bookwyrm.connectors.openlibrary.Connector.' \
'get_authors_from_data') as mock:
mock.return_value = []
result = self.connector.create_edition_from_data(
work, self.edition_data)
self.assertEqual(result.parent_work, work)
self.assertEqual(result.title, 'Sabriel')
self.assertEqual(result.isbn_10, '0060273224')

View file

@ -30,6 +30,12 @@ class ActivitypubMixins(TestCase):
outbox='https://example.com/users/rat/outbox',
)
self.object_mock = {
'to': 'to field', 'cc': 'cc field',
'content': 'hi', 'id': 'bip', 'type': 'Test',
'published': '2020-12-04T17:52:22.623807+00:00',
}
# ActivitypubMixin
def test_to_activity(self):
@ -292,15 +298,10 @@ class ActivitypubMixins(TestCase):
def test_to_create_activity(self):
''' wrapper for ActivityPub "create" action '''
object_activity = {
'to': 'to field', 'cc': 'cc field',
'content': 'hi',
'published': '2020-12-04T17:52:22.623807+00:00',
}
MockSelf = namedtuple('Self', ('remote_id', 'to_activity'))
mock_self = MockSelf(
'https://example.com/status/1',
lambda *args: object_activity
lambda *args: self.object_mock
)
activity = ObjectMixin.to_create_activity(
mock_self, self.local_user)
@ -312,7 +313,7 @@ class ActivitypubMixins(TestCase):
self.assertEqual(activity['type'], 'Create')
self.assertEqual(activity['to'], 'to field')
self.assertEqual(activity['cc'], 'cc field')
self.assertEqual(activity['object'], object_activity)
self.assertIsInstance(activity['object'], dict)
self.assertEqual(
activity['signature'].creator,
'%s#main-key' % self.local_user.remote_id
@ -323,7 +324,7 @@ class ActivitypubMixins(TestCase):
MockSelf = namedtuple('Self', ('remote_id', 'to_activity'))
mock_self = MockSelf(
'https://example.com/status/1',
lambda *args: {}
lambda *args: self.object_mock
)
activity = ObjectMixin.to_delete_activity(
mock_self, self.local_user)
@ -346,7 +347,7 @@ class ActivitypubMixins(TestCase):
MockSelf = namedtuple('Self', ('remote_id', 'to_activity'))
mock_self = MockSelf(
'https://example.com/status/1',
lambda *args: {}
lambda *args: self.object_mock
)
activity = ObjectMixin.to_update_activity(
mock_self, self.local_user)
@ -361,7 +362,7 @@ class ActivitypubMixins(TestCase):
self.assertEqual(
activity['to'],
['https://www.w3.org/ns/activitystreams#Public'])
self.assertEqual(activity['object'], {})
self.assertIsInstance(activity['object'], dict)
# Activity mixin
@ -370,7 +371,7 @@ class ActivitypubMixins(TestCase):
MockSelf = namedtuple('Self', ('remote_id', 'to_activity', 'user'))
mock_self = MockSelf(
'https://example.com/status/1',
lambda *args: {},
lambda *args: self.object_mock,
self.local_user,
)
activity = ActivityMixin.to_undo_activity(mock_self)
@ -380,4 +381,4 @@ class ActivitypubMixins(TestCase):
)
self.assertEqual(activity['actor'], self.local_user.remote_id)
self.assertEqual(activity['type'], 'Undo')
self.assertEqual(activity['object'], {})
self.assertIsInstance(activity['object'], dict)

View file

@ -17,6 +17,7 @@ from django.db import models
from django.test import TestCase
from django.utils import timezone
from bookwyrm import activitypub
from bookwyrm.activitypub.base_activity import ActivityObject
from bookwyrm.models import fields, User, Status
from bookwyrm.models.base_model import BookWyrmModel
@ -275,7 +276,7 @@ class ActivitypubFields(TestCase):
'rat', 'rat@rat.rat', 'ratword',
local=True, localname='rat')
with patch('bookwyrm.models.user.set_remote_server.delay'):
value = instance.field_from_activity(userdata)
value = instance.field_from_activity(activitypub.Person(**userdata))
self.assertIsInstance(value, User)
self.assertNotEqual(value, unrelated_user)
self.assertEqual(value.remote_id, 'https://example.com/user/mouse')
@ -300,7 +301,7 @@ class ActivitypubFields(TestCase):
local=True, localname='rat')
with patch('bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast'):
value = instance.field_from_activity(userdata)
value = instance.field_from_activity(activitypub.Person(**userdata))
self.assertEqual(value, user)

View file

@ -171,6 +171,8 @@ class ImportJob(TestCase):
'bookwyrm.connectors.connector_manager.first_search_result'
) as search:
search.return_value = result
book = self.item_1.get_book_from_isbn()
with patch('bookwyrm.connectors.openlibrary.Connector.' \
'get_authors_from_data'):
book = self.item_1.get_book_from_isbn()
self.assertEqual(book.title, 'Sabriel')