Пример #1
0
def test_json_storage_get_states(dataset, string, res):
    storage = JsonStorage()
    storage.add_links([('0', ('XX', ), 'xy'), ('0', ('xy', ), 'yz'),
                       ('0', ('yz', ), 'xz'), ('0', ('xz', ), None),
                       ('1', ('x', ), 'y')])
    test = storage.get_states(dataset, string)
    test.sort()
    assert test == res
Пример #2
0
def test_json_storage_get_links(args, res):
    dataset = ({
        'x': [1, 'y'],
        'y': [[1, 2, 3], ['x', 'y', 'z']],
        'x y': [1, 'z']
    }, {
        'x': [1, 'z']
    })
    storage = JsonStorage(backward=True)
    assert storage.get_links(dataset, *args) == res
Пример #3
0
def test_json_storage_add_links_backward():
    storage = JsonStorage(backward=True)
    storage.add_links([('0', ('x', 'y'), 'y'), ('0', ('y', 'z'), 'z'),
                       ('0', ('z', 'x'), 'x')])
    assert storage.backward == {
        '0': {
            'y y': [1, 'x'],
            'z z': [1, 'y'],
            'x x': [1, 'z']
        }
    }
Пример #4
0
def test_json_storage_add_links():
    storage = JsonStorage()
    storage.add_links([('0', ('x', ), 'y'), ('0', ('y', ), 'z'),
                       ('0', ('x', ), 'y')])
    assert storage.nodes == {'0': {'x': [2, 'y'], 'y': [1, 'z']}}
    storage.add_links([('0', ('z', ), 'x'), ('0', ('x', ), 'z'),
                       ('0', ('x', ), 'y')])
    assert storage.nodes == {
        '0': {
            'x': [[3, 1], ['y', 'z']],
            'y': [1, 'z'],
            'z': [1, 'x']
        }
    }
    storage.add_links([('1', ('x', ), 'y')])
    assert storage.nodes == {
        '0': {
            'x': [[3, 1], ['y', 'z']],
            'y': [1, 'z'],
            'z': [1, 'x']
        },
        '1': {
            'x': [1, 'y']
        }
    }
    assert storage.backward is None
Пример #5
0
def test_json_storage_state_separator():
    storage = JsonStorage(backward=True)
    storage.add_links([('0', ('x', 'y'), 'z'), ('1', ('y', 'z'), 'x')])
    assert storage.nodes == {'0': {'x y': [1, 'z']}, '1': {'y z': [1, 'x']}}
    assert storage.backward == {'0': {'y z': [1, 'x']}, '1': {'z x': [1, 'y']}}
    storage.state_separator = ':'
    assert storage.nodes == {
        '0': {
            'x:y': [1, 'z'],
        },
        '1': {
            'y:z': [1, 'x']
        }
    }
    assert storage.backward == {'0': {'y:z': [1, 'x']}, '1': {'z:x': [1, 'y']}}
Пример #6
0
def test_json_storage_save_load():
    storage = JsonStorage(backward=True, settings={'state_separator': ':'})
    storage.add_links([('0', ('x', ), 'y'), ('0', ('y', ), 'z'),
                       ('1', ('x', ), 'y'), ('1', ('x', ), 'z')])
    fp = StringIO()
    storage.save(fp)
    fp.seek(0)
    loaded = JsonStorage.load(fp)
    assert storage == loaded
Пример #7
0
def test_json_storage_get_dataset():
    storage = JsonStorage()
    with pytest.raises(KeyError):
        storage.get_dataset('0')
    data = storage.get_dataset('0', True)
    assert data == ({}, None)
    assert storage.get_dataset('0', True)[0] is data[0]
    assert storage.get_dataset('1', True)[0] is not data[0]
Пример #8
0
def test_save_json(mocker, fname, bz2, stdout):
    open_ = mocker.patch('builtins.open', new_callable=mock_open)
    bz2open = mocker.patch('bz2.open', new_callable=mock_open)
    stdout_ = mocker.patch('sys.stdout', new_callable=StringIO)
    markov = Mock(storage=JsonStorage(), save=Mock())

    if bz2:
        handle = bz2open()
    else:
        handle = open_()

    args = Namespace(progress=stdout, settings={})

    save(markov, fname, args)
    assert (stdout_.getvalue() != '') == stdout
    if bz2:
        assert not open_.called
        bz2open.assert_called_with(fname, 'wt')
    else:
        assert not bz2open.called
        open_.assert_called_with(fname, 'wt')
    markov.save.assert_called_once_with(handle)
Пример #9
0
def test_json_storage_get_state(state, size, res):
    storage = JsonStorage()
    assert storage.get_state(state, size) == deque(res, maxlen=size)
Пример #10
0
def test_json_storage_empty():
    storage = JsonStorage()
    assert storage.nodes == {}
    assert storage.settings == {}
Пример #11
0
def test_json_storage_get_dataset_backward():
    storage = JsonStorage(nodes={}, backward=True)
    data = storage.get_dataset('0', True)
    assert data == ({}, {})
    assert data[0] is not data[1]
Пример #12
0
def test_json_storage_close():
    storage = JsonStorage()
    storage.close()
Пример #13
0
def test_json_storage_eq(test, test2, res):
    assert (JsonStorage(*test) == JsonStorage(*test2)) == res
Пример #14
0
def test_json_storage_follow_link(args, res):
    storage = JsonStorage()
    assert storage.follow_link(*args) == deque(res)