Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

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")