Skip to content

Commit 0f01d38

Browse files
committed
Remove spurious assertions about Content-Length header value.
They are 'magic numbers' which break when code-not-under-test changes.
1 parent 8b97707 commit 0f01d38

File tree

1 file changed

+55
-101
lines changed

1 file changed

+55
-101
lines changed

gcloud/datastore/test_connection.py

Lines changed: 55 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,13 @@ def test__request_w_200(self):
6161
])
6262
http = conn._http = Http({'status': '200'}, 'CONTENT')
6363
self.assertEqual(conn._request(DATASET_ID, METHOD, DATA), 'CONTENT')
64-
expected_called_with = {
65-
'uri': URI,
66-
'method': 'POST',
67-
'headers': {
68-
'Content-Type': 'application/x-protobuf',
69-
'Content-Length': '4',
70-
'User-Agent': conn.USER_AGENT,
71-
},
72-
'body': DATA,
73-
}
74-
self.assertEqual(http._called_with, expected_called_with)
64+
self.assertEqual(http._called_with['uri'], URI)
65+
self.assertEqual(http._called_with['method'], 'POST')
66+
self.assertEqual(http._called_with['headers']['Content-Type'],
67+
'application/x-protobuf')
68+
self.assertEqual(http._called_with['headers']['User-Agent'],
69+
conn.USER_AGENT)
70+
self.assertEqual(http._called_with['body'], DATA)
7571

7672
def test__request_not_200(self):
7773
DATASET_ID = 'DATASET'
@@ -89,7 +85,7 @@ def test__rpc(self):
8985
class ReqPB(object):
9086

9187
def SerializeToString(self):
92-
return b'REQPB'
88+
return REQPB
9389

9490
class RspPB(object):
9591

@@ -100,6 +96,7 @@ def __init__(self, pb):
10096
def FromString(cls, pb):
10197
return cls(pb)
10298

99+
REQPB = b'REQPB'
103100
DATASET_ID = 'DATASET'
104101
METHOD = 'METHOD'
105102
conn = self._makeOne()
@@ -115,17 +112,13 @@ def FromString(cls, pb):
115112
response = conn._rpc(DATASET_ID, METHOD, ReqPB(), RspPB)
116113
self.assertTrue(isinstance(response, RspPB))
117114
self.assertEqual(response._pb, 'CONTENT')
118-
expected_called_with = {
119-
'uri': URI,
120-
'method': 'POST',
121-
'headers': {
122-
'Content-Type': 'application/x-protobuf',
123-
'Content-Length': '5',
124-
'User-Agent': conn.USER_AGENT,
125-
},
126-
'body': b'REQPB',
127-
}
128-
self.assertEqual(http._called_with, expected_called_with)
115+
self.assertEqual(http._called_with['uri'], URI)
116+
self.assertEqual(http._called_with['method'], 'POST')
117+
self.assertEqual(http._called_with['headers']['Content-Type'],
118+
'application/x-protobuf')
119+
self.assertEqual(http._called_with['headers']['User-Agent'],
120+
conn.USER_AGENT)
121+
self.assertEqual(http._called_with['body'], REQPB)
129122

