示例#1
0
def test_get_set_state(monitor, monkeypatch):
    """ Test get_state.

    """
    entry = monitor._create_default_entry('root/test', 1)
    entry.name = 'Custom'
    entry.path = 'custom'
    entry.formatting = 'This test n {root/test_loop}*{root/test2_loop}'
    entry.depend_on = ['root/test_loop', 'root/test2_loop']
    monitor.custom_entries.append(entry)

    rule = FormatRule(id='Test', suffixes=['loop', 'index'],
                      new_entry_suffix='progress',
                      new_entry_formatting='{index}/{loop}')
    monitor.rules.append(rule)

    monitor.rules.append(monitor._plugin.build_rule('Measure entries'))

    monitor.handle_database_change(('added', 'root/test_loop', 10))
    monitor.handle_database_change(('added', 'root/test2_index', 1))
    monitor.handle_database_change(('added', 'root/test_index', 1))
    monitor.handle_database_change(('added', 'root/test2_loop', 10))

    state = monitor.get_state()

    assert 'rule_0' in state
    rule = state['rule_0']
    assert (rule == {'class_id': 'ecpy.FormatRule', 'id': 'Test',
                     'description': '',
                     'hide_entries': 'True',
                     'suffixes': "[u'loop', u'index']",
                     'new_entry_suffix': 'progress',
                     'new_entry_formatting': '{index}/{loop}'})

    assert 'custom_0' in state
    custom = state['custom_0']
    aux = {'name': 'Custom', 'path': 'custom',
           'formatting': 'This test n {root/test_loop}*{root/test2_loop}',
           'depend_on': "[u'root/test_loop', u'root/test2_loop']"}
    assert custom == aux

    assert (state['displayed'] ==
            repr([e.path for e in monitor.displayed_entries]))
    assert (state['undisplayed'] ==
            repr([e.path for e in monitor.undisplayed_entries]))
    assert (state['hidden'] ==
            repr([e.path for e in monitor.hidden_entries]))

    monitor._clear_state()
    import ecpy.measure.monitors.text_monitor.monitor as mod
    monkeypatch.setattr(mod, 'information', lambda *args, **kwargs: None)
    monitor.set_state(state, {'root/test_loop': 10, 'root/test2_index': 1,
                              'root/test_index': 1, 'root/test2_loop': 10,
                              'root/r': 1})

    assert monitor.rules
    assert monitor.rules[0].id == 'Test'
    assert (sorted([e.path for e in monitor.displayed_entries]) ==
            sorted(['custom', 'root/test_progress', 'root/test2_progress',
                    'root/r']))
示例#2
0
def test_format_rule(monitor):
    """Test simplifying some entries through formatting.

    """
    f = FormatRule(suffixes=['index', 'number'], new_entry_suffix='progress',
                   new_entry_formatting='{index}/{number}')

    f.try_apply('root/test_value', monitor)
    assert len(monitor.displayed_entries) == 3

    f.try_apply('root/test_index', monitor)
    assert len(monitor.displayed_entries) == 2
    assert len(monitor.hidden_entries) == 2

    f.hide_entries = False

    monitor.handle_database_change(('added', 'root/simp/test_index', 0))
    f.try_apply('root/simp/test_index', monitor)
    assert len(monitor.displayed_entries) == 3

    monitor.handle_database_change(('added', 'root/simp/test_number', 0))
    f.try_apply('root/simp/test_number', monitor)
    assert len(monitor.displayed_entries) == 5
    assert len(monitor.hidden_entries) == 2
示例#3
0
def test_process_news(monitor):
    """ Test processing news coming from a database.

    """
    rule = FormatRule(id='Test', suffixes=['loop', 'index'],
                      new_entry_suffix='progress',
                      new_entry_formatting='{index}/{loop}',
                      hide_entries=False)
    monitor.rules.append(rule)
    monitor.handle_database_change(('added', 'root/test_loop', 10))
    monitor.handle_database_change(('added', 'root/test_index', 1))

    monitor.process_news(('root/test_index', 2))
    process_app_events()
    assert monitor.displayed_entries[0].value == '10'
    assert monitor.displayed_entries[1].value == '2'
    assert monitor.displayed_entries[2].value == '2/10'
