[segyio] 145/376: Added a TestContext to make it possible to clean up after a test.

Jørgen Kvalsvik jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:23 UTC 2017


This is an automated email from the git hooks/post-receive script.

jokva-guest pushed a commit to branch debian
in repository segyio.

commit fbecab16c32c20be2cf38ba766033b340554f4bd
Author: Jean-Paul Balabanian <jepebe at users.noreply.github.com>
Date:   Mon Dec 5 11:51:10 2016 +0100

    Added a TestContext to make it possible to clean up after a test.
---
 tests/CMakeLists.txt       |   2 +
 tests/test_context.py      |  47 ++++
 tests/test_segy.py         | 562 +++++++++++++++++++++++----------------------
 tests/test_segyio_c.py     | 122 +++++-----
 tests/test_test_context.py |  32 +++
 5 files changed, 428 insertions(+), 337 deletions(-)

diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 37d7aa3..fc72111 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -16,7 +16,9 @@ endif()
 
 
 if(BUILD_PYTHON)
+    configure_file(test_context.py test_context.py COPYONLY)
     add_python_test(python.segy test_segy.py)
     add_python_test(python.h.segy test_segyio_c.py)
     add_python_test(python.enum.segy test_enum.py)
+    add_python_test(python.test_context test_test_context.py)
 endif()
diff --git a/tests/test_context.py b/tests/test_context.py
new file mode 100644
index 0000000..0b4dc0b
--- /dev/null
+++ b/tests/test_context.py
@@ -0,0 +1,47 @@
+import os
+import tempfile
+
+import shutil
+
+import errno
+
+
+class TestContext(object):
+    def __init__(self, name="unnamed", cleanup=True):
+        super(TestContext, self).__init__()
+        self._name = name
+        self._cwd = os.getcwd()
+        self._cleanup = cleanup
+
+    def __enter__(self):
+        temp_path = tempfile.mkdtemp("_%s" % self._name)
+        os.chdir(temp_path)
+        self._temp_path = os.getcwd()
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        os.chdir(self._cwd)
+        if self._cleanup:
+            shutil.rmtree(self._temp_path)
+
+    def copy_file(self, source, target="."):
+        if not os.path.isabs(source):
+            source = os.path.join(self._cwd, source)
+
+        source_dir, source_filename = os.path.split(source)
+
+        target = os.path.join(self._temp_path, target, source_filename)
+        try:
+            os.makedirs(os.path.dirname(target))
+        except OSError, e:
+            if e.errno != errno.EEXIST:
+                raise
+        shutil.copyfile(source, target)
+
+    @property
+    def temp_path(self):
+        return self._temp_path
+
+    @property
+    def cwd(self):
+        return self._cwd
\ No newline at end of file
diff --git a/tests/test_segy.py b/tests/test_segy.py
index 0eeaa8c..1b8221c 100644
--- a/tests/test_segy.py
+++ b/tests/test_segy.py
@@ -3,9 +3,10 @@ from types import GeneratorType
 import itertools
 import numpy as np
 from unittest import TestCase
+from test_context import TestContext
+
 import segyio
 from segyio import TraceField, BinField
-import shutil
 import filecmp
 
 from segyio._field import Field
@@ -167,32 +168,32 @@ class TestSegy(TestCase):
             self.assertEqual(50, f.samples)
 
     def test_dt_fallback(self):
-        f_name = self.filename.replace( ".sgy", "_dt_test.sgy")
-        shutil.copyfile(self.filename, f_name)
-        with segyio.open(f_name, "r+") as f:
-            # Both zero
-            f.bin[BinField.Interval] = 0
-            f.header[0][TraceField.TRACE_SAMPLE_INTERVAL] = 0
-            f.flush()
-            fallback_dt = 4
-            np.testing.assert_almost_equal(segyio.dt(f, fallback_dt), fallback_dt)
-
-            # dt in bin header different from first trace
-            f.bin[BinField.Interval] = 6000
-            f.header[0][TraceField.TRACE_SAMPLE_INTERVAL] = 1000
-            f.flush()
-            fallback_dt = 4
-            np.testing.assert_almost_equal(segyio.dt(f, fallback_dt), fallback_dt)
+        with TestContext("dt_fallback") as context:
+            context.copy_file(self.filename)
+            with segyio.open("small.sgy", "r+") as f:
+                # Both zero
+                f.bin[BinField.Interval] = 0
+                f.header[0][TraceField.TRACE_SAMPLE_INTERVAL] = 0
+                f.flush()
+                fallback_dt = 4
+                np.testing.assert_almost_equal(segyio.dt(f, fallback_dt), fallback_dt)
+
+                # dt in bin header different from first trace
+                f.bin[BinField.Interval] = 6000
+                f.header[0][TraceField.TRACE_SAMPLE_INTERVAL] = 1000
+                f.flush()
+                fallback_dt = 4
+                np.testing.assert_almost_equal(segyio.dt(f, fallback_dt), fallback_dt)
 
     def test_dt_no_fallback(self):
-        f_name = self.filename.replace( ".sgy", "_dt_test.sgy")
-        shutil.copyfile(self.filename, f_name)
-        dt_us = 6000
-        with segyio.open(f_name, "r+") as f:
-            f.bin[BinField.Interval] = dt_us
-            f.header[0][TraceField.TRACE_SAMPLE_INTERVAL] = dt_us
-            f.flush()
-            np.testing.assert_almost_equal(segyio.dt(f), dt_us/1000)
+        with TestContext("dt_no_fallback") as context:
+            context.copy_file(self.filename)
+            dt_us = 6000
+            with segyio.open("small.sgy", "r+") as f:
+                f.bin[BinField.Interval] = dt_us
+                f.header[0][TraceField.TRACE_SAMPLE_INTERVAL] = dt_us
+                f.flush()
+                np.testing.assert_almost_equal(segyio.dt(f), dt_us/1000)
 
     def test_traces_slicing(self):
         with segyio.open(self.filename, "r") as f:
@@ -239,23 +240,21 @@ class TestSegy(TestCase):
             self.assertNotEqual(f.header[1][xl], f.header[2][xl])
 
     def test_headers_line_offset(self):
-        fname = self.prestack.replace( ".sgy", "-line-header.sgy")
-        shutil.copyfile(self.prestack, fname)
-
-        il, xl = TraceField.INLINE_3D, TraceField.CROSSLINE_3D
-        with segyio.open(fname, "r+") as f:
-            f.header.iline[1,2] = { il: 11 }
-            f.header.iline[1,2] = { xl: 13 }
-
-        with segyio.open(fname, "r") as f:
-            self.assertEqual(f.header[0][il], 1)
-            self.assertEqual(f.header[1][il], 11)
-            self.assertEqual(f.header[2][il], 1)
+        with TestContext("headers_line_offset") as context:
+            context.copy_file(self.prestack)
+            il, xl = TraceField.INLINE_3D, TraceField.CROSSLINE_3D
+            with segyio.open("small-ps.sgy", "r+") as f:
+                f.header.iline[1,2] = { il: 11 }
+                f.header.iline[1,2] = { xl: 13 }
 
-            self.assertEqual(f.header[0][xl], 1)
-            self.assertEqual(f.header[1][xl], 13)
-            self.assertEqual(f.header[2][xl], 2)
+            with segyio.open("small-ps.sgy", "r") as f:
+                self.assertEqual(f.header[0][il], 1)
+                self.assertEqual(f.header[1][il], 11)
+                self.assertEqual(f.header[2][il], 1)
 
+                self.assertEqual(f.header[0][xl], 1)
+                self.assertEqual(f.header[1][xl], 13)
+                self.assertEqual(f.header[2][xl], 2)
 
     def test_iline_offset(self):
         with segyio.open(self.prestack, "r") as f:
@@ -380,96 +379,95 @@ class TestSegy(TestCase):
                 f.header[0][700]
 
     def test_write_header(self):
-        fname = self.filename + "write-header"
-        shutil.copyfile(self.filename, fname)
-        with segyio.open(fname, "r+") as f:
-            # assign to a field in a header, write immediately
-            f.header[0][189] = 42
-            f.flush()
+        with TestContext("write_header") as context:
+            context.copy_file(self.filename)
+            with segyio.open("small.sgy", "r+") as f:
+                # assign to a field in a header, write immediately
+                f.header[0][189] = 42
+                f.flush()
 
-            self.assertEqual(42, f.header[0][189])
-            self.assertEqual(1, f.header[1][189])
+                self.assertEqual(42, f.header[0][189])
+                self.assertEqual(1, f.header[1][189])
 
-            # accessing non-existing offsets raises exceptions
-            with self.assertRaises(IndexError):
-                f.header[0][188] = 1 # between byte offsets
+                # accessing non-existing offsets raises exceptions
+                with self.assertRaises(IndexError):
+                    f.header[0][188] = 1 # between byte offsets
 
-            with self.assertRaises(IndexError):
-                f.header[0][-1] = 1
+                with self.assertRaises(IndexError):
+                    f.header[0][-1] = 1
 
-            with self.assertRaises(IndexError):
-                f.header[0][700] = 1
+                with self.assertRaises(IndexError):
+                    f.header[0][700] = 1
 
-            d = { TraceField.INLINE_3D: 43,
-                  TraceField.CROSSLINE_3D: 11,
-                  TraceField.offset: 15 }
+                d = { TraceField.INLINE_3D: 43,
+                      TraceField.CROSSLINE_3D: 11,
+                      TraceField.offset: 15 }
 
-            # assign multiple fields at once by using a dict
-            f.header[1] = d
+                # assign multiple fields at once by using a dict
+                f.header[1] = d
 
-            f.flush()
-            self.assertEqual(43, f.header[1][TraceField.INLINE_3D])
-            self.assertEqual(11, f.header[1][TraceField.CROSSLINE_3D])
-            self.assertEqual(15, f.header[1][TraceField.offset])
+                f.flush()
+                self.assertEqual(43, f.header[1][TraceField.INLINE_3D])
+                self.assertEqual(11, f.header[1][TraceField.CROSSLINE_3D])
+                self.assertEqual(15, f.header[1][TraceField.offset])
 
-            # looking up multiple values at once returns a { TraceField: value } dict
-            self.assertEqual(d, f.header[1][TraceField.INLINE_3D, TraceField.CROSSLINE_3D, TraceField.offset])
+                # looking up multiple values at once returns a { TraceField: value } dict
+                self.assertEqual(d, f.header[1][TraceField.INLINE_3D, TraceField.CROSSLINE_3D, TraceField.offset])
 
-            # slice-support over headers (similar to trace)
-            for th in f.header[0:10]:
-                pass
+                # slice-support over headers (similar to trace)
+                for th in f.header[0:10]:
+                    pass
 
