From e87d0f6864f3ccc8160566f473bfb46ff2c95338 Mon Sep 17 00:00:00 2001
From: Martin Bauer <martin.bauer@fau.de>
Date: Fri, 16 Aug 2019 14:12:26 +0200
Subject: [PATCH] pytest.raises: checking for exception error message fixed

- str(e) did work in previous pytest version
- but the correct usage is str(e.value)
- in newer pytest version the string for the e object is changed so
  the tests did not work any more
---
 pystencils_tests/test_dtype_check.py          |  2 +-
 pystencils_tests/test_fd_derivation.py        |  2 +-
 pystencils_tests/test_field.py                | 22 +++++++++----------
 .../test_size_and_layout_checks.py            | 18 +++++++--------
 4 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/pystencils_tests/test_dtype_check.py b/pystencils_tests/test_dtype_check.py
index 842e89c..194a633 100644
--- a/pystencils_tests/test_dtype_check.py
+++ b/pystencils_tests/test_dtype_check.py
@@ -16,7 +16,7 @@ def test_dtype_check_wrong_type():
 
     with pytest.raises(ValueError) as e:
         kernel(x=array, y=output)
-    assert 'Wrong data type' in str(e)
+    assert 'Wrong data type' in str(e.value)
 
 
 def test_dtype_check_correct_type():
diff --git a/pystencils_tests/test_fd_derivation.py b/pystencils_tests/test_fd_derivation.py
index 73d38dc..c2bb1aa 100644
--- a/pystencils_tests/test_fd_derivation.py
+++ b/pystencils_tests/test_fd_derivation.py
@@ -31,4 +31,4 @@ def test_linear_equation_system():
 
     with pytest.raises(ValueError) as e:
         m.add_equation(x**2 - 1)
-    assert 'Not a linear equation' in str(e)
+    assert 'Not a linear equation' in str(e.value)
diff --git a/pystencils_tests/test_field.py b/pystencils_tests/test_field.py
index 4742282..d4b9c6a 100644
--- a/pystencils_tests/test_field.py
+++ b/pystencils_tests/test_field.py
@@ -40,56 +40,56 @@ def test_error_handling():
     Field.create_generic('f', spatial_dimensions=2, index_dimensions=0, dtype=struct_dtype)
     with pytest.raises(ValueError) as e:
         Field.create_generic('f', spatial_dimensions=2, index_dimensions=1, dtype=struct_dtype)
-    assert 'index dimension' in str(e)
+    assert 'index dimension' in str(e.value)
 
     arr = np.array([[1, 2.0, 3], [1, 2.0, 3]], dtype=struct_dtype)
     Field.create_from_numpy_array('f', arr, index_dimensions=0)
     with pytest.raises(ValueError) as e:
         Field.create_from_numpy_array('f', arr, index_dimensions=1)
-    assert 'Structured arrays' in str(e)
+    assert 'Structured arrays' in str(e.value)
 
     arr = np.zeros([3, 3, 3])
     Field.create_from_numpy_array('f', arr, index_dimensions=2)
     with pytest.raises(ValueError) as e:
         Field.create_from_numpy_array('f', arr, index_dimensions=3)
-    assert 'Too many' in str(e)
+    assert 'Too many' in str(e.value)
 
     Field.create_fixed_size('f', (3, 2, 4), index_dimensions=0, dtype=struct_dtype, layout='reverse_numpy')
     with pytest.raises(ValueError) as e:
         Field.create_fixed_size('f', (3, 2, 4), index_dimensions=1, dtype=struct_dtype, layout='reverse_numpy')
-    assert 'Structured arrays' in str(e)
+    assert 'Structured arrays' in str(e.value)
 
     f = Field.create_fixed_size('f', (10, 10))
     with pytest.raises(ValueError) as e:
         f[1]
-    assert 'Wrong number of spatial indices' in str(e)
+    assert 'Wrong number of spatial indices' in str(e.value)
 
     f = Field.create_generic('f', spatial_dimensions=2, index_shape=(3,))
     with pytest.raises(ValueError) as e:
         f(3)
-    assert 'out of bounds' in str(e)
+    assert 'out of bounds' in str(e.value)
 
     f = Field.create_fixed_size('f', (10, 10, 3, 4), index_dimensions=2)
     with pytest.raises(ValueError) as e:
         f(3, 0)
-    assert 'out of bounds' in str(e)
+    assert 'out of bounds' in str(e.value)
 
     with pytest.raises(ValueError) as e:
         f(1, 0)(1, 0)
