From c203be2f1e7157319a3d0b5136f4195da80d7deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Wiik=20=C3=85nes?= Date: Sun, 22 Sep 2019 15:06:57 +0200 Subject: [PATCH] Add tests for mmap_mode='r+b' and not lazy and padding check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Håkon Wiik Ånes --- kikuchipy/io_plugins/nordif.py | 5 ++-- kikuchipy/io_plugins/test/test_nordif.py | 37 +++++++++++++++--------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/kikuchipy/io_plugins/nordif.py b/kikuchipy/io_plugins/nordif.py index 517b9f2c..4f1c2465 100644 --- a/kikuchipy/io_plugins/nordif.py +++ b/kikuchipy/io_plugins/nordif.py @@ -75,10 +75,9 @@ def file_reader( 'write' in mmap_mode and 'copyonwrite' != mmap_mode): if lazy: raise ValueError("Lazy loading does not support in-place writing") - f = open(filename, 'r+b') - scan['attributes']['_lazy'] = True + f = open(filename, mode='r+b') else: - f = open(filename, 'rb') + f = open(filename, mode='rb') # Get metadata from setting file sem_node, ebsd_node = metadata_nodes() diff --git a/kikuchipy/io_plugins/test/test_nordif.py b/kikuchipy/io_plugins/test/test_nordif.py index b528141d..36812fa6 100644 --- a/kikuchipy/io_plugins/test/test_nordif.py +++ b/kikuchipy/io_plugins/test/test_nordif.py @@ -171,19 +171,25 @@ def test_load(self, setting_file): .static_background.all() == static_bg.all() @pytest.mark.parametrize('nav_shape, sig_shape', [ - ((3, 3), (60, 60)), ((3, 4), (50, 50))]) + ((3, 3), (60, 60)), ((3, 4), (60, 60)), (None, None)]) def test_load_parameters(self, nav_shape, sig_shape): - if sum(nav_shape + sig_shape) > 126: - # Check if zero padding user warning is raised if sum of data shape - # is bigger than file size - with pytest.warns(UserWarning): + if nav_shape is None and sig_shape is None: + with pytest.raises(ValueError): + kp.load( + PATTERN_FILE, setting_file='Setting.txt', + scan_size=nav_shape, pattern_size=sig_shape) + else: + if sum(nav_shape + sig_shape) > 126: + # Check if zero padding user warning is raised if sum of data + # shape is bigger than file size + with pytest.warns(UserWarning): + s = kp.load( + PATTERN_FILE, scan_size=nav_shape, + pattern_size=sig_shape) + else: s = kp.load( PATTERN_FILE, scan_size=nav_shape, pattern_size=sig_shape) - else: - s = kp.load( - PATTERN_FILE, scan_size=nav_shape, pattern_size=sig_shape) - - assert s.data.shape == nav_shape[::-1] + sig_shape + assert s.data.shape == nav_shape[::-1] + sig_shape def test_load_save_cycle(self, save_path): s = kp.load(PATTERN_FILE) @@ -238,9 +244,14 @@ def test_load_readonly(self): with pytest.raises(NotImplementedError): s.data[:] = 23 - def test_load_inplace(self): - with pytest.raises(ValueError): - kp.load(PATTERN_FILE, lazy=True, mmap_mode='r+') + @pytest.mark.parametrize('lazy', [True, False]) + def test_load_inplace(self, lazy): + if lazy: + with pytest.raises(ValueError): + s = kp.load(PATTERN_FILE, lazy=lazy, mmap_mode='r+') + else: + s = kp.load(PATTERN_FILE, lazy=lazy, mmap_mode='r+') + assert s.axes_manager.as_dictionary() == AXES_MANAGER def test_save_fresh(self, save_path): scan_size = (10, 3)