/
test_recommender.py
62 lines (51 loc) · 2.28 KB
/
test_recommender.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
from prelims import Post
from prelims.processor import Recommender
from unittest import TestCase
import os
class RecommenderTestCase(TestCase):
def setUp(self):
# file paths
self.path_a = os.path.abspath(
os.path.join(os.sep, 'path', 'to', 'articles', 'a.md'))
self.path_b = os.path.abspath(
os.path.join(os.sep, 'path', 'to', 'articles', 'b.md'))
self.path_c = os.path.abspath(
os.path.join(os.sep, 'path', 'to', 'articles', 'c', 'index.md'))
# urls
self.permalink_base = '/diary/post'
self.permalink_a = '/diary/post/a/'
self.permalink_b = '/diary/post/b/'
self.permalink_c = '/diary/post/c/'
def test_process(self):
post_a = Post(self.path_a, {'title': 'foo'},
'', 'Hello world.', 'utf-8')
post_b = Post(self.path_b, {'title': 'bar'},
'', 'This is a pen.', 'utf-8')
post_c = Post(self.path_c, {'title': 'buzz'},
'', 'There is a man in the high castle.', 'utf-8')
posts = [post_a, post_b, post_c]
recommender = Recommender(permalink_base=self.permalink_base,
stop_words='english')
recommender.process(posts)
# sort alphabetically since the order may differ
post_a.front_matter['keywords'] = sorted(
post_a.front_matter['keywords'])
self.assertEqual(post_a.front_matter, {
'title': 'foo',
'recommendations': [self.permalink_c, self.permalink_b],
'keywords': ['castle', 'hello', 'high', 'man', 'pen', 'world']
})
post_b.front_matter['keywords'] = sorted(
post_b.front_matter['keywords'])
self.assertEqual(post_b.front_matter, {
'title': 'bar',
'recommendations': [self.permalink_c, self.permalink_a],
'keywords': ['castle', 'hello', 'high', 'man', 'pen', 'world']
})
post_c.front_matter['keywords'] = sorted(
post_c.front_matter['keywords'])
self.assertEqual(post_c.front_matter, {
'title': 'buzz',
'recommendations': [self.permalink_b, self.permalink_a],
'keywords': ['castle', 'hello', 'high', 'man', 'pen', 'world']
})