• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pymorphy2-fork / DAWG-Python / 14779646156

01 May 2025 05:04PM UTC coverage: 89.925%. Remained the same
14779646156

Pull #46

github

web-flow
Merge 13b24eede into bf6170229
Pull Request #46: Bump typing-extensions from 4.12.2 to 4.13.2

204 of 237 branches covered (86.08%)

Branch coverage included in aggregate %.

635 of 696 relevant lines covered (91.24%)

6.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/tests/test_dawg.py
1
import pickle
7✔
2
import tempfile
7✔
3

4
import pytest
7✔
5

6
import dawg_python
7✔
7

8
from .utils import data_path
7✔
9

10

11
def test_c_dawg_contains():
7✔
12
    dawg = pytest.importorskip("dawg")  # import dawg
7✔
13
    bin_dawg = dawg.IntDAWG({"foo": 1, "bar": 2, "foobar": 3})
7✔
14

15
    d = dawg_python.Dictionary()
7✔
16

17
    fd, path = tempfile.mkstemp()
7✔
18
    bin_dawg.save(path)
7✔
19

20
    with open(path, "rb") as f:
7✔
21
        d.read(f)
7✔
22

23
    assert d.contains(b"foo")
7✔
24
    assert not d.contains(b"x")
7✔
25
    assert d.contains(b"foobar")
7✔
26
    assert d.contains(b"bar")
7✔
27

28

29
class TestCompletionDAWG:
7✔
30
    keys = ["f", "bar", "foo", "foobar"]
7✔
31

32
    def dawg(self):
7✔
33
        return dawg_python.CompletionDAWG().load(data_path("small", "completion.dawg"))
7✔
34

35
    def test_contains(self):
7✔
36
        d = self.dawg()
7✔
37
        for key in self.keys:
7✔
38
            assert key in d
7✔
39

40
    def test_contains_bytes(self):
7✔
41
        d = self.dawg()
7✔
42
        for key in self.keys:
7✔
43
            assert key.encode("utf8") in d
7✔
44

45
    def test_keys(self):
7✔
46
        d = self.dawg()
7✔
47
        assert d.keys() == sorted(self.keys)
7✔
48

49
    def test_iterkeys(self):
7✔
50
        d = self.dawg()
7✔
51
        assert list(d.iterkeys()) == d.keys()
7✔
52

53
    def test_completion(self):
7✔
54
        d = self.dawg()
7✔
55

56
        assert d.keys("z") == []
7✔
57
        assert d.keys("b") == ["bar"]
7✔
58
        assert d.keys("foo") == ["foo", "foobar"]
7✔
59

60
    def test_no_segfaults_on_invalid_file(self):
7✔
61
        d = self.dawg()
7✔
62
        fd, path = tempfile.mkstemp()
7✔
63
        with open(path, "w") as f:
7✔
64
            f.write("foo")
7✔
65

66
        with pytest.raises(Exception):
7✔
67
            d.load(path)
7✔
68

69
    def test_empty_dawg(self):
7✔
70
        d = dawg_python.CompletionDAWG().load(data_path("small", "completion-empty.dawg"))
7✔
71
        assert d.keys() == []
7✔
72

73
    def test_prefixes(self):
7✔
74
        d = self.dawg()
7✔
75
        assert d.prefixes("foobarz") == ["f", "foo", "foobar"]
7✔
76
        assert d.prefixes("x") == []
7✔
77
        assert d.prefixes("bar") == ["bar"]
7✔
78

79

80
class TestIntDAWG:
7✔
81
    payload = {"foo": 1, "bar": 5, "foobar": 3}
7✔
82

83
    def dawg(self):
7✔
84
        return dawg_python.IntDAWG().load(data_path("small", "int_dawg.dawg"))
7✔
85

86
    def test_getitem(self):
7✔
87
        d = self.dawg()
7✔
88
        for key in self.payload:
7✔
89
            assert d[key] == self.payload[key]
7✔
90

91
        with pytest.raises(KeyError):
7✔
92
            d["fo"]
7✔
93

94
    def test_pickling(self):
7✔
95
        d = self.dawg()
7✔
96

97
        data = pickle.dumps(d)
7✔
98
        d2 = pickle.loads(data)
7✔
99

100
        for key, value in self.payload.items():
7✔
101
            assert key in d2
7✔
102
            assert d[key] == value
7✔
103

104

105
class TestIntCompletionDawg(TestIntDAWG):
7✔
106
    def dawg(self):
7✔
107
        return dawg_python.IntCompletionDAWG().load(data_path("small", "int_completion_dawg.dawg"))
7✔
108

109
    def test_completion_keys(self):
7✔
110
        assert self.dawg().keys() == sorted(self.payload.keys())
7✔
111

112
    def test_completion_keys_with_prefix(self):
7✔
113
        assert self.dawg().keys("fo") == ["foo", "foobar"]
7✔
114
        assert self.dawg().keys("foo") == ["foo", "foobar"]
7✔
115
        assert self.dawg().keys("foob") == ["foobar"]
7✔
116
        assert self.dawg().keys("z") == []
7✔
117
        assert self.dawg().keys("b") == ["bar"]
7✔
118

119
    def test_completion_items(self):
7✔
120
        assert self.dawg().items() == sorted(self.payload.items(), key=lambda r: r[0])
7✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc