Exemplo n.º 1
0
def test_delete_rec_index_and_delay_commit(index, delay_commit, input_retval):
    """test delete rec, index and delay commit."""
    bdb = BukuDb()
    bdb_dc = BukuDb()  # instance for delay_commit check.

    # Fill bookmark
    for bookmark in TEST_BOOKMARKS:
        bdb.add_rec(*bookmark)
    db_len = len(TEST_BOOKMARKS)

    n_index = index

    with mock.patch('builtins.input', return_value=input_retval):
        res = bdb.delete_rec(index=index, delay_commit=delay_commit)

    if n_index < 0:
        assert not res
    elif n_index > db_len:
        assert not res
        assert len(bdb.get_rec_all()) == db_len
    elif index == 0 and input_retval != 'y':
        assert not res
        assert len(bdb.get_rec_all()) == db_len
    else:
        assert res
        assert len(bdb.get_rec_all()) == db_len - 1
        if delay_commit:
            assert len(bdb_dc.get_rec_all()) == db_len
        else:
            assert len(bdb_dc.get_rec_all()) == db_len - 1

    # teardown
    os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
Exemplo n.º 2
0
def test_exportdb_to_db():
    with NamedTemporaryFile(delete=False) as f1, NamedTemporaryFile(
            delete=False, suffix='.db') as f2:
        db = BukuDb(dbfile=f1.name)
        db.add_rec('http://example.com')
        db.add_rec('http://google.com')
        with mock.patch('builtins.input', return_value='y'):
            db.exportdb(f2.name)
        db2 = BukuDb(dbfile=f2.name)
        assert db.get_rec_all() == db2.get_rec_all()
Exemplo n.º 3
0
def test_delete_rec_index_and_delay_commit(index, delay_commit, input_retval):
    """test delete rec, index and delay commit."""
    bdb = BukuDb()
    bdb_dc = BukuDb()  # instance for delay_commit check.

    # Fill bookmark
    for bookmark in TEST_BOOKMARKS:
        bdb.add_rec(*bookmark)
    db_len = len(TEST_BOOKMARKS)

    n_index = index

    if index.bit_length() > 63:
        with pytest.raises(OverflowError):
            bdb.delete_rec(index=index, delay_commit=delay_commit)
        return

    with mock.patch('builtins.input', return_value=input_retval):
        res = bdb.delete_rec(index=index, delay_commit=delay_commit)

    if n_index < 0:
        assert not res
    elif n_index > db_len:
        assert not res
        assert len(bdb.get_rec_all()) == db_len
    elif index == 0 and input_retval != 'y':
        assert not res
        assert len(bdb.get_rec_all()) == db_len
    else:
        assert res
        assert len(bdb.get_rec_all()) == db_len - 1
        if delay_commit:
            assert len(bdb_dc.get_rec_all()) == db_len
        else:
            assert len(bdb_dc.get_rec_all()) == db_len - 1

    # teardown
    os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
Exemplo n.º 4
0
def test_delete_rec_on_non_interger(index, low, high, is_range):
    """test delete rec on non integer arg."""
    bdb = BukuDb()

    for bookmark in TEST_BOOKMARKS:
        bdb.add_rec(*bookmark)
    db_len = len(TEST_BOOKMARKS)

    if is_range and not (isinstance(low, int) and isinstance(high, int)):
        with pytest.raises(TypeError):
            bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
        return
    elif not is_range and not isinstance(index, int):
        res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
        assert not res
        assert len(bdb.get_rec_all()) == db_len
    else:
        assert bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
Exemplo n.º 5
0
def test_delete_rec_on_non_interger(index, low, high, is_range):
    """test delete rec on non integer arg."""
    bdb = BukuDb()

    for bookmark in TEST_BOOKMARKS:
        bdb.add_rec(*bookmark)
    db_len = len(TEST_BOOKMARKS)

    if is_range and not (isinstance(low, int) and isinstance(high, int)):
        with pytest.raises(TypeError):
            bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
        return
    elif not is_range and not isinstance(index, int):
        res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
        assert not res
        assert len(bdb.get_rec_all()) == db_len
    else:
        assert bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
Exemplo n.º 6
0
def test_delete_rec_range_and_delay_commit(
    setup, tmp_path, low, high, delay_commit, input_retval, exp_res
):
    """test delete rec, range and delay commit."""
    bdb = BukuDb(dbfile=tmp_path / "tmp.db")
    kwargs = {"is_range": True, "low": low, "high": high, "delay_commit": delay_commit}
    kwargs["index"] = 0

    # Fill bookmark
    for bookmark in TEST_BOOKMARKS:
        bdb.add_rec(*bookmark)

    with mock.patch("builtins.input", return_value=input_retval):
        res = bdb.delete_rec(**kwargs)

    assert (res, bdb.get_rec_all()) == exp_res

    # teardown
    os.environ["XDG_DATA_HOME"] = TEST_TEMP_DIR_PATH
Exemplo n.º 7
0
def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit,
                                           input_retval):
    """test delete rec, range and delay commit."""
    bdb = BukuDb()
    bdb_dc = BukuDb()  # instance for delay_commit check.
    index = 0
    is_range = True

    # Fill bookmark
    for bookmark in TEST_BOOKMARKS:
        bdb.add_rec(*bookmark)
    db_len = len(TEST_BOOKMARKS)

    # use normalized high and low variable
    n_low, n_high = normalize_range(db_len=db_len, low=low, high=high)

    exp_res = True
    if n_high > db_len and n_low <= db_len:
        exp_db_len = db_len - (db_len + 1 - n_low)
    elif n_high == n_low and n_low > db_len:
        exp_db_len = db_len
        exp_res = False
    elif n_high == n_low and n_low <= db_len:
        exp_db_len = db_len - 1
    else:
        exp_db_len = db_len - (n_high + 1 - n_low)

    with mock.patch('builtins.input', return_value=input_retval):
        res = bdb.delete_rec(index=index,
                             low=low,
                             high=high,
                             is_range=is_range,
                             delay_commit=delay_commit)

    if n_low < 0:
        assert not res
        assert len(bdb_dc.get_rec_all()) == db_len
        # teardown
        os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
        return
    elif (low == 0 or high == 0) and input_retval != 'y':
        assert not res
        assert len(bdb_dc.get_rec_all()) == db_len
        # teardown
        os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
        return
    elif (low == 0 or high == 0) and input_retval == 'y':
        assert res == exp_res
        with pytest.raises(sqlite3.OperationalError):
            bdb.get_rec_all()
        # teardown
        os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
        return
    elif n_low > db_len and n_low > 0:
        assert not res
        assert len(bdb_dc.get_rec_all()) == db_len
        # teardown
        os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
        return
    assert res == exp_res
    assert len(bdb.get_rec_all()) == exp_db_len
    if delay_commit:
        assert len(bdb_dc.get_rec_all()) == db_len
    else:
        assert len(bdb_dc.get_rec_all()) == exp_db_len

    # teardown
    os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
