test_models.py
1 from unittest.mock import patch 2 3 import pytest 4 5 from webmention.models import WebMentionResponse 6 7 8 @pytest.fixture 9 def test_response_body(): 10 return "foo" 11 12 13 @pytest.mark.django_db 14 def test_str(test_source, test_target, test_response_body): 15 webmention = WebMentionResponse.objects.create( 16 source=test_source, response_to=test_target, response_body=test_response_body 17 ) 18 webmention.save() 19 20 assert str(webmention) == webmention.source 21 22 23 @pytest.mark.django_db 24 def test_source_for_admin(test_source, test_target, test_response_body): 25 webmention = WebMentionResponse.objects.create( 26 source=test_source, response_to=test_target, response_body=test_response_body 27 ) 28 webmention.save() 29 30 assert webmention.source_for_admin() == '<a href="{href}">{href}</a>'.format(href=webmention.source) 31 32 33 @pytest.mark.django_db 34 def test_response_to_for_admin(test_source, test_target, test_response_body): 35 webmention = WebMentionResponse.objects.create( 36 source=test_source, response_to=test_target, response_body=test_response_body 37 ) 38 webmention.save() 39 40 assert webmention.response_to_for_admin() == '<a href="{href}">{href}</a>'.format(href=webmention.response_to) 41 42 43 @patch("webmention.models.WebMentionResponse.save") 44 def test_invalidate_when_not_previously_saved(mock_save): 45 webmention = WebMentionResponse() 46 webmention.invalidate() 47 48 assert not mock_save.called 49 50 51 @pytest.mark.django_db 52 def test_invalidate_when_previously_saved(test_source, test_target, test_response_body): 53 webmention = WebMentionResponse.objects.create( 54 source=test_source, response_to=test_target, response_body=test_response_body 55 ) 56 webmention.save() 57 webmention.invalidate() 58 59 assert not webmention.current 60 61 62 @patch("webmention.models.WebMentionResponse.save") 63 def test_update_when_previously_invalid(mock_save, test_source, test_target, test_response_body): 64 webmention = WebMentionResponse.objects.create(source="foo", response_to="bar", response_body="baz", current=False) 65 assert mock_save.call_count == 1 66 webmention.update(test_source, test_target, test_response_body) 67 68 assert webmention.current 69 assert webmention.source == test_source 70 assert webmention.response_to == test_target 71 assert webmention.response_body == test_response_body 72 assert mock_save.call_count == 2