2021-03-08 16:49:10 +00:00
|
|
|
""" testing models """
|
2020-05-12 21:45:30 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import models
|
2020-12-04 19:22:08 +00:00
|
|
|
from bookwyrm.models import base_model
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
2020-05-12 21:45:30 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-05-12 21:45:30 +00:00
|
|
|
class BaseModel(TestCase):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" functionality shared across models """
|
|
|
|
|
2020-05-13 01:56:28 +00:00
|
|
|
def test_remote_id(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" these should be generated """
|
2020-12-04 19:22:08 +00:00
|
|
|
instance = base_model.BookWyrmModel()
|
2020-05-12 21:45:30 +00:00
|
|
|
instance.id = 1
|
2020-05-13 01:56:28 +00:00
|
|
|
expected = instance.get_remote_id()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(expected, "https://%s/bookwyrmmodel/1" % DOMAIN)
|
2020-05-12 21:59:14 +00:00
|
|
|
|
2020-05-13 01:56:28 +00:00
|
|
|
def test_remote_id_with_user(self):
|
2021-03-08 16:49:10 +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(
|
2021-03-08 16:49:10 +00:00
|
|
|
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
|
|
|
|
)
|
2020-12-04 19:22:08 +00:00
|
|
|
instance = base_model.BookWyrmModel()
|
2020-05-12 22:07:31 +00:00
|
|
|
instance.user = user
|
|
|
|
instance.id = 1
|
2020-05-13 01:56:28 +00:00
|
|
|
expected = instance.get_remote_id()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(expected, "https://%s/user/mouse/bookwyrmmodel/1" % DOMAIN)
|
2020-12-04 19:22:08 +00:00
|
|
|
|
|
|
|
def test_execute_after_save(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" this function sets remote ids after creation """
|
2020-12-04 19:22:08 +00:00
|
|
|
# using Work because it BookWrymModel is abstract and this requires save
|
|
|
|
# Work is a relatively not-fancy model.
|
2021-03-08 16:49:10 +00:00
|
|
|
instance = models.Work.objects.create(title="work title")
|
2020-12-04 19:22:08 +00:00
|
|
|
instance.remote_id = None
|
|
|
|
base_model.execute_after_save(None, instance, True)
|
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
instance.remote_id, "https://%s/book/%d" % (DOMAIN, instance.id)
|
2020-12-04 19:22:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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)
|