Example #1
0
def test_import_sections_on_event(config_with_modules, freezer):
    config_with_modules['module/weekday_module']['on_event'][
        'import_context'] = [{
            'to_section': 'week',
            'from_path': 'astrality/tests/templates/weekday.yml',
            'from_section': '{event}',
        }]
    config_with_modules.pop('module/solar_module')
    module_manager = ModuleManager(config_with_modules)

    assert module_manager.application_context['fonts'] == {
        1: 'FuraCode Nerd Font'
    }

    sunday = datetime(year=2018, month=2, day=4)
    freezer.move_to(sunday)
    module_manager.finish_tasks()

    # Make application_context comparisons easier
    module_manager.application_context.pop('env')

    # Startup does not count as a event, so no context has been imported
    assert module_manager.application_context == {
        'fonts': Resolver({1: 'FuraCode Nerd Font'}),
    }

    monday = datetime(year=2018, month=2, day=5)
    freezer.move_to(monday)
    module_manager.finish_tasks()

    # The event has now changed, so context should be imported
    assert module_manager.application_context == {
        'fonts': Resolver({1: 'FuraCode Nerd Font'}),
        'week': Resolver({'day': 'monday'}),
    }
Example #2
0
 def test_update(self):
     one_conf_dict = {
         'key1': 'value1',
         1: 'value2',
         2: {
             1: 'some_value'
         },
     }
     another_conf_dict = {
         'key3': ('one', 'two', 'three'),
         'key4': {
             1: 'uno',
             'key4-2': 'dos'
         }
     }
     merged_conf_dicts = {
         'key1': 'value1',
         1: 'value2',
         2: {
             1: 'some_value'
         },
         'key3': ('one', 'two', 'three'),
         'key4': {
             1: 'uno',
             'key4-2': 'dos'
         }
     }
     config = Resolver(one_conf_dict)
     config.update(another_conf_dict)
     assert config == merged_conf_dicts
Example #3
0
 def test_items(self):
     config = Resolver()
     config['4'] = 'test'
     config['font'] = 'Comic Sans'
     config['5'] = '8'
     assert list(config.items()) == [(
         '4',
         'test',
     ), (
         'font',
         'Comic Sans',
     ), (
         '5',
         '8',
     )]
Example #4
0
    def test_accessing_existing_key(self):
        config = Resolver()
        config['some_key'] = 'some_value'
        assert config['some_key'] == 'some_value'

        config[-2] = 'some_other_value'
        assert config[-2] == 'some_other_value'
Example #5
0
 def test_integer_index_resolution_without_earlier_index_key(self):
     config = Resolver()
     config['some_key'] = 'some_value'
     with pytest.raises(KeyError) as exception:
         config[2]
     assert exception.value.args[0] == \
         'Integer index "2" is non-existent and ' \
         'had no lower index to be substituted for'
Example #6
0
def test_import_sections_on_startup(config_with_modules, freezer):
    # Insert day the module was started into 'start day'
    config_with_modules['module/weekday_module']['on_startup'][
        'import_context'] = [{
            'to_section': 'start_day',
            'from_path': 'astrality/tests/templates/weekday.yml',
            'from_section': '{event}',
        }]

    # Insert the current day into 'day_now'
    config_with_modules['module/weekday_module']['on_event'][
        'import_context'] = [{
            'to_section': 'day_now',
            'from_path': 'astrality/tests/templates/weekday.yml',
            'from_section': '{event}',
        }]
    config_with_modules.pop('module/solar_module')
    module_manager = ModuleManager(config_with_modules)

    # Remove 'env' context for easier comparisons
    module_manager.application_context.pop('env')

    # Before finishing tasks, no context sections are imported
    assert module_manager.application_context['fonts'] == {
        1: 'FuraCode Nerd Font'
    }

    # Start module on a monday
    sunday = datetime(year=2018, month=2, day=4)
    freezer.move_to(sunday)
    module_manager.finish_tasks()
    assert module_manager.application_context == {
        'fonts': Resolver({1: 'FuraCode Nerd Font'}),
        'start_day': Resolver({'day': 'sunday'}),
    }

    # 'now_day' should now be added, but 'start_day' should remain unchanged
    monday = datetime(year=2018, month=2, day=5)
    freezer.move_to(monday)
    module_manager.finish_tasks()
    assert module_manager.application_context == {
        'fonts': Resolver({1: 'FuraCode Nerd Font'}),
        'start_day': Resolver({'day': 'sunday'}),
        'day_now': Resolver({'day': 'monday'}),
    }
