|
1 | 1 | # Test methods with long descriptive names can omit docstrings |
2 | 2 | # pylint: disable=missing-docstring, abstract-method, protected-access |
3 | 3 | import unittest |
4 | | -from unittest.mock import patch |
| 4 | +from unittest.mock import patch, Mock |
5 | 5 |
|
6 | 6 | import numpy as np |
7 | 7 |
|
@@ -421,6 +421,123 @@ def test_summary(self): |
421 | 421 | self.assertEqual(info._StateInfo__output_summary.brief, "") |
422 | 422 | self.assertEqual(info._StateInfo__output_summary.details, no_output) |
423 | 423 |
|
| 424 | + def _create_compute_values(self): |
| 425 | + a1, a2, a3, a4, c1 = self.iris.domain.variables |
| 426 | + |
| 427 | + def times2(*_): |
| 428 | + return 2 |
| 429 | + |
| 430 | + na1 = a1.copy() |
| 431 | + na2 = a2.copy(compute_value=times2) |
| 432 | + na3 = a3.copy(compute_value=lambda *_: 3) |
| 433 | + na4 = a4.copy(compute_value=lambda *_: 4) |
| 434 | + nc1 = c1.copy(compute_value=lambda *_: 5) |
| 435 | + |
| 436 | + ma1 = a1.copy() |
| 437 | + ma2 = a2.copy(compute_value=times2) |
| 438 | + ma3 = a3.copy(compute_value=lambda x: 6) |
| 439 | + ma4 = a4.copy(compute_value=lambda x: 7) |
| 440 | + |
| 441 | + table_n = self.iris.transform(Domain([na1, na2, na3, na4], nc1)) |
| 442 | + table_m = self.iris.transform(Domain([ma1, ma2, ma3], None, [ma4])) |
| 443 | + return table_n, table_m |
| 444 | + |
| 445 | + def test_dumb_tables(self): |
| 446 | + self.widget.apply = Mock() |
| 447 | + table_n, table_m = self._create_compute_values() |
| 448 | + na1, na2, na3, na4, nc1 = table_n.domain.variables |
| 449 | + ma1, ma2, ma3 = table_m.domain.attributes |
| 450 | + ma4 = table_m.domain.metas[0] |
| 451 | + |
| 452 | + self.send_signal(self.widget.Inputs.additional_data, table_n, 1) |
| 453 | + self.send_signal(self.widget.Inputs.additional_data, table_m, 2) |
| 454 | + |
| 455 | + # pylint: disable=unbalanced-tuple-unpacking |
| 456 | + dtable_n, dtable_m = self.widget._dumb_tables() |
| 457 | + dna1, dna2, dna3, dna4, dnc1 = dtable_n.domain.variables |
| 458 | + dma1, dma2, dma3 = dtable_m.domain.attributes |
| 459 | + dma4 = dtable_m.domain.metas[0] |
| 460 | + |
| 461 | + # No copying: same name and no compute value |
| 462 | + self.assertIs(na1, dna1) |
| 463 | + self.assertIs(ma1, dma1) |
| 464 | + |
| 465 | + # No copying: same name and same compute value |
| 466 | + self.assertIs(na2, dna2) |
| 467 | + self.assertIs(ma2, dma2) |
| 468 | + |
| 469 | + # Copy: same name and different compute value |
| 470 | + self.assertIsNot(na3, dna3) |
| 471 | + self.assertIsNot(ma3, dma3) |
| 472 | + self.assertIsNone(dna3.compute_value) |
| 473 | + self.assertIsNone(dma3.compute_value) |
| 474 | + |
| 475 | + # No copying: same name and different compute value, but different part |
| 476 | + self.assertIs(na4, dna4) |
| 477 | + self.assertIs(ma4, dma4) |
| 478 | + |
| 479 | + # No copying: does not appear in the other table |
| 480 | + self.assertIs(nc1, dnc1) |
| 481 | + |
| 482 | + np.testing.assert_equal(table_m.X, dtable_m.X) |
| 483 | + np.testing.assert_equal(table_m.Y, dtable_m.Y) |
| 484 | + np.testing.assert_equal(table_n.X, dtable_n.X) |
| 485 | + np.testing.assert_equal(table_n.metas, dtable_n.metas) |
| 486 | + |
| 487 | + def test_dont_ignore_compute_value(self): |
| 488 | + table_n, table_m = self._create_compute_values() |
| 489 | + na1, na2, na3, na4, nc1 = table_n.domain.variables |
| 490 | + ma3 = table_m.domain.attributes[2] |
| 491 | + ma4 = table_m.domain.metas[0] |
| 492 | + |
| 493 | + self.send_signal(self.widget.Inputs.additional_data, table_n, 1) |
| 494 | + self.send_signal(self.widget.Inputs.additional_data, table_m, 2) |
| 495 | + |
| 496 | + self.widget.ignore_compute_value = False |
| 497 | + self.widget.apply() |
| 498 | + |
| 499 | + output = self.get_output(self.widget.Outputs.data) |
| 500 | + attributes = output.domain.attributes |
| 501 | + self.assertEqual(len(attributes), 5) |
| 502 | + self.assertIs(attributes[0], na1) |
| 503 | + self.assertIs(attributes[1], na2) |
| 504 | + self.assertIs(attributes[2].compute_value.variable, na3) # renamed |
| 505 | + self.assertIs(attributes[3].compute_value.variable, na4) # renamed |
| 506 | + self.assertIs(attributes[4].compute_value.variable, ma3) # renamed |
| 507 | + |
| 508 | + self.assertIs(output.domain.class_var, nc1) |
| 509 | + |
| 510 | + self.assertEqual(len(output.domain.metas), 1) |
| 511 | + self.assertIs(output.domain.metas[0].compute_value.variable, ma4) |
| 512 | + |
| 513 | + def test_ignore_compute_value(self): |
| 514 | + table_n, table_m = self._create_compute_values() |
| 515 | + na1, na2, na3, na4, nc1 = table_n.domain.variables |
| 516 | + ma3 = table_m.domain.attributes[2] |
| 517 | + ma4 = table_m.domain.metas[0] |
| 518 | + |
| 519 | + self.send_signal(self.widget.Inputs.additional_data, table_n, 1) |
| 520 | + self.send_signal(self.widget.Inputs.additional_data, table_m, 2) |
| 521 | + |
| 522 | + self.widget.ignore_compute_value = True |
| 523 | + self.widget.apply() |
| 524 | + |
| 525 | + output = self.get_output(self.widget.Outputs.data) |
| 526 | + attributes = output.domain.attributes |
| 527 | + self.assertEqual(len(attributes), 4) |
| 528 | + self.assertIs(attributes[0], na1) |
| 529 | + self.assertIs(attributes[1], na2) |
| 530 | + self.assertIsNot(attributes[2], na3) |
| 531 | + self.assertIsNot(attributes[2], ma3) |
| 532 | + self.assertIsNone(attributes[2].compute_value, ma3) # renamed |
| 533 | + self.assertEqual(attributes[2].name, na3.name) |
| 534 | + self.assertIs(attributes[3].compute_value.variable, na4) # renamed |
| 535 | + |
| 536 | + self.assertIs(output.domain.class_var, nc1) |
| 537 | + |
| 538 | + self.assertEqual(len(output.domain.metas), 1) |
| 539 | + self.assertIs(output.domain.metas[0].compute_value.variable, ma4) # renamed |
| 540 | + |
424 | 541 |
|
425 | 542 | if __name__ == "__main__": |
426 | 543 | unittest.main() |
0 commit comments