3
3
import pytest
4
4
import requests
5
5
6
+ from plugins import horoscope
7
+
6
8
URL = (
7
9
"http://www.horoscope.com/us/horoscopes/general/"
8
10
"horoscope-general-daily-today.aspx?sign={sign}"
9
11
)
10
12
11
13
12
14
def setup_db (mock_db ):
13
- from plugins import horoscope
14
-
15
15
horoscope .table .create (mock_db .engine , checkfirst = True )
16
16
sess = mock_db .session ()
17
17
sess .execute (horoscope .table .delete ())
@@ -20,13 +20,11 @@ def setup_db(mock_db):
20
20
21
21
22
22
def test_horoscope (mock_requests , mock_db ):
23
- from plugins import horoscope
24
-
25
23
sess = setup_db (mock_db )
26
24
mock_requests .add (
27
- ' GET' ,
25
+ " GET" ,
28
26
URL .format (sign = 1 ),
29
- headers = {' User-Agent' : "Some user agent" },
27
+ headers = {" User-Agent" : "Some user agent" },
30
28
body = """
31
29
<div class="main-horoscope">
32
30
<p>Some horoscope text</p>
@@ -39,40 +37,36 @@ def test_horoscope(mock_requests, mock_db):
39
37
bot = MagicMock ()
40
38
bot .user_agent = "Some user agent"
41
39
42
- response = horoscope .horoscope ("aries" , sess , bot , ' some_user' , event )
40
+ response = horoscope .horoscope ("aries" , sess , bot , " some_user" , event )
43
41
44
42
assert response is None
45
43
46
44
event .message .assert_called_once_with ("\x02 aries\x02 Some horoscope text" )
47
45
48
- assert mock_db .get_data (horoscope .table ) == [(' some_user' , ' aries' )]
46
+ assert mock_db .get_data (horoscope .table ) == [(" some_user" , " aries" )]
49
47
50
48
51
49
def test_invalid_syntax (mock_requests , mock_db ):
52
- from plugins import horoscope
53
-
54
50
sess = setup_db (mock_db )
55
51
56
52
event = MagicMock ()
57
53
bot = MagicMock ()
58
54
bot .user_agent = "Some user agent"
59
55
60
- response = horoscope .horoscope ('' , sess , bot , ' some_user' , event )
56
+ response = horoscope .horoscope ("" , sess , bot , " some_user" , event )
61
57
62
58
assert response is None
63
59
64
60
assert event .notice_doc .call_count == 1
65
61
66
62
67
63
def test_database_read (mock_requests , mock_db ):
68
- from plugins import horoscope
69
-
70
64
sess = setup_db (mock_db )
71
65
72
66
mock_requests .add (
73
- ' GET' ,
67
+ " GET" ,
74
68
URL .format (sign = 4 ),
75
- headers = {' User-Agent' : "Some user agent" },
69
+ headers = {" User-Agent" : "Some user agent" },
76
70
body = """
77
71
<div class="main-horoscope">
78
72
<p>Some horoscope text</p>
@@ -81,28 +75,26 @@ def test_database_read(mock_requests, mock_db):
81
75
match_querystring = True ,
82
76
)
83
77
84
- mock_db .add_row (horoscope .table , nick = ' some_user' , sign = ' cancer' )
78
+ mock_db .add_row (horoscope .table , nick = " some_user" , sign = " cancer" )
85
79
86
80
event = MagicMock ()
87
81
bot = MagicMock ()
88
82
bot .user_agent = "Some user agent"
89
83
90
- response = horoscope .horoscope ('' , sess , bot , ' some_user' , event )
84
+ response = horoscope .horoscope ("" , sess , bot , " some_user" , event )
91
85
92
86
assert response is None
93
87
94
88
event .message .assert_called_once_with ("\x02 cancer\x02 Some horoscope text" )
95
89
96
90
97
91
def test_parse_fail (mock_requests , mock_db ):
98
- from plugins import horoscope
99
-
100
92
sess = setup_db (mock_db )
101
93
102
94
mock_requests .add (
103
- ' GET' ,
95
+ " GET" ,
104
96
URL .format (sign = 4 ),
105
- headers = {' User-Agent' : "Some user agent" },
97
+ headers = {" User-Agent" : "Some user agent" },
106
98
body = """
107
99
<div class="main-horoscope">
108
100
</div>
@@ -115,25 +107,35 @@ def test_parse_fail(mock_requests, mock_db):
115
107
bot .user_agent = "Some user agent"
116
108
117
109
with pytest .raises (horoscope .HoroscopeParseError ):
118
- horoscope .horoscope (' cancer' , sess , bot , ' some_user' , event )
110
+ horoscope .horoscope (" cancer" , sess , bot , " some_user" , event )
119
111
120
112
event .reply .assert_called_once_with ("Unable to parse horoscope posting" )
121
113
122
114
123
115
def test_page_error (mock_requests , mock_db ):
124
- from plugins import horoscope
125
-
126
116
sess = setup_db (mock_db )
127
117
128
118
event = MagicMock ()
129
119
bot = MagicMock ()
130
120
bot .user_agent = "Some user agent"
131
121
132
- mock_requests .add (' GET' , URL .format (sign = 1 ), status = 404 )
122
+ mock_requests .add (" GET" , URL .format (sign = 1 ), status = 404 )
133
123
134
124
with pytest .raises (requests .RequestException ):
135
- horoscope .horoscope (' aries' , sess , bot , ' some_user' , event )
125
+ horoscope .horoscope (" aries" , sess , bot , " some_user" , event )
136
126
137
127
event .reply .assert_called_once_with (
138
- "Could not get horoscope: 404 Client Error: Not Found for url: {}. URL Error" .format (URL .format (sign = 1 ))
128
+ "Could not get horoscope: 404 Client Error: Not Found for url: {}. URL Error" .format (
129
+ URL .format (sign = 1 )
130
+ )
139
131
)
132
+
133
+
134
+ def test_bad_sign (mock_requests ):
135
+ db = MagicMock ()
136
+ event = MagicMock ()
137
+ sign = "some_sign"
138
+ res , err = horoscope .parse_or_lookup (sign , db , "foobar" , event )
139
+ assert res is None
140
+ assert err is None
141
+ event .notice .assert_called_with ("Unknown sign: {}" .format (sign ))
0 commit comments