MCPcopy
hub / github.com/django/django / TestData

Class TestData

django/test/testcases.py:1340–1375  ·  view source on GitHub ↗

Descriptor to provide TestCase instance isolation for attributes assigned during the setUpTestData() phase. Allow safe alteration of objects assigned in setUpTestData() by test methods by exposing deep copies instead of the original objects. Objects are deep copied using a mem

Source from the content-addressed store, hash-verified

1338
1339
1340class TestData:
1341 """
1342 Descriptor to provide TestCase instance isolation for attributes assigned
1343 during the setUpTestData() phase.
1344
1345 Allow safe alteration of objects assigned in setUpTestData() by test
1346 methods by exposing deep copies instead of the original objects.
1347
1348 Objects are deep copied using a memo kept on the test case instance in
1349 order to maintain their original relationships.
1350 """
1351
1352 memo_attr = "_testdata_memo"
1353
1354 def __init__(self, name, data):
1355 self.name = name
1356 self.data = data
1357
1358 def get_memo(self, testcase):
1359 try:
1360 memo = getattr(testcase, self.memo_attr)
1361 except AttributeError:
1362 memo = {}
1363 setattr(testcase, self.memo_attr, memo)
1364 return memo
1365
1366 def __get__(self, instance, owner):
1367 if instance is None:
1368 return self.data
1369 memo = self.get_memo(instance)
1370 data = deepcopy(self.data, memo)
1371 setattr(instance, self.name, data)
1372 return data
1373
1374 def __repr__(self):
1375 return "<TestData: name=%r, data=%r>" % (self.name, self.data)
1376
1377
1378class TestCase(TransactionTestCase):

Callers 2

test_reprMethod · 0.90
setUpClassMethod · 0.85

Calls

no outgoing calls

Tested by 2

test_reprMethod · 0.72
setUpClassMethod · 0.68