File: db_test.py

package info (click to toggle)
python-wn 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,100 kB
  • sloc: python: 8,429; xml: 566; sql: 238; makefile: 12
file content (86 lines) | stat: -rw-r--r-- 2,629 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sqlite3
import threading

import pytest

import wn
from wn import lmf


@pytest.mark.usefixtures("mini_db")
def test_schema_compatibility():
    conn = sqlite3.connect(str(wn.config.database_path))
    schema_hash = wn._db.schema_hash(conn)
    assert schema_hash in wn._db.COMPATIBLE_SCHEMA_HASHES


@pytest.mark.usefixtures("mini_db")
def test_db_multithreading():
    """
    See https://github.com/goodmami/wn/issues/86
    Thanks: @fushinari
    """

    class WNThread:
        w = None

        def __init__(self):
            w_thread = threading.Thread(target=self.set_w)
            w_thread.start()
            w_thread.join()
            self.w.synsets()

        def set_w(self):
            if self.w is None:
                self.w = wn.Wordnet()

    # close the connections by resetting the pool
    wn._db.pool = {}
    with pytest.raises(sqlite3.ProgrammingError):
        WNThread()
    wn._db.pool = {}
    wn.config.allow_multithreading = True
    WNThread()  # no error
    wn.config.allow_multithreading = False
    wn._db.pool = {}


def test_remove_extension(datadir, tmp_path):
    old_data_dir = wn.config.data_directory
    wn.config.data_directory = tmp_path / "wn_data_1_1_trigger"
    wn.add(datadir / "mini-lmf-1.0.xml")
    wn.add(datadir / "mini-lmf-1.1.xml")
    assert len(wn.lexicons()) == 4
    wn.remove("test-en-ext")
    assert len(wn.lexicons()) == 3
    wn.remove("test-ja")
    assert len(wn.lexicons()) == 2
    wn.add(datadir / "mini-lmf-1.1.xml")
    assert len(wn.lexicons()) == 4
    wn.remove("test-en")
    assert {lex.id for lex in wn.lexicons()} == {"test-es", "test-ja"}
    wn.config.data_directory = old_data_dir
    # close any open DB connections before teardown
    for conn in wn._db.pool.values():
        conn.close()


def test_add_lexical_resource(datadir, tmp_path):
    old_data_dir = wn.config.data_directory
    wn.config.data_directory = tmp_path / "wn_data_add_lexical_resource"
    wn.add_lexical_resource(lmf.load(datadir / "mini-lmf-1.0.xml"))
    assert len(wn.lexicons()) == 2
    wn.add_lexical_resource(lmf.load(datadir / "mini-lmf-1.1.xml"))
    assert len(wn.lexicons()) == 4
    wn.config.data_directory = old_data_dir
    # close any open DB connections before teardown
    for conn in wn._db.pool.values():
        conn.close()


@pytest.mark.usefixtures("empty_db")
def test_reset_database(datadir):
    wn.add(datadir / "mini-lmf-1.0.xml")
    assert {lex.specifier() for lex in wn.lexicons()} == {"test-en:1", "test-es:1"}
    wn.reset_database(rebuild=False)  # cannot rebuild from unindexed local files
    assert wn.lexicons() == []