Example #7
0
    def test_resolver_class(self):
        resolver = Resolver()
        resolver[1] = 'firs_value'
        resolver[2] = 'second_value'
        resolver['string_key'] = 'string_value'

        assert resolver[1] == 'firs_value'
        assert resolver[2] == 'second_value'
        assert resolver[3] == 'second_value'
        assert resolver['string_key'] == 'string_value'
Example #8
0
    def test_getter(self):
        config = Resolver()
        assert config.get('from_empty_config') is None

        config['test'] = 'something'
        assert config.get('test') == 'something'
        assert config.get('test', '4') == 'something'

        assert config.get('non_existent_key') is None
        assert config.get('non_existent_key', '4') == '4'
Example #9
0
 def test_initialization_of_config_class_with_dict(self):
     conf_dict = {
         'key1': 'value1',
         'key2': 'value2',
         'key3': ('one', 'two', 'three'),
         'key4': {
             'key4-1': 'uno',
             'key4-2': 'dos'
         }
     }
     config = Resolver(conf_dict)
     assert config == conf_dict
Example #10
0
    def test_updating_resolver_with_resolver(self):
        resolver1 = Resolver({'key1': 1})
        resolver2 = Resolver({'key2': 2})

        resolver1.update(resolver2)
        expected_result = Resolver({'key1': 1, 'key2': 2})
        assert resolver1 == expected_result
Example #11
0
    def test_values_for_max_key_property(self):
        config = Resolver()
        assert config._max_key == -inf

        config['string_key'] = 1
        assert config._max_key == -inf

        config[2] = 'string_value'
        assert config._max_key == 2

        config[1] = 'string_value'
        assert config._max_key == 2

        config[3] = 'string_value'
        assert config._max_key == 3
Example #12
0
def context(config: ApplicationConfig) -> Context:
    """
    Return a context dictionary based on the contents of a config dict.

    Only sections named context/* are considered to be context sections, and
    these sections are returned with 'context/' stripped away, and with their
    contents cast to a Resolver instance.
    """
    contents: Context = {}
    for section_name, section in config.items():
        if not isinstance(section_name, str) or \
           not len(section_name) > 8 or \
           not section_name[:8].lower() == 'context/':
            continue
        else:
            category = section_name[8:]
            contents[category] = Resolver(section)

    return contents
Example #13
0
    def test_use_of_recursive_config_objects_created_by_dicts(self):
        conf_dict = {
            'key1': 'value1',
            1: 'value2',
            2: {
                1: 'some_value'
            },
            'key3': ('one', 'two', 'three'),
            'key4': {
                1: 'uno',
                'key4-2': 'dos'
            }
        }
        config = Resolver(conf_dict)
        assert config == conf_dict
        assert config[3][2] == 'some_value'
        assert config[2] == {1: 'some_value'}
        assert config[3] == {1: 'some_value'}

        assert isinstance(config['key4'], Resolver)
        assert config['key4'] == {1: 'uno', 'key4-2': 'dos'}
        assert config['key4'][1] == 'uno'
        assert config['key4'][2] == 'uno'
Example #14
0
 def test_invocation_of_class_with_application_config(self, conf):
     Resolver(conf)
Example #15
0
def test_integer_indexed_templates(jinja_test_env):
    template = jinja_test_env.get_template('integer_indexed')
    context = Resolver({'section': {1: 'one', 2: 'two'}})
    assert template.render(context) == 'one\ntwo\ntwo'
Example #16
0
 def test_initialization_of_config_class_with_no_config_parser(self):
     Resolver()
Example #17
0
 def test_index_resolution_with_string_key(self):
     config = Resolver()
     config[2] = 'some_value'
     with pytest.raises(KeyError) as exception:
         config['test']
     assert exception.value.args[0] == 'test'
Example #18
0
 def test_keys(self):
     config = Resolver()
     config['4'] = 'test'
     config['font'] = 'Comic Sans'
     config['5'] = '8'
     assert list(config.keys()) == ['4', 'font', '5']
Example #19
0
 def test_integer_index_resolution(self):
     config = Resolver()
     config['some_key'] = 'some_value'
     config[1] = 'FureCode Nerd Font'
     assert config[2] == 'FureCode Nerd Font'
Example #20
0
 def test_values(self):
     config = Resolver()
     config['4'] = 'test'
     config['font'] = 'Comic Sans'
     config['5'] = '8'
     assert list(config.values()) == ['test', 'Comic Sans', '8']
Example #21
0
 def test_getting_item_from_empty_config(self):
     config = Resolver()
     with pytest.raises(KeyError) as exception:
         config['empty_config_with_no_key']
Example #22
0
 def test_initializing_resolver_with_resolver(self):
     resolver1 = Resolver({'key1': 1})
     resolver2 = Resolver(resolver1)
     assert resolver1 == resolver2