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
| 1338 | |
| 1339 | |
| 1340 | class 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 | |
| 1378 | class TestCase(TransactionTestCase): |
no outgoing calls