Skip to content

Commit

Permalink
test(views): rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
rachmadaniHaryono committed Nov 1, 2022
1 parent 6401acb commit 1bf0188
Showing 1 changed file with 43 additions and 30 deletions.
73 changes: 43 additions & 30 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
import logging
"""test for views.
resources: https://flask.palletsprojects.com/en/2.2.x/testing/
"""
from argparse import Namespace

import pytest
from flask import current_app, request
from flask import request

from buku import BukuDb
from bukuserver import server
from bukuserver.views import BookmarkModelView, TagModelView


@pytest.fixture
def client(tmp_path):
test_db = tmp_path / 'test.db'
app = server.create_app(test_db.as_posix())
app_context = app.test_request_context()
app_context.push()
client = app.test_client()
return client
@pytest.fixture()
def app(tmp_path):
app = server.create_app((tmp_path / "test.db").as_posix())
app.config.update(
{
"TESTING": True,
}
)
# other setup can go here
yield app
# clean up / reset resources here


@pytest.fixture()
def client(app):
return app.test_client()


@pytest.fixture()
def runner(app):
return app.test_cli_runner()


def get_tmp_bukudb(tmp_path):
return BukuDb(dbfile=(tmp_path / "test.db").as_posix())


@pytest.mark.parametrize('disable_favicon', [False, True])
def test_bookmark_model_view(tmp_path, client, disable_favicon):
logging.debug('client: %s', client)
test_db = tmp_path / 'test.db'
bukudb = BukuDb(dbfile=test_db.as_posix())
inst = BookmarkModelView(bukudb)
model = Namespace(
description='randomdesc', id=1, tags='tags1',
title='Example Domain', url='http://example.com')
current_app.config['BUKUSERVER_DISABLE_FAVICON'] = disable_favicon
assert inst._list_entry(None, model, 'Entry')
def test_bookmark_model_view(tmp_path, disable_favicon, app):
inst = BookmarkModelView(get_tmp_bukudb(tmp_path))
model = Namespace(description="randomdesc", id=1, tags="tags1", title="Example Domain", url="http://example.com")
app.config["BUKUSERVER_DISABLE_FAVICON"] = disable_favicon
with app.test_request_context():
assert inst._list_entry(None, model, "Entry")


@pytest.fixture
def tmv_instance(tmp_path):
"""define tag model view instance"""
test_db = tmp_path / 'test.db'
bukudb = BukuDb(dbfile=test_db.as_posix())
inst = TagModelView(bukudb)
inst = TagModelView(get_tmp_bukudb(tmp_path))
return inst


Expand Down Expand Up @@ -67,17 +81,16 @@ def test_tag_model_view_get_list(tmv_instance, sort_field, sort_desc, filters, e
@pytest.fixture
def bmv_instance(tmp_path):
"""define tag model view instance"""
test_db = tmp_path / 'test.db'
bukudb = BukuDb(dbfile=test_db.as_posix())
inst = BookmarkModelView(bukudb)
inst = BookmarkModelView(get_tmp_bukudb(tmp_path))
return inst


@pytest.mark.parametrize('url, exp_url', [
['http://example.com', 'http://example.com'],
['/bookmark/', None],
])
def test_bmv_create_form(bmv_instance, url, exp_url):
request.args = {'url': url}
form = bmv_instance.create_form()
assert form.url.data == exp_url
def test_bmv_create_form(bmv_instance, url, exp_url, app):
with app.test_request_context():
request.args = {"url": url}
form = bmv_instance.create_form()
assert form.url.data == exp_url

0 comments on commit 1bf0188

Please sign in to comment.