Пример #1
0
def test_manager_get_file():
    class Test:
        pass

    t = Test()
    assert ComponentManager._get_file(t) == __file__
    t.__module__ = '__main__'
    assert ComponentManager._get_file(t) == '__main__'
Пример #2
0
def test_apply_configuration_defaults_no_op():
    config = build_simulation_configuration()
    c = config.to_dict()
    cm = ComponentManager()
    cm.configuration = config
    component = MockComponentA()

    cm.apply_configuration_defaults(component)
    assert config.to_dict() == c
Пример #3
0
def test_apply_configuration_defaults():
    config = build_simulation_configuration()
    c = config.to_dict()
    cm = ComponentManager()
    cm.configuration = config
    component = MockGenericComponent('test_component')

    cm.apply_configuration_defaults(component)
    c.update(component.configuration_defaults)
    assert config.to_dict() == c
Пример #4
0
def test_setup_components(mocker):
    builder = mocker.Mock()
    mock_a = MockComponentA('test_a')
    mock_b = MockComponentB('test_b')
    components = OrderedComponentSet(mock_a, mock_b)
    ComponentManager._setup_components(builder, components)

    assert mock_a.builder_used_for_setup is None  # class has no setup method
    assert mock_b.builder_used_for_setup is builder

    builder.value.register_value_modifier.assert_called_once_with('metrics', mock_b.metrics)
Пример #5
0
def test_ComponentManager_add_components_unnamed(components):
    config = build_simulation_configuration()
    cm = ComponentManager()
    cm.configuration = config
    with pytest.raises(ComponentConfigError, match='no name'):
        cm.add_managers(components)

    config = build_simulation_configuration()
    cm = ComponentManager()
    cm.configuration = config
    with pytest.raises(ComponentConfigError, match='no name'):
        cm.add_components(components)
Пример #6
0
def test_manager_init():
    m = ComponentManager()
    assert not m._managers
    assert not m._components
    assert not m.configuration
    assert m.name == 'component_manager'
    assert repr(m) == str(m) == 'ComponentManager()'
Пример #7
0
def test_flatten_with_sub_components():
    components = []
    for i in range(5):
        name, *args = [str(5*i + j) for j in range(5)]
        components.append(MockComponentB(*args, name=name))
    out = ComponentManager._flatten(components)
    expected = [MockComponentB(name=str(i)) for i in range(25)]
    assert out == expected
Пример #8
0
def test_flatten_with_lists():
    components = []
    for i in range(5):
        for j in range(5):
            components.append(MockComponentA(name=str(5*i + j)))
    out = ComponentManager._flatten(components)
    expected = [MockComponentA(name=str(i)) for i in range(25)]
    assert out == expected
Пример #9
0
def test_flatten_with_nested_sub_components():
    def nest(start, depth):
        if depth == 1:
            return MockComponentA(name=str(start))
        c = MockComponentA(name=str(start))
        c.sub_components = [nest(start + 1, depth - 1)]
        return c

    components = []
    for i in range(5):
        components.append(nest(5*i, 5))
    out = ComponentManager._flatten(components)
    expected = [MockComponentA(name=str(i)) for i in range(25)]
    assert out == expected

    # Lists with nested subcomponents
    out = ComponentManager._flatten([components, components])
    assert out == 2*expected
Пример #10
0
def test_apply_configuration_defaults_duplicate():
    config = build_simulation_configuration()
    c = config.to_dict()
    cm = ComponentManager()
    cm.configuration = config
    component = MockGenericComponent('test_component')

    cm.apply_configuration_defaults(component)
    cm._components.add(component)
    with pytest.raises(ComponentConfigError, match='but it has already been set'):
        cm.apply_configuration_defaults(component)
Пример #11
0
def test_add_components():
    config = build_simulation_configuration()
    cm = ComponentManager()
    cm.configuration = config

    assert not cm._managers
    managers = [MockGenericComponent(f'manager_{i}') for i in range(5)]
    components = [MockGenericComponent(f'component_{i}') for i in range(5)]
    cm.add_managers(managers)
    cm.add_components(components)
    assert cm._managers == OrderedComponentSet(*managers)
    assert cm._components == OrderedComponentSet(*components)
    for c in managers + components:
        assert config[c.name].to_dict() == c.configuration_defaults[c.name]

    assert cm.list_components() == {c.name: c for c in components}
Пример #12
0
def test_apply_configuration_defaults_bad_structure():
    config = build_simulation_configuration()
    c = config.to_dict()
    cm = ComponentManager()
    cm.configuration = config
    component1 = MockGenericComponent('test_component')
    component2 = MockComponentA(name='test_component2')
    component2.configuration_defaults = {'test_component': 'val'}

    cm.apply_configuration_defaults(component1)
    cm._components.add(component1)
    with pytest.raises(ComponentConfigError, match='attempting to alter the structure'):
        cm.apply_configuration_defaults(component2)
Пример #13
0
def test_ComponentManager_add_components():
    manager = ComponentManager()

    components = [None, MockComponentA('Eric'), MockComponentB('half', 'a', 'bee')]
    for list_type in ['_managers', '_components']:
        manager._add_components(getattr(manager, list_type), components)
        assert getattr(manager, list_type) == components
        setattr(manager, list_type, [])

    components.append(components[:])
    for list_type in ['_managers', '_components']:
        manager._add_components(getattr(manager, list_type), components)
        assert getattr(manager, list_type) == 2*components[:-1]
Пример #14
0
def test_ComponentManager__setup_components(mocker):
    config = build_simulation_configuration()
    manager = ComponentManager()
    builder = mocker.Mock()
    builder.components = manager

    manager.add_components([None, MockComponentA('Eric'),
                            MockComponentB('half', 'a', 'bee')])
    with pytest.raises(ComponentConfigError):
        manager.setup_components(builder, config)

    manager._components = []
    manager.add_components([MockComponentA('Eric'), MockComponentB('half', 'a', 'bee')])
    manager.setup_components(builder, config)

    mock_a, mock_b, mock_b_child1, mock_b_child2, mock_b_child3 = manager._components

    assert mock_a.builder_used_for_setup is None  # class has no setup method
    assert mock_b.builder_used_for_setup is builder
    assert mock_b_child1.args == ('half',)
    assert mock_b_child1.builder_used_for_setup is builder
    assert mock_b_child2.args == ('a',)
    assert mock_b_child2.builder_used_for_setup is builder
    assert mock_b_child3.args == ('bee',)
    assert mock_b_child3.builder_used_for_setup is builder
Пример #15
0
def test_ComponentManager_add_components(components):
    config = build_simulation_configuration()
    cm = ComponentManager()
    cm.configuration = config
    cm.add_managers(components)
    assert cm._managers == OrderedComponentSet(*ComponentManager._flatten(components))

    config = build_simulation_configuration()
    cm = ComponentManager()
    cm.configuration = config
    cm.add_components(components)
    assert cm._components == OrderedComponentSet(*ComponentManager._flatten(components))
Пример #16
0
def test_flatten_simple():
    components = [MockComponentA(name=str(i)) for i in range(10)]
    assert ComponentManager._flatten(components) == components