130123
def test_build_api_url_w_default_base_version(self):
131124
DATASET_ID = 'DATASET'
@@ -226,12 +219,9 @@ def test_begin_transaction_default_serialize(self):
226219
cw = http._called_with
227220
self.assertEqual(cw['uri'], URI)
228221
self.assertEqual(cw['method'], 'POST')
229-
expected_headers = {
230-
'Content-Type': 'application/x-protobuf',
231-
'Content-Length': '2',
232-
'User-Agent': conn.USER_AGENT,
233-
}
234-
self.assertEqual(cw['headers'], expected_headers)
222+
self.assertEqual(cw['headers']['Content-Type'],
223+
'application/x-protobuf')
224+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
235225
rq_class = datastore_pb.BeginTransactionRequest
236226
request = rq_class()
237227
request.ParseFromString(cw['body'])
@@ -258,12 +248,9 @@ def test_begin_transaction_explicit_serialize(self):
258248
cw = http._called_with
259249
self.assertEqual(cw['uri'], URI)
260250
self.assertEqual(cw['method'], 'POST')
261-
expected_headers = {
262-
'Content-Type': 'application/x-protobuf',
263-
'Content-Length': '2',
264-
'User-Agent': conn.USER_AGENT,
265-
}
266-
self.assertEqual(cw['headers'], expected_headers)
251+
self.assertEqual(cw['headers']['Content-Type'],
252+
'application/x-protobuf')
253+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
267254
rq_class = datastore_pb.BeginTransactionRequest
268255
request = rq_class()
269256
request.ParseFromString(cw['body'])
@@ -312,12 +299,9 @@ def id(self):
312299
cw = http._called_with
313300
self.assertEqual(cw['uri'], URI)
314301
self.assertEqual(cw['method'], 'POST')
315-
expected_headers = {
316-
'Content-Type': 'application/x-protobuf',
317-
'Content-Length': '6',
318-
'User-Agent': conn.USER_AGENT,
319-
}
320-
self.assertEqual(cw['headers'], expected_headers)
302+
self.assertEqual(cw['headers']['Content-Type'],
303+
'application/x-protobuf')
304+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
321305
rq_class = datastore_pb.RollbackRequest
322306
request = rq_class()
323307
request.ParseFromString(cw['body'])
@@ -349,12 +333,9 @@ def test_run_query_wo_namespace_empty_result(self):
349333
cw = http._called_with
350334
self.assertEqual(cw['uri'], URI)
351335
self.assertEqual(cw['method'], 'POST')
352-
expected_headers = {
353-
'Content-Type': 'application/x-protobuf',
354-
'Content-Length': '14',
355-
'User-Agent': conn.USER_AGENT,
356-
}
357-
self.assertEqual(cw['headers'], expected_headers)
336+
self.assertEqual(cw['headers']['Content-Type'],
337+
'application/x-protobuf')
338+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
358339
rq_class = datastore_pb.RunQueryRequest
359340
request = rq_class()
360341
request.ParseFromString(cw['body'])
@@ -388,12 +369,9 @@ def test_run_query_w_namespace_nonempty_result(self):
388369
cw = http._called_with
389370
self.assertEqual(cw['uri'], URI)
390371
self.assertEqual(cw['method'], 'POST')
391-
expected_headers = {
392-
'Content-Type': 'application/x-protobuf',
393-
'Content-Length': '16',
394-
'User-Agent': conn.USER_AGENT,
395-
}
396-
self.assertEqual(cw['headers'], expected_headers)
372+
self.assertEqual(cw['headers']['Content-Type'],
373+
'application/x-protobuf')
374+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
397375
rq_class = datastore_pb.RunQueryRequest
398376
request = rq_class()
399377
request.ParseFromString(cw['body'])
@@ -423,12 +401,9 @@ def test_lookup_single_key_empty_response(self):
423401
cw = http._called_with
424402
self.assertEqual(cw['uri'], URI)
425403
self.assertEqual(cw['method'], 'POST')
426-
expected_headers = {
427-
'Content-Type': 'application/x-protobuf',
428-
'Content-Length': '26',
429-
'User-Agent': conn.USER_AGENT,
430-
}
431-
self.assertEqual(cw['headers'], expected_headers)
404+
self.assertEqual(cw['headers']['Content-Type'],
405+
'application/x-protobuf')
406+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
432407
rq_class = datastore_pb.LookupRequest
433408
request = rq_class()
434409
request.ParseFromString(cw['body'])
@@ -464,12 +439,9 @@ def test_lookup_single_key_nonempty_response(self):
464439
cw = http._called_with
465440
self.assertEqual(cw['uri'], URI)
466441
self.assertEqual(cw['method'], 'POST')
467-
expected_headers = {
468-
'Content-Type': 'application/x-protobuf',
469-
'Content-Length': '26',
470-
'User-Agent': conn.USER_AGENT,
471-
}
472-
self.assertEqual(cw['headers'], expected_headers)
442+
self.assertEqual(cw['headers']['Content-Type'],
443+
'application/x-protobuf')
444+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
473445
rq_class = datastore_pb.LookupRequest
474446
request = rq_class()
475447
request.ParseFromString(cw['body'])
@@ -502,12 +474,9 @@ def test_lookup_multiple_keys_empty_response(self):
502474
cw = http._called_with
503475
self.assertEqual(cw['uri'], URI)
504476
self.assertEqual(cw['method'], 'POST')
505-
expected_headers = {
506-
'Content-Type': 'application/x-protobuf',
507-
'Content-Length': '52',
508-
'User-Agent': conn.USER_AGENT,
509-
}
510-
self.assertEqual(cw['headers'], expected_headers)
477+
self.assertEqual(cw['headers']['Content-Type'],
478+
'application/x-protobuf')
479+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
511480
rq_class = datastore_pb.LookupRequest
512481
request = rq_class()
513482
request.ParseFromString(cw['body'])
@@ -547,12 +516,9 @@ def test_commit_wo_transaction(self):
547516
cw = http._called_with
548517
self.assertEqual(cw['uri'], URI)
549518
self.assertEqual(cw['method'], 'POST')
550-
expected_headers = {
551-
'Content-Type': 'application/x-protobuf',
552-
'Content-Length': '47',
553-
'User-Agent': conn.USER_AGENT,
554-
}
555-
self.assertEqual(cw['headers'], expected_headers)
519+
self.assertEqual(cw['headers']['Content-Type'],
520+
'application/x-protobuf')
521+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
556522
rq_class = datastore_pb.CommitRequest
557523
request = rq_class()
558524
request.ParseFromString(cw['body'])
@@ -595,12 +561,9 @@ def id(self):
595561
cw = http._called_with
596562
self.assertEqual(cw['uri'], URI)
597563
self.assertEqual(cw['method'], 'POST')
598-
expected_headers = {
599-
'Content-Type': 'application/x-protobuf',
600-
'Content-Length': '53',
601-
'User-Agent': conn.USER_AGENT,
602-
}
603-
self.assertEqual(cw['headers'], expected_headers)
564+
self.assertEqual(cw['headers']['Content-Type'],
565+
'application/x-protobuf')
566+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
604567
rq_class = datastore_pb.CommitRequest
605568
request = rq_class()
606569
request.ParseFromString(cw['body'])
@@ -632,12 +595,9 @@ def test_save_entity_wo_transaction_w_upsert(self):
632595
cw = http._called_with
633596
self.assertEqual(cw['uri'], URI)
634597
self.assertEqual(cw['method'], 'POST')
635-
expected_headers = {
636-
'Content-Type': 'application/x-protobuf',
637-
'Content-Length': '47',
638-
'User-Agent': conn.USER_AGENT,
639-
}
640-
self.assertEqual(cw['headers'], expected_headers)
598+
self.assertEqual(cw['headers']['Content-Type'],
599+
'application/x-protobuf')
600+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
641601
rq_class = datastore_pb.CommitRequest
642602
request = rq_class()
643603
request.ParseFromString(cw['body'])
@@ -685,12 +645,9 @@ def test_save_entity_wo_transaction_w_auto_id(self):
685645
cw = http._called_with
686646
self.assertEqual(cw['uri'], URI)
687647
self.assertEqual(cw['method'], 'POST')
688-
expected_headers = {
689-
'Content-Type': 'application/x-protobuf',
690-
'Content-Length': '44',
691-
'User-Agent': conn.USER_AGENT,
692-
}
693-
self.assertEqual(cw['headers'], expected_headers)
648+
self.assertEqual(cw['headers']['Content-Type'],
649+
'application/x-protobuf')
650+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
694651
rq_class = datastore_pb.CommitRequest
695652
request = rq_class()
696653
request.ParseFromString(cw['body'])
@@ -782,12 +739,9 @@ def test_delete_entities_wo_transaction(self):
782739
cw = http._called_with
783740
self.assertEqual(cw['uri'], URI)
784741
self.assertEqual(cw['method'], 'POST')
785-
expected_headers = {
786-
'Content-Type': 'application/x-protobuf',
787-
'Content-Length': '30',
788-
'User-Agent': conn.USER_AGENT,
789-
}
790-
self.assertEqual(cw['headers'], expected_headers)
742+
self.assertEqual(cw['headers']['Content-Type'],
743+
'application/x-protobuf')
744+
self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT)
791745
rq_class = datastore_pb.CommitRequest
792746
request = rq_class()
793747
request.ParseFromString(cw['body'])

0 commit comments

Comments
 (0)