from django.test import SimpleTestCase
from main.views_functions import clean_string, Fanfic
from unittest import mock
class CleanStringTests(SimpleTestCase):
def test_clean_normal_string(self):
text = "this is an example"
self.assertEquals(clean_string(text), text)
def test_clean_empty_string(self):
text = ""
self.assertEquals(clean_string(text), text)
class FanficTests(SimpleTestCase):
''' Get the site name of url '''
def test_get_correct_site_ficwad(self):
url = "ficwad.com"
self.assertEquals(Fanfic.get_site(url), "ficwad.com")
def test_get_correct_site_avengersfanfiction(self):
url = "avengersfanfiction.com"
self.assertEquals(Fanfic.get_site(url),
"avengersfanfiction.com")
def test_get_correct_site_archiveofourown(self):
url = "archiveofourown.org"
self.assertEquals(Fanfic.get_site(url), "archiveofourown.org")
def test_get_incorrect_site(self):
url = "whatever.org"
self.assertEquals(Fanfic.get_site(url), None)
''' Check if url format is ok '''
def test_check_url_format_ok(self):
url = "http://ficwad.com/whatever-whatever"
self.assertEquals(Fanfic.check_url_format(url), True)
def test_check_url_format_not_ok(self):
url = "http://sdfsdf.com/whatever-whatever"
self.assertEquals(Fanfic.check_url_format(url), False)
''' Check if online '''
@mock.patch('requests.head')
def test_check_if_online_true(self, mock_request):
mock_request.return_value.status_code = 200
url = "valid-url"
self.assertEquals(Fanfic.check_if_online(url), True)
@mock.patch('requests.head')
def test_check_if_online_false(self, mock_request):
mock_request.return_value.status_code = 404
url = "url-not-valid"
self.assertEquals(Fanfic.check_if_online(url), False)
''' Get title and author
CAREFUL: The requests are real, we need to know if the page has changed
something so we update our code '''
def test_get_title_and_author_ficwad(self):
url = "https://ficwad.com/story/204190"
title, author = Fanfic.get_title_and_author(url)
self.assertEquals(title, "My Dirty Little Secret")
self.assertEquals(author, "BleedingValentine")
def test_get_title_and_author_avengersfanfiction(self):
url = "http://www.avengersfanfiction.com/Story/86623/The-silvers-tears"
title, author = Fanfic.get_title_and_author(url)
self.assertEquals(title, "The silver's tears")
self.assertEquals(author, "Lokinada")
def test_get_title_and_author_archiveofourown(self):
url = "https://archiveofourown.org/works/8109805"
title, author = Fanfic.get_title_and_author(url)
self.assertEquals(title, "WIP Amnesty: Stranger Things Have Happened")
self.assertEquals(author, "aimmyarrowshigh")
|