-            self.assertEqual(6, len(list(f.header[10::-2])))
-            self.assertEqual(5, len(list(f.header[10:5:-1])))
-            self.assertEqual(0, len(list(f.header[10:5])))
+                self.assertEqual(6, len(list(f.header[10::-2])))
+                self.assertEqual(5, len(list(f.header[10:5:-1])))
+                self.assertEqual(0, len(list(f.header[10:5])))
 
-            # for-each support
-            for th in f.header:
-                pass
+                # for-each support
+                for th in f.header:
+                    pass
 
-            # copy a header
-            f.header[2] = f.header[1]
-            f.flush()
+                # copy a header
+                f.header[2] = f.header[1]
+                f.flush()
 
-            # don't use this interface in production code, it's only for testing
-            # i.e. don't access buf of treat it as a list
-            #self.assertEqual(list(f.header[2].buf), list(f.header[1].buf))
+                # don't use this interface in production code, it's only for testing
+                # i.e. don't access buf of treat it as a list
+                #self.assertEqual(list(f.header[2].buf), list(f.header[1].buf))
 
     def test_write_binary(self):
-        fname = self.filename.replace( ".sgy", "-binary.sgy")
-        shutil.copyfile(self.filename, fname)
+        with TestContext("write_binary") as context:
+            context.copy_file(self.filename)
+            with segyio.open("small.sgy", "r+") as f:
+                f.bin[3213] = 5
+                f.flush()
 
-        with segyio.open(fname, "r+") as f:
-            f.bin[3213] = 5
-            f.flush()
+                self.assertEqual(5, f.bin[3213])
 
-            self.assertEqual(5, f.bin[3213])
+                # accessing non-existing offsets raises exceptions
+                with self.assertRaises(IndexError):
+                    f.bin[0]
 
-            # accessing non-existing offsets raises exceptions
-            with self.assertRaises(IndexError):
-                f.bin[0]
+                with self.assertRaises(IndexError):
+                    f.bin[50000]
 
-            with self.assertRaises(IndexError):
-                f.bin[50000]
+                with self.assertRaises(IndexError):
+                    f.bin[3214]
 
-            with self.assertRaises(IndexError):
-                f.bin[3214]
-
-            d = { BinField.Traces: 43,
-                  BinField.SweepFrequencyStart: 11 }
+                d = { BinField.Traces: 43,
+                      BinField.SweepFrequencyStart: 11 }
 
-            # assign multiple fields at once by using a dict
-            f.bin = d
+                # assign multiple fields at once by using a dict
+                f.bin = d
 
-            f.flush()
-            self.assertEqual(43, f.bin[BinField.Traces])
-            self.assertEqual(11, f.bin[BinField.SweepFrequencyStart])
+                f.flush()
+                self.assertEqual(43, f.bin[BinField.Traces])
+                self.assertEqual(11, f.bin[BinField.SweepFrequencyStart])
 
-            # looking up multiple values at once returns a { TraceField: value } dict
-            self.assertEqual(d, f.bin[BinField.Traces, BinField.SweepFrequencyStart])
+                # looking up multiple values at once returns a { TraceField: value } dict
+                self.assertEqual(d, f.bin[BinField.Traces, BinField.SweepFrequencyStart])
 
-            # copy a header
-            f.bin = f.bin
+                # copy a header
+                f.bin = f.bin
 
     def test_fopen_error(self):
         # non-existent file
@@ -500,189 +498,198 @@ class TestSegy(TestCase):
                 pass
 
     def test_create_sgy(self):
-        dstfile = self.filename.replace(".sgy", "-created.sgy")
-
-        with segyio.open(self.filename, "r") as src:
-            spec = segyio.spec()
-            spec.format     = int(src.format)
-            spec.sorting    = int(src.sorting)
-            spec.samples    = src.samples
-            spec.ilines     = src.ilines
-            spec.xlines     = src.xlines
+        with TestContext("create_sgy") as context:
+            context.copy_file(self.filename)
+            src_file = "small.sgy"
+            dst_file = "small_created.sgy"
+            with segyio.open(src_file, "r") as src:
+                spec = segyio.spec()
+                spec.format     = int(src.format)
+                spec.sorting    = int(src.sorting)
+                spec.samples    = src.samples
+                spec.ilines     = src.ilines
+                spec.xlines     = src.xlines
 
-            with segyio.create(dstfile, spec) as dst:
-                dst.text[0] = src.text[0]
-                dst.bin     = src.bin
+                with segyio.create(dst_file, spec) as dst:
+                    dst.text[0] = src.text[0]
+                    dst.bin     = src.bin
 
-                # copy all headers
-                dst.header = src.header
+                    # copy all headers
+                    dst.header = src.header
 
-                for i, srctr in enumerate(src.trace):
-                    dst.trace[i] = srctr
+                    for i, srctr in enumerate(src.trace):
+                        dst.trace[i] = srctr
 
-                dst.trace = src.trace
+                    dst.trace = src.trace
 
-                # this doesn't work yet, some restructuring is necessary
-                # if it turns out to be a desired feature it's rather easy to do
-                #for dsth, srch in zip(dst.header, src.header):
-                #    dsth = srch
+                    # this doesn't work yet, some restructuring is necessary
+                    # if it turns out to be a desired feature it's rather easy to do
+                    #for dsth, srch in zip(dst.header, src.header):
+                    #    dsth = srch
 
