1
1
import time
2
2
from unittest import mock
3
3
4
- from django . test import TestCase
4
+ import pytest
5
5
6
6
from planet .models import Feed , FeedItem
7
7
from planet .management .commands .update_planet import Command
@@ -14,6 +14,7 @@ class Result(dict):
14
14
def get (self , value ):
15
15
return getattr (self , value )
16
16
17
+
17
18
class Entry (dict ):
18
19
title = 'title'
19
20
description = 'lorem ipsum'
@@ -26,64 +27,88 @@ def get(self, value):
26
27
return getattr (self , value )
27
28
28
29
29
- class UpdatePlanetTest (TestCase ):
30
+ @pytest .fixture
31
+ def command ():
32
+ yield Command ()
33
+
34
+
35
+ @pytest .fixture
36
+ def feed (db ):
37
+ return Feed (title = 'test' , website = 'http://archlinux.org' ,
38
+ website_rss = 'http://archlinux.org/feed.rss' )
39
+
40
+
41
+ class MockParse :
42
+ @staticmethod
43
+ def parse ():
44
+ return {}
45
+
46
+
47
+ @pytest .fixture
48
+ def mock_parse (monkeypatch ):
49
+ def mock_get (* args , ** kwargs ):
50
+ return MockParse ()
30
51
31
- def setUp (self ):
32
- self .command = Command ()
33
- self .feed = Feed (title = 'test' , website = 'http://archlinux.org' ,
34
- website_rss = 'http://archlinux.org/feed.rss' )
52
+ monkeypatch .setattr (feedparser , "parse" , mock_get )
35
53
36
- # Test when feedparser receives an exception and returns no status
37
- @mock .patch ('feedparser.parse' )
38
- def test_parse_feed_wrong (self , parse ):
54
+
55
+ # Test when feedparser receives an exception and returns no status
56
+ def test_parse_feed_wrong (feed , command ):
57
+ with mock .patch ('feedparser.parse' ) as parse :
39
58
parse .return_value = {}
40
- self . command .parse_feed (self . feed )
59
+ command .parse_feed (feed )
41
60
assert FeedItem .objects .count () == 0
42
61
43
- @mock .patch ('feedparser.parse' )
44
- def test_parse_feed_304 (self , parse ):
62
+
63
+ def test_parse_feed_304 (feed , command ):
64
+ with mock .patch ('feedparser.parse' ) as parse :
45
65
parse .return_value = {'status' : 304 }
46
- self . command .parse_feed (self . feed )
66
+ command .parse_feed (feed )
47
67
assert FeedItem .objects .count () == 0
48
68
49
- @mock .patch ('feedparser.parse' )
50
- def test_parse_feed_unknown (self , parse ):
69
+
70
+ def test_parse_feed_unknown (feed , command ):
71
+ with mock .patch ('feedparser.parse' ) as parse :
51
72
parse .return_value = {'status' : 201 }
52
- self . command .parse_feed (self . feed )
73
+ command .parse_feed (feed )
53
74
assert FeedItem .objects .count () == 0
54
75
55
- @mock .patch ('feedparser.parse' )
56
- def test_parse_entries_empty (self , parse ):
76
+
77
+ def test_parse_entries_empty (feed , command ):
78
+ with mock .patch ('feedparser.parse' ) as parse :
57
79
parse .return_value = Result ()
58
- self . command .parse_feed (self . feed )
80
+ command .parse_feed (feed )
59
81
assert FeedItem .objects .count () == 0
60
82
61
- @mock .patch ('feedparser.parse' )
62
- def test_parse_entries_not_published (self , parse ):
83
+
84
+ def test_parse_entries_not_published (feed , command ):
85
+ with mock .patch ('feedparser.parse' ) as parse :
63
86
value = Result ()
64
87
entry = Entry ()
65
88
entry .published_parsed = None
66
89
value .entries = [entry ]
67
90
parse .return_value = value
68
- self . command .parse_feed (self . feed )
91
+ command .parse_feed (feed )
69
92
assert FeedItem .objects .count () == 0
70
93
71
- @mock .patch ('feedparser.parse' )
72
- def test_parse_entries (self , parse ):
94
+
95
+ def test_parse_entries (feed , command ):
96
+ with mock .patch ('feedparser.parse' ) as parse :
73
97
value = Result ()
74
98
value .entries = [Entry ()]
75
99
parse .return_value = value
76
- self . command .parse_feed (self . feed )
100
+ command .parse_feed (feed )
77
101
assert FeedItem .objects .count () == 1
78
102
79
- @mock .patch ('feedparser.parse' )
80
- def test_parse_entries_atom (self , parse ):
103
+
104
+ def test_parse_entries_atom (feed , command ):
105
+ with mock .patch ('feedparser.parse' ) as parse :
81
106
value = Result ()
82
107
entry = Entry ()
83
108
entry .published_parsed = None
84
109
entry .updated_parsed = time .localtime (time .time ())
85
110
86
111
value .entries = [entry ]
87
112
parse .return_value = value
88
- self . command .parse_feed (self . feed )
113
+ command .parse_feed (feed )
89
114
assert FeedItem .objects .count () == 1
0 commit comments