Skip to content

Commit 225bc7b

Browse files
committed
Do not include TZ information in queries
Same as twingly/twingly-search-api-ruby#69. Re-record fixtures to get fixtures without published time bugs.
1 parent 942ac14 commit 225bc7b

File tree

10 files changed

+27
-199
lines changed

10 files changed

+27
-199
lines changed

tests/fixtures/vcr_cassettes/search_for_spotify_on_sv_blogs.json

Lines changed: 1 addition & 45 deletions
Large diffs are not rendered by default.

tests/fixtures/vcr_cassettes/search_for_spotify_on_sv_blogs_from_query.json

Lines changed: 1 addition & 45 deletions
Large diffs are not rendered by default.

tests/fixtures/vcr_cassettes/search_for_spotify_on_sv_blogs_using_query.json

Lines changed: 1 addition & 45 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1 @@
1-
{
2-
"http_interactions": [
3-
{
4-
"request": {
5-
"body": {
6-
"string": "",
7-
"encoding": "utf-8"
8-
},
9-
"headers": {
10-
"Connection": "keep-alive",
11-
"Accept-Encoding": "gzip, deflate",
12-
"Accept": "*/*",
13-
"User-Agent": "Twingly Search Python Client/1.2.1"
14-
},
15-
"method": "GET",
16-
"uri": "https://api.twingly.com/blog/search/api/v3/search?q=something&apiKey=wrong"
17-
},
18-
"response": {
19-
"body": {
20-
"string": "<?xml version=\"1.0\" encoding=\"utf-8\"?><error code=\"40101\"><message>Unauthorized</message></error>",
21-
"encoding": "utf-8"
22-
},
23-
"headers": {
24-
"Content-Length": "97",
25-
"Expires": "-1",
26-
"Server": "nginx",
27-
"Connection": "keep-alive",
28-
"Pragma": "no-cache",
29-
"Cache-Control": "no-cache",
30-
"Date": "Sat, 13 May 2017 20:29:15 GMT",
31-
"Content-Type": "application/xml; charset=utf-8"
32-
},
33-
"status": {
34-
"message": "Unauthorized",
35-
"code": 401
36-
},
37-
"url": "https://api.twingly.com/blog/search/api/v3/search?q=something&apiKey=wrong"
38-
},
39-
"recorded_at": "2017-05-13T20:29:14"
40-
}
41-
],
42-
"recorded_with": "betamax/0.8.0"
43-
}
1+
{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Connection": "keep-alive", "Accept-Encoding": "gzip, deflate", "Accept": "*/*", "User-Agent": "Twingly Search Python Client/2.1.0"}, "method": "GET", "uri": "https://api.twingly.com/blog/search/api/v3/search?q=something&apiKey=wrong"}, "response": {"body": {"string": "<?xml version=\"1.0\" encoding=\"utf-8\"?><error code=\"40101\"><message>Unauthorized</message></error>", "encoding": "utf-8"}, "headers": {"Content-Length": "97", "Expires": "-1", "Server": "nginx", "Connection": "keep-alive", "Pragma": "no-cache", "Cache-Control": "no-cache", "Date": "Mon, 16 Oct 2017 08:54:09 GMT", "Content-Type": "application/xml; charset=utf-8"}, "status": {"message": "Unauthorized", "code": 401}, "url": "https://api.twingly.com/blog/search/api/v3/search?q=something&apiKey=wrong"}, "recorded_at": "2017-10-16T08:54:09"}], "recorded_with": "betamax/0.5.1"}

tests/test_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,5 @@ def test_search_for_spotify_on_sv_blogs_using_query_object(self):
6868
result = c.execute_query(q)
6969
self.assertIsNotNone(result)
7070
self.assertEqual(result.incomplete_result, False)
71-
self.assertEqual(result.number_of_matches_returned, 1)
72-
self.assertEqual(len(result.posts), 1)
71+
self.assertEqual(result.number_of_matches_returned, 20)
72+
self.assertEqual(len(result.posts), 20)

tests/test_query.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ def test_query_should_add_start_date(self):
5858
q.search_query = "spotify"
5959
q.start_time = self.datetime_with_timezone(datetime(2012, 12, 28, 9, 1, 22), "UTC")
6060
query_string = q.build_query_string()
61-
self.assertEqual(query_string, "spotify start-date:2012-12-28T09:01:22Z")
61+
self.assertEqual(query_string, "spotify start-date:2012-12-28 09:01:22")
6262

6363
def test_query_should_add_end_date(self):
6464
q = self._client.query()
6565
q.search_query = "spotify"
6666
q.end_time = self.datetime_with_timezone(datetime(2012, 12, 28, 9, 1, 22), "UTC")
6767
query_string = q.build_query_string()
68-
self.assertEqual(query_string, "spotify end-date:2012-12-28T09:01:22Z")
68+
self.assertEqual(query_string, "spotify end-date:2012-12-28 09:01:22")
6969

7070
def test_query_should_add_lang(self):
7171
q = self._client.query()
@@ -82,7 +82,7 @@ def test_query_should_build_query_string_with_deprecated_params(self):
8282
q.end_time = self.datetime_with_timezone(datetime(2013, 12, 28, 9, 1, 22), "UTC")
8383
query_string = q.build_query_string()
8484
self.assertEqual(query_string,
85-
"spotify lang:en start-date:2012-12-28T09:01:22Z end-date:2013-12-28T09:01:22Z")
85+
"spotify lang:en start-date:2012-12-28 09:01:22 end-date:2013-12-28 09:01:22")
8686

8787
def test_query_should_build_query_string(self):
8888
q = self._client.query()
@@ -92,31 +92,31 @@ def test_query_should_build_query_string(self):
9292
q.end_time = self.datetime_with_timezone(datetime(2013, 12, 28, 9, 1, 22), "UTC")
9393
query_string = q.build_query_string()
9494
self.assertEqual(query_string,
95-
"spotify lang:en start-date:2012-12-28T09:01:22Z end-date:2013-12-28T09:01:22Z")
95+
"spotify lang:en start-date:2012-12-28 09:01:22 end-date:2013-12-28 09:01:22")
9696

9797
def test_query_should_add_start_time(self):
9898
q = self._client.query()
9999
q.pattern = "spotify"
100100
q.start_time = self.datetime_with_timezone(datetime(2012, 12, 28, 9, 1, 22), "UTC")
101-
self.assertIn("start-date:2012-12-28T09:01:22Z", q.build_query_string())
101+
self.assertIn("start-date:2012-12-28 09:01:22", q.build_query_string())
102102

103103
def test_query_using_start_time_without_timezone(self):
104104
q = self._client.query()
105105
q.pattern = "spotify"
106106
q.start_time = datetime(2012, 12, 28, 9, 1, 22)
107-
self.assertIn("start-date:2012-12-28T09:01:22Z", q.build_query_string())
107+
self.assertIn("start-date:2012-12-28 09:01:22", q.build_query_string())
108108

109109
def test_query_using_start_time_with_timezone_other_than_utc(self):
110110
q = self._client.query()
111111
q.pattern = "spotify"
112112
q.start_time = self.datetime_with_timezone(datetime(2012, 12, 28, 9, 1, 22), "Europe/Stockholm")
113-
self.assertIn("start-date:2012-12-28T08:01:22Z", q.build_query_string())
113+
self.assertIn("start-date:2012-12-28 08:01:22", q.build_query_string())
114114

115115
def test_query_using_start_time_parsed_by_dateutil(self):
116116
q = self._client.query()
117117
q.pattern = "spotify"
118118
q.start_time = dateutil.parser.parse("2012-12-28 09:01:22 -0800")
119-
self.assertIn("start-date:2012-12-28T17:01:22Z", q.build_query_string())
119+
self.assertIn("start-date:2012-12-28 17:01:22", q.build_query_string())
120120

121121
def test_query_when_start_time_is_not_a_datetime(self):
122122
q = self._client.query()
@@ -127,25 +127,25 @@ def test_query_should_add_end_time(self):
127127
q = self._client.query()
128128
q.pattern = "spotify"
129129
q.end_time = self.datetime_with_timezone(datetime(2012, 12, 28, 9, 1, 22), "UTC")
130-
self.assertIn("end-date:2012-12-28T09:01:22Z", q.build_query_string())
130+
self.assertIn("end-date:2012-12-28 09:01:22", q.build_query_string())
131131

132132
def test_query_using_end_time_without_timezone(self):
133133
q = self._client.query()
134134
q.pattern = "spotify"
135135
q.end_time = datetime(2012, 12, 28, 9, 1, 22)
136-
self.assertIn("end-date:2012-12-28T09:01:22Z", q.build_query_string())
136+
self.assertIn("end-date:2012-12-28 09:01:22", q.build_query_string())
137137

138138
def test_query_using_end_time_with_timezone_other_than_utc(self):
139139
q = self._client.query()
140140
q.pattern = "spotify"
141141
q.end_time = self.datetime_with_timezone(datetime(2012, 12, 28, 9, 1, 22), "Europe/Stockholm")
142-
self.assertIn("end-date:2012-12-28T08:01:22Z", q.build_query_string())
142+
self.assertIn("end-date:2012-12-28 08:01:22", q.build_query_string())
143143

144144
def test_query_using_end_time_parsed_by_dateutil(self):
145145
q = self._client.query()
146146
q.pattern = "spotify"
147147
q.end_time = dateutil.parser.parse("2012-12-28 09:01:22 +0800")
148-
self.assertIn("2012-12-28T01:01:22Z", q.build_query_string())
148+
self.assertIn("2012-12-28 01:01:22", q.build_query_string())
149149

150150
def test_query_when_end_time_is_not_a_datetime(self):
151151
q = self._client.query()
@@ -156,7 +156,7 @@ def test_query_should_encode_url_parameters(self):
156156
q = self._client.query()
157157
q.pattern = "spotify"
158158
q.end_time = self.datetime_with_timezone(datetime(2012, 12, 28, 9, 1, 22), "UTC")
159-
self.assertIn("end-date%3A2012-12-28T09%3A01%3A22Z", q.url_parameters())
159+
self.assertIn("end-date%3A2012-12-28+09%3A01%3A22", q.url_parameters())
160160

161161
def test_query_pattern(self):
162162
q = self._client.query()

twingly_search/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
from .post import Post
2020
from .result import Result
2121
from .constants import TWINGLY_SEARCH_KEY
22-
from .constants import DATE_TIME_FORMAT
22+
from .constants import POST_DATE_TIME_FORMAT
23+
from .constants import QUERY_DATE_TIME_FORMAT

twingly_search/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
from __future__ import unicode_literals
33

44
TWINGLY_SEARCH_KEY = 'TWINGLY_SEARCH_KEY'
5-
DATE_TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
5+
POST_DATE_TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
6+
QUERY_DATE_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'

twingly_search/post.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pytz import utc
99

1010
import twingly_search
11-
from twingly_search.constants import DATE_TIME_FORMAT
11+
from twingly_search.constants import POST_DATE_TIME_FORMAT
1212

1313

1414
class Post(object):
@@ -118,7 +118,7 @@ def set_values(self, params):
118118
self.longitude = self.coordinates.get("longitude", None)
119119

120120
def _parse_time(self, time):
121-
parsed_time = datetime.datetime.strptime(time, DATE_TIME_FORMAT)
121+
parsed_time = datetime.datetime.strptime(time, POST_DATE_TIME_FORMAT)
122122
return utc.localize(parsed_time)
123123

124124
def __unicode__(self):

twingly_search/query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pytz import utc
55

66
import twingly_search
7-
from twingly_search.constants import DATE_TIME_FORMAT
7+
from twingly_search.constants import QUERY_DATE_TIME_FORMAT
88
from twingly_search.errors import TwinglySearchQueryException
99

1010
try:
@@ -149,7 +149,7 @@ def _time_to_utc_string(self, time):
149149
return ''
150150

151151
time_in_utc = self._time_to_utc(time)
152-
result = time_in_utc.strftime(DATE_TIME_FORMAT)
152+
result = time_in_utc.strftime(QUERY_DATE_TIME_FORMAT)
153153
return result
154154

155155
def _time_to_utc(self, time):

0 commit comments

Comments
 (0)