-                #for dsttr, srctr in zip(dst.trace, src.trace):
-                #    dsttr = srctr
+                    #for dsttr, srctr in zip(dst.trace, src.trace):
+                    #    dsttr = srctr
 
-        self.assertTrue(filecmp.cmp(self.filename, dstfile))
+            self.assertTrue(filecmp.cmp(src_file, dst_file))
 
     def test_create_sgy_shorter_traces(self):
-        dstfile = self.filename.replace(".sgy", "-shorter.sgy")
+        with TestContext("create_sgy_shorter_traces") as context:
+            context.copy_file(self.filename)
+            src_file = "small.sgy"
+            dst_file = "small_created_shorter.sgy"
+
+            with segyio.open(src_file, "r") as src:
+                spec = segyio.spec()
+                spec.format  = int(src.format)
+                spec.sorting = int(src.sorting)
+                spec.samples = 20 # reduces samples per trace
+                spec.ilines  = src.ilines
+                spec.xlines  = src.xlines
+
+                with segyio.create(dst_file, spec) as dst:
+                    for i, srch in enumerate(src.header):
+                        dst.header[i] = srch
+                        d = { TraceField.INLINE_3D: srch[TraceField.INLINE_3D] + 100 }
+                        dst.header[i] = d
+
+                    for lineno in dst.ilines:
+                        dst.iline[lineno] = src.iline[lineno]
+
+                    # alternative form using left-hand-side slices
+                    dst.iline[2:4] = src.iline
+
+                    for lineno in dst.xlines:
+                        dst.xline[lineno] = src.xline[lineno]
+
+                with segyio.open(dst_file, "r") as dst:
+                    self.assertEqual(20, dst.samples)
+                    self.assertEqual([x + 100 for x in src.ilines], list(dst.ilines))
 
-        with segyio.open(self.filename, "r") as src:
+    def test_create_from_naught(self):
+        with TestContext("create_from_naught") as context:
+            fname = "mk.sgy"
             spec = segyio.spec()
-            spec.format     = int(src.format)
-            spec.sorting    = int(src.sorting)
-            spec.samples    = 20 # reduces samples per trace
-            spec.ilines     = src.ilines
-            spec.xlines     = src.xlines
+            spec.format  = 5
+            spec.sorting = 2
+            spec.samples = 150
+            spec.ilines  = range(1, 11)
+            spec.xlines  = range(1, 6)
 
-            with segyio.create(dstfile, spec) as dst:
-                for i, srch in enumerate(src.header):
-                    dst.header[i] = srch
-                    d = { TraceField.INLINE_3D: srch[TraceField.INLINE_3D] + 100 }
-                    dst.header[i] = d
+            with segyio.create(fname, spec) as dst:
+                tr = np.arange( start = 1.000, stop = 1.151, step = 0.001, dtype = np.single)
 
-                for lineno in dst.ilines:
-                    dst.iline[lineno] = src.iline[lineno]
+                for i in range( len( dst.trace ) ):
+                    dst.trace[i] = tr
+                    tr += 1.000
 
-                # alternative form using left-hand-side slices
-                dst.iline[2:4] = src.iline
+                for il in spec.ilines:
+                    dst.header.iline[il] = { TraceField.INLINE_3D: il }
 
-                for lineno in dst.xlines:
-                    dst.xline[lineno] = src.xline[lineno]
+                for xl in spec.xlines:
+                    dst.header.xline[xl] = { TraceField.CROSSLINE_3D: xl }
 
-            with segyio.open(dstfile, "r") as dst:
-                self.assertEqual(20, dst.samples)
-                self.assertEqual([x + 100 for x in src.ilines], list(dst.ilines))
+                # Set header field 'offset' to 1 in all headers
+                dst.header = { TraceField.offset: 1 }
 
-    def test_create_from_naught(self):
-        fname = "test-data/mk.sgy"
-        spec = segyio.spec()
-        spec.format  = 5
-        spec.sorting = 2
-        spec.samples = 150
-        spec.ilines  = range(1, 11)
-        spec.xlines  = range(1, 6)
-
-        with segyio.create(fname, spec) as dst:
-            tr = np.arange( start = 1.000, stop = 1.151, step = 0.001, dtype = np.single)
-
-            for i in range( len( dst.trace ) ):
-                dst.trace[i] = tr
-                tr += 1.000
-
-            for il in spec.ilines:
-                dst.header.iline[il] = { TraceField.INLINE_3D: il }
-
-            for xl in spec.xlines:
-                dst.header.xline[xl] = { TraceField.CROSSLINE_3D: xl }
-
-            # Set header field 'offset' to 1 in all headers
-            dst.header = { TraceField.offset: 1 }
-
-        with segyio.open(fname, "r") as f:
-            self.assertAlmostEqual(1,      f.trace[0][0],   places = 4)
-            self.assertAlmostEqual(1.001,  f.trace[0][1],   places = 4)
-            self.assertAlmostEqual(1.149,  f.trace[0][-1],   places = 4)
-            self.assertAlmostEqual(50.100, f.trace[-1][100], places = 4)
-            self.assertEqual(f.header[0][TraceField.offset], f.header[1][TraceField.offset])
-            self.assertEqual(1, f.header[1][TraceField.offset])
+            with segyio.open(fname, "r") as f:
+                self.assertAlmostEqual(1,      f.trace[0][0],   places = 4)
+                self.assertAlmostEqual(1.001,  f.trace[0][1],   places = 4)
+                self.assertAlmostEqual(1.149,  f.trace[0][-1],   places = 4)
+                self.assertAlmostEqual(50.100, f.trace[-1][100], places = 4)
+                self.assertEqual(f.header[0][TraceField.offset], f.header[1][TraceField.offset])
+                self.assertEqual(1, f.header[1][TraceField.offset])
 
     def test_create_from_naught_prestack(self):
