This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_helpers.py
150 lines (118 loc) · 3.79 KB
/
test_helpers.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import json
import tweepy
import requests
def mocked_get_tweepy_api():
class API:
"""
Class mocking a tweepy API
"""
def me(self):
"""
get the current user's screen name
:return: str
"""
return 'test'
def send_direct_message(self):
"""
send a direct message
"""
def update_status(self, status, lat=None, long=None):
"""
Update status
:return: Status
"""
class Status:
"""
Class mocking a tweepy Status
"""
def __init__(self, text):
"""
:param text: str
"""
self.text = text
if status == 'error':
raise tweepy.TweepError('error on purpose')
else:
return Status(status)
def user_timeline(self, screen_name, include_rts, count):
"""
Get a user's timeline
:return: list
"""
class Box:
def __init__(self):
self.coordinates = [[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]]]
class Place:
def __init__(self, place_name):
self.full_name = place_name
self.bounding_box = Box()
class Tweet:
def __init__(self):
if screen_name == 'nocoords':
self.coordinates = None
self.place = Place('cool place')
elif screen_name == 'coordsnoplace':
self.coordinates = {
'coordinates': [1.5, 2.5]
}
self.place = None
else:
self.coordinates = {
'coordinates': [1, 2]
}
self.place = Place('test')
if screen_name == 'error':
raise tweepy.TweepError('uh oh')
elif screen_name != 'no tweets':
return [Tweet()]
else:
return []
return API()
def mocked_requests_get(*args, **kwargs):
"""
Mocked requests.get
:return: MockResponse
"""
class MockResponse:
"""
Class mocking the response of calling request.get in the python-forecastio library
"""
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
self.headers = None
def raise_for_status(self):
"""
This method is used to check for errors, but none will (should) exist in a mocked response
"""
pass
def json(self):
"""
:return: dict
"""
return self.json_data
with open(args[0], 'r', encoding='utf-8') as file_stream:
return MockResponse(json.load(file_stream), 200)
def mocked_forecastio_load_forecast(*args, **kwargs):
class Response:
def __init__(self, status_code):
self.status_code = status_code
class Forecast:
"""
Class mocking a Forecast
"""
def __init__(self):
self.response = Response(200)
self.json = {
'flags': {
'units': 'us'
}
}
return Forecast()
def mocked_forecastio_load_forecast_error(*args, **kwargs):
raise requests.exceptions.HTTPError
def mocked_tweepy_o_auth_handler(key, secret):
class Auth:
def set_access_token(self, token, secret):
pass
return Auth()