"Fossies" - the Fresh Open Source Software Archive

Member "buku-4.3/tests/test_views.py" (31 Jan 2020, 3480 Bytes) of package /linux/privat/buku-4.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 from argparse import Namespace
    2 from types import SimpleNamespace
    3 
    4 import pytest
    5 from flask import current_app
    6 
    7 from buku import BukuDb
    8 from bukuserver import server
    9 from bukuserver.views import BookmarkModelView, TagModelView
   10 
   11 
   12 @pytest.fixture
   13 def client(tmp_path):
   14     test_db = tmp_path / 'test.db'
   15     app = server.create_app(test_db.as_posix())
   16     app_context = app.test_request_context()
   17     app_context.push()
   18     client = app.test_client()
   19     return client
   20 
   21 
   22 @pytest.mark.parametrize('disable_favicon', [False, True])
   23 def test_bookmark_model_view(tmp_path, client, disable_favicon):
   24     test_db = tmp_path / 'test.db'
   25     bukudb = BukuDb(dbfile=test_db.as_posix())
   26     inst = BookmarkModelView(bukudb)
   27     model = Namespace(
   28         description='randomdesc', id=1, tags='tags1',
   29         title='Example Domain', url='http://example.com')
   30     current_app.config['BUKUSERVER_DISABLE_FAVICON'] = disable_favicon
   31     img_html = ''
   32     if not disable_favicon:
   33         img_html = \
   34             '<img src="http://www.google.com/s2/favicons?domain=example.com"/> '
   35     res = inst._list_entry(None, model, 'Entry')
   36     exp_res = \
   37         (
   38             '<a href="http://example.com">Example Domain</a><br/>'
   39             '<a href="http://example.com">http://example.com</a><br/>'
   40             '<a class="btn btn-default" '
   41             'href="/bookmark/?flt2_url_netloc_match=example.com">netloc:example.com</a>'
   42             '<a class="btn btn-default" href="/bookmark/?flt2_tags_contain=tags1">tags1</a>'
   43             '<br/>randomdesc')
   44     exp_res = ''.join([img_html, exp_res])
   45     assert str(res) == exp_res
   46 
   47 
   48 @pytest.fixture
   49 def tmv_instance(tmp_path):
   50     """define tag model view instance"""
   51     test_db = tmp_path / 'test.db'
   52     bukudb = BukuDb(dbfile=test_db.as_posix())
   53     inst = TagModelView(bukudb)
   54     return inst
   55 
   56 
   57 def test_tag_model_view_get_list_empty_db(tmv_instance):
   58     res = tmv_instance.get_list(None, None, None, None, [])
   59     assert res == (0, [])
   60 
   61 
   62 @pytest.mark.parametrize('sort_field, sort_desc, filters, exp_res', [
   63     [
   64         None, False, [], (3, [
   65             SimpleNamespace(name='t1', usage_count=1),
   66             SimpleNamespace(name='t2', usage_count=2),
   67             SimpleNamespace(name='t3', usage_count=3),
   68         ])
   69     ],
   70     [
   71         None, False, [(0, 'name', 't2')], (1, [
   72             SimpleNamespace(name='t2', usage_count=2)
   73         ])
   74     ],
   75     [
   76         'name', False, [], (3, [
   77             SimpleNamespace(name='t1', usage_count=1),
   78             SimpleNamespace(name='t2', usage_count=2),
   79             SimpleNamespace(name='t3', usage_count=3),
   80         ])
   81     ],
   82     [
   83         'name', True, [], (3, [
   84             SimpleNamespace(name='t3', usage_count=3),
   85             SimpleNamespace(name='t2', usage_count=2),
   86             SimpleNamespace(name='t1', usage_count=1),
   87         ])
   88     ],
   89     [
   90         'usage_count', True, [], (3, [
   91             SimpleNamespace(name='t3', usage_count=3),
   92             SimpleNamespace(name='t2', usage_count=2),
   93             SimpleNamespace(name='t1', usage_count=1),
   94         ])
   95     ],
   96 ])
   97 def test_tag_model_view_get_list(tmv_instance, sort_field, sort_desc, filters, exp_res):
   98     tmv_instance.bukudb.add_rec('http://example.com/1.jpg', tags_in='t1,t2,t3')
   99     tmv_instance.bukudb.add_rec('http://example.com/2.jpg', tags_in='t2,t3')
  100     tmv_instance.bukudb.add_rec('http://example.com/3.jpg', tags_in='t3')
  101     res = tmv_instance.get_list(0, sort_field, sort_desc, None, filters)
  102     assert res == exp_res