Ejemplo n.º 1
0
    def test_writeTempOverride_diffDir(self, mock_open, mock_os_open,
                                       mock_os_close, mock_fcntl, mock_scd):
        source_filepath = '/path/to/file.png'
        target_dir = '/some/other/dir'

        d = DataFileElement(source_filepath)
        fp = d.write_temp(temp_dir=target_dir)

        ntools.assert_not_equal(fp, source_filepath)
        ntools.assert_equal(os.path.dirname(fp), target_dir)

        # subsequent call to write temp should not invoke creation of a new file
        fp2 = d.write_temp()
        ntools.assert_equal(fp2, source_filepath)

        # request in same dir should return same path as first request with that
        # directory
        fp3 = d.write_temp(target_dir)
        ntools.assert_equal(fp, fp3)

        # request different target dir
        target2 = '/even/different/path'
        fp4 = d.write_temp(target2)
        ntools.assert_equal(os.path.dirname(fp4), target2)
        ntools.assert_not_equal(fp, fp4)
        ntools.assert_equal(len(d._temp_filepath_stack), 2)
Ejemplo n.º 2
0
 def test_cleanTemp(self):
     # a write temp and clean temp should not affect original file
     source_file = os.path.join(TEST_DATA_DIR, 'test_file.dat')
     self.assertTrue(os.path.isfile(source_file))
     d = DataFileElement(source_file)
     d.write_temp()
     self.assertEqual(len(d._temp_filepath_stack), 0)
     d.clean_temp()
     self.assertTrue(os.path.isfile(source_file))
Ejemplo n.º 3
0
 def test_cleanTemp(self):
     # a write temp and clean temp should not affect original file
     source_file = os.path.join(TEST_DATA_DIR, 'test_file.dat')
     self.assertTrue(os.path.isfile(source_file))
     d = DataFileElement(source_file)
     d.write_temp()
     self.assertEqual(len(d._temp_filepath_stack), 0)
     d.clean_temp()
     self.assertTrue(os.path.isfile(source_file))
Ejemplo n.º 4
0
    def test_load_dataset_tempfile(self):
        """
        Test DataElement temporary file based context loader.
        """
        # Creating separate element from global so we can mock it up.
        e = DataFileElement(GH_IMAGE_FP, readonly=True)
        e.write_temp = mock.MagicMock(wraps=e.write_temp)
        e.clean_temp = mock.MagicMock(wraps=e.clean_temp)
        e.get_bytes = mock.MagicMock(wraps=e.get_bytes)

        # Using explicit patcher start/stop in order to avoid using ``patch``
        # as a decorator because ``osgeo`` might not be defined when
        # decorating the method.
        patcher_gdal_open = mock.patch('smqtk.algorithms.image_io.gdal_io.gdal'
                                       '.Open', wraps=osgeo.gdal.Open)
        self.addCleanup(patcher_gdal_open.stop)

        m_gdal_open = patcher_gdal_open.start()

        with load_dataset_tempfile(e) as gdal_ds:
            # noinspection PyUnresolvedReferences
            e.write_temp.assert_called_once_with()
            # noinspection PyUnresolvedReferences
            e.get_bytes.assert_not_called()

            m_gdal_open.assert_called_once()

            assert gdal_ds.RasterCount == 3
            assert gdal_ds.RasterXSize == 512
            assert gdal_ds.RasterYSize == 600

        # noinspection PyUnresolvedReferences
        e.clean_temp.assert_called_once_with()
        assert len(e._temp_filepath_stack) == 0
Ejemplo n.º 5
0
    def test_writeTempOverride(self, mock_DataElement_wt):
        # no manual directory, should return the base filepath
        expected_filepath = '/path/to/file.txt'
        d = DataFileElement(expected_filepath)
        fp = d.write_temp()

        self.assertFalse(mock_DataElement_wt.called)
        self.assertEqual(expected_filepath, fp)