-    assert 'Indexing an already indexed' in str(e)
+    assert 'Indexing an already indexed' in str(e.value)
 
     with pytest.raises(ValueError) as e:
         f(1)
-    assert 'Wrong number of indices' in str(e)
+    assert 'Wrong number of indices' in str(e.value)
 
     with pytest.raises(ValueError) as e:
         Field.create_generic('f', spatial_dimensions=2, layout='wrong')
-    assert 'Unknown layout descriptor' in str(e)
+    assert 'Unknown layout descriptor' in str(e.value)
 
     assert layout_string_to_tuple('fzyx', dim=4) == (3, 2, 1, 0)
     with pytest.raises(ValueError) as e:
         layout_string_to_tuple('wrong', dim=4)
-    assert 'Unknown layout descriptor' in str(e)
+    assert 'Unknown layout descriptor' in str(e.value)
 
 
 def test_decorator_scoping():
diff --git a/pystencils_tests/test_size_and_layout_checks.py b/pystencils_tests/test_size_and_layout_checks.py
index 597bf2c..27696e1 100644
--- a/pystencils_tests/test_size_and_layout_checks.py
+++ b/pystencils_tests/test_size_and_layout_checks.py
@@ -23,7 +23,7 @@ def test_size_check():
 
     with pytest.raises(ValueError) as e:
         func(src=src, dst=dst)
-    assert 'Wrong shape' in str(e)
+    assert 'Wrong shape' in str(e.value)
 
 
 def test_fixed_size_mismatch_check():
@@ -38,7 +38,7 @@ def test_fixed_size_mismatch_check():
 
     with pytest.raises(ValueError) as e:
         create_kernel([update_rule])
-    assert 'Differently sized field accesses' in str(e)
+    assert 'Differently sized field accesses' in str(e.value)
 
 
 def test_fixed_and_variable_field_check():
@@ -53,7 +53,7 @@ def test_fixed_and_variable_field_check():
 
     with pytest.raises(ValueError) as e:
         create_kernel(update_rule)
-    assert 'Mixing fixed-shaped and variable-shape fields' in str(e)
+    assert 'Mixing fixed-shaped and variable-shape fields' in str(e.value)
 
 
 def test_two_variable_shaped_fields():
@@ -70,7 +70,7 @@ def test_two_variable_shaped_fields():
 
     with pytest.raises(TypeError) as e:
         func(src=src, dst=dst)
-    assert 'must have same' in str(e)
+    assert 'must have same' in str(e.value)
 
 
 def test_ssa_checks():
@@ -81,18 +81,18 @@ def test_ssa_checks():
         create_kernel([Assignment(c, f[0, 1]),
                        Assignment(c, f[1, 0]),
                        Assignment(g[0, 0], c)])
-    assert 'Assignments not in SSA form' in str(e)
+    assert 'Assignments not in SSA form' in str(e.value)
 
     with pytest.raises(ValueError) as e:
         create_kernel([Assignment(c, a + 3),
                        Assignment(a, 42),
                        Assignment(g[0, 0], c)])
-    assert 'Symbol a is written, after it has been read' in str(e)
+    assert 'Symbol a is written, after it has been read' in str(e.value)
 
     with pytest.raises(ValueError) as e:
         create_kernel([Assignment(c, c + 1),
                        Assignment(g[0, 0], c)])
-    assert 'Symbol c is written, after it has been read' in str(e)
+    assert 'Symbol c is written, after it has been read' in str(e.value)
 
 
 def test_loop_independence_checks():
@@ -102,7 +102,7 @@ def test_loop_independence_checks():
     with pytest.raises(ValueError) as e:
         create_kernel([Assignment(g[0, 1], f[0, 1]),
                        Assignment(g[0, 0], f[1, 0])])
-    assert 'Field g is written at two different locations' in str(e)
+    assert 'Field g is written at two different locations' in str(e.value)
 
     # This is allowed - because only one element of g is accessed
     create_kernel([Assignment(g[0, 2], f[0, 1]),
@@ -115,4 +115,4 @@ def test_loop_independence_checks():
     with pytest.raises(ValueError) as e:
         create_kernel([Assignment(g[0, 1], 3),
                        Assignment(f[0, 1], 2 * g[0, 2])])
-    assert 'Field g is read at (0, 2) and written at (0, 1)' in str(e)
+    assert 'Field g is read at (0, 2) and written at (0, 1)' in str(e.value)
-- 
GitLab