|
1 | 1 | import os
|
2 | 2 | import tempfile
|
3 | 3 | import unittest
|
| 4 | +import shutil |
| 5 | +import pickle |
4 | 6 |
|
5 | 7 | import numpy as np
|
6 | 8 | from Orange.data import Table, Domain, StringVariable, ContinuousVariable
|
@@ -430,6 +432,38 @@ def test_migrate_settings(self):
|
430 | 432 | self.wait_until_finished(widget=widget)
|
431 | 433 | self.assertIsNone(widget.language)
|
432 | 434 |
|
| 435 | + def test_relative_corpus_path_serialization(self): |
| 436 | + """ |
| 437 | + Test if relative paths are properly saved and reloaded. |
| 438 | + """ |
| 439 | + # Create a dummy corpus file |
| 440 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 441 | + corpus = Corpus.from_file("book-excerpts") |
| 442 | + corpus_path = os.path.join(tmp_dir, "test.corpus") |
| 443 | + with open(corpus_path, "wb") as f: |
| 444 | + pickle.dump(corpus, f) |
| 445 | + |
| 446 | + # Simulate loading the file into widget |
| 447 | + self.widget.workflow_file = os.path.join(tmp_dir, "workflow.ows") |
| 448 | + self.widget.corpus_path = corpus_path |
| 449 | + |
| 450 | + settings = {} |
| 451 | + self.widget.save_settings(settings) |
| 452 | + |
| 453 | + # Simulate moving workflow and corpus to new directory |
| 454 | + with tempfile.TemporaryDirectory() as new_dir: |
| 455 | + new_corpus = os.path.join(new_dir, "test.corpus") |
| 456 | + new_workflow = os.path.join(new_dir, "workflow.ows") |
| 457 | + shutil.copy2(corpus_path, new_corpus) |
| 458 | + |
| 459 | + # Simulate loading settings in new widget |
| 460 | + restored = self.create_widget(OWCorpus) |
| 461 | + restored.workflow_file = new_workflow |
| 462 | + settings["corpus_path"] = os.path.relpath(new_corpus, new_dir) |
| 463 | + restored.load_settings(settings) |
| 464 | + |
| 465 | + self.assertTrue(os.path.exists(restored.corpus_path)) |
| 466 | + self.assertTrue(os.path.isabs(restored.corpus_path)) |
433 | 467 |
|
434 | 468 | if __name__ == "__main__":
|
435 | 469 | unittest.main()
|
0 commit comments