-        fname = "test-data/mk-ps.sgy"
-        spec = segyio.spec()
-        spec.format  = 5
-        spec.sorting = 2
-        spec.samples = 7
-        spec.ilines  = range(1, 4)
-        spec.xlines  = range(1, 3)
-        spec.offsets = range(1, 6)
-
-        cube_size = len(spec.ilines) * len(spec.xlines)
-
-        with segyio.create(fname, spec) as dst:
-            arr = np.arange( start = 0.000,
-                             stop = 0.007,
-                             step = 0.001,
-                             dtype = np.single)
-
-            arr = np.concatenate([[arr + 0.01], [arr + 0.02]], axis = 0)
-            lines = [arr + i for i in spec.ilines]
-            cube = [(off * 100) + line for line in lines for off in spec.offsets]
-
-            dst.iline[:,:] = cube
-
-            for of in spec.offsets:
-                for il in spec.ilines:
-                    dst.header.iline[il,of] = { TraceField.INLINE_3D: il,
-                                                TraceField.offset: of
-                                              }
-                for xl in spec.xlines:
-                    dst.header.xline[xl,of] = { TraceField.CROSSLINE_3D: xl }
-
-        with segyio.open(fname, "r") as f:
-            self.assertAlmostEqual(101.010, f.trace[0][0],  places = 4)
-            self.assertAlmostEqual(101.011, f.trace[0][1],  places = 4)
-            self.assertAlmostEqual(101.016, f.trace[0][-1], places = 4)
-            self.assertAlmostEqual(503.025, f.trace[-1][5], places = 4)
-            self.assertNotEqual(f.header[0][TraceField.offset], f.header[1][TraceField.offset])
-            self.assertEqual(1, f.header[0][TraceField.offset])
-            self.assertEqual(2, f.header[1][TraceField.offset])
-
-            for x, y in zip(f.iline[:,:], cube):
-                self.assertListEqual(list(x.flatten()), list(y.flatten()))
+        with TestContext("create_from_naught_prestack") as context:
+            fname = "mk-ps.sgy"
+            spec = segyio.spec()
+            spec.format  = 5
+            spec.sorting = 2
+            spec.samples = 7
+            spec.ilines  = range(1, 4)
+            spec.xlines  = range(1, 3)
+            spec.offsets = range(1, 6)
+
+            cube_size = len(spec.ilines) * len(spec.xlines)
+
+            with segyio.create(fname, spec) as dst:
+                arr = np.arange( start = 0.000,
+                                 stop = 0.007,
+                                 step = 0.001,
+                                 dtype = np.single)
+
+                arr = np.concatenate([[arr + 0.01], [arr + 0.02]], axis = 0)
+                lines = [arr + i for i in spec.ilines]
+                cube = [(off * 100) + line for line in lines for off in spec.offsets]
+
+                dst.iline[:,:] = cube
+
+                for of in spec.offsets:
+                    for il in spec.ilines:
+                        dst.header.iline[il,of] = { TraceField.INLINE_3D: il,
+                                                    TraceField.offset: of
+                                                  }
+                    for xl in spec.xlines:
+                        dst.header.xline[xl,of] = { TraceField.CROSSLINE_3D: xl }
+
+            with segyio.open(fname, "r") as f:
+                self.assertAlmostEqual(101.010, f.trace[0][0],  places = 4)
+                self.assertAlmostEqual(101.011, f.trace[0][1],  places = 4)
+                self.assertAlmostEqual(101.016, f.trace[0][-1], places = 4)
+                self.assertAlmostEqual(503.025, f.trace[-1][5], places = 4)
+                self.assertNotEqual(f.header[0][TraceField.offset], f.header[1][TraceField.offset])
+                self.assertEqual(1, f.header[0][TraceField.offset])
+                self.assertEqual(2, f.header[1][TraceField.offset])
+
+                for x, y in zip(f.iline[:,:], cube):
+                    self.assertListEqual(list(x.flatten()), list(y.flatten()))
 
     def test_create_write_lines(self):
-        fname = "test-data/mklines.sgy"
+        fname = "mklines.sgy"
 
-        mklines(fname)
+        with TestContext("create_write_lines") as context:
+            mklines(fname)
 
-        with segyio.open(fname, "r") as f:
-            self.assertAlmostEqual(1,     f.iline[1][0][0], places = 4)
-            self.assertAlmostEqual(2.004, f.iline[2][0][4], places = 4)
-            self.assertAlmostEqual(2.014, f.iline[2][1][4], places = 4)
-            self.assertAlmostEqual(8.043, f.iline[8][4][3], places = 4)
+            with segyio.open(fname, "r") as f:
+                self.assertAlmostEqual(1,     f.iline[1][0][0], places = 4)
+                self.assertAlmostEqual(2.004, f.iline[2][0][4], places = 4)
+                self.assertAlmostEqual(2.014, f.iline[2][1][4], places = 4)
+                self.assertAlmostEqual(8.043, f.iline[8][4][3], places = 4)
 
     def test_create_sgy_skip_lines(self):
