|
4 | 4 | from tempfile import mkdtemp |
5 | 5 | from pathlib import Path |
6 | 6 | from unittest import TestCase |
| 7 | +from datetime import datetime |
7 | 8 |
|
8 | 9 | import numpy as np |
9 | 10 | from pynwb import NWBFile, NWBHDF5IO, TimeSeries |
@@ -144,7 +145,7 @@ def assertLogFileContentsEqual( |
144 | 145 | if ".nwb" in test_line: |
145 | 146 | # Transform temporary testing path and formatted to hardcoded fake path |
146 | 147 | str_loc = test_line.find(".nwb") |
147 | | - correction_str = test_line.replace(test_line[5 : str_loc - 8], "./") |
| 148 | + correction_str = test_line.replace(test_line[5 : str_loc - 8], "./") # noqa E203 (black) |
148 | 149 | test_file_lines[line_number] = correction_str |
149 | 150 | self.assertEqual(first=test_file_lines[skip_first_n_lines:-1], second=true_file_lines) |
150 | 151 |
|
@@ -561,3 +562,75 @@ def test_dandiset_streaming_cli_parallel(self): |
561 | 562 | f"> {console_output_file}" |
562 | 563 | ) |
563 | 564 | self.assertFileExists(path=self.tempdir / "test_nwbinspector_streaming_report_7.txt") |
| 565 | + |
| 566 | + |
| 567 | +class TestCheckUniqueIdentifiersPass(TestCase): |
| 568 | + maxDiff = None |
| 569 | + |
| 570 | + @classmethod |
| 571 | + def setUpClass(cls): |
| 572 | + cls.tempdir = Path(mkdtemp()) |
| 573 | + num_nwbfiles = 3 |
| 574 | + unique_id_nwbfiles = list() |
| 575 | + for j in range(num_nwbfiles): |
| 576 | + unique_id_nwbfiles.append(make_minimal_nwbfile()) |
| 577 | + |
| 578 | + cls.unique_id_nwbfile_paths = [str(cls.tempdir / f"unique_id_testing{j}.nwb") for j in range(num_nwbfiles)] |
| 579 | + for nwbfile_path, nwbfile in zip(cls.unique_id_nwbfile_paths, unique_id_nwbfiles): |
| 580 | + with NWBHDF5IO(path=nwbfile_path, mode="w") as io: |
| 581 | + io.write(nwbfile) |
| 582 | + |
| 583 | + @classmethod |
| 584 | + def tearDownClass(cls): |
| 585 | + rmtree(cls.tempdir) |
| 586 | + |
| 587 | + def test_check_unique_identifiers_pass(self): |
| 588 | + assert list(inspect_all(path=self.tempdir, select=["check_data_orientation"])) == [] |
| 589 | + |
| 590 | + |
| 591 | +class TestCheckUniqueIdentifiersFail(TestCase): |
| 592 | + maxDiff = None |
| 593 | + |
| 594 | + @classmethod |
| 595 | + def setUpClass(cls): |
| 596 | + cls.tempdir = Path(mkdtemp()) |
| 597 | + num_nwbfiles = 3 |
| 598 | + non_unique_id_nwbfiles = list() |
| 599 | + for j in range(num_nwbfiles): |
| 600 | + non_unique_id_nwbfiles.append( |
| 601 | + NWBFile( |
| 602 | + session_description="", |
| 603 | + identifier="not a unique identifier!", |
| 604 | + session_start_time=datetime.now().astimezone(), |
| 605 | + ) |
| 606 | + ) |
| 607 | + |
| 608 | + cls.non_unique_id_nwbfile_paths = [ |
| 609 | + str(cls.tempdir / f"non_unique_id_testing{j}.nwb") for j in range(num_nwbfiles) |
| 610 | + ] |
| 611 | + for nwbfile_path, nwbfile in zip(cls.non_unique_id_nwbfile_paths, non_unique_id_nwbfiles): |
| 612 | + with NWBHDF5IO(path=nwbfile_path, mode="w") as io: |
| 613 | + io.write(nwbfile) |
| 614 | + |
| 615 | + @classmethod |
| 616 | + def tearDownClass(cls): |
| 617 | + rmtree(cls.tempdir) |
| 618 | + |
| 619 | + def test_check_unique_identifiers_fail(self): |
| 620 | + print(list(inspect_all(path=self.tempdir, select=["check_data_orientation"]))) |
| 621 | + assert list(inspect_all(path=self.tempdir, select=["check_data_orientation"])) == [ |
| 622 | + InspectorMessage( |
| 623 | + message=( |
| 624 | + "The identifier 'not a unique identifier!' is used across the .nwb files: " |
| 625 | + f"{[str(x) for x in self.non_unique_id_nwbfile_paths]}\n" |
| 626 | + "The identifier of any NWBFile should be a completely unique value - " |
| 627 | + "we recommend using uuid4 to achieve this." |
| 628 | + ), |
| 629 | + importance=Importance.CRITICAL, |
| 630 | + check_function_name="check_unique_identifiers", |
| 631 | + object_type="NWBFile", |
| 632 | + object_name="root", |
| 633 | + location="/", |
| 634 | + file_path=str(Path(self.non_unique_id_nwbfile_paths[0]).parent), |
| 635 | + ) |
| 636 | + ] |
0 commit comments