def test__setitem__(self, empty_fs):
     empty_fs['A'] = 1
     assert empty_fs['A'] == 1
     empty_fs['a'] = 3
     assert empty_fs['A'] == 3
     empty_fs['B.C'] = 4
     assert empty_fs['B'] == tfs.FeatureStructure({'C': 4})
Exemple #2
0
def test_FeatureStructure():
    fs = tfs.FeatureStructure()
    assert fs.features() == []
    with pytest.raises(KeyError):
        fs['unknown']
    assert fs.get('unknown') is None
    assert fs == tfs.FeatureStructure()

    fs = tfs.FeatureStructure({'A': 'xYz', 'B': 2})
    assert sorted(fs.features()) == [('A', 'xYz'), ('B', 2)]
    assert fs['A'] == fs.get('A') == 'xYz'  # case sensitive values
    assert fs['A'] == fs['a']  # case insensitive keys
    assert fs['B'] == fs.get('B') == 2
    assert fs == tfs.FeatureStructure([('A', 'xYz'), ('B', 2)])

    fs = tfs.FeatureStructure([('A.B.C', 1), ('A.B.D', 2), ('B', 3)])
    assert sorted(fs.features()) == [('A.B',
                                      tfs.FeatureStructure([('C', 1),
                                                            ('D', 2)])),
                                     ('B', 3)]
    assert sorted(fs.features(expand=True)) == [('A.B.C', 1), ('A.B.D', 2),
                                                ('B', 3)]
    assert fs['A.B.C'] == fs['a.B.c'] == 1
    assert fs['A.B.D'] == fs['A']['B']['D'] == 2
    assert fs['B'] == 3
    assert fs['A'] == tfs.FeatureStructure([('B.C', 1), ('B.D', 2)])
    with pytest.raises(KeyError):
        fs['A.B.E']
 def test_features(self, empty_fs, flat_fs, nested_fs):
     assert empty_fs.features() == []
     assert sorted(flat_fs.features()) == [('A', 'xYz'), ('B', 2)]
     assert sorted(nested_fs.features()) == [
         ('A.B', tfs.FeatureStructure([('C', 1), ('D', 2)])), ('B', 3)
     ]
     assert sorted(nested_fs.features(expand=True)) == [('A.B.C', 1),
                                                        ('A.B.D', 2),
                                                        ('B', 3)]
 def test__delitem__(self, nested_fs):
     del nested_fs['A.B.C']
     assert 'A.B.C' not in nested_fs
     assert nested_fs['A.B.D'] == 2
     del nested_fs['A.B.D']
     assert nested_fs['A.B'] == tfs.FeatureStructure()
     del nested_fs['A']
     assert 'A' not in nested_fs
     del nested_fs['b']
     assert 'B' not in nested_fs
 def test_eq(self, empty_fs, flat_fs, nested_fs):
     assert empty_fs == tfs.FeatureStructure()
     assert empty_fs != flat_fs
     assert flat_fs == flat_fs
     flat2 = tfs.FeatureStructure({'A': 'XyZ', 'B': 2})
     assert flat_fs != flat2
     flat3 = tfs.FeatureStructure({'a': 'xYz', 'b': 2})
     assert flat_fs == flat3
     flat4 = tfs.FeatureStructure({'A': 'xYz', 'B': 2, 'C': 1})
     assert flat_fs != flat4
     assert nested_fs == tfs.FeatureStructure([('A',
                                                tfs.FeatureStructure({
                                                    'B.C':
                                                    1,
                                                    'B.D':
                                                    2
                                                })), ('B', 3)])
def empty_fs():
    return tfs.FeatureStructure()
 def test_init(self):
     tfs.FeatureStructure()
     tfs.FeatureStructure({'A': 'b'})
     tfs.FeatureStructure([('A', 'b')])
     tfs.FeatureStructure([('A.B.C', 1), ('A.B.D', 2), ('B', 3)])
def nested_fs():
    return tfs.FeatureStructure([('A.B.C', 1), ('A.B.D', 2), ('B', 3)])
def flat_fs():
    return tfs.FeatureStructure({'A': 'xYz', 'B': 2})