Exemplo n.º 8
0
    def index(self):
        bukudb = BukuDb()
        global STATISTIC_DATA
        statistic_data = STATISTIC_DATA
        if not statistic_data or request.method == 'POST':
            all_bookmarks = bukudb.get_rec_all()
            netloc = [urlparse(x[1]).netloc for x in all_bookmarks]
            tag_set = [x[3] for x in all_bookmarks]
            tag_items = []
            for tags in tag_set:
                tag_items.extend([x.strip() for x in tags.split(',') if x.strip()])
            tag_counter = Counter(tag_items)
            title_items = [x[2] for x in all_bookmarks]
            title_counter = Counter(title_items)
            statistic_datetime = arrow.now()
            STATISTIC_DATA = {
                'datetime': statistic_datetime,
                'netloc': netloc,
                'tag_counter': tag_counter,
                'title_counter': title_counter,
            }
        else:
            netloc = statistic_data['netloc']
            statistic_datetime = statistic_data['datetime']
            tag_counter = statistic_data['tag_counter']
            title_counter = statistic_data['title_counter']

        netloc_counter = Counter(netloc)
        unique_netloc_len = len(set(netloc))
        colors = [
            "#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA",
            "#ABCDEF", "#DDDDDD", "#ABCABC", "#4169E1",
            "#C71585", "#FF4500", "#FEDCBA", "#46BFBD"]
        show_netloc_table = False
        if unique_netloc_len > len(colors):
            max_netloc_item = len(colors)
            netloc_colors = colors
            show_netloc_table = True
        else:
            netloc_colors = colors[:unique_netloc_len]
            max_netloc_item = unique_netloc_len
        most_common_netlocs = netloc_counter.most_common(max_netloc_item)
        most_common_netlocs = [
            [val[0], val[1], netloc_colors[idx]] for idx, val in enumerate(most_common_netlocs)]

        unique_tag_len = len(tag_counter)
        show_tag_rank_table = False
        if unique_tag_len > len(colors):
            max_tag_item = len(colors)
            tag_colors = colors
            show_tag_rank_table = True
        else:
            tag_colors = colors[:unique_tag_len]
            max_tag_item = unique_tag_len
        most_common_tags = tag_counter.most_common(max_tag_item)
        most_common_tags = [
            [val[0], val[1], tag_colors[idx]] for idx, val in enumerate(most_common_tags)]

        unique_title_len = len(title_counter)
        show_title_rank_table = False
        if unique_title_len > len(colors):
            max_title_item = len(colors)
            title_colors = colors
            show_title_rank_table = True
        else:
            title_colors = colors[:unique_title_len]
            max_title_item = unique_title_len
        most_common_titles = title_counter.most_common(max_title_item)
        most_common_titles = [
            [val[0], val[1], title_colors[idx]] for idx, val in enumerate(most_common_titles)]

        return self.render(
            'bukuserver/statistic.html',
            most_common_netlocs=most_common_netlocs,
            netloc_counter=netloc_counter,
            show_netloc_table=show_netloc_table,
            most_common_tags=most_common_tags,
            tag_counter=tag_counter,
            show_tag_rank_table=show_tag_rank_table,
            most_common_titles=most_common_titles,
            title_counter=title_counter,
            show_title_rank_table=show_title_rank_table,
            datetime=statistic_datetime,
            datetime_text=statistic_datetime.humanize(arrow.now(), granularity='second'),
        )
