def test_add_variable():
    """Test add_variable() function for Vocabulary object."""
    def test_TypeError(vocabulary, v):
        """Test TypeErrors in add_constant."""
        with pytest.raises(TypeError) as excinfo:
            vocabulary.add_variable(v)

    def test_ValueError(vocabulary, v):
        """Test ValueErrors in add constant."""
        with pytest.raises(ValueError) as excinfo:
            vocabulary.add_variable(v)

    C, R, V = ['C'], [RelationSymbol('R', 1)], ['V']
    vocab = Vocabulary(C, R, V)

    test_TypeError(vocab, None)
    test_TypeError(vocab, object)
    test_ValueError(vocab, 'C')
    test_ValueError(vocab, 'V')
    vocab.add_variable('x')
    assert vocab._V == ['V', 'x']
    vocab.add_variable('a')
    assert vocab._V == ['a', 'V', 'x']
def test_add_variable():
    """Test add_variable() function for Vocabulary object."""

    def test_TypeError(vocabulary, v):
        """Test TypeErrors in add_constant."""
        with pytest.raises(TypeError) as excinfo:
            vocabulary.add_variable(v)

    def test_ValueError(vocabulary, v):
        """Test ValueErrors in add constant."""
        with pytest.raises(ValueError) as excinfo:
            vocabulary.add_variable(v)

    C, R, V = ["C"], [RelationSymbol("R", 1)], ["V"]
    vocab = Vocabulary(C, R, V)

    test_TypeError(vocab, None)
    test_TypeError(vocab, object)
    test_ValueError(vocab, "C")
    test_ValueError(vocab, "V")
    vocab.add_variable("x")
    assert vocab._V == ["V", "x"]
    vocab.add_variable("a")
    assert vocab._V == ["a", "V", "x"]
def test___init__():
    """Test Assignment constructor."""
    def test_TypeError(vocabulary, attribute_system):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            Assignment(vocabulary, attribute_system)

    vocabulary = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V'])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr = AttributeStructure(a, b)
    objs = ['a', 'b', 'c']
    attribute_system = AttributeSystem(astr, objs)

    # test type errors
    test_TypeError(None, attribute_system)
    test_TypeError('', attribute_system)
    test_TypeError(object, attribute_system)
    test_TypeError(vocabulary, None)
    test_TypeError(vocabulary, '')
    test_TypeError(vocabulary, object)

    # test reference keeping and breaking
    A = Assignment(vocabulary, attribute_system)
    assert vocabulary is A._vocabulary
    assert attribute_system is not A._attribute_system

    vocabulary.add_constant('cx')
    vocabulary.add_variable('vx')
    assert 'cx' in A._vocabulary._C
    assert 'vx' in A._vocabulary._V
    A._vocabulary.add_constant('cx2')
    A._vocabulary.add_variable('vx2')
    assert 'cx2' in vocabulary._C
    assert 'vx2' in vocabulary._V
