From 8e02c7a2781acb0f0df33f042853d181d457ae23 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Wed, 5 Jul 2017 16:50:20 +0100 Subject: [PATCH 1/9] Correct STASH regular expression --- lib/iris/fileformats/pp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 5f79a8966e..af5ec5832b 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -305,7 +305,8 @@ def from_msi(msi): if not isinstance(msi, six.string_types): raise TypeError('Expected STASH code MSI string, got %r' % (msi,)) - msi_match = re.match('^\s*m(.*)s(.*)i(.*)\s*$', msi, re.IGNORECASE) + msi_match = re.match('^\s*m(\d{2})s(\d{2})i(\d{3})\s*$', msi, + re.IGNORECASE) if msi_match is None: raise ValueError('Expected STASH code MSI string "mXXsXXiXXX", ' From 44b9c7918ae8913b9d8b869b7f90caba0dead8f6 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Wed, 5 Jul 2017 17:15:03 +0100 Subject: [PATCH 2/9] Revert "Correct STASH regular expression" This reverts commit 8e02c7a2781acb0f0df33f042853d181d457ae23. --- lib/iris/fileformats/pp.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index af5ec5832b..5f79a8966e 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -305,8 +305,7 @@ def from_msi(msi): if not isinstance(msi, six.string_types): raise TypeError('Expected STASH code MSI string, got %r' % (msi,)) - msi_match = re.match('^\s*m(\d{2})s(\d{2})i(\d{3})\s*$', msi, - re.IGNORECASE) + msi_match = re.match('^\s*m(.*)s(.*)i(.*)\s*$', msi, re.IGNORECASE) if msi_match is None: raise ValueError('Expected STASH code MSI string "mXXsXXiXXX", ' From bfa7b88227e8168a0196af77317041deca0344d4 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Wed, 5 Jul 2017 17:18:21 +0100 Subject: [PATCH 3/9] Add test to show failure of current STASH regular expression --- lib/iris/tests/test_pp_stash.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/iris/tests/test_pp_stash.py b/lib/iris/tests/test_pp_stash.py index 6a9d87e076..56533416ab 100644 --- a/lib/iris/tests/test_pp_stash.py +++ b/lib/iris/tests/test_pp_stash.py @@ -109,6 +109,11 @@ def test_illegal_stash_format(self): with self.assertRaises(ValueError): self.assertEqual('abc', iris.fileformats.pp.STASH(1, 2, 3)) + with self.assertRaises(ValueError): + self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'mlotstmin') + with self.assertRaises(ValueError): + self.assertEqual('mlotstmin', iris.fileformats.pp.STASH(1, 2, 3)) + with self.assertRaises(ValueError): self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02003') with self.assertRaises(ValueError): From 58bf91f859d6b02569d70bb43b37c61616232239 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 6 Jul 2017 14:16:10 +0100 Subject: [PATCH 4/9] Correct STASH regular expression --- lib/iris/fileformats/pp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 5f79a8966e..af5ec5832b 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -305,7 +305,8 @@ def from_msi(msi): if not isinstance(msi, six.string_types): raise TypeError('Expected STASH code MSI string, got %r' % (msi,)) - msi_match = re.match('^\s*m(.*)s(.*)i(.*)\s*$', msi, re.IGNORECASE) + msi_match = re.match('^\s*m(\d{2})s(\d{2})i(\d{3})\s*$', msi, + re.IGNORECASE) if msi_match is None: raise ValueError('Expected STASH code MSI string "mXXsXXiXXX", ' From e97a314a110b722eedde538a8ff426be1b39f54a Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 6 Jul 2017 14:30:00 +0100 Subject: [PATCH 5/9] Update STASH-related tests accordingly --- lib/iris/tests/test_pp_stash.py | 86 +++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/lib/iris/tests/test_pp_stash.py b/lib/iris/tests/test_pp_stash.py index 56533416ab..5111bfe34d 100644 --- a/lib/iris/tests/test_pp_stash.py +++ b/lib/iris/tests/test_pp_stash.py @@ -1,4 +1,4 @@ -# (C) British Crown Copyright 2010 - 2015, Met Office +# (C) British Crown Copyright 2010 - 2017, Met Office # # This file is part of Iris. # @@ -58,45 +58,11 @@ def test_stash_against_str(self): self.assertNotEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm02s03i004') self.assertNotEqual('m02s03i004', iris.fileformats.pp.STASH(1, 2, 3)) - def test_irregular_stash_str(self): - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02i0000000003') - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02i3') - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s2i3') - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm1s2i3') - - self.assertEqual('m01s02i0000000003', iris.fileformats.pp.STASH(1, 2, 3)) - self.assertEqual('m01s02i3', iris.fileformats.pp.STASH(1, 2, 3)) - self.assertEqual('m01s2i3', iris.fileformats.pp.STASH(1, 2, 3)) - self.assertEqual('m1s2i3', iris.fileformats.pp.STASH(1, 2, 3)) - - self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s02i0000000003') - self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s02i3') - self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s2i3') - self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm1s2i3') - - self.assertNotEqual('m01s02i0000000003', iris.fileformats.pp.STASH(2, 3, 4)) - self.assertNotEqual('m01s02i3', iris.fileformats.pp.STASH(2, 3, 4)) - self.assertNotEqual('m01s2i3', iris.fileformats.pp.STASH(2, 3, 4)) - self.assertNotEqual('m1s2i3', iris.fileformats.pp.STASH(2, 3, 4)) - - self.assertEqual(iris.fileformats.pp.STASH.from_msi('M01s02i003'), 'm01s02i003') - self.assertEqual('m01s02i003', iris.fileformats.pp.STASH.from_msi('M01s02i003')) - def test_illegal_stash_str_range(self): - self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm??s02i003') - self.assertNotEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm01s02i003') - self.assertEqual('m??s02i003', iris.fileformats.pp.STASH(0, 2, 3)) - self.assertNotEqual('m01s02i003', iris.fileformats.pp.STASH(0, 2, 3)) - - self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm??s02i003') self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm00s02i003') - self.assertEqual('m??s02i003', iris.fileformats.pp.STASH(0, 2, 3)) + self.assertNotEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm01s02i003') self.assertEqual('m00s02i003', iris.fileformats.pp.STASH(0, 2, 3)) - - self.assertEqual(iris.fileformats.pp.STASH(100, 2, 3), 'm??s02i003') - self.assertEqual(iris.fileformats.pp.STASH(100, 2, 3), 'm100s02i003') - self.assertEqual('m??s02i003', iris.fileformats.pp.STASH(100, 2, 3)) - self.assertEqual('m100s02i003', iris.fileformats.pp.STASH(100, 2, 3)) + self.assertNotEqual('m01s02i003', iris.fileformats.pp.STASH(0, 2, 3)) def test_illegal_stash_stash_range(self): self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), iris.fileformats.pp.STASH(0, 2, 3)) @@ -119,6 +85,52 @@ def test_illegal_stash_format(self): with self.assertRaises(ValueError): self.assertEqual('m01s02003', iris.fileformats.pp.STASH(1, 2, 3)) + with self.assertRaises(ValueError): + self.assertEqual(iris.fileformats.pp.STASH(100, 2, 3), 'm100s02i003') + with self.assertRaises(ValueError): + self.assertEqual('m100s02i003', iris.fileformats.pp.STASH(100, 2, 3)) + + with self.assertRaises(ValueError): + self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02i0000000003') + with self.assertRaises(ValueError): + self.assertEqual('m01s02i0000000003', iris.fileformats.pp.STASH(1, 2, 3)) + with self.assertRaises(ValueError): + self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s02i0000000003') + with self.assertRaises(ValueError): + self.assertNotEqual('m01s02i0000000003', iris.fileformats.pp.STASH(2, 3, 4)) + + with self.assertRaises(ValueError): + self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02i3') + with self.assertRaises(ValueError): + self.assertEqual('m01s02i3', iris.fileformats.pp.STASH(1, 2, 3)) + with self.assertRaises(ValueError): + self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s02i3') + with self.assertRaises(ValueError): + self.assertNotEqual('m01s02i3', iris.fileformats.pp.STASH(2, 3, 4)) + + with self.assertRaises(ValueError): + self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s2i3') + with self.assertRaises(ValueError): + self.assertEqual('m01s2i3', iris.fileformats.pp.STASH(1, 2, 3)) + with self.assertRaises(ValueError): + self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s2i3') + with self.assertRaises(ValueError): + self.assertNotEqual('m01s2i3', iris.fileformats.pp.STASH(2, 3, 4)) + + with self.assertRaises(ValueError): + self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm1s2i3') + with self.assertRaises(ValueError): + self.assertEqual('m1s2i3', iris.fileformats.pp.STASH(1, 2, 3)) + with self.assertRaises(ValueError): + self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm1s2i3') + with self.assertRaises(ValueError): + self.assertNotEqual('m1s2i3', iris.fileformats.pp.STASH(2, 3, 4)) + + with self.assertRaises(ValueError): + self.assertEqual(iris.fileformats.pp.STASH.from_msi('M01s02i003'), 'm01s02i003') + with self.assertRaises(ValueError): + self.assertEqual('m01s02i003', iris.fileformats.pp.STASH.from_msi('M01s02i003')) + def test_illegal_stash_type(self): with self.assertRaises(TypeError): self.assertEqual(iris.fileformats.pp.STASH.from_msi(102003), 'm01s02i003') From 506c6d3d2ccc009514c07af36e0d2af8b9ece375 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Thu, 6 Jul 2017 14:42:15 +0100 Subject: [PATCH 6/9] Restore a irregular STASH test --- lib/iris/tests/test_pp_stash.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/iris/tests/test_pp_stash.py b/lib/iris/tests/test_pp_stash.py index 5111bfe34d..4262560572 100644 --- a/lib/iris/tests/test_pp_stash.py +++ b/lib/iris/tests/test_pp_stash.py @@ -58,6 +58,10 @@ def test_stash_against_str(self): self.assertNotEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm02s03i004') self.assertNotEqual('m02s03i004', iris.fileformats.pp.STASH(1, 2, 3)) + def test_irregular_stash_str(self): + self.assertEqual(iris.fileformats.pp.STASH.from_msi('M01s02i003'), 'm01s02i003') + self.assertEqual('m01s02i003', iris.fileformats.pp.STASH.from_msi('M01s02i003')) + def test_illegal_stash_str_range(self): self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm00s02i003') self.assertNotEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm01s02i003') @@ -126,11 +130,6 @@ def test_illegal_stash_format(self): with self.assertRaises(ValueError): self.assertNotEqual('m1s2i3', iris.fileformats.pp.STASH(2, 3, 4)) - with self.assertRaises(ValueError): - self.assertEqual(iris.fileformats.pp.STASH.from_msi('M01s02i003'), 'm01s02i003') - with self.assertRaises(ValueError): - self.assertEqual('m01s02i003', iris.fileformats.pp.STASH.from_msi('M01s02i003')) - def test_illegal_stash_type(self): with self.assertRaises(TypeError): self.assertEqual(iris.fileformats.pp.STASH.from_msi(102003), 'm01s02i003') From 38d91c03442aacfd79f843ba12cdd30894bc590c Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Wed, 19 Jul 2017 10:20:48 +0100 Subject: [PATCH 7/9] Update regular expression to handle question marks for the model number --- lib/iris/fileformats/pp.py | 2 +- lib/iris/tests/test_pp_stash.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index af5ec5832b..75f3098ade 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -305,7 +305,7 @@ def from_msi(msi): if not isinstance(msi, six.string_types): raise TypeError('Expected STASH code MSI string, got %r' % (msi,)) - msi_match = re.match('^\s*m(\d{2})s(\d{2})i(\d{3})\s*$', msi, + msi_match = re.match('^\s*m(\d{2}|\?{2})s(\d{2})i(\d{3})\s*$', msi, re.IGNORECASE) if msi_match is None: diff --git a/lib/iris/tests/test_pp_stash.py b/lib/iris/tests/test_pp_stash.py index 4262560572..8894941dff 100644 --- a/lib/iris/tests/test_pp_stash.py +++ b/lib/iris/tests/test_pp_stash.py @@ -64,8 +64,10 @@ def test_irregular_stash_str(self): def test_illegal_stash_str_range(self): self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm00s02i003') + self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm??s02i003') self.assertNotEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm01s02i003') self.assertEqual('m00s02i003', iris.fileformats.pp.STASH(0, 2, 3)) + self.assertEqual('m??s02i003', iris.fileformats.pp.STASH(0, 2, 3)) self.assertNotEqual('m01s02i003', iris.fileformats.pp.STASH(0, 2, 3)) def test_illegal_stash_stash_range(self): From fe3fd3ce1bf435b21541ffe431bd7cf5c07b2743 Mon Sep 17 00:00:00 2001 From: Emma Hogan Date: Mon, 4 Sep 2017 15:29:54 +0100 Subject: [PATCH 8/9] Changes in response to review: update format for illegal_stash tests, fix failing tests --- lib/iris/fileformats/pp.py | 5 +- lib/iris/tests/test_pp_stash.py | 94 ++++++++++----------------------- 2 files changed, 31 insertions(+), 68 deletions(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 75f3098ade..13e023af98 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -305,8 +305,9 @@ def from_msi(msi): if not isinstance(msi, six.string_types): raise TypeError('Expected STASH code MSI string, got %r' % (msi,)) - msi_match = re.match('^\s*m(\d{2}|\?{2})s(\d{2})i(\d{3})\s*$', msi, - re.IGNORECASE) + msi_match = re.match( + '^\s*m(\d{2}|\?{2})s(\d{2}|\?{2})i(\d{3}|\?{3})\s*$', msi, + re.IGNORECASE) if msi_match is None: raise ValueError('Expected STASH code MSI string "mXXsXXiXXX", ' diff --git a/lib/iris/tests/test_pp_stash.py b/lib/iris/tests/test_pp_stash.py index 8894941dff..400c74ac06 100644 --- a/lib/iris/tests/test_pp_stash.py +++ b/lib/iris/tests/test_pp_stash.py @@ -76,74 +76,36 @@ def test_illegal_stash_stash_range(self): self.assertEqual(iris.fileformats.pp.STASH(100, 2, 3), iris.fileformats.pp.STASH(999, 2, 3)) def test_illegal_stash_format(self): - with self.assertRaises(ValueError): - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'abc') - with self.assertRaises(ValueError): - self.assertEqual('abc', iris.fileformats.pp.STASH(1, 2, 3)) - - with self.assertRaises(ValueError): - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'mlotstmin') - with self.assertRaises(ValueError): - self.assertEqual('mlotstmin', iris.fileformats.pp.STASH(1, 2, 3)) - - with self.assertRaises(ValueError): - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02003') - with self.assertRaises(ValueError): - self.assertEqual('m01s02003', iris.fileformats.pp.STASH(1, 2, 3)) - - with self.assertRaises(ValueError): - self.assertEqual(iris.fileformats.pp.STASH(100, 2, 3), 'm100s02i003') - with self.assertRaises(ValueError): - self.assertEqual('m100s02i003', iris.fileformats.pp.STASH(100, 2, 3)) - - with self.assertRaises(ValueError): - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02i0000000003') - with self.assertRaises(ValueError): - self.assertEqual('m01s02i0000000003', iris.fileformats.pp.STASH(1, 2, 3)) - with self.assertRaises(ValueError): - self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s02i0000000003') - with self.assertRaises(ValueError): - self.assertNotEqual('m01s02i0000000003', iris.fileformats.pp.STASH(2, 3, 4)) - - with self.assertRaises(ValueError): - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02i3') - with self.assertRaises(ValueError): - self.assertEqual('m01s02i3', iris.fileformats.pp.STASH(1, 2, 3)) - with self.assertRaises(ValueError): - self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s02i3') - with self.assertRaises(ValueError): - self.assertNotEqual('m01s02i3', iris.fileformats.pp.STASH(2, 3, 4)) - - with self.assertRaises(ValueError): - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s2i3') - with self.assertRaises(ValueError): - self.assertEqual('m01s2i3', iris.fileformats.pp.STASH(1, 2, 3)) - with self.assertRaises(ValueError): - self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s2i3') - with self.assertRaises(ValueError): - self.assertNotEqual('m01s2i3', iris.fileformats.pp.STASH(2, 3, 4)) - - with self.assertRaises(ValueError): - self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm1s2i3') - with self.assertRaises(ValueError): - self.assertEqual('m1s2i3', iris.fileformats.pp.STASH(1, 2, 3)) - with self.assertRaises(ValueError): - self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm1s2i3') - with self.assertRaises(ValueError): - self.assertNotEqual('m1s2i3', iris.fileformats.pp.STASH(2, 3, 4)) + test_values = ( + ('abc', (1, 2, 3)), + ('mlotstmin', (1, 2, 3)), + ('m1s2i3', (1, 2, 3)), + ('m1s2i3', (2, 3, 4)), + ('m01s2i3', (1, 2, 3)), + ('m01s2i3', (2, 3, 4)), + ('m01s02i3', (1, 2, 3)), + ('m01s02i3', (2, 3, 4)), + ('m01s02003', (1, 2, 3)), + ('m100s02i003', (100, 2, 3)), + ('m01s02i0000000003', (1, 2, 3)), + ('m01s02i0000000003', (2, 3, 4)), + ) + + for (test_value, reference) in test_values: + msg = 'Expected STASH code .* {!r}'.format(test_value) + with self.assertRaisesRegexp(ValueError, msg): + test_value == iris.fileformats.pp.STASH(*reference) def test_illegal_stash_type(self): - with self.assertRaises(TypeError): - self.assertEqual(iris.fileformats.pp.STASH.from_msi(102003), 'm01s02i003') - - with self.assertRaises(TypeError): - self.assertEqual('m01s02i003', iris.fileformats.pp.STASH.from_msi(102003)) - - with self.assertRaises(TypeError): - self.assertEqual(iris.fileformats.pp.STASH.from_msi(['m01s02i003']), 'm01s02i003') - - with self.assertRaises(TypeError): - self.assertEqual('m01s02i003', iris.fileformats.pp.STASH.from_msi(['m01s02i003'])) + test_values = ( + (102003, 'm01s02i003'), + (['m01s02i003'], 'm01s02i003'), + ) + + for (test_value, reference) in test_values: + msg = 'Expected STASH code .* {!r}'.format(test_value) + with self.assertRaisesRegexp(TypeError, msg): + iris.fileformats.pp.STASH.from_msi(test_value) == reference def test_stash_lbuser(self): stash = iris.fileformats.pp.STASH(2, 32, 456) From 5317a5a40127e4d5ee3879a8e1b2d47602a0257e Mon Sep 17 00:00:00 2001 From: Daniel Kirkham Date: Wed, 27 Sep 2017 16:36:22 +0100 Subject: [PATCH 9/9] Relax regex to allow varying lengths and reinstate tests --- lib/iris/fileformats/pp.py | 2 +- lib/iris/tests/test_pp_stash.py | 50 ++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 13e023af98..deff053faf 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -306,7 +306,7 @@ def from_msi(msi): raise TypeError('Expected STASH code MSI string, got %r' % (msi,)) msi_match = re.match( - '^\s*m(\d{2}|\?{2})s(\d{2}|\?{2})i(\d{3}|\?{3})\s*$', msi, + '^\s*m(\d+|\?+)s(\d+|\?+)i(\d+|\?+)\s*$', msi, re.IGNORECASE) if msi_match is None: diff --git a/lib/iris/tests/test_pp_stash.py b/lib/iris/tests/test_pp_stash.py index 400c74ac06..303dc4cc7e 100644 --- a/lib/iris/tests/test_pp_stash.py +++ b/lib/iris/tests/test_pp_stash.py @@ -59,17 +59,47 @@ def test_stash_against_str(self): self.assertNotEqual('m02s03i004', iris.fileformats.pp.STASH(1, 2, 3)) def test_irregular_stash_str(self): + self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02i0000000003') + self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s02i3') + self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm01s2i3') + self.assertEqual(iris.fileformats.pp.STASH(1, 2, 3), 'm1s2i3') + + self.assertEqual('m01s02i0000000003', iris.fileformats.pp.STASH(1, 2, 3)) + self.assertEqual('m01s02i3', iris.fileformats.pp.STASH(1, 2, 3)) + self.assertEqual('m01s2i3', iris.fileformats.pp.STASH(1, 2, 3)) + self.assertEqual('m1s2i3', iris.fileformats.pp.STASH(1, 2, 3)) + + self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s02i0000000003') + self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s02i3') + self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm01s2i3') + self.assertNotEqual(iris.fileformats.pp.STASH(2, 3, 4), 'm1s2i3') + + self.assertNotEqual('m01s02i0000000003', iris.fileformats.pp.STASH(2, 3, 4)) + self.assertNotEqual('m01s02i3', iris.fileformats.pp.STASH(2, 3, 4)) + self.assertNotEqual('m01s2i3', iris.fileformats.pp.STASH(2, 3, 4)) + self.assertNotEqual('m1s2i3', iris.fileformats.pp.STASH(2, 3, 4)) + self.assertEqual(iris.fileformats.pp.STASH.from_msi('M01s02i003'), 'm01s02i003') self.assertEqual('m01s02i003', iris.fileformats.pp.STASH.from_msi('M01s02i003')) def test_illegal_stash_str_range(self): - self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm00s02i003') + self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm??s02i003') self.assertNotEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm01s02i003') - self.assertEqual('m00s02i003', iris.fileformats.pp.STASH(0, 2, 3)) + self.assertEqual('m??s02i003', iris.fileformats.pp.STASH(0, 2, 3)) self.assertNotEqual('m01s02i003', iris.fileformats.pp.STASH(0, 2, 3)) + self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm??s02i003') + self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), 'm00s02i003') + self.assertEqual('m??s02i003', iris.fileformats.pp.STASH(0, 2, 3)) + self.assertEqual('m00s02i003', iris.fileformats.pp.STASH(0, 2, 3)) + + self.assertEqual(iris.fileformats.pp.STASH(100, 2, 3), 'm??s02i003') + self.assertEqual(iris.fileformats.pp.STASH(100, 2, 3), 'm100s02i003') + self.assertEqual('m??s02i003', iris.fileformats.pp.STASH(100, 2, 3)) + self.assertEqual('m100s02i003', iris.fileformats.pp.STASH(100, 2, 3)) + def test_illegal_stash_stash_range(self): self.assertEqual(iris.fileformats.pp.STASH(0, 2, 3), iris.fileformats.pp.STASH(0, 2, 3)) self.assertEqual(iris.fileformats.pp.STASH(100, 2, 3), iris.fileformats.pp.STASH(100, 2, 3)) @@ -79,22 +109,14 @@ def test_illegal_stash_format(self): test_values = ( ('abc', (1, 2, 3)), ('mlotstmin', (1, 2, 3)), - ('m1s2i3', (1, 2, 3)), - ('m1s2i3', (2, 3, 4)), - ('m01s2i3', (1, 2, 3)), - ('m01s2i3', (2, 3, 4)), - ('m01s02i3', (1, 2, 3)), - ('m01s02i3', (2, 3, 4)), - ('m01s02003', (1, 2, 3)), - ('m100s02i003', (100, 2, 3)), - ('m01s02i0000000003', (1, 2, 3)), - ('m01s02i0000000003', (2, 3, 4)), - ) + ('m01s02003', (1, 2, 3))) for (test_value, reference) in test_values: msg = 'Expected STASH code .* {!r}'.format(test_value) with self.assertRaisesRegexp(ValueError, msg): test_value == iris.fileformats.pp.STASH(*reference) + with self.assertRaisesRegexp(ValueError, msg): + iris.fileformats.pp.STASH(*reference) == test_value def test_illegal_stash_type(self): test_values = ( @@ -106,6 +128,8 @@ def test_illegal_stash_type(self): msg = 'Expected STASH code .* {!r}'.format(test_value) with self.assertRaisesRegexp(TypeError, msg): iris.fileformats.pp.STASH.from_msi(test_value) == reference + with self.assertRaisesRegexp(TypeError, msg): + reference == iris.fileformats.pp.STASH.from_msi(test_value) def test_stash_lbuser(self): stash = iris.fileformats.pp.STASH(2, 32, 456)