-        fname = "test-data/lines.sgy"
-        dstfile = fname.replace(".sgy", "-halved.sgy")
-
-        mklines(fname)
-
-        with segyio.open(fname, "r") as src:
-            spec = segyio.spec()
-            spec.format     = int(src.format)
-            spec.sorting    = int(src.sorting)
-            spec.samples    = src.samples
-            spec.ilines     = src.ilines[::2]
-            spec.xlines     = src.xlines[::2]
-
-            with segyio.create(dstfile, spec) as dst:
-                # use the inline headers as base
-                dst.header.iline = src.header.iline[::2]
-                # then update crossline numbers from the crossline headers
-                for xl in dst.xlines:
-                    f = next(src.header.xline[xl])[TraceField.CROSSLINE_3D]
-                    dst.header.xline[xl] = { TraceField.CROSSLINE_3D: f }
-
-                # but we override the last xline to be 6, not 5
-                dst.header.xline[5] = { TraceField.CROSSLINE_3D: 6 }
-                dst.iline = src.iline[::2]
-
-        with segyio.open(dstfile, "r") as f:
-            self.assertListEqual(list(f.ilines), list(spec.ilines))
-            self.assertListEqual(list(f.xlines), [1, 3, 6])
-            self.assertAlmostEqual(1,     f.iline[1][0][0], places = 4)
-            self.assertAlmostEqual(3.004, f.iline[3][0][4], places = 4)
-            self.assertAlmostEqual(3.014, f.iline[3][1][4], places = 4)
-            self.assertAlmostEqual(7.023, f.iline[7][2][3], places = 4)
+        fname = "lines.sgy"
+        dstfile = "lines-halved.sgy"
+
+        with TestContext("create_sgy_skip_lines") as context:
+            mklines(fname)
+
+            with segyio.open(fname, "r") as src:
+                spec = segyio.spec()
+                spec.format     = int(src.format)
+                spec.sorting    = int(src.sorting)
+                spec.samples    = src.samples
+                spec.ilines     = src.ilines[::2]
+                spec.xlines     = src.xlines[::2]
+
+                with segyio.create(dstfile, spec) as dst:
+                    # use the inline headers as base
+                    dst.header.iline = src.header.iline[::2]
+                    # then update crossline numbers from the crossline headers
+                    for xl in dst.xlines:
+                        f = next(src.header.xline[xl])[TraceField.CROSSLINE_3D]
+                        dst.header.xline[xl] = { TraceField.CROSSLINE_3D: f }
+
+                    # but we override the last xline to be 6, not 5
+                    dst.header.xline[5] = { TraceField.CROSSLINE_3D: 6 }
+                    dst.iline = src.iline[::2]
+
+            with segyio.open(dstfile, "r") as f:
+                self.assertListEqual(list(f.ilines), list(spec.ilines))
+                self.assertListEqual(list(f.xlines), [1, 3, 6])
+                self.assertAlmostEqual(1,     f.iline[1][0][0], places = 4)
+                self.assertAlmostEqual(3.004, f.iline[3][0][4], places = 4)
+                self.assertAlmostEqual(3.014, f.iline[3][1][4], places = 4)
+                self.assertAlmostEqual(7.023, f.iline[7][2][3], places = 4)
 
     def test_segyio_types(self):
         with segyio.open(self.filename, "r") as f:
@@ -752,21 +759,22 @@ class TestSegy(TestCase):
             slice = f.depth_slice[f.samples]
 
     def test_depth_slice_writing(self):
-        fname = self.filename.replace("small", "small-depth")
-        shutil.copyfile(self.filename, fname)
+        with TestContext("depth_slice_writing") as context:
+            context.copy_file(self.filename)
+            fname = ("small.sgy")
 
-        buf = np.empty(shape=(5, 5), dtype=np.single)
+            buf = np.empty(shape=(5, 5), dtype=np.single)
 