示例#4
0
def test_clear_state(monitor):
    """ Test clearing the monitor state.

    """
    rule = FormatRule(id='Test', suffixes=['loop', 'index'],
                      new_entry_suffix='progress',
                      new_entry_formatting='{index}/{loop}')
    monitor.rules.append(rule)
    monitor.handle_database_change(('added', 'root/test_loop', 10))
    monitor.handle_database_change(('added', 'root/test2_index', 1))
    monitor.handle_database_change(('added', 'root/test_index', 1))

    monitor._clear_state()
    assert not monitor.displayed_entries
    assert not monitor.undisplayed_entries
    assert not monitor.hidden_entries
    assert not monitor.updaters
    assert not monitor.custom_entries
    assert not monitor.monitored_entries
示例#5
0
def test_plugin_save_rule(text_monitor_plugin):
    """Test adding a new rule definition to a plugin.

    """

    rule = FormatRule(id='Test', suffixes=['loop', 'index'],
                      new_entry_suffix='progress',
                      new_entry_formatting='{index}/{loop}')

    text_monitor_plugin.save_rule(rule)

    assert 'Test' in text_monitor_plugin.rules
    rule_conf = text_monitor_plugin._user_rules['Test']
    assert rule_conf == {'class_id': 'ecpy.FormatRule',
                         'id': 'Test',
                         'description': '',
                         'hide_entries': 'True',
                         'suffixes': repr(['loop', 'index']),
                         'new_entry_suffix': 'progress',
                         'new_entry_formatting': '{index}/{loop}'}
示例#6
0
def test_clear_state(monitor, database):
    """Test clearing the monitor state.

    """
    rule = FormatRule(id='Test',
                      suffixes=['loop', 'index'],
                      new_entry_suffix='progress',
                      new_entry_formatting='{index}/{loop}')
    monitor.rules.append(rule)
    database.observe('notifier', monitor.handle_database_entries_change)
    database.set_value('root', 'test_loop', 10)
    database.set_value('root', 'test_index', 1)
    database.set_value('root', 'test2_index', 1)

    monitor._clear_state()
    assert not monitor.displayed_entries
    assert not monitor.undisplayed_entries
    assert not monitor.hidden_entries
    assert not monitor.updaters
    assert not monitor.custom_entries
    assert not monitor.monitored_entries
示例#7
0
def test_handle_database_change5(monitor, database):
    """Test handling entries being renamed.

    """
    rule = FormatRule(id='Test',
                      suffixes=['loop', 'index'],
                      new_entry_suffix='progress',
                      new_entry_formatting='{index}/{loop}')
    monitor.rules.append(rule)
    database.observe('notifier', monitor.handle_database_entries_change)
    database.set_value('root', 'test_loop', 10)
    database.set_value('root', 'test_index', 10)

    old_updaters = monitor.updaters.copy()

    database.rename_values('root', ['test_loop', 'test_index'],
                           ['new_loop', 'new_index'])

    progress_entry = monitor.displayed_entries[0]
    assert progress_entry.name == 'new_progress'
    assert (sorted(progress_entry.depend_on) == sorted(
        ['root/new_index', 'root/new_loop']))

    assert len(monitor.hidden_entries) == 2
    for e in monitor.hidden_entries:
        assert e.name in ('new_index', 'new_loop')
        assert e.path in ('root/new_index', 'root/new_loop')
        assert e.depend_on[0] in ('root/new_index', 'root/new_loop')

    assert len(monitor.updaters) == 2
    assert (monitor.updaters['root/new_index'] is
            old_updaters['root/test_index'])
    assert (monitor.updaters['root/new_loop'] is
            old_updaters['root/test_loop'])

    assert len(monitor._database_values) == 2
    assert 'root/new_index' in monitor._database_values
    assert 'root/new_loop' in monitor._database_values
示例#8
0
def test_process_news(monitor, database):
    """ Test processing news coming from a database.

    """
    rule = FormatRule(id='Test',
                      suffixes=['loop', 'index'],
                      new_entry_suffix='progress',
                      new_entry_formatting='{index}/{loop}',
                      hide_entries=False)
    monitor.rules.append(rule)
    database.observe('notifier', monitor.handle_database_entries_change)
    database.set_value('root', 'test_loop', 10)
    database.set_value('root', 'test_index', 1)

    monitor.process_news(('root/test_index', 2))
    process_app_events()
    assert monitor.displayed_entries[0].value == '10'
    assert monitor.displayed_entries[1].value == '2'
    assert monitor.displayed_entries[2].value == '2/10'

    monitor.updaters = {}
    monitor.process_news(('root/test_index', 2))
    process_app_events()
示例#9
0
def test_format_rule(monitor):
    """Test simplifying some entries through formatting.

    """
    f = FormatRule(suffixes=['index', 'number'],
                   new_entry_suffix='progress',
                   new_entry_formatting='{index}/{number}')

    f.try_apply('root/test_value', monitor)
    assert len(monitor.displayed_entries) == 3

    f.try_apply('root/test_index', monitor)
    assert len(monitor.displayed_entries) == 2
    assert len(monitor.hidden_entries) == 2

    f.hide_entries = False

    monitor.handle_database_entries_change(
        ('added', 'root/simp/test_index', 0))
    f.try_apply('root/simp/test_index', monitor)
    assert len(monitor.displayed_entries) == 3

    monitor.handle_database_entries_change(
        ('added', 'root/simp/test_number', 0))
    f.try_apply('root/simp/test_number', monitor)
    assert len(monitor.displayed_entries) == 5
    assert len(monitor.hidden_entries) == 2
示例#10
0
def test_format_rule_editor(windows, plugin, dialog_sleep):
    """Test editing a format rule.

    """
    r = FormatRule(suffixes=['foo', 'bar'],
                   new_entry_suffix='barfoo',
                   new_entry_formatting='{bar}/{foo}')
    w = FormatRuleView(plugin=plugin, rule=r)

    # Test editing suffixes
    window = ContainerTestingWindow(widget=w)
    window.show()
    process_app_events()
    widget = w.widgets()[-6]
    assert widget.text == 'foo, bar'
    sleep(dialog_sleep)

    widget.text = 'bar'
    process_app_events()
    assert r.suffixes == ['bar']
    sleep(dialog_sleep)

    r.suffixes = ['foo']
    process_app_events()
    assert widget.text == 'foo'
    sleep(dialog_sleep)

    widget.text = 'bar, foo, barfoo'
    process_app_events()
    assert r.suffixes == ['bar', 'foo', 'barfoo']
    sleep(dialog_sleep)

    # Set new suffix
    widget = w.widgets()[-4]
    assert widget.text == 'barfoo'
    widget.text = 'foobar'
    process_app_events()
    assert r.new_entry_suffix == 'foobar'
    sleep(dialog_sleep)

    # Set new formatting
    widget = w.widgets()[-2]
    assert widget.text == '{bar}/{foo}'
    widget.text = '{foo}/{bar}'
    process_app_events()
    assert r.new_entry_formatting == '{foo}/{bar}'
    sleep(dialog_sleep)

    # Set hide entries
    widget = w.widgets()[-1]
    assert widget.checked
    widget.checked = False
    process_app_events()
    assert not r.hide_entries
    sleep(dialog_sleep)

    # Test validate function
    r.suffixes = ['foo', 'bar']
    assert w.validate()[0]
    r.suffixes = []
    assert not w.validate()[0]
    r.suffixes = ['foo', 'bar']
    assert w.validate()

    r.new_entry_suffix = ''
    assert not w.validate()[0]
    r.new_entry_suffix = 'foobar'
    assert w.validate()[0]

    r.new_entry_formatting = '{foo}'
    assert not w.validate()[0]
示例#11
0
def test_handle_database_change3(app, monitor):
    """ Test handling the adding of entries subject to a format rule.

    """
    rule = FormatRule(id='Test', suffixes=['loop', 'index'],
                      new_entry_suffix='progress',
                      new_entry_formatting='{index}/{loop}')
    monitor.rules.append(rule)
    monitor.handle_database_change(('added', 'root/test_loop', 10))

    assert monitor.monitored_entries == ['root/test_loop']
    assert len(monitor.displayed_entries) == 1
    assert not monitor.undisplayed_entries
    assert not monitor.hidden_entries
    assert monitor.displayed_entries[0].depend_on == ['root/test_loop']
    assert monitor._database_values == {'root/test_loop': 10}
    assert 'root/test_loop' in monitor.updaters

    monitor.handle_database_change(('added', 'root/test2_index', 1))

    assert (monitor.monitored_entries == ['root/test_loop',
                                          'root/test2_index'])
    assert len(monitor.displayed_entries) == 2
    assert not monitor.undisplayed_entries
    assert not monitor.hidden_entries
    assert (monitor._database_values == {'root/test_loop': 10,
                                         'root/test2_index': 1})

    monitor.handle_database_change(('added', 'root/test_index', 1))

    assert (monitor.monitored_entries == ['root/test_loop',
                                          'root/test2_index',
                                          'root/test_index'])
    assert len(monitor.displayed_entries) == 2
    assert not monitor.undisplayed_entries
    assert len(monitor.hidden_entries) == 2
    assert (monitor._database_values == {'root/test_loop': 10,
                                         'root/test2_index': 1,
                                         'root/test_index': 1})
    assert len(monitor.updaters['root/test_loop']) == 1
    assert len(monitor.updaters['root/test_index']) == 1

    entry = monitor.displayed_entries[0]
    if entry.name != 'test_progress':
        entry = monitor.displayed_entries[1]

    assert entry.name == 'test_progress'
    assert entry.path == 'root/test_progress'
    assert entry.depend_on == ['root/test_loop', 'root/test_index']
    assert entry.formatting == '{root/test_index}/{root/test_loop}'
    entry.update(monitor._database_values)
    process_app_events()
    assert entry.value == '1/10'

    rule.hide_entries = False
    monitor.handle_database_change(('added', 'root/test2_loop', 10))
    assert (monitor.monitored_entries == ['root/test_loop',
                                          'root/test2_index',
                                          'root/test_index',
                                          'root/test2_loop'])
    assert len(monitor.displayed_entries) == 4
    assert not monitor.undisplayed_entries
    assert len(monitor.hidden_entries) == 2
    assert (monitor._database_values == {'root/test_loop': 10,
                                         'root/test2_index': 1,
                                         'root/test_index': 1,
                                         'root/test2_loop': 10})
    assert len(monitor.updaters['root/test2_loop']) == 2
    assert len(monitor.updaters['root/test2_index']) == 2
示例#12
0
def test_format_rule_editor(windows, plugin, dialog_sleep):
    """Test editing a format rule.

    """
    r = FormatRule(suffixes=['foo', 'bar'], new_entry_suffix='barfoo',
                   new_entry_formatting='{bar}/{foo}')
    w = FormatRuleView(plugin=plugin, rule=r)

    # Test editing suffixes
    window = ContainerTestingWindow(widget=w)
    window.show()
    process_app_events()
    widget = w.widgets()[-6]
    assert widget.text == 'foo, bar'
    sleep(dialog_sleep)

    widget.text = 'bar'
    process_app_events()
    assert r.suffixes == ['bar']
    sleep(dialog_sleep)

    r.suffixes = ['foo']
    process_app_events()
    assert widget.text == 'foo'
    sleep(dialog_sleep)

    widget.text = 'bar, foo, barfoo'
    process_app_events()
    assert r.suffixes == ['bar', 'foo', 'barfoo']
    sleep(dialog_sleep)

    # Set new suffix
    widget = w.widgets()[-4]
    assert widget.text == 'barfoo'
    widget.text = 'foobar'
    process_app_events()
    assert r.new_entry_suffix == 'foobar'
    sleep(dialog_sleep)

    # Set new formatting
    widget = w.widgets()[-2]
    assert widget.text == '{bar}/{foo}'
    widget.text = '{foo}/{bar}'
    process_app_events()
    assert r.new_entry_formatting == '{foo}/{bar}'
    sleep(dialog_sleep)

    # Set hide entries
    widget = w.widgets()[-1]
    assert widget.checked
    widget.checked = False
    process_app_events()
    assert not r.hide_entries
    sleep(dialog_sleep)

    # Test validate function
    r.suffixes = ['foo', 'bar']
    assert w.validate()[0]
    r.suffixes = []
    assert not w.validate()[0]
    r.suffixes = ['foo', 'bar']
    assert w.validate()

    r.new_entry_suffix = ''
    assert not w.validate()[0]
    r.new_entry_suffix = 'foobar'
    assert w.validate()[0]

    r.new_entry_formatting = '{foo}'
    assert not w.validate()[0]
示例#13
0
def test_get_set_state(monitor, monkeypatch, measure, database):
    """ Test get_state.

    """
    entry = monitor._create_default_entry('root/test', 1)
    entry.name = 'Custom'
    entry.path = 'custom'
    entry.formatting = 'This test n {root/test_loop}*{root/test2_loop}'
    entry.depend_on = ['root/test_loop', 'root/test2_loop']
    monitor.custom_entries.append(entry)

    rule = FormatRule(id='Test',
                      suffixes=['loop', 'index'],
                      new_entry_suffix='progress',
                      new_entry_formatting='{index}/{loop}')
    monitor.rules.append(rule)

    monitor.rules.append(monitor._plugin.build_rule('Measure entries'))

    database.observe('notifier', monitor.handle_database_entries_change)
    database.set_value('root', 'test_loop', 10)
    database.set_value('root', 'test_index', 1)
    database.set_value('root', 'test2_index', 1)
    database.set_value('root', 'test2_loop', 10)

    state = monitor.get_state()

    assert 'rule_0' in state
    rule = state['rule_0']
    assert (rule == {
        'class_id': 'ecpy.FormatRule',
        'id': 'Test',
        'description': '',
        'hide_entries': 'True',
        'suffixes': repr([u'loop', u'index']),
        'new_entry_suffix': 'progress',
        'new_entry_formatting': '{index}/{loop}'
    })

    assert 'custom_0' in state
    custom = state['custom_0']
    aux = {
        'name': 'Custom',
        'path': 'custom',
        'formatting': 'This test n {root/test_loop}*{root/test2_loop}',
        'depend_on': repr([u'root/test_loop', u'root/test2_loop'])
    }
    assert custom == aux

    assert (state['displayed'] == repr(
        [e.path for e in monitor.displayed_entries]))
    assert (state['undisplayed'] == repr(
        [e.path for e in monitor.undisplayed_entries]))
    assert (state['hidden'] == repr([e.path for e in monitor.hidden_entries]))

    monitor._clear_state()
    import ecpy.measure.monitors.text_monitor.monitor as mod
    monkeypatch.setattr(mod, 'information',
                        lambda *args, **kwargs: print(args, kwargs))

    monitor.set_state(state)

    assert monitor.rules
    assert monitor.rules[0].id == 'Test'
    assert monitor._state

    state2 = monitor.get_state()
    assert state == state2

    from ecpy.tasks.tasks.database import TaskDatabase
    monkeypatch.setattr(
        TaskDatabase, 'list_all_entries', lambda *args, **kwargs: {
            'root/test_loop': 10,
            'root/test2_index': 1,
            'root/test_index': 1,
            'root/test2_loop': 10,
            'root/r': 1
        })
    monitor.link_to_measure(measure)

    assert not monitor._state
    print(sorted([e.path for e in monitor.displayed_entries]))
    assert (sorted([e.path for e in monitor.displayed_entries]) == sorted(
        ['custom', 'root/test_progress', 'root/test2_progress', 'root/r']))