aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_cluster.py
blob: 793798bd1253b3a1322102df902ff35f2f186e30 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import collections
import io
import json
import os
import tempfile

import pytest

from fuzzycat.cluster import (Cluster, release_key_title, release_key_title_normalized,
                              release_key_title_nysiis)

Case = collections.namedtuple("Case", 'input output')


def test_release_key_title():
    with pytest.raises(KeyError):
        release_key_title({})
    with pytest.raises(KeyError, match='title'):
        release_key_title({'ident': '123'})
    with pytest.raises(KeyError, match='ident'):
        release_key_title({'title': 'deep learning backdoor'})
    with pytest.raises(ValueError, match='title.*missing'):
        release_key_title({'ident': '', 'title': ''})
    cases = (
        Case(input={
            'ident': '',
            'title': 'simhash'
        }, output=('', 'simhash')),
        Case(input={
            'ident': '',
            'title': 'Simhash'
        }, output=('', 'Simhash')),
        Case(input={
            'ident': '',
            'title': 'Sim  hash'
        }, output=('', 'Sim  hash')),
    )
    for case in cases:
        assert case.output == release_key_title(case.input)


def test_release_key_title_normalized():
    cases = (
        Case(input={
            'ident': '',
            'title': 'simhash'
        }, output=('', 'simhash')),
        Case(input={
            'ident': '',
            'title': 'Simhash'
        }, output=('', 'simhash')),
        Case(input={
            'ident': '',
            'title': 'Sim  hash'
        }, output=('', 'simhash')),
        Case(input={
            'ident': '',
            'title': 'THE year 1929'
        }, output=('', 'theyear1929')),
        Case(input={
            'ident': '',
            'title': '2019?'
        }, output=('', '2019')),
        Case(input={
            'ident': '123',
            'title': 'H~~2019?'
        }, output=('123', 'h2019')),
    )
    for case in cases:
        assert case.output == release_key_title_normalized(case.input), 'failed case {}'.format(
            case.input)


def test_release_key_title_nysiis():
    cases = (
        Case(input={
            'ident': '',
            'title': 'simhash'
        }, output=('', 'SANAS')),
        Case(input={
            'ident': '',
            'title': 'Simhash'
        }, output=('', 'SANAS')),
        Case(input={
            'ident': '',
            'title': 'Sim  hash'
        }, output=('', 'SANAS')),
        Case(input={
            'ident': '',
            'title': 'THE year 1929'
        }, output=('', 'TAR')),
        Case(input={
            'ident': '',
            'title': '2019?'
        }, output=('', '')),
        Case(input={
            'ident': '123',
            'title': 'H~~2019?'
        }, output=('123', 'H')),
        Case(input={
            'ident': '123',
            'title': '世界'
        }, output=('123', '')),
    )
    for case in cases:
        assert case.output == release_key_title_nysiis(case.input), 'failed case {}'.format(
            case.input)


def test_cluster():
    sio = io.StringIO()
    lines = [
        json.dumps(doc) for doc in [
            {
                "title": "hello world",
                "ident": 1,
            },
            {
                "title": "hello world!",
                "ident": 2,
            },
        ]
    ]
    cluster = Cluster(lines, release_key_title_normalized, output=sio)
    stats = cluster.run()
    assert stats == {
        "key_fail": 0,
        "key_ok": 2,
        "key_empty": 0,
        "key_denylist": 0,
        "num_clusters": 1
    }
    assert json.loads(sio.getvalue()) == {
        "k": "helloworld",
        "v": [{
            "title": "hello world!",
            "ident": 2
        }, {
            "title": "hello world",
            "ident": 1
        }]
    }

    sio = io.StringIO()
    cluster = Cluster([
        json.dumps(line) for line in [
            {
                "title": "hello world",
                "ident": 1
            },
            {
                "title": "hello world!",
                "ident": 2
            },
            {
                "title": "other",
                "ident": 3
            },
        ]
    ],
                      release_key_title_normalized,
                      min_cluster_size=1,
                      output=sio)
    stats = cluster.run()
    assert stats == {
        "key_fail": 0,
        "key_ok": 3,
        "key_empty": 0,
        "key_denylist": 0,
        "num_clusters": 2
    }
    assert [json.loads(line) for line in sio.getvalue().split("\n") if line] == [{
        "k":
        "helloworld",
        "v": [{
            "title": "hello world!",
            "ident": 2
        }, {
            "title": "hello world",
            "ident": 1
        }]
    }, {
        'k':
        'other',
        'v': [{
            'ident': 3,
            'title': 'other'
        }]
    }]