Exemplo n.º 1
0
def test__apply_component_default_configuration():

    class UnladenSwallow:

        configuration_defaults = {
            'unladen_swallow': {
                'airspeed_velocity': 11,
            }
        }

    us = UnladenSwallow()
    config = build_simulation_configuration()
    assert 'unladen_swallow' not in config
    _apply_component_default_configuration(config, us)
    assert config.unladen_swallow.metadata('airspeed_velocity') == [
        {'layer': 'component_configs', 'value': 11,
         'source': os.path.realpath(__file__), 'default': False}
    ]

    us = UnladenSwallow()
    us.__module__ = '__main__'
    config = build_simulation_configuration()
    assert 'unladen_swallow' not in config
    _apply_component_default_configuration(config, us)
    assert config.unladen_swallow.metadata('airspeed_velocity') == [
        {'layer': 'component_configs', 'value': 11, 'source': '__main__', 'default': False}
    ]
Exemplo n.º 2
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))
Exemplo n.º 3
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)
def base_config():
    config = build_simulation_configuration()

    config.update(
        {
            'time': {
                'start': {
                    'year': 2011
                },
                'end': {
                    'year': 2012
                },
                'step_size': 10
            },
            'randomness': {
                'key_columns': ['entrance_time', 'age']
            },
            'input_data': {
                'location': 'Kenya'
            },
        },
        source=str(Path(__file__).resolve()),
        layer='model_override')

    return config
Exemplo n.º 5
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
Exemplo n.º 6
0
def test__setup_components(mocker, apply_default_config_mock):
    config = build_simulation_configuration()
    builder = mocker.Mock()

    components = [None, MockComponentA('Eric'),  MockComponentB('half', 'a', 'bee')]

    with pytest.raises(ComponentConfigError):
        _setup_components(builder, components, config)

    duplicate = MockComponentA('Eric')
    components = [duplicate, duplicate]
    finished = _setup_components(builder, components, config)

    apply_default_config_mock.assert_called_once_with(config, duplicate)
    assert [duplicate] == finished
    apply_default_config_mock.reset_mock()

    components = [MockComponentA('Eric'), MockComponentB('half', 'a', 'bee')]
    finished = _setup_components(builder, components, config)
    mock_a, mock_b = finished

    assert apply_default_config_mock.mock_calls == [mocker.call(config, component) for component in finished]

    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.args == ('half', 'a', 'bee')
Exemplo n.º 7
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
Exemplo n.º 8
0
def test_ComponentConfigurationParser_get_components(
        import_and_instantiate_mock, components):
    config = build_simulation_configuration()
    config.update(components)

    parser = ComponentConfigurationParser()
    parser.get_components(config.components)

    import_and_instantiate_mock.assert_called_once_with(
        TEST_COMPONENTS_PREPPED)
Exemplo n.º 9
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
Exemplo n.º 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)
Exemplo n.º 11
0
def test_build_simulation_configuration(mocker, test_user_config):
    expand_user_mock = mocker.patch(
        'vivarium.framework.configuration.Path.expanduser')
    expand_user_mock.return_value = test_user_config

    config = build_simulation_configuration()

    assert expand_user_mock.called_once_with('~/vivarium.yaml')

    with test_user_config.open() as f:
        data = yaml.full_load(f)

    assert config.to_dict() == data
Exemplo n.º 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)
Exemplo n.º 13
0
def base_config():
    config = build_simulation_configuration()
    config.update(
        {
            'time': {
                'start': {
                    'year': 1990,
                },
                'end': {
                    'year': 2010
                },
                'step_size': 30.5
            }
        }, **metadata(__file__))
    return config
Exemplo n.º 14
0
def test_build_simulation_configuration(mocker):
    test_dir = os.path.dirname(os.path.realpath(__file__))
    user_config = test_dir + '/../test_data/mock_user_config.yaml'

    expand_user_mock = mocker.patch(
        'vivarium.framework.configuration.os.path.expanduser')
    expand_user_mock.return_value = user_config

    config = build_simulation_configuration()

    assert expand_user_mock.called_once_with('~/vivarium.yaml')

    with open(user_config) as f:
        data = yaml.load(f)

    assert config.to_dict() == data
Exemplo n.º 15
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}
Exemplo n.º 16
0
def initialize_simulation(components, input_config=None, plugin_config=None):
    config = build_simulation_configuration()
    config.update(input_config)
    plugin_manager = PluginManager(plugin_config)

    return InteractiveContext(config, components, plugin_manager)