def test_api(app, tmpdir):
    """Test view."""
    hello = tmpdir.join('hello.txt')
    hello.write('Hello world!')

    bye = tmpdir.join('bye.txt')
    bye.write('Bye bye!')

    with app.app_context():
        record = Record.create({'title': 'Greetings',
                                'document': hello.strpath})
        document = Document(record, '/document')

        assert document.open().read() == hello.read()

        # Change document uri
        document.uri = bye.strpath
        assert record['document'] == bye.strpath
        assert document.open().read() == bye.read()

        # Move bye.txt to done.txt
        done = tmpdir.join('done.txt')
        document.move(os.path.abspath(done.strpath))
        assert document.uri == done.strpath
        assert record['document'] == done.strpath
        assert 'Bye bye!' == document.open().read()

        # Copy done.txt to copy.txt
        copy = tmpdir.join('copy.txt')
        copy_patch = document.copy(copy.strpath)
        assert document.uri == done.strpath
        assert copy_patch[0]['value'] == copy.strpath
        assert done.read() == copy.read()

        # Set hello.txt to done.txt
        document.setcontents(hello)
        assert document.open().read() == hello.read()
        assert done.read() == hello.read()

        # Set copy.txt to done.txt via path
        document.setcontents(copy.strpath)
        assert document.open().read() == copy.read()
        assert done.read() == copy.read()

        # Remove done.txt from metadata
        document.remove()
        assert document.uri is None
        assert os.path.exists(done.strpath)

        # Remove copy.txt from filesystem
        assert os.path.exists(copy.strpath)
        document.uri = copy.strpath
        document.remove(force=True)
        assert document.uri is None
        assert not os.path.exists(copy.strpath)