lundi 16 novembre 2020

accessing properties of a mock opened with context manager

Im trying to mock something but I cant get to the properties of the mock once the it is opened with context

# create mocks
mock_1 = MagicMock()
mock_2 = MagicMock()

# set a nested property
type(mock_1.bounds).left = PropertyMock(return_value=-10)
type(mock_2.bounds).left = PropertyMock(return_value= 50)

# at this point I can do less than on the property and it works
mock_1.bounds.left < 50

# setup rasterio mock  open
rasterio_mock = mock_open()
rasterio_mock.side_effect = [mock_1, mock_2]

tiles = []

# add our opened  mocks to list
with patch('rasterio.open', rasterio_mock):
    with ExitStack() as stack:
        tiles.append(stack.enter_context(rasterio.open(f"/vsis3/thing", 'r', driver='GTiff')))
        tiles.append(stack.enter_context(rasterio.open(f"/vsis3/thing", 'r', driver='GTiff')))

        for tile in tiles:
            # same attempt to access the property now  fails :(
            if tile.bounds.left < 50:
                pass
TypeError: '<' not supported between instances of 'MagicMock' and 'int'

Im unsure how to resolve this currently, how can I perform < operations on a property of the mock once it is opened like this? Why isnt the above working?

Aucun commentaire:

Enregistrer un commentaire