This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
141 lines (109 loc) · 3.57 KB
/
tests.py
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
import pytest
from run import Dataset, deduplicate_datasets
@pytest.fixture
def dataset_1():
return Dataset(
nb_hits=1,
default_order=1,
id="dataset-1",
title="title",
page="",
acronym="",
post_url="",
description="",
)
@pytest.fixture
def dataset_2():
return Dataset(
nb_hits=1,
default_order=1,
id="dataset-2",
title="title",
page="",
acronym="",
post_url="",
description="",
)
def test_dataset_deduplicate_different_ids(dataset_1, dataset_2):
dataset_2.nb_hits = 2
datasets = deduplicate_datasets([dataset_1, dataset_2])
assert [d.id for d in sorted(datasets)] == [
"dataset-1",
"dataset-2",
]
def test_dataset_deduplicate_same_id(dataset_1, dataset_2):
dataset_2.nb_hits = 2
dataset_2.id = "dataset-1"
datasets = deduplicate_datasets([dataset_1, dataset_2])
assert len(datasets) == 1
assert [d.id for d in sorted(datasets)] == [
"dataset-1",
]
def test_dataset_order_default_by_nb_hits(dataset_1, dataset_2):
dataset_1.nb_hits = 2
assert [d.id for d in sorted([dataset_1, dataset_2])] == [
"dataset-2",
"dataset-1",
]
def test_dataset_order_fallback_on_default_order(dataset_1, dataset_2):
dataset_1.default_order = 2
assert [d.id for d in sorted([dataset_1, dataset_2])] == [
"dataset-2",
"dataset-1",
]
def test_dataset_populate_excerpt_on_creation():
dataset = Dataset(
nb_hits=1,
default_order=1,
id="dataset-1",
title="title",
page="",
acronym="",
post_url="",
description="foo bar",
)
assert dataset.excerpt == "<p>foo bar</p>"
def test_dataset_populate_excerpt_manual(dataset_1):
assert dataset_1.excerpt == ""
dataset_1.populate_excerpt("<p>foo bar</p>")
assert dataset_1.excerpt == "<p>foo bar</p>"
def test_dataset_populate_excerpt_removes_whitelisted_html_tags(dataset_1):
dataset_1.populate_excerpt("<p>foo</p> <script></script> <strong>bar</strong>")
assert dataset_1.excerpt == "<p>foo</p> bar"
def test_dataset_populate_excerpt_removes_urls(dataset_1):
dataset_1.populate_excerpt("foo http://foo.bar https://baz.quux bar")
assert dataset_1.excerpt == "foo bar"
def test_dataset_populate_excerpt_keeps_punctuation(dataset_1):
dataset_1.populate_excerpt("foo, bar.")
assert dataset_1.excerpt == "foo, bar."
def test_dataset_populate_excerpt_keeps_apostrophe(dataset_1):
dataset_1.populate_excerpt("foo l’bar")
assert dataset_1.excerpt == "foo l’bar"
def test_dataset_populate_excerpt_keeps_stop_words(dataset_1):
dataset_1.populate_excerpt("foo seraient bar")
assert dataset_1.excerpt == "foo seraient bar"
def test_dataset_populate_excerpt_truncates_words(dataset_1):
dataset_1.populate_excerpt("foo bar", num_words=1)
assert dataset_1.excerpt == "foo…"
def test_dataset_populate_excerpt_truncates_long_words(dataset_1):
dataset_1.populate_excerpt("fooooooooo bar", num_words=1)
assert dataset_1.excerpt == "foooooooo…"
def test_dataset_asdict():
dataset = Dataset(
nb_hits=1,
default_order=1,
id="dataset-1",
title="title",
page="page",
acronym="ACRONYM",
post_url="post_url",
description="foo bar",
)
assert dataset.asdict == {
"id": "dataset-1",
"title": "title",
"acronym": "ACRONYM",
"page": "page",
"excerpt": "<p>foo bar</p>",
"post_url": "post_url",
}