Ejemplo n.º 6
0
    def test_writeTempOverride(self, mock_DataElement_wt):
        # no manual directory, should return the base filepath
        expected_filepath = '/path/to/file.txt'
        d = DataFileElement(expected_filepath)
        fp = d.write_temp()

        self.assertFalse(mock_DataElement_wt.called)
        self.assertEqual(expected_filepath, fp)
Ejemplo n.º 7
0
    def test_writeTempOverride_sameDir(self, mock_DataElement_wt):
        expected_filepath = '/path/to/file.txt'
        target_dir = '/path/to'

        d = DataFileElement(expected_filepath)
        fp = d.write_temp(temp_dir=target_dir)

        self.assertFalse(mock_DataElement_wt.called)
        self.assertEqual(fp, expected_filepath)
Ejemplo n.º 8
0
    def test_writeTempOverride_sameDir(self, mock_DataElement_wt):
        expected_filepath = '/path/to/file.txt'
        target_dir = '/path/to'

        d = DataFileElement(expected_filepath)
        fp = d.write_temp(temp_dir=target_dir)

        self.assertFalse(mock_DataElement_wt.called)
        self.assertEqual(fp, expected_filepath)
Ejemplo n.º 9
0
    def test_writeTempOverride_sameDir(self, mock_DataElement_wt):
        expected_filepath = "/path/to/file.txt"
        target_dir = "/path/to"

        d = DataFileElement(expected_filepath)
        fp = d.write_temp(temp_dir=target_dir)

        ntools.assert_false(mock_DataElement_wt.called)
        ntools.assert_equal(fp, expected_filepath)
Ejemplo n.º 10
0
    def test_writeTempOverride_diffDir(self, mock_open, mock_os_open,
                                       mock_os_close, mock_fcntl, mock_scd,
                                       mock_isfile):
        source_filepath = '/path/to/file.png'
        target_dir = '/some/other/dir'

        # If file-magic is available, skip it by getting ``isfile`` to
        # temporarily return false.
        mock_isfile.side_effect = lambda *args: False
        d = DataFileElement(source_filepath)
        fp = d.write_temp(temp_dir=target_dir)

        # Custom side-effect for os.path.isfile for simulated files
        simulate = True

        def osp_isfile_side_effect(path):
            if simulate and path == fp:
                return True
            else:
                return False

        mock_isfile.side_effect = osp_isfile_side_effect

        ntools.assert_not_equal(fp, source_filepath)
        ntools.assert_equal(os.path.dirname(fp), target_dir)

        # subsequent call to write temp should not invoke creation of a new file
        fp2 = d.write_temp()
        ntools.assert_equal(fp2, source_filepath)

        # request in same dir should return same path as first request with that
        # directory
        fp3 = d.write_temp(target_dir)
        ntools.assert_equal(fp, fp3)

        # request different target dir
        target2 = '/even/different/path'
        fp4 = d.write_temp(target2)
        ntools.assert_equal(os.path.dirname(fp4), target2)
        ntools.assert_not_equal(fp, fp4)
        ntools.assert_equal(len(d._temp_filepath_stack), 2)

        # Restore normal os.path.isfile functionality
        simulate = False
Ejemplo n.º 11
0
    def test_writeTempOverride_diffDir(self, mock_open, mock_os_open, mock_os_close, mock_fcntl, mock_scd, mock_isfile):
        source_filepath = "/path/to/file.png"
        target_dir = "/some/other/dir"

        d = DataFileElement(source_filepath)
        fp = d.write_temp(temp_dir=target_dir)

        # Custom side-effect for os.path.isfile for simulated files
        simulate = True

        def osp_isfile_side_effect(path):
            if simulate and path == fp:
                return True
            else:
                return False

        mock_isfile.side_effect = osp_isfile_side_effect

        ntools.assert_not_equal(fp, source_filepath)
        ntools.assert_equal(os.path.dirname(fp), target_dir)

        # subsequent call to write temp should not invoke creation of a new file
        fp2 = d.write_temp()
        ntools.assert_equal(fp2, source_filepath)

        # request in same dir should return same path as first request with that
        # directory
        fp3 = d.write_temp(target_dir)
        ntools.assert_equal(fp, fp3)

        # request different target dir
        target2 = "/even/different/path"
        fp4 = d.write_temp(target2)
        ntools.assert_equal(os.path.dirname(fp4), target2)
        ntools.assert_not_equal(fp, fp4)
        ntools.assert_equal(len(d._temp_filepath_stack), 2)

        # Restore normal os.path.isfile functionality
        simulate = False