-        def value(x, y):
-            return x + (1.0 // 5) * y
+            def value(x, y):
+                return x + (1.0 // 5) * y
 
-        for x, y in itertools.product(range(5), range(5)):
-            buf[x][y] = value(x, y)
+            for x, y in itertools.product(range(5), range(5)):
+                buf[x][y] = value(x, y)
 
-        with segyio.open(fname, "r+") as f:
-            f.depth_slice[7] = buf * 3.14 # assign to depth 7
-            self.assertTrue(np.allclose(f.depth_slice[7], buf * 3.14))
+            with segyio.open(fname, "r+") as f:
+                f.depth_slice[7] = buf * 3.14 # assign to depth 7
+                self.assertTrue(np.allclose(f.depth_slice[7], buf * 3.14))
 
-            f.depth_slice = [buf * i for i in range(len(f.depth_slice))]  # assign to all depths
-            for index, depth_slice in enumerate(f.depth_slice):
-                self.assertTrue(np.allclose(depth_slice, buf * index))
+                f.depth_slice = [buf * i for i in range(len(f.depth_slice))]  # assign to all depths
+                for index, depth_slice in enumerate(f.depth_slice):
+                    self.assertTrue(np.allclose(depth_slice, buf * index))
diff --git a/tests/test_segyio_c.py b/tests/test_segyio_c.py
index 6882218..14c067a 100644
--- a/tests/test_segyio_c.py
+++ b/tests/test_segyio_c.py
@@ -1,8 +1,8 @@
-import shutil
 from unittest import TestCase
 
 import numpy
 import segyio._segyio as _segyio
+from test_context import TestContext
 
 ACTUAL_TEXT_HEADER = b"C 1 DATE: 2016-09-19                                                            " \
                      b"C 2 AN INCREASE IN AMPLITUDE EQUALS AN INCREASE IN ACOUSTIC IMPEDANCE           " \
@@ -89,22 +89,23 @@ class _segyioTests(TestCase):
         _segyio.close(f)
 
     def test_write_text_header(self):
-        fname = self.filename.replace("small", "txt_hdr_wrt")
-        shutil.copyfile(self.filename, fname)
-        f = _segyio.open(fname, "r+")
+        with TestContext("write_text_header") as context:
+            context.copy_file(self.filename)
 
-        with self.assertRaises(ValueError):
-            _segyio.write_textheader(f, 0, "")
+            f = _segyio.open("small.sgy", "r+")
 
-        self.assertEqual(_segyio.read_textheader(f, 0), ACTUAL_TEXT_HEADER)
+            with self.assertRaises(ValueError):
+                _segyio.write_textheader(f, 0, "")
 
-        _segyio.write_textheader(f, 0, "yolo" * 800)
+            self.assertEqual(_segyio.read_textheader(f, 0), ACTUAL_TEXT_HEADER)
 
-        textheader = _segyio.read_textheader(f, 0)
-        textheader = textheader.decode('ascii')  # Because in Python 3.5 bytes are not comparable to strings
-        self.assertEqual(textheader, "yolo" * 800)
+            _segyio.write_textheader(f, 0, "yolo" * 800)
 
-        _segyio.close(f)
+            textheader = _segyio.read_textheader(f, 0)
+            textheader = textheader.decode('ascii')  # Because in Python 3.5 bytes are not comparable to strings
+            self.assertEqual(textheader, "yolo" * 800)
+
+            _segyio.close(f)
 
     def test_read_and_write_binary_header(self):
         with self.assertRaises(Exception):
@@ -113,18 +114,18 @@ class _segyioTests(TestCase):
         with self.assertRaises(Exception):
             _segyio.write_binaryheader(None, None)
 
-        fname = self.filename.replace("small", "bin_hdr_wrt")
-        shutil.copyfile(self.filename, fname)
-        f = _segyio.open(fname, "r+")
+        with TestContext("read_and_write_bin_header") as context:
+            context.copy_file(self.filename)
 
-        binary_header = _segyio.read_binaryheader(f)
+            f = _segyio.open("small.sgy", "r+")
 
-        with self.assertRaises(Exception):
-            _segyio.write_binaryheader(f, "Not the correct type")
+            binary_header = _segyio.read_binaryheader(f)
 
-        _segyio.write_binaryheader(f, binary_header)
-        _segyio.close(f)
+            with self.assertRaises(Exception):
+                _segyio.write_binaryheader(f, "Not the correct type")
 
+            _segyio.write_binaryheader(f, binary_header)
+            _segyio.close(f)
 
     def test_read_binary_header_fields(self):
         f = _segyio.open(self.filename, "r")
@@ -179,7 +180,6 @@ class _segyioTests(TestCase):
         self.assertEqual(metrics['iline_length'], 5)
         self.assertEqual(metrics['iline_stride'], 1)
 
-
     def test_metrics(self):
         f = _segyio.open(self.filename, "r")
         binary_header = _segyio.read_binaryheader(f)
@@ -336,67 +336,69 @@ class _segyioTests(TestCase):
         self.assertEqual(_segyio.get_field(hdr, 9), 19)
 
     def test_read_and_write_traceheader(self):
-        fname = self.filename.replace("small", "bin_hdr_wrt")
-        shutil.copyfile(self.filename, fname)
-        f = _segyio.open(fname, "r+")
-        binary_header = _segyio.read_binaryheader(f)
-        ilb = 189
-        xlb = 193
-        metrics = _segyio.init_metrics(f, binary_header, ilb, xlb)
+        with TestContext("read_and_write_trace_header") as context:
+            context.copy_file(self.filename)
 
-        empty = _segyio.empty_traceheader()
+            f = _segyio.open("small.sgy", "r+")
+            binary_header = _segyio.read_binaryheader(f)
+            ilb = 189
+            xlb = 193
+            metrics = _segyio.init_metrics(f, binary_header, ilb, xlb)
 
-        with self.assertRaises(TypeError):
-            trace_header = _segyio.read_traceheader("+", )
+            empty = _segyio.empty_traceheader()
 
-        with self.assertRaises(TypeError):
-            trace_header = _segyio.read_traceheader(f, 0, None)
+            with self.assertRaises(TypeError):
+                trace_header = _segyio.read_traceheader("+", )
 
-        trace_header = _segyio.read_traceheader(f, 0, _segyio.empty_traceheader(), metrics['trace0'], metrics['trace_bsize'])
+            with self.assertRaises(TypeError):
+                trace_header = _segyio.read_traceheader(f, 0, None)
 
-        self.assertEqual(_segyio.get_field(trace_header, ilb), 1)
-        self.assertEqual(_segyio.get_field(trace_header, xlb), 20)
+            trace_header = _segyio.read_traceheader(f, 0, _segyio.empty_traceheader(), metrics['trace0'], metrics['trace_bsize'])
 
-        trace_header = _segyio.read_traceheader(f, 1, _segyio.empty_traceheader(), metrics['trace0'], metrics['trace_bsize'])
+            self.assertEqual(_segyio.get_field(trace_header, ilb), 1)
+            self.assertEqual(_segyio.get_field(trace_header, xlb), 20)
 
-        self.assertEqual(_segyio.get_field(trace_header, ilb), 1)
-        self.assertEqual(_segyio.get_field(trace_header, xlb), 21)
+            trace_header = _segyio.read_traceheader(f, 1, _segyio.empty_traceheader(), metrics['trace0'], metrics['trace_bsize'])
 
-        _segyio.set_field(trace_header, ilb, 99)
-        _segyio.set_field(trace_header, xlb, 42)
+            self.assertEqual(_segyio.get_field(trace_header, ilb), 1)
+            self.assertEqual(_segyio.get_field(trace_header, xlb), 21)
 
-        _segyio.write_traceheader(f, 0, trace_header, metrics['trace0'], metrics['trace_bsize'])
+            _segyio.set_field(trace_header, ilb, 99)
+            _segyio.set_field(trace_header, xlb, 42)
 
-        trace_header = _segyio.read_traceheader(f, 0, _segyio.empty_traceheader(), metrics['trace0'], metrics['trace_bsize'])
+            _segyio.write_traceheader(f, 0, trace_header, metrics['trace0'], metrics['trace_bsize'])
 
-        self.assertEqual(_segyio.get_field(trace_header, ilb), 99)
-        self.assertEqual(_segyio.get_field(trace_header, xlb), 42)
+            trace_header = _segyio.read_traceheader(f, 0, _segyio.empty_traceheader(), metrics['trace0'], metrics['trace_bsize'])
 
-        _segyio.close(f)
+            self.assertEqual(_segyio.get_field(trace_header, ilb), 99)
+            self.assertEqual(_segyio.get_field(trace_header, xlb), 42)
+
+            _segyio.close(f)
 
     def test_read_and_write_trace(self):
-        f = _segyio.open("test-data/trace-wrt.sgy", "w+")
+        with TestContext("read_and_write_trace") as context:
+            f = _segyio.open("trace-wrt.sgy", "w+")
 
-        buf = numpy.ones(25, dtype=numpy.single)
-        buf[11] = 3.1415
-        _segyio.write_trace(f, 0, buf, 0, 100, 1, 25)
-        buf[:] = 42.0
-        _segyio.write_trace(f, 1, buf, 0, 100, 1, 25)
+            buf = numpy.ones(25, dtype=numpy.single)
+            buf[11] = 3.1415
+            _segyio.write_trace(f, 0, buf, 0, 100, 1, 25)
+            buf[:] = 42.0
+            _segyio.write_trace(f, 1, buf, 0, 100, 1, 25)
 
-        _segyio.flush(f)
+            _segyio.flush(f)
 
-        buf = numpy.zeros(25, dtype=numpy.single)
+            buf = numpy.zeros(25, dtype=numpy.single)
 
-        _segyio.read_trace(f, 0, 25, buf, 0, 100, 1, 25)
+            _segyio.read_trace(f, 0, 25, buf, 0, 100, 1, 25)
 
-        self.assertAlmostEqual(buf[10], 1.0, places=4)
-        self.assertAlmostEqual(buf[11], 3.1415, places=4)
+            self.assertAlmostEqual(buf[10], 1.0, places=4)
+            self.assertAlmostEqual(buf[11], 3.1415, places=4)
 
-        _segyio.read_trace(f, 1, 25, buf, 0, 100, 1, 25)
+            _segyio.read_trace(f, 1, 25, buf, 0, 100, 1, 25)
 
-        self.assertAlmostEqual(sum(buf), 42.0 * 25, places=4)
+            self.assertAlmostEqual(sum(buf), 42.0 * 25, places=4)
 
-        _segyio.close(f)
+            _segyio.close(f)
 
     def read_small(self, mmap = False):
         f = _segyio.open(self.filename, "r")
diff --git a/tests/test_test_context.py b/tests/test_test_context.py
new file mode 100644
index 0000000..2e2ad0a
--- /dev/null
+++ b/tests/test_test_context.py
@@ -0,0 +1,32 @@
+import os
+from unittest import TestCase
+
+from test_context import TestContext
+
+
+class TestContextTest(TestCase):
+    def test_test_context(self):
+        original_cwd = os.getcwd()
+        with TestContext() as context:
+            cwd = os.getcwd()
+            self.assertNotEqual(original_cwd, cwd)
+            self.assertEqual(original_cwd, context.cwd)
+            self.assertEqual(cwd, context.temp_path)
+            self.assertTrue(cwd.endswith("_unnamed"))
+            context_path = context.temp_path
+
+        self.assertFalse(os.path.exists(context_path))
+        self.assertEqual(original_cwd, os.getcwd())
+
+    def test_named_test_context(self):
+        with TestContext(name="named") as context:
+            self.assertTrue(context.temp_path.endswith("_named"))
+
+    def test_test_context_copy(self):
+        with TestContext() as context:
+            small = "test-data/small.sgy"
+            context.copy_file(small)
+            context.copy_file(small, "test")
+
+            self.assertTrue(os.path.exists("small.sgy"))
+            self.assertTrue(os.path.exists("test/small.sgy"))

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/segyio.git



More information about the debian-science-commits mailing list