1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
# Copyright © 2020, Joseph Berry, Rico Tabor (opendrop.dev@gmail.com)
# OpenDrop is released under the GNU GPL License. You are free to
# modify and distribute the code, but always under the same license
# (i.e. you cannot make commercial derivatives).
#
# If you use this software in your research, please cite the following
# journal articles:
#
# J. D. Berry, M. J. Neeson, R. R. Dagastine, D. Y. C. Chan and
# R. F. Tabor, Measurement of surface and interfacial tension using
# pendant drop tensiometry. Journal of Colloid and Interface Science 454
# (2015) 226–237. https://doi.org/10.1016/j.jcis.2015.05.012
#
# E. Huang, T. Denning, A. Skoufis, J. Qi, R. R. Dagastine, R. F. Tabor
# and J. D. Berry, OpenDrop: Open-source software for pendant drop
# tensiometry & contact angle measurements, submitted to the Journal of
# Open Source Software
#
# These citations help us not only to understand who is using and
# developing OpenDrop, and for what purpose, but also to justify
# continued development of this code and other open source resources.
#
# OpenDrop is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details. You
# should have received a copy of the GNU General Public License along
# with this software. If not, see <https://www.gnu.org/licenses/>.
from typing import Mapping, Any, Optional, cast as tcast
from unittest import mock
from unittest.mock import Mock, call
import pytest
from opendrop.mvp.component import ComponentSymbol, ComponentFactory, Component
from opendrop.utility.events import Event
def test_component_symbol_set_view():
csymbol = ComponentSymbol()
@csymbol.view(options=['a', 'b'])
class MyView:
pass
assert csymbol._view_cls == MyView
assert set(csymbol._view_options) == {'a', 'b'}
def test_component_symbol_set_view_with_no_options():
csymbol = ComponentSymbol()
@csymbol.view()
class MyView:
pass
def test_component_symbol_set_presenter():
csymbol = ComponentSymbol()
@csymbol.presenter(options=['x', 'y'])
class MyPresenter:
pass
assert csymbol._presenter_cls == MyPresenter
assert set(csymbol._presenter_options) == {'x', 'y'}
def test_component_symbol_set_presenter_with_no_options():
csymbol = ComponentSymbol()
@csymbol.presenter()
class MyPresenter:
pass
@mock.patch('opendrop.mvp.component.ComponentFactory')
def test_component_symbol_factory(MockComponentFactory):
csymbol = MockComponentSymbol()
cfactory = csymbol.factory(a=1, x=2)
MockComponentFactory.assert_called_once_with(csymbol, options={'a': 1, 'x': 2})
expected_cfactory = MockComponentFactory.return_value
assert cfactory == expected_cfactory
@pytest.mark.parametrize(
'options, expect_view, expect_presenter', [
({'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {}),
({'x': 1, 'y': 2}, {}, {'x': 1, 'y': 2}),
({'a': 1, 'x': 2}, {'a': 1}, {'x': 2}),
({'a': 1}, {'a': 1}, {}),
({'x': 1}, {}, {'x': 1}),
]
)
def test_component_factory_create_constructs_view_and_presenter_with_right_arguments \
(options, expect_view, expect_presenter):
csymbol = MockComponentSymbol(view_options=['a', 'b'], presenter_options=['x', 'y'])
cfactory = ComponentFactory(csymbol, options)
cfactory.create()
csymbol._view_cls.assert_called_once_with(env=None, options=expect_view)
csymbol.presenter_cls.assert_called_once_with(env=None, options=expect_presenter)
def test_component_factory_create_with_environment():
csymbol = MockComponentSymbol(view_options=['a', 'b'], presenter_options=['x', 'y'])
cfactory = ComponentFactory(csymbol)
mock_view_env = Mock()
mock_presenter_env = Mock()
cfactory.create(view_env=mock_view_env, presenter_env=mock_presenter_env)
csymbol._view_cls.assert_called_once_with(env=mock_view_env, options={})
csymbol.presenter_cls.assert_called_once_with(env=mock_presenter_env, options={})
@mock.patch('opendrop.mvp.component.Component')
def test_component_factory_create_constructs_component(MockComponent):
csymbol = MockComponentSymbol(view_options=['a', 'b'], presenter_options=['x', 'y'])
cfactory = ComponentFactory(csymbol)
component = cfactory.create()
new_view = csymbol._view_cls.return_value
new_presenter = csymbol.presenter_cls.return_value
MockComponent.assert_called_once_with(view=new_view, presenter=new_presenter)
expect_component = MockComponent.return_value
assert component == expect_component
def test_component_factory_create_too_many_options():
csymbol = MockComponentSymbol(view_options=['a', 'b'], presenter_options=['x', 'y'])
# Pass unknown options 'm' and 'n'
cfactory = ComponentFactory(csymbol, options={'x': 1, 'y': 2, 'm': 100, 'n': 200})
with pytest.raises(TypeError):
cfactory.create()
def test_component_factory_fork():
csymbol = MockComponentSymbol(view_options=['a', 'b'], presenter_options=['x', 'y'])
cfactory1 = ComponentFactory(csymbol, options={'a': 1, 'x': 3})
cfactory2 = cfactory1.fork(b=2, y=4)
# Create using cfactory1
cfactory1.create()
csymbol.view_cls.assert_called_once_with(env=None, options={'a': 1})
csymbol.presenter_cls.assert_called_once_with(env=None, options={'x': 3})
# Reset mocks
csymbol.view_cls.reset_mock()
csymbol.presenter_cls.reset_mock()
# Create using cfactory2
cfactory2.create()
csymbol.view_cls.assert_called_once_with(env=None, options={'a': 1, 'b': 2})
csymbol.presenter_cls.assert_called_once_with(env=None, options={'x': 3, 'y': 4})
def test_component_inits_view_and_presenter():
mock_view = Mock()
mock_presenter = Mock()
manager = Mock()
manager.attach_mock(mock_view, 'mock_view')
manager.attach_mock(mock_presenter, 'mock_presenter')
component = Component(view=mock_view, presenter=mock_presenter)
assert manager.mock_calls == [call.mock_presenter._init(mock_view), call.mock_view._init(mock_presenter)]
def test_component_view_rep():
mock_view = Mock()
component = Component(view=mock_view, presenter=Mock())
assert component.view_rep == mock_view._init.return_value
def test_component_sets_presenter_component_hooks():
mock_presenter = Mock()
component = Component(view=Mock(), presenter=mock_presenter)
assert mock_presenter._component_hooks.component_destroy == component.destroy
def test_component_sets_view_component_hooks():
mock_view = Mock()
component = Component(view=mock_view, presenter=Mock())
assert mock_view._component_hooks.new_component == component._new_component_for_view
assert mock_view._component_hooks.remove_component == component._remove_component_for_view
def test_component_new_component():
mock_view = Mock()
mock_presenter = Mock()
component = Component(view=mock_view, presenter=mock_presenter)
mock_cfactory = MockComponentFactory()
new_child = component.new_component(mock_cfactory)
mock_cfactory.create.assert_called_once_with(
view_env=mock_view.extend_env.return_value,
presenter_env=mock_presenter.extend_env.return_value
)
assert new_child == mock_cfactory.mock_child
def test_component_remove_component():
component = Component(view=Mock(), presenter=Mock())
mock_cfactory = MockComponentFactory()
mock_child = tcast(Mock, component.new_component(mock_cfactory))
component.remove_component(mock_child)
mock_child.destroy.assert_called_once_with()
# Should raise an error if we try to remove again.
with pytest.raises(ValueError):
component.remove_component(mock_child)
def test_component_removes_child_if_child_is_destroyed():
component = Component(view=Mock(), presenter=Mock())
mock_cfactory = MockComponentFactory()
new_child = tcast(MockComponent, component.new_component(mock_cfactory))
new_child.sim_destroy()
# Should raise an error since child should have been automatically removed after it is destroyed.
with pytest.raises(ValueError):
component.remove_component(new_child)
# Child should not be destroyed again by previous call to `component.remove_component()`
new_child.destroy.assert_not_called()
def test_component_detaches_from_child_after_child_is_remove():
component = Component(view=Mock(), presenter=Mock())
mock_cfactory = MockComponentFactory()
new_child = component.new_component(mock_cfactory)
component.remove_component(new_child)
assert new_child.on_destroyed.num_connections == 0
def test_component_destroy():
mock_view = Mock()
mock_presenter = Mock()
component = Component(view=mock_view, presenter=mock_presenter)
component.destroy()
assert component.is_destroyed
mock_view._destroy.assert_called_once_with()
mock_presenter._destroy.assert_called_once_with()
def test_component_on_destroyed():
component = Component(view=Mock(), presenter=Mock())
callback = Mock()
component.on_destroyed.connect(callback)
component.destroy()
callback.assert_called_once_with(component)
def test_component_destroy_destroys_child_components():
component = Component(view=Mock(), presenter=Mock())
mock_cfactories = [MockComponentFactory() for _ in range(3)]
mock_children = [tcast(Mock, component.new_component(mock_cfactory)) for mock_cfactory in mock_cfactories]
component.destroy()
for mock_child in mock_children:
mock_child.destroy.assert_called_once_with()
def test_component_new_component_for_view():
mock_component = Mock()
mock_child = mock_component.new_component.return_value
mock_cfactory = Mock()
return_value = Component._new_component_for_view(mock_component, mock_cfactory)
mock_component.new_component.assert_called_once_with(mock_cfactory)
expected_return_value = (
mock_child,
mock_child.view_rep,
)
assert return_value == expected_return_value
def test_component_remove_component_for_view():
mock_component = Mock()
mock_child = Mock()
Component._remove_component_for_view(mock_component, mock_child)
mock_component.remove_component.assert_called_once_with(mock_child)
class MockComponentSymbol(ComponentSymbol):
def __init__(self, view_options: Optional[Mapping[str, Any]] = None,
presenter_options: Optional[Mapping[str, Any]] = None) -> None:
super().__init__()
self.view_cls = Mock()
self.presenter_cls = Mock()
self.view(options=view_options or {})(self.view_cls)
self.presenter(options=presenter_options or {})(self.presenter_cls)
class MockComponentFactory:
def __init__(self):
self.mock_child = MockComponent()
self.create = Mock(return_value=self.mock_child)
class MockComponent:
def __init__(self):
self.view_rep = None
self.on_destroyed = Event()
self.is_destroyed = False
self.destroy = Mock(side_effect=self.sim_destroy)
def sim_destroy(self):
self.is_destroyed = True
self.on_destroyed.fire(self)
|