11import time
22from unittest import mock
33
4- from django . test import TestCase
4+ import pytest
55
66from planet .models import Feed , FeedItem
77from planet .management .commands .update_planet import Command
@@ -14,6 +14,7 @@ class Result(dict):
1414 def get (self , value ):
1515 return getattr (self , value )
1616
17+
1718class Entry (dict ):
1819 title = 'title'
1920 description = 'lorem ipsum'
@@ -26,64 +27,88 @@ def get(self, value):
2627 return getattr (self , value )
2728
2829
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 ()
3051
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 )
3553
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 :
3958 parse .return_value = {}
40- self . command .parse_feed (self . feed )
59+ command .parse_feed (feed )
4160 assert FeedItem .objects .count () == 0
4261
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 :
4565 parse .return_value = {'status' : 304 }
46- self . command .parse_feed (self . feed )
66+ command .parse_feed (feed )
4767 assert FeedItem .objects .count () == 0
4868
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 :
5172 parse .return_value = {'status' : 201 }
52- self . command .parse_feed (self . feed )
73+ command .parse_feed (feed )
5374 assert FeedItem .objects .count () == 0
5475
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 :
5779 parse .return_value = Result ()
58- self . command .parse_feed (self . feed )
80+ command .parse_feed (feed )
5981 assert FeedItem .objects .count () == 0
6082
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 :
6386 value = Result ()
6487 entry = Entry ()
6588 entry .published_parsed = None
6689 value .entries = [entry ]
6790 parse .return_value = value
68- self . command .parse_feed (self . feed )
91+ command .parse_feed (feed )
6992 assert FeedItem .objects .count () == 0
7093
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 :
7397 value = Result ()
7498 value .entries = [Entry ()]
7599 parse .return_value = value
76- self . command .parse_feed (self . feed )
100+ command .parse_feed (feed )
77101 assert FeedItem .objects .count () == 1
78102
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 :
81106 value = Result ()
82107 entry = Entry ()
83108 entry .published_parsed = None
84109 entry .updated_parsed = time .localtime (time .time ())
85110
86111 value .entries = [entry ]
87112 parse .return_value = value
88- self . command .parse_feed (self . feed )
113+ command .parse_feed (feed )
89114 assert FeedItem .objects .count () == 1
0 commit comments