Skip to content

Commit

Permalink
Convert to pytest.
Browse files Browse the repository at this point in the history
  • Loading branch information
pp-mo committed Mar 19, 2024
1 parent b19650d commit dd5036a
Showing 1 changed file with 12 additions and 39 deletions.
51 changes: 12 additions & 39 deletions lib/iris/tests/unit/fileformats/abf/test_ABFField.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,23 @@
# This file is part of Iris and is released under the BSD license.
# See LICENSE in the root of the repository for full licensing details.
"""Unit tests for the `iris.fileformats.abf.ABFField` class."""

# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests # isort:skip

from unittest import mock

from iris.fileformats.abf import ABFField


class MethodCounter:
def __init__(self, method_name):
self.method_name = method_name
self.count = 0

def __enter__(self):
self.orig_method = getattr(ABFField, self.method_name)

def new_method(*args, **kwargs):
self.count += 1
self.orig_method(*args, **kwargs)

setattr(ABFField, self.method_name, new_method)
return self

def __exit__(self, exc_type, exc_value, traceback):
setattr(ABFField, self.method_name, self.orig_method)
return False


class Test_data(tests.IrisTest):
def test_single_read(self):
class Test_data:
def test_single_read(self, mocker):
path = "0000000000000000jan00000"
field = ABFField(path)

with mock.patch("iris.fileformats.abf.np.fromfile") as fromfile:
with MethodCounter("__getattr__") as getattr:
with MethodCounter("_read") as read:
field.data

fromfile.assert_called_once_with(path, dtype=">u1")
self.assertEqual(getattr.count, 1)
self.assertEqual(read.count, 1)
fromfile = mocker.patch("iris.fileformats.abf.np.fromfile")
getattr = mocker.patch(
"iris.fileformats.abf.ABFField.__getattr__", wraps=field.__getattr__
)
read = mocker.patch("iris.fileformats.abf.ABFField._read", wraps=field._read)

# do the access
field.data

if __name__ == "__main__":
tests.main()
fromfile.assert_called_once_with(path, dtype=">u1")
assert getattr.call_count == 1
assert read.call_count == 1

0 comments on commit dd5036a

Please sign in to comment.