Ejemplo n.º 1
0
def test_json():
    # Write contents
    storage = JSONStorage(path)
    storage.write(element)

    # Verify contents
    assert_equal(element, storage.read())
Ejemplo n.º 2
0
def test_json():
    # Write contents
    storage = JSONStorage(path)
    storage.write(element)

    # Verify contents
    assert_equal(element, storage.read())
Ejemplo n.º 3
0
def test_json(tmpdir):
    # Write contents
    path = str(tmpdir.join('test.db'))
    storage = JSONStorage(path)
    storage.write(element)

    # Verify contents
    assert element == storage.read()
Ejemplo n.º 4
0
def test_json(tmpdir):
    # Write contents
    path = str(tmpdir.join('test.db'))
    storage = JSONStorage(path)
    storage.write(element)

    # Verify contents
    assert element == storage.read()
Ejemplo n.º 5
0
    def __init__(self, path: PathType, **kwargs):
        """Create a new instance

        :param str path: Path to file"""
        if (not globals().get("auto_convert_to_pathlib", False)
                or locals().get("auto_convert_to_pathlib", False)):
            from . import auto_convert_to_pathlib

        current_path = auto_convert_to_pathlib(path)

        self.path = current_path

        MemoryStorage.__init__(self)
        JSONStorage.__init__(self, current_path, **kwargs)
def test_no_error_json_storage(thing):
    storage = JSONStorage('./noerrors.json')

    storage.write(thing)

    assert thing == storage.read()
    storage.close()
Ejemplo n.º 7
0
def test_json(tmpdir):
    # Write contents
    path = str(tmpdir.join('test.db'))
    storage = JSONStorage(path)
    storage.write(doc)

    # Verify contents
    assert doc == storage.read()
    storage.close()
Ejemplo n.º 8
0
def test_create_dirs():
    temp_dir = tempfile.gettempdir()
    db_dir = ''
    db_file = ''

    while True:
        dname = os.path.join(temp_dir, str(random.getrandbits(20)))
        if not os.path.exists(dname):
            db_dir = dname
            db_file = os.path.join(db_dir, 'db.json')
            break

    db_conn = JSONStorage(db_file, create_dirs=True)
    db_conn.close()

    db_exists = os.path.exists(db_file)

    os.remove(db_file)
    os.rmdir(db_dir)

    assert db_exists
Ejemplo n.º 9
0
def test_create_dirs():
    temp_dir = tempfile.gettempdir()

    while True:
        dname = os.path.join(temp_dir, str(random.getrandbits(20)))
        if not os.path.exists(dname):
            db_dir = dname
            db_file = os.path.join(db_dir, 'db.json')
            break

    with pytest.raises(IOError):
        JSONStorage(db_file)

    JSONStorage(db_file, create_dirs=True).close()
    assert os.path.exists(db_file)

    # Use create_dirs with already existing directory
    JSONStorage(db_file, create_dirs=True).close()
    assert os.path.exists(db_file)

    os.remove(db_file)
    os.rmdir(db_dir)
Ejemplo n.º 10
0
def test_encoding(tmpdir):
    japanese_doc = {"Test": u"こんにちは世界"}

    path = str(tmpdir.join('test.db'))
    # cp936 is used for japanese encodings
    jap_storage = JSONStorage(path, encoding="cp936")
    jap_storage.write(japanese_doc)

    try:
        exception = json.decoder.JSONDecodeError
    except AttributeError:
        exception = ValueError

    with pytest.raises(exception):
        # cp037 is used for english encodings
        eng_storage = JSONStorage(path, encoding="cp037")
        eng_storage.read()

    jap_storage = JSONStorage(path, encoding="cp936")
    assert japanese_doc == jap_storage.read()