Ejemplo n.º 12
0
    def test_load_dataset_vsimem(self):
        """
        Test that VSIMEM loading context
        """
        if LooseVersion(osgeo.__version__).version[0] < 2:
            pytest.skip("Skipping VSIMEM test because GDAL version < 2")

        # Creating separate element from global so we can mock it up.
        e = DataFileElement(GH_IMAGE_FP, readonly=True)
        e.write_temp = mock.MagicMock(wraps=e.write_temp)
        e.clean_temp = mock.MagicMock(wraps=e.clean_temp)
        e.get_bytes = mock.MagicMock(wraps=e.get_bytes)

        vsimem_path_re = re.compile(r'^/vsimem/\w+$')

        # Using explicit patcher start/stop in order to avoid using ``patch``
        # as a *decorator* because ``osgeo`` might not be defined when
        # decorating the method.
        patcher_gdal_open = mock.patch(
            'smqtk.algorithms.image_io.gdal_io.gdal.Open',
            wraps=osgeo.gdal.Open,
        )
        self.addCleanup(patcher_gdal_open.stop)
        patcher_gdal_unlink = mock.patch(
            'smqtk.algorithms.image_io.gdal_io.gdal.Unlink',
            wraps=osgeo.gdal.Unlink,
        )
        self.addCleanup(patcher_gdal_unlink.stop)

        m_gdal_open = patcher_gdal_open.start()
        m_gdal_unlink = patcher_gdal_unlink.start()

        with load_dataset_vsimem(e) as gdal_ds:
            # noinspection PyUnresolvedReferences
            e.write_temp.assert_not_called()
            # noinspection PyUnresolvedReferences
            e.get_bytes.assert_called_once_with()

            m_gdal_open.assert_called_once()
            ds_path = gdal_ds.GetDescription()
            assert vsimem_path_re.match(ds_path)

            assert gdal_ds.RasterCount == 3
            assert gdal_ds.RasterXSize == 512
            assert gdal_ds.RasterYSize == 600

        m_gdal_unlink.assert_called_once_with(ds_path)
        # noinspection PyUnresolvedReferences
        e.clean_temp.assert_not_called()
        assert len(e._temp_filepath_stack) == 0
Ejemplo n.º 13
0
    def test_writeTempOverride_diffDir(self, mock_DataElement_wt):
        """
        Test that adding ``temp_dir`` parameter triggers call to parent class
        """
        source_filepath = '/path/to/file.png'
        target_dir = '/some/other/dir'

        d = DataFileElement(source_filepath)

        # Should call parent class write_temp since target is not the same dir
        # that the source file is in.
        mock_DataElement_wt.return_value = 'expected'
        v = d.write_temp(temp_dir=target_dir)
        self.assertEqual(v, 'expected')
        mock_DataElement_wt.assert_called_with(target_dir)
Ejemplo n.º 14
0
    def test_writeTempOverride_diffDir(self, mock_DataElement_wt):
        """
        Test that adding ``temp_dir`` parameter triggers call to parent class
        """
        source_filepath = '/path/to/file.png'
        target_dir = '/some/other/dir'

        d = DataFileElement(source_filepath)

        # Should call parent class write_temp since target is not the same dir
        # that the source file is in.
        mock_DataElement_wt.return_value = 'expected'
        v = d.write_temp(temp_dir=target_dir)
        self.assertEqual(v, 'expected')
        mock_DataElement_wt.assert_called_with(target_dir)