Exemple #4
0
def test___init__():
    """Test AttributeInterpretation constructor."""
    def test_TypeError(vocabulary, attribute_structure, mapping, profiles):
        """Test TypeError catching in AttributeInterpretation constructor."""
        with pytest.raises(TypeError) as excinfo:
            AttributeInterpretation(
                vocabulary, attribute_structure, mapping, profiles)

    def test_ValueError(vocabulary, attribute_structure, mapping, profiles):
        """Test ValueError catching in AttributeInterpretation constructor."""
        with pytest.raises(ValueError) as excinfo:
            AttributeInterpretation(
                vocabulary, attribute_structure, mapping, profiles)

    a = Attribute('hour', ['0,...,23'])
    a2 = Attribute('minute', ['0,...,59'])
    r_ahead = Relation('R1(h1,m1,h2,m2) <=> h1 > h2 or (h1 = h2 and m1 > m2)',
                       ['hour', 'minute', 'hour', 'minute'], 1)
    r_behind = Relation('R2(h1,m1,h2,m2) <=> h1 < h2 or (h1 = h2 and m1 < m2)',
                        ['hour', 'minute', 'hour', 'minute'], 2)
    r_pm = Relation('R3(h1) <=> h1 > 12', ['hour'], 3)
    r_am = Relation('R4(h1) <=> h1 < 12', ['hour'], 4)
    attribute_structure = AttributeStructure(
        a, a2, r_ahead, r_behind, r_pm, r_am)

    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, pm_rs], ['V1', 'V2'])

    profiles = [
        [ahead_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [pm_rs, ('hour', 1)]]
    bad_profiles = [
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [pm_rs, ('hour', 1)]]
    mapping = {ahead_rs: 1, behind_rs: 2, pm_rs: 3}
    bad_source_mapping = {ahead_rs: 1, "behind_rs": 2, pm_rs: 3}
    bad_target_mapping = {ahead_rs: 1, behind_rs: 'R2', pm_rs: 3}
    bad_target_mapping2 = {ahead_rs: 1, behind_rs: 2.0, pm_rs: 3}
    dup_subscr_mapping = {ahead_rs: 2, behind_rs: 2, pm_rs: 3}

    ai = AttributeInterpretation(
        vocabulary, attribute_structure, mapping, profiles)

    test_TypeError(None, attribute_structure, mapping, profiles)
    test_TypeError(object, attribute_structure, mapping, profiles)
    test_TypeError(vocabulary, None, mapping, profiles)
    test_TypeError(vocabulary, object, mapping, profiles)
    test_TypeError(vocabulary, AttributeSystem(attribute_structure, ['o']),
                   mapping, profiles)
    test_TypeError(vocabulary, attribute_structure, None, profiles)
    test_TypeError(vocabulary, attribute_structure, object, profiles)
    test_TypeError(vocabulary, attribute_structure, mapping, None)
    test_TypeError(vocabulary, attribute_structure, mapping, object)

    test_ValueError(
        vocabulary, attribute_structure, bad_source_mapping, profiles)
    test_ValueError(
        vocabulary, attribute_structure, bad_target_mapping, profiles)
    test_ValueError(
        vocabulary, attribute_structure, bad_target_mapping2, profiles)

    test_ValueError(vocabulary, attribute_structure, mapping, bad_profiles)
    test_ValueError(
        vocabulary, attribute_structure, dup_subscr_mapping, profiles)

    bad_mapping = {RelationSymbol("not in Vocabulary or profiles", 2): 1,
                   behind_rs: 2, pm_rs: 3}
    test_ValueError(vocabulary, attribute_structure, bad_mapping, profiles)
    bad_vocabulary = Vocabulary(['C1', 'C2'],
                                [ahead_rs, behind_rs, pm_rs,
                                RelationSymbol("not in source/profiles", 2)],
                                ['V1', 'V2'])

    test_ValueError(bad_vocabulary, attribute_structure, mapping, profiles)
    bad_target_mapping = {ahead_rs: 1, behind_rs: 6, pm_rs: 3}
    test_ValueError(
        vocabulary, attribute_structure, bad_target_mapping, profiles)

    bad_profiles = [
        [ahead_rs, ('doesn\'t exist', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [pm_rs, ('hour', 1)]]
    test_ValueError(vocabulary, attribute_structure, mapping, bad_profiles)
    bad_profiles = [
        [ahead_rs, ('hour', 10), ('minute', 1), ('hour', 2), ('minute', 2)],
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [pm_rs, ('hour', 1)]]
    test_ValueError(vocabulary, attribute_structure, mapping, bad_profiles)

    AI = AttributeInterpretation(
        vocabulary, attribute_structure, mapping, profiles)
    assert AI._attribute_structure == attribute_structure
    assert AI._attribute_structure is not attribute_structure
    assert AI._vocabulary == vocabulary
    assert AI._vocabulary is vocabulary

    AI._vocabulary.add_constant('cx')
    AI._vocabulary.add_variable('vx')
    assert 'cx' in vocabulary._C
    assert 'vx' in vocabulary._V
    vocabulary.add_constant('cx2')
    vocabulary.add_variable('vx2')
    assert 'cx2' in AI._vocabulary._C
    assert 'vx2' in AI._vocabulary._V