moviewyrm/bookwyrm/tests/models/test_base_model.py

46 lines
1.7 KiB
Python
Raw Normal View History

2020-05-12 21:45:30 +00:00
''' testing models '''
from django.test import TestCase
from bookwyrm import models
from bookwyrm.models import base_model
from bookwyrm.settings import DOMAIN
2020-05-12 21:45:30 +00:00
class BaseModel(TestCase):
2020-12-12 21:39:55 +00:00
''' functionality shared across models '''
def test_remote_id(self):
2020-12-12 21:39:55 +00:00
''' these should be generated '''
instance = base_model.BookWyrmModel()
2020-05-12 21:45:30 +00:00
instance.id = 1
expected = instance.get_remote_id()
self.assertEqual(expected, 'https://%s/bookwyrmmodel/1' % DOMAIN)
2020-05-12 21:59:14 +00:00
def test_remote_id_with_user(self):
2020-12-12 21:39:55 +00:00
''' format of remote id when there's a user object '''
2020-05-12 22:07:31 +00:00
user = models.User.objects.create_user(
'mouse', 'mouse@mouse.com', 'mouseword',
local=True, localname='mouse')
instance = base_model.BookWyrmModel()
2020-05-12 22:07:31 +00:00
instance.user = user
instance.id = 1
expected = instance.get_remote_id()
2020-05-12 22:07:31 +00:00
self.assertEqual(
expected,
'https://%s/user/mouse/bookwyrmmodel/1' % DOMAIN)
def test_execute_after_save(self):
''' this function sets remote ids after creation '''
# using Work because it BookWrymModel is abstract and this requires save
# Work is a relatively not-fancy model.
instance = models.Work.objects.create(title='work title')
instance.remote_id = None
base_model.execute_after_save(None, instance, True)
self.assertEqual(
instance.remote_id,
'https://%s/book/%d' % (DOMAIN, instance.id)
)
# shouldn't set remote_id if it's not created
instance.remote_id = None
base_model.execute_after_save(None, instance, False)
self.assertIsNone(instance.remote_id)