Exemplo n.º 1
0
def test_databases_only():
    # Check sources not in provided sources are excluded from generated template
    ag_a = Agent('A')
    ag_b = Agent('B')
    evidences = []
    colors = []
    not_in_html = []
    for source_type, info in DEFAULT_SOURCE_COLORS:
        for n, source in enumerate(info['sources']):
            # Only get 4 first sources for each type
            if n < 4 and source_type == 'databases':
                ev = Evidence(source_api=source,
                              text=f'Evidence from {source}')
                evidences.append(ev)
                colors.append(info['sources'][source])
            else:
                not_in_html.append(source)
    stmt = Activation(ag_a, ag_b, evidence=evidences)
    ha = HtmlAssembler(statements=[stmt])
    ha.save_model('./temp_no_show_db_only.html', show_only_available=True)
    with open('./temp_no_show_db_only.html') as fh:
        no_show_html = fh.read()
    assert all(color in no_show_html for color in colors)

    badge_class = 'class="badge badge-source source-{src}"'
    assert all(
        badge_class.format(src=src) not in no_show_html for src in not_in_html)
Exemplo n.º 2
0
def test_assembler():
    stmt = make_stmt()
    ha = HtmlAssembler([stmt])
    result = ha.make_model()
    assert isinstance(result, str)
    # Read from the template file and make sure the beginning and end of the
    # content matches
    template, _, _ = loader.get_source(None, 'indra/template.html')
    assert result.startswith(template[0:100])
    # Make sure assembler works with other parameters provided
    stmt2 = make_bad_stmt()
    ha = HtmlAssembler(source_counts={
        stmt.get_hash(): {
            'test': 1
        },
        stmt2.get_hash(): {
            'test': 1
        }
    },
                       ev_counts={
                           stmt.get_hash(): 1,
                           stmt2.get_hash(): 1
                       },
                       db_rest_url='test.db.url')
    ha.add_statements([stmt, stmt2])
    result = ha.make_model(grouping_level='agent-pair')
    assert isinstance(result, str)
    result = ha.make_model(grouping_level='statement')
    assert isinstance(result, str)
    # Check simple=False
    result = ha.make_model(grouping_level='statement', simple=False)
    assert isinstance(result, str)
    # Test belief badges
    result = ha.make_model(grouping_level='statement', show_belief=True)
    assert isinstance(result, str)
    assert '<small\n' \
           '      class="badge badge-pill badge-belief"\n' \
           '      title="Belief score for this statement">1.0</small>' in result
    result = ha.make_model(grouping_level='statement', show_belief=False)
    assert isinstance(result, str)
    assert '<small\n' \
           '      class="badge badge-pill badge-belief"\n' \
           '      title="Belief score for this statement">1</small>' \
           not in result
    # Test if source URL exists
    assert 'http://www.causalbionet.com/' in result
    # Make sure warning can be appended
    ha.append_warning('warning')
    assert ('\t<span style="color:red;">(CAUTION: warning occurred when '
            'creating this page.)</span>' in ha.model)
    # Make sure model is created before saving
    ha = HtmlAssembler([stmt])
    assert not ha.model
    ha.save_model('tempfile.html')
    assert ha.model
Exemplo n.º 3
0
def test_custom_colors_in_html():
    ag_a = Agent('A')
    ag_b = Agent('B')
    custom_sources: SourceInfo = {
        "src_a": {
            "name": "Src A",
            "link": "https://example.com/src_a",
            "type": "reader",
            "domain": "general",
            "default_style": {
                "color": "white",
                "background-color": "blue"
            }
        },
        "src_b": {
            "name": "Src B",
            "link": "https://example.com/src_b",
            "type": "database",
            "domain": "general",
            "default_style": {
                "color": "black",
                "background-color": "#bebada"
            }
        },
    }

    evidences = []
    colors = []
    sources = []
    for source, source_info in custom_sources.items():
        sources.append(source)
        ev = Evidence(source_api=source, text=f'Evidence from {source}')
        evidences.append(ev)
        colors.append(source_info['default_style']['background-color'])

    stmt = Activation(ag_a, ag_b, evidence=evidences)
    ha = HtmlAssembler(statements=[stmt], custom_sources=custom_sources)
    ha.save_model('./temp_custom_colors_simple.html')
    with open('./temp_custom_colors_simple.html') as fh:
        simple_html = fh.read()

    ha = HtmlAssembler(statements=[stmt], custom_sources=custom_sources)
    ha.save_model('./temp_not_simple.html', simple=False)
    with open('./temp_custom_colors_simple.html') as fh:
        not_simple_html = fh.read()

    # Check if style rule appears
    assert all(color in simple_html for color in colors)

    # Test if badge appears
    badge_str = 'class="badge badge-source source-{src}"'
    assert all(badge_str.format(src=src) in simple_html for src in sources)
Exemplo n.º 4
0
def test_colors_in_html():
    ag_a = Agent('A')
    ag_b = Agent('B')
    evidences = []
    colors = []
    for source_type, info in DEFAULT_SOURCE_COLORS:
        for source in info['sources']:
            ev = Evidence(source_api=source, text=f'Evidence from {source}')
            evidences.append(ev)
            colors.append(info['sources'][source])

    stmt = Activation(ag_a, ag_b, evidence=evidences)
    ha = HtmlAssembler(statements=[stmt])
    ha.save_model('./temp_simple.html')
    ha = HtmlAssembler(statements=[stmt])
    ha.save_model('./temp_not_simple.html', simple=False)
    with open('./temp_simple.html') as fh:
        simple_html = fh.read()
    with open('./temp_not_simple.html') as fh:
        not_simple_html = fh.read()
    assert all(color in simple_html for color in colors)
    assert all(color in not_simple_html for color in colors)
Exemplo n.º 5
0
def test_assembler():
    stmt = make_stmt()
    ha = HtmlAssembler([stmt])
    result = ha.make_model()
    assert isinstance(result, str)
    # Read from the template file and make sure the beginning and end of the
    # content matches
    template, _, _ = loader.get_source(None, 'indra/template.html')
    assert result.startswith(template[0:100])
    # Make sure assembler works with other parameters provided
    stmt2 = make_bad_stmt()
    ha = HtmlAssembler(source_counts={
        stmt.get_hash(): {
            'test': 1
        },
        stmt2.get_hash(): {
            'test': 1
        }
    },
                       ev_totals={
                           stmt.get_hash(): 1,
                           stmt2.get_hash(): 1
                       },
                       db_rest_url='test.db.url')
    ha.add_statements([stmt, stmt2])
    result = ha.make_model(with_grouping=True)
    assert isinstance(result, str)
    result = ha.make_model(with_grouping=False)
    assert isinstance(result, str)
    # Make sure warning can be appended
    ha.append_warning('warning')
    assert ('\t<span style="color:red;">(CAUTION: warning occurred when '
            'creating this page.)</span>' in ha.model)
    # Make sure model is created before saving
    ha = HtmlAssembler([stmt])
    assert not ha.model
    ha.save_model('tempfile.html')
    assert ha.model