Merge lp:~jtv/maas/bug-1372735 into lp:~maas-committers/maas/trunk

Proposed by Jeroen T. Vermeulen
Status: Rejected
Rejected by: Jeroen T. Vermeulen
Proposed branch: lp:~jtv/maas/bug-1372735
Merge into: lp:~maas-committers/maas/trunk
Prerequisite: lp:~jtv/maas/bug-1372732
Diff against target: 567 lines (+88/-75)
15 files modified
src/maasserver/api/commissioning_scripts.py (+3/-3)
src/maasserver/api/tests/test_nodegroup.py (+3/-3)
src/maasserver/forms.py (+2/-2)
src/maasserver/models/filestorage.py (+3/-3)
src/maasserver/models/tests/test_node.py (+3/-3)
src/maasserver/testing/factory.py (+4/-4)
src/maasserver/tests/test_third_party_drivers.py (+2/-2)
src/metadataserver/api.py (+4/-3)
src/metadataserver/fields.py (+25/-16)
src/metadataserver/migrations/0012_commission_result_binary_data_recode.py (+2/-3)
src/metadataserver/models/commissioningscript.py (+3/-3)
src/metadataserver/models/nodeuserdata.py (+4/-4)
src/metadataserver/models/tests/test_nodecommissionresult.py (+3/-3)
src/metadataserver/models/tests/test_noderesults.py (+2/-2)
src/metadataserver/tests/test_fields.py (+25/-21)
To merge this branch: bzr merge lp:~jtv/maas/bug-1372735
Reviewer Review Type Date Requested Status
Gavin Panella (community) Needs Information
Review via email: mp+235559@code.launchpad.net

This proposal supersedes a proposal from 2014-09-23.

Commit message

Fix DeprecationWarning in Bin class. It was showing up in Twisted logs, which in turn broke Node model tests sometimes (because they checked for the log to be empty).

The warning was about object.__init__ not wanting to receive arguments during the super() upcall from Bin.__init__. Even making just a direct upcall to bytes.__init__ (because Bin is derived from bytes, not from object directly) did not fix it. And so I had to replace use of the Bin constructor with a little factory.

Description of the change

I did want to keep the safety of checking against the wrong values being passed in, especially unicode or None. I don't see any way to check that _after_ construction, and I couldn't write my own constructor, so I had to get in _before_ construction.

Most of the diff will be pointless context around tiny mechanical changes in usage. It's actually quite a small change.

Jeroen

To post a comment you must log in.
Revision history for this message
Newell Jensen (newell-jensen) : Posted in a previous version of this proposal
review: Approve
Revision history for this message
Gavin Panella (allenap) wrote :

I think there's a much simpler fix, but have a look and check that it does what you need.

review: Needs Information
Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

Proposing your version as a fresh branch.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/api/commissioning_scripts.py'
2--- src/maasserver/api/commissioning_scripts.py 2014-08-17 01:33:52 +0000
3+++ src/maasserver/api/commissioning_scripts.py 2014-09-23 04:43:27 +0000
4@@ -21,7 +21,7 @@
5 from django.shortcuts import get_object_or_404
6 from maasserver.api.support import OperationsHandler
7 from maasserver.api.utils import get_mandatory_param
8-from metadataserver.fields import Bin
9+from metadataserver.fields import wrap_bin
10 from metadataserver.models import CommissioningScript
11 from piston.utils import rc
12
13@@ -80,7 +80,7 @@
14 is ignored; MAAS will know it by the name you pass to the request.
15 """
16 name = get_mandatory_param(request.data, 'name')
17- content = Bin(get_content_parameter(request))
18+ content = wrap_bin(get_content_parameter(request))
19 return CommissioningScript.objects.create(name=name, content=content)
20
21 @classmethod
22@@ -114,7 +114,7 @@
23
24 def update(self, request, name):
25 """Update a commissioning script."""
26- content = Bin(get_content_parameter(request))
27+ content = wrap_bin(get_content_parameter(request))
28 script = get_object_or_404(CommissioningScript, name=name)
29 script.content = content
30 script.save()
31
32=== modified file 'src/maasserver/api/tests/test_nodegroup.py'
33--- src/maasserver/api/tests/test_nodegroup.py 2014-09-15 14:28:28 +0000
34+++ src/maasserver/api/tests/test_nodegroup.py 2014-09-23 04:43:27 +0000
35@@ -48,7 +48,7 @@
36 from maastesting.celery import CeleryFixture
37 from maastesting.matchers import MockCalledOnceWith
38 from metadataserver.enum import RESULT_TYPE
39-from metadataserver.fields import Bin
40+from metadataserver.fields import wrap_bin
41 from metadataserver.models import (
42 commissioningscript,
43 NodeResult,
44@@ -416,7 +416,7 @@
45 NodeResult.objects.store_data(
46 node, commissioningscript.LSHW_OUTPUT_NAME,
47 script_result=0, result_type=RESULT_TYPE.COMMISSIONING,
48- data=Bin(data))
49+ data=wrap_bin(data))
50
51 example_lldp_details = dedent("""\
52 <?xml version="1.0" encoding="UTF-8"?>
53@@ -429,7 +429,7 @@
54 NodeResult.objects.store_data(
55 node, commissioningscript.LLDP_OUTPUT_NAME,
56 script_result=0, result_type=RESULT_TYPE.COMMISSIONING,
57- data=Bin(data))
58+ data=wrap_bin(data))
59
60 def test_nodegroup_requires_authentication(self):
61 nodegroup = factory.make_NodeGroup()
62
63=== modified file 'src/maasserver/forms.py'
64--- src/maasserver/forms.py 2014-09-22 05:36:10 +0000
65+++ src/maasserver/forms.py 2014-09-23 04:43:27 +0000
66@@ -142,7 +142,7 @@
67 list_osystem_choices,
68 list_release_choices,
69 )
70-from metadataserver.fields import Bin
71+from metadataserver.fields import wrap_bin
72 from metadataserver.models import CommissioningScript
73 from netaddr import IPAddress
74 from provisioningserver.drivers.osystem import OperatingSystemRegistry
75@@ -1620,7 +1620,7 @@
76 content = self.cleaned_data['content']
77 CommissioningScript.objects.create(
78 name=content.name,
79- content=Bin(content.read()))
80+ content=wrap_bin(content.read()))
81
82
83 class UnconstrainedMultipleChoiceField(MultipleChoiceField):
84
85=== modified file 'src/maasserver/models/filestorage.py'
86--- src/maasserver/models/filestorage.py 2013-10-07 09:12:40 +0000
87+++ src/maasserver/models/filestorage.py 2014-09-23 04:43:27 +0000
88@@ -1,4 +1,4 @@
89-# Copyright 2012 Canonical Ltd. This software is licensed under the
90+# Copyright 2012-2014 Canonical Ltd. This software is licensed under the
91 # GNU Affero General Public License version 3 (see the file LICENSE).
92
93 """Storage for uploaded files."""
94@@ -31,8 +31,8 @@
95 from maasserver import DefaultMeta
96 from maasserver.models.cleansave import CleanSave
97 from metadataserver.fields import (
98- Bin,
99 BinaryField,
100+ wrap_bin,
101 )
102
103
104@@ -59,7 +59,7 @@
105 """
106 # This probably ought to read in chunks but large files are
107 # not expected.
108- content = Bin(file_object.read())
109+ content = wrap_bin(file_object.read())
110 storage, created = self.get_or_create(
111 filename=filename, owner=owner, defaults={'content': content})
112 if not created:
113
114=== modified file 'src/maasserver/models/tests/test_node.py'
115--- src/maasserver/models/tests/test_node.py 2014-09-22 06:29:06 +0000
116+++ src/maasserver/models/tests/test_node.py 2014-09-23 04:43:27 +0000
117@@ -78,7 +78,7 @@
118 from maastesting.testcase import MAASTestCase
119 from metadataserver import commissioning
120 from metadataserver.enum import RESULT_TYPE
121-from metadataserver.fields import Bin
122+from metadataserver.fields import wrap_bin
123 from metadataserver.models import (
124 NodeResult,
125 NodeUserData,
126@@ -1050,7 +1050,7 @@
127 node, factory.make_string(),
128 random.randint(0, 10),
129 RESULT_TYPE.COMMISSIONING,
130- Bin(factory.make_bytes()))
131+ wrap_bin(factory.make_bytes()))
132 node.start_commissioning(factory.make_admin())
133 self.assertItemsEqual([], node.noderesult_set.all())
134
135@@ -1061,7 +1061,7 @@
136 script_result = random.randint(0, 10)
137 NodeResult.objects.store_data(
138 node, filename, script_result, RESULT_TYPE.COMMISSIONING,
139- Bin(data))
140+ wrap_bin(data))
141 other_node = factory.make_Node(status=NODE_STATUS.NEW)
142 other_node.start_commissioning(factory.make_admin())
143 self.assertEqual(
144
145=== modified file 'src/maasserver/testing/factory.py'
146--- src/maasserver/testing/factory.py 2014-09-19 12:48:48 +0000
147+++ src/maasserver/testing/factory.py 2014-09-23 04:43:27 +0000
148@@ -76,7 +76,7 @@
149 import maastesting.factory
150 from maastesting.factory import NO_VALUE
151 from metadataserver.enum import RESULT_TYPE
152-from metadataserver.fields import Bin
153+from metadataserver.fields import wrap_bin
154 from metadataserver.models import (
155 CommissioningScript,
156 NodeResult,
157@@ -445,7 +445,7 @@
158 script_result = random.randint(0, 10)
159 ncr = NodeResult(
160 node=node, name=name, script_result=script_result,
161- result_type=RESULT_TYPE.COMMISSIONING, data=Bin(data))
162+ result_type=RESULT_TYPE.COMMISSIONING, data=wrap_bin(data))
163 ncr.save()
164 return ncr
165
166@@ -462,7 +462,7 @@
167 script_result = random.randint(0, 10)
168 ncr = NodeResult(
169 node=node, name=name, script_result=script_result,
170- result_type=RESULT_TYPE.INSTALLING, data=Bin(data))
171+ result_type=RESULT_TYPE.INSTALLING, data=wrap_bin(data))
172 ncr.save()
173 return ncr
174
175@@ -707,7 +707,7 @@
176 if content is None:
177 content = b'content:' + self.make_string().encode('ascii')
178 return CommissioningScript.objects.create(
179- name=name, content=Bin(content))
180+ name=name, content=wrap_bin(content))
181
182 def make_DownloadProgress(self, nodegroup=None, filename=None,
183 size=NO_VALUE, bytes_downloaded=NO_VALUE,
184
185=== modified file 'src/maasserver/tests/test_third_party_drivers.py'
186--- src/maasserver/tests/test_third_party_drivers.py 2014-09-10 16:20:31 +0000
187+++ src/maasserver/tests/test_third_party_drivers.py 2014-09-23 04:43:27 +0000
188@@ -28,7 +28,7 @@
189 from maastesting import root
190 from maastesting.testcase import MAASTestCase
191 from metadataserver.enum import RESULT_TYPE
192-from metadataserver.fields import Bin
193+from metadataserver.fields import wrap_bin
194 from metadataserver.models import (
195 commissioningscript,
196 NodeResult,
197@@ -42,7 +42,7 @@
198 node = factory.make_Node()
199 NodeResult.objects.store_data(
200 node, commissioningscript.LIST_MODALIASES_OUTPUT_NAME,
201- 0, RESULT_TYPE.COMMISSIONING, Bin(test_data))
202+ 0, RESULT_TYPE.COMMISSIONING, wrap_bin(test_data))
203
204 aliases = node_modaliases(node)
205 self.assertEqual(['hulla', 'baloo'], aliases)
206
207=== modified file 'src/metadataserver/api.py'
208--- src/metadataserver/api.py 2014-08-22 16:25:29 +0000
209+++ src/metadataserver/api.py 2014-09-23 04:43:27 +0000
210@@ -67,7 +67,7 @@
211 RESULT_TYPE,
212 SIGNAL_STATUS,
213 )
214-from metadataserver.fields import Bin
215+from metadataserver.fields import wrap_bin
216 from metadataserver.models import (
217 CommissioningScript,
218 NodeKey,
219@@ -210,7 +210,7 @@
220 raw_content = uploaded_file.read()
221 NodeResult.objects.store_data(
222 node, name, script_result=0,
223- result_type=RESULT_TYPE.INSTALLING, data=Bin(raw_content))
224+ result_type=RESULT_TYPE.INSTALLING, data=wrap_bin(raw_content))
225
226 def _store_commissioning_results(self, node, request):
227 """Store commissioning result files for `node`."""
228@@ -224,7 +224,8 @@
229 exit_status=script_result)
230 NodeResult.objects.store_data(
231 node, name, script_result,
232- result_type=RESULT_TYPE.COMMISSIONING, data=Bin(raw_content))
233+ result_type=RESULT_TYPE.COMMISSIONING,
234+ data=wrap_bin(raw_content))
235
236 @operation(idempotent=False)
237 def signal(self, request, version=None, mac=None):
238
239=== modified file 'src/metadataserver/fields.py'
240--- src/metadataserver/fields.py 2013-10-21 05:31:09 +0000
241+++ src/metadataserver/fields.py 2014-09-23 04:43:27 +0000
242@@ -1,4 +1,4 @@
243-# Copyright 2012 Canonical Ltd. This software is licensed under the
244+# Copyright 2012-2014 Canonical Ltd. This software is licensed under the
245 # GNU Affero General Public License version 3 (see the file LICENSE).
246
247 """Custom field types for the metadata server."""
248@@ -14,6 +14,7 @@
249 __metaclass__ = type
250 __all__ = [
251 'BinaryField',
252+ 'wrap_bin',
253 ]
254
255 from base64 import (
256@@ -29,6 +30,26 @@
257 from south.modelsinspector import add_introspection_rules
258
259
260+def wrap_bin(binary_data):
261+ """Wrap a `bytes` into a `Bin`.
262+
263+ Use this to construct a `Bin`, a marker class with which we wrap binary
264+ data. Otherwise Django field conversions won't be able to tell a binary
265+ value as found in the database from one that's already been "converted" to
266+ a Python object.
267+
268+ :param binary_data: A `bytes` object. Nothing else is accepted.
269+ :return: A `Bin` containing `binary_data`.
270+ """
271+ # We can't give `Bin` its own constructor to check this, because the
272+ # upcall to bytes.__init__ triggers a DeprecationWarning about
273+ # object.__init__ no longer accepting arguments. (This happens even with
274+ # a direct upcall; it's not just super() that causes it.)
275+ assert isinstance(binary_data, bytes), (
276+ "Not a binary string: '%s'." % repr(binary_data))
277+ return Bin(binary_data)
278+
279+
280 class Bin(bytes):
281 """Wrapper class to convince django that a string is really binary.
282
283@@ -39,24 +60,12 @@
284 can stay as it is). The line between bytes and unicode is dangerously
285 thin.
286
287- So, to store a value in a BinaryField, wrap it in a Bin:
288+ So, to store a value in a BinaryField, wrap it in a Bin. Use the
289+ `wrap_bin` factory:
290
291- my_model_object.binary_data = Bin(b"\x01\x02\x03")
292+ my_model_object.binary_data = wrap_bin(b"\x01\x02\x03")
293 """
294
295- def __init__(self, initializer):
296- """Wrap a bytes.
297-
298- :param initializer: Binary string of data for this Bin. This must
299- be a bytes. Anything else is almost certainly a mistake, so e.g.
300- this constructor will refuse to render None as b'None'.
301- :type initializer: bytes
302- """
303- if not isinstance(initializer, bytes):
304- raise AssertionError(
305- "Not a binary string: '%s'" % repr(initializer))
306- super(Bin, self).__init__(initializer)
307-
308 def __emittable__(self):
309 """Emit base-64 encoded bytes.
310
311
312=== modified file 'src/metadataserver/migrations/0012_commission_result_binary_data_recode.py'
313--- src/metadataserver/migrations/0012_commission_result_binary_data_recode.py 2014-03-27 04:15:45 +0000
314+++ src/metadataserver/migrations/0012_commission_result_binary_data_recode.py 2014-09-23 04:43:27 +0000
315@@ -1,7 +1,6 @@
316 # -*- coding: utf-8 -*-
317 import datetime
318
319-from django.db import models
320 from south.db import db
321 from south.v2 import DataMigration
322
323@@ -10,9 +9,9 @@
324
325 def forwards(self, orm):
326 "Write your forwards methods here."
327- from metadataserver.fields import Bin
328+ from metadataserver.fields import wrap_bin
329 for result in orm.NodeCommissionResult.objects.all():
330- result.data_bin = Bin(result.data.encode("utf-8"))
331+ result.data_bin = wrap_bin(result.data.encode("utf-8"))
332 result.save()
333
334 def backwards(self, orm):
335
336=== modified file 'src/metadataserver/models/commissioningscript.py'
337--- src/metadataserver/models/commissioningscript.py 2014-08-13 21:49:35 +0000
338+++ src/metadataserver/models/commissioningscript.py 2014-09-23 04:43:27 +0000
339@@ -1,4 +1,4 @@
340-# Copyright 2012, 2013 Canonical Ltd. This software is licensed under the
341+# Copyright 2012-2014 Canonical Ltd. This software is licensed under the
342 # GNU Affero General Public License version 3 (see the file LICENSE).
343
344 """Custom commissioning scripts, and their database backing."""
345@@ -47,8 +47,8 @@
346 from metadataserver import DefaultMeta
347 from metadataserver.enum import RESULT_TYPE
348 from metadataserver.fields import (
349- Bin,
350 BinaryField,
351+ wrap_bin,
352 )
353 from metadataserver.models.noderesult import NodeResult
354
355@@ -467,7 +467,7 @@
356 assert isinstance(output, bytes)
357 NodeResult.objects.store_data(
358 node, name, script_result=exit_status,
359- result_type=RESULT_TYPE.COMMISSIONING, data=Bin(output))
360+ result_type=RESULT_TYPE.COMMISSIONING, data=wrap_bin(output))
361 if name in BUILTIN_COMMISSIONING_SCRIPTS:
362 postprocess_hook = BUILTIN_COMMISSIONING_SCRIPTS[name]['hook']
363 postprocess_hook(node=node, output=output, exit_status=exit_status)
364
365=== modified file 'src/metadataserver/models/nodeuserdata.py'
366--- src/metadataserver/models/nodeuserdata.py 2014-09-02 12:34:10 +0000
367+++ src/metadataserver/models/nodeuserdata.py 2014-09-23 04:43:27 +0000
368@@ -1,4 +1,4 @@
369-# Copyright 2012 Canonical Ltd. This software is licensed under the
370+# Copyright 2012-2014 Canonical Ltd. This software is licensed under the
371 # GNU Affero General Public License version 3 (see the file LICENSE).
372
373 """Node user-data for cloud-init's use."""
374@@ -25,8 +25,8 @@
375 from maasserver.models.cleansave import CleanSave
376 from metadataserver import DefaultMeta
377 from metadataserver.fields import (
378- Bin,
379 BinaryField,
380+ wrap_bin,
381 )
382
383
384@@ -53,7 +53,7 @@
385
386 def _set(self, node, data):
387 """Set actual user data for a node. Not usable if data is None."""
388- wrapped_data = Bin(data)
389+ wrapped_data = wrap_bin(data)
390 (existing_entry, created) = self.get_or_create(
391 node=node, defaults={'data': wrapped_data})
392 if not created:
393@@ -72,7 +72,7 @@
394 self.filter(node__in=nodes).delete()
395 if data is not None:
396 self.bulk_create((
397- self.model(node=node, data=Bin(data))
398+ self.model(node=node, data=wrap_bin(data))
399 for node in nodes
400 ))
401
402
403=== modified file 'src/metadataserver/models/tests/test_nodecommissionresult.py'
404--- src/metadataserver/models/tests/test_nodecommissionresult.py 2014-09-15 14:28:28 +0000
405+++ src/metadataserver/models/tests/test_nodecommissionresult.py 2014-09-23 04:43:27 +0000
406@@ -22,7 +22,7 @@
407 from maasserver.utils.converters import XMLToYAML
408 from maastesting.djangotestcase import DjangoTestCase
409 from metadataserver.enum import RESULT_TYPE
410-from metadataserver.fields import Bin
411+from metadataserver.fields import wrap_bin
412 from metadataserver.models import NodeResult
413 from metadataserver.models.commissioningscript import (
414 LLDP_OUTPUT_NAME,
415@@ -133,7 +133,7 @@
416 script_result = randint(0, 10)
417 result = NodeResult.objects.store_data(
418 node, name=name, script_result=script_result,
419- result_type=RESULT_TYPE.COMMISSIONING, data=Bin(data))
420+ result_type=RESULT_TYPE.COMMISSIONING, data=wrap_bin(data))
421 result_in_db = NodeResult.objects.get(node=node)
422
423 self.assertAttributes(result_in_db, dict(name=name, data=data))
424@@ -148,7 +148,7 @@
425 data = factory.make_bytes(1024 * 1024)
426 NodeResult.objects.store_data(
427 node, name=name, script_result=script_result,
428- result_type=RESULT_TYPE.COMMISSIONING, data=Bin(data))
429+ result_type=RESULT_TYPE.COMMISSIONING, data=wrap_bin(data))
430
431 self.assertAttributes(
432 NodeResult.objects.get(node=node),
433
434=== modified file 'src/metadataserver/models/tests/test_noderesults.py'
435--- src/metadataserver/models/tests/test_noderesults.py 2014-09-10 16:20:31 +0000
436+++ src/metadataserver/models/tests/test_noderesults.py 2014-09-23 04:43:27 +0000
437@@ -45,7 +45,7 @@
438 from maastesting.matchers import MockCalledOnceWith
439 from maastesting.utils import sample_binary_data
440 from metadataserver.enum import RESULT_TYPE
441-from metadataserver.fields import Bin
442+from metadataserver.fields import wrap_bin
443 from metadataserver.models import (
444 CommissioningScript,
445 commissioningscript as cs_module,
446@@ -153,7 +153,7 @@
447 def test_scripts_may_be_binary(self):
448 name = make_script_name()
449 CommissioningScript.objects.create(
450- name=name, content=Bin(sample_binary_data))
451+ name=name, content=wrap_bin(sample_binary_data))
452 stored_script = CommissioningScript.objects.get(name=name)
453 self.assertEqual(sample_binary_data, stored_script.content)
454
455
456=== modified file 'src/metadataserver/tests/test_fields.py'
457--- src/metadataserver/tests/test_fields.py 2014-07-18 17:05:57 +0000
458+++ src/metadataserver/tests/test_fields.py 2014-09-23 04:43:27 +0000
459@@ -22,6 +22,7 @@
460 from metadataserver.fields import (
461 Bin,
462 BinaryField,
463+ wrap_bin,
464 )
465 from metadataserver.tests.models import BinaryFieldModel
466
467@@ -29,24 +30,27 @@
468 class TestBin(MAASServerTestCase):
469 """Test Bin helper class."""
470
471- def test_is_basically_bytes(self):
472- self.assertEqual(b"Hello", Bin(b"Hello"))
473-
474- def test_refuses_to_construct_from_unicode(self):
475- self.assertRaises(AssertionError, Bin, "Hello")
476-
477- def test_refuses_to_construct_from_None(self):
478- self.assertRaises(AssertionError, Bin, None)
479-
480- def test_emits_base64(self):
481+ def test__is_what_wrap_bin_returns(self):
482+ self.assertIsInstance(wrap_bin(b"Hi"), Bin)
483+
484+ def test__is_basically_bytes(self):
485+ self.assertEqual(b"Hello", wrap_bin(b"Hello"))
486+
487+ def test__refuses_to_construct_from_unicode(self):
488+ self.assertRaises(AssertionError, wrap_bin, "Hello")
489+
490+ def test__refuses_to_construct_from_None(self):
491+ self.assertRaises(AssertionError, wrap_bin, None)
492+
493+ def test__emits_base64(self):
494 # Piston hooks onto an __emittable__() method, if present.
495 # Bin() returns a base-64 encoded string so that it can be
496 # transmitted in JSON.
497- self.assertEqual(b"", Bin(b"").__emittable__())
498+ self.assertEqual(b"", wrap_bin(b"").__emittable__())
499 example_bytes = factory.make_bytes()
500 self.assertEqual(
501 b64encode(example_bytes),
502- Bin(example_bytes).__emittable__())
503+ wrap_bin(example_bytes).__emittable__())
504
505
506 class TestBinaryField(TestModelMixin, MAASServerTestCase):
507@@ -62,7 +66,7 @@
508 BinaryFieldModel.objects.get(id=binary_item.id).data)
509
510 def test_stores_and_retrieves_empty_data(self):
511- binary_item = BinaryFieldModel(data=Bin(b''))
512+ binary_item = BinaryFieldModel(data=wrap_bin(b''))
513 self.assertEqual(b'', binary_item.data)
514 binary_item.save()
515 self.assertEqual(
516@@ -70,7 +74,7 @@
517
518 def test_does_not_truncate_at_zero_bytes(self):
519 data = b"BEFORE THE ZERO\x00AFTER THE ZERO"
520- binary_item = BinaryFieldModel(data=Bin(data))
521+ binary_item = BinaryFieldModel(data=wrap_bin(data))
522 self.assertEqual(data, binary_item.data)
523 binary_item.save()
524 self.assertEqual(
525@@ -78,24 +82,24 @@
526
527 def test_stores_and_retrieves_binary_data(self):
528 data = b"\x01\x02\xff\xff\xfe\xff\xff\xfe"
529- binary_item = BinaryFieldModel(data=Bin(data))
530+ binary_item = BinaryFieldModel(data=wrap_bin(data))
531 self.assertEqual(data, binary_item.data)
532 binary_item.save()
533 self.assertEqual(
534 data, BinaryFieldModel.objects.get(id=binary_item.id).data)
535
536 def test_returns_bytes_not_text(self):
537- binary_item = BinaryFieldModel(data=Bin(b"Data"))
538+ binary_item = BinaryFieldModel(data=wrap_bin(b"Data"))
539 binary_item.save()
540 retrieved_data = BinaryFieldModel.objects.get(id=binary_item.id).data
541 self.assertIsInstance(retrieved_data, bytes)
542
543 def test_looks_up_data(self):
544 data = b"Binary item"
545- binary_item = BinaryFieldModel(data=Bin(data))
546+ binary_item = BinaryFieldModel(data=wrap_bin(data))
547 binary_item.save()
548 self.assertEqual(
549- binary_item, BinaryFieldModel.objects.get(data=Bin(data)))
550+ binary_item, BinaryFieldModel.objects.get(data=wrap_bin(data)))
551
552 def test_get_default_returns_None(self):
553 field = BinaryField(null=True)
554@@ -104,10 +108,10 @@
555
556 def test_get_default_returns_Bin(self):
557 field = BinaryField(null=True)
558- self.patch(field, "default", Bin(b"wotcha"))
559- self.assertEqual(Bin(b"wotcha"), field.get_default())
560+ self.patch(field, "default", wrap_bin(b"wotcha"))
561+ self.assertEqual(wrap_bin(b"wotcha"), field.get_default())
562
563 def test_get_default_returns_Bin_from_bytes(self):
564 field = BinaryField(null=True)
565 self.patch(field, "default", b"wotcha")
566- self.assertEqual(Bin(b"wotcha"), field.get_default())
567+ self.assertEqual(wrap_bin(b"wotcha"), field.get_default())