Exemplo n.º 9
0
    def index(self):
        bukudb = BukuDb()
        global STATISTIC_DATA
        statistic_data = STATISTIC_DATA
        if not statistic_data or request.method == 'POST':
            all_bookmarks = bukudb.get_rec_all()
            netloc = [urlparse(x[1]).netloc for x in all_bookmarks]
            tag_set = [x[3] for x in all_bookmarks]
            tag_items = []
            for tags in tag_set:
                tag_items.extend(
                    [x.strip() for x in tags.split(',') if x.strip()])
            tag_counter = Counter(tag_items)
            title_items = [x[2] for x in all_bookmarks]
            title_counter = Counter(title_items)
            statistic_datetime = arrow.now()
            STATISTIC_DATA = {
                'datetime': statistic_datetime,
                'netloc': netloc,
                'tag_counter': tag_counter,
                'title_counter': title_counter,
            }
        else:
            netloc = statistic_data['netloc']
            statistic_datetime = statistic_data['datetime']
            tag_counter = statistic_data['tag_counter']
            title_counter = statistic_data['title_counter']

        netloc_counter = Counter(netloc)
        unique_netloc_len = len(set(netloc))
        colors = [
            "#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA", "#ABCDEF", "#DDDDDD",
            "#ABCABC", "#4169E1", "#C71585", "#FF4500", "#FEDCBA", "#46BFBD"
        ]
        show_netloc_table = False
        if unique_netloc_len > len(colors):
            max_netloc_item = len(colors)
            netloc_colors = colors
            show_netloc_table = True
        else:
            netloc_colors = colors[:unique_netloc_len]
            max_netloc_item = unique_netloc_len
        most_common_netlocs = netloc_counter.most_common(max_netloc_item)
        most_common_netlocs = [[val[0], val[1], netloc_colors[idx]]
                               for idx, val in enumerate(most_common_netlocs)]

        unique_tag_len = len(tag_counter)
        show_tag_rank_table = False
        if unique_tag_len > len(colors):
            max_tag_item = len(colors)
            tag_colors = colors
            show_tag_rank_table = True
        else:
            tag_colors = colors[:unique_tag_len]
            max_tag_item = unique_tag_len
        most_common_tags = tag_counter.most_common(max_tag_item)
        most_common_tags = [[val[0], val[1], tag_colors[idx]]
                            for idx, val in enumerate(most_common_tags)]

        unique_title_len = len(title_counter)
        show_title_rank_table = False
        if unique_title_len > len(colors):
            max_title_item = len(colors)
            title_colors = colors
            show_title_rank_table = True
        else:
            title_colors = colors[:unique_title_len]
            max_title_item = unique_title_len
        most_common_titles = title_counter.most_common(max_title_item)
        most_common_titles = [[val[0], val[1], title_colors[idx]]
                              for idx, val in enumerate(most_common_titles)]

        return self.render(
            'bukuserver/statistic.html',
            most_common_netlocs=most_common_netlocs,
            netloc_counter=netloc_counter,
            show_netloc_table=show_netloc_table,
            most_common_tags=most_common_tags,
            tag_counter=tag_counter,
            show_tag_rank_table=show_tag_rank_table,
            most_common_titles=most_common_titles,
            title_counter=title_counter,
            show_title_rank_table=show_title_rank_table,
            datetime=statistic_datetime,
            datetime_text=statistic_datetime.humanize(arrow.now(),
                                                      granularity='second'),
        )
Exemplo n.º 10
0
def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input_retval):
    """test delete rec, range and delay commit."""
    bdb = BukuDb()
    bdb_dc = BukuDb()  # instance for delay_commit check.
    index = 0
    is_range = True

    # Fill bookmark
    for bookmark in TEST_BOOKMARKS:
        bdb.add_rec(*bookmark)
    db_len = len(TEST_BOOKMARKS)

    # use normalized high and low variable
    n_low, n_high = normalize_range(db_len=db_len, low=low, high=high)

    exp_res = True
    if n_high > db_len and n_low <= db_len:
        exp_db_len = db_len - (db_len + 1 - n_low)
    elif n_high == n_low and n_low > db_len:
        exp_db_len = db_len
        exp_res = False
    elif n_high == n_low and n_low <= db_len:
        exp_db_len = db_len - 1
    else:
        exp_db_len = db_len - (n_high + 1 - n_low)

    with mock.patch('builtins.input', return_value=input_retval):
        res = bdb.delete_rec(
            index=index, low=low, high=high, is_range=is_range, delay_commit=delay_commit)

    if n_low < 0:
        assert not res
        assert len(bdb_dc.get_rec_all()) == db_len
        # teardown
        os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
        return
    elif (low == 0 or high == 0) and input_retval != 'y':
        assert not res
        assert len(bdb_dc.get_rec_all()) == db_len
        # teardown
        os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
        return
    elif (low == 0 or high == 0) and input_retval == 'y':
        assert res == exp_res
        with pytest.raises(sqlite3.OperationalError):
            bdb.get_rec_all()
        # teardown
        os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
        return
    elif n_low > db_len and n_low > 0:
        assert not res
        assert len(bdb_dc.get_rec_all()) == db_len
        # teardown
        os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
        return
    assert res == exp_res
    assert len(bdb.get_rec_all()) == exp_db_len
    if delay_commit:
        assert len(bdb_dc.get_rec_all()) == db_len
    else:
        assert len(bdb_dc.get_rec_all()) == exp_db_len

    # teardown
    os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH