162 lines
5.7 KiB
Python
162 lines
5.7 KiB
Python
|
||
import unittest
|
||
|
||
from collections import defaultdict
|
||
|
||
from kman import *
|
||
|
||
class TestKman(unittest.TestCase):
|
||
# initial
|
||
def setUp(self):
|
||
CLIPPATH = './test.data'
|
||
OUTPREF = './test.clip'
|
||
LOG2FILE = 1
|
||
DELIMITER= '|'
|
||
|
||
global t_bm_sec
|
||
global t_hl_sec
|
||
global t_nt_sec
|
||
global t_books
|
||
t_bm_sec = ["""另一半中国史 (高洪雷)
|
||
""", \
|
||
"""- 您在位置 #2468 的书签 | 添加于 2020年1月12日星期日 下午11:09:06
|
||
"""]
|
||
t_hl_sec = ["""薛兆丰经济学讲义 (薛兆丰)
|
||
""", \
|
||
"""- 您在位置 #1408-1410的标注 | 添加于 2020年1月13日星期一 上午8:11:05
|
||
""", \
|
||
"""边际就是“新增”带来的“新增”。
|
||
"""]
|
||
t_nt_sec = ["""薛兆丰经济学讲义 (薛兆丰)
|
||
""", \
|
||
"""- 您在位置 #4286 的笔记 | 添加于 2020年1月30日星期四 下午10:26:31
|
||
""", \
|
||
"""山寨 假货 问题
|
||
"""]
|
||
t_books = defaultdict(dict)
|
||
|
||
def cre_tbooks(self):
|
||
# parsing section & fill data structure
|
||
t_secd = parse_section(t_bm_sec,0)
|
||
|
||
t_secd = parse_section(t_hl_sec,1)
|
||
bn = t_secd['bookname']
|
||
t_books[bn]['author'] = t_secd[bn]['author']
|
||
t_books[bn]['1'] = t_secd[bn]['1']
|
||
t_secd.clear()
|
||
|
||
t_secd = parse_section(t_nt_sec,2)
|
||
bn = t_secd['bookname']
|
||
t_books[bn]['author'] = t_secd[bn]['author']
|
||
t_books[bn]['2'] = t_secd[bn]['2']
|
||
t_secd.clear()
|
||
|
||
return t_books
|
||
|
||
# test function parse_section
|
||
def test_parse_section(self):
|
||
# parsing section & fill data structure
|
||
t_secd = parse_section(t_bm_sec,0)
|
||
self.assertEqual(t_secd,False)
|
||
|
||
t_secd = parse_section(t_hl_sec,1)
|
||
bn = t_secd['bookname']
|
||
self.assertIsNotNone(t_secd)
|
||
self.assertEqual(bn,'薛兆丰经济学讲义 ')
|
||
self.assertEqual(t_secd[bn]['author'],'薛兆丰')
|
||
self.assertEqual(t_secd[bn]['1']['type'],'HL')
|
||
self.assertEqual(t_secd[bn]['1']['position'],'1408-1410')
|
||
self.assertEqual(t_secd[bn]['1']['day'],'2020年1月13日')
|
||
self.assertEqual(t_secd[bn]['1']['week'],'星期一')
|
||
self.assertEqual(t_secd[bn]['1']['meridiem'],'上午')
|
||
self.assertEqual(t_secd[bn]['1']['time'],'8:11:05')
|
||
self.assertEqual(t_secd[bn]['1']['content'],'边际就是“新增”带来的“新增”。\n')
|
||
t_books[bn]['author'] = t_secd[bn]['author']
|
||
t_books[bn]['1'] = t_secd[bn]['1']
|
||
t_secd.clear()
|
||
|
||
t_secd = parse_section(t_nt_sec,2)
|
||
bn = t_secd['bookname']
|
||
self.assertIsNotNone(t_secd)
|
||
self.assertEqual(bn,'薛兆丰经济学讲义 ')
|
||
self.assertEqual(t_secd[bn]['author'],'薛兆丰')
|
||
self.assertEqual(t_secd[bn]['2']['type'],'NT')
|
||
self.assertEqual(t_secd[bn]['2']['position'],'4286')
|
||
self.assertEqual(t_secd[bn]['2']['day'],'2020年1月30日')
|
||
self.assertEqual(t_secd[bn]['2']['week'],'星期四')
|
||
self.assertEqual(t_secd[bn]['2']['meridiem'],'下午')
|
||
self.assertEqual(t_secd[bn]['2']['time'],'10:26:31')
|
||
self.assertEqual(t_secd[bn]['2']['content'],'山寨 假货 问题\n')
|
||
t_books[bn]['author'] = t_secd[bn]['author']
|
||
t_books[bn]['2'] = t_secd[bn]['2']
|
||
t_secd.clear()
|
||
|
||
# test drop_duplicate
|
||
def test_drop_duplicate(self):
|
||
t_books = self.cre_tbooks()
|
||
t_secd = parse_section(t_hl_sec,3)
|
||
bn = t_secd['bookname']
|
||
t_books_du = t_books.copy()
|
||
t_books_du[bn]['3'] = t_secd[bn]['3']
|
||
t_books_du[bn]['4'] = t_secd[bn]['3']
|
||
self.assertIsInstance(t_books_du[bn]['3'],dict)
|
||
|
||
try:
|
||
t_books_du = drop_duplicate(t_books_du)
|
||
t = t_books_du[bn]['3']
|
||
except KeyError as keyerror:
|
||
print("与预期匹配,sidx 3 重复被删除,抛出: %s" % 'keyerror')
|
||
|
||
t_secd.clear()
|
||
|
||
# test function format_time()
|
||
def test_format_time(self):
|
||
t_ds = '2020年1月13日 星期一 下午 8:11:05'
|
||
t_ds = format_time(t_ds)
|
||
self.assertEqual(t_ds, '2020/1/13 20:11:05')
|
||
|
||
# test function format_data
|
||
def test_format_data(self):
|
||
t_books = self.cre_tbooks()
|
||
t_out = format_data(t_books, ft='MD')
|
||
self.assertEqual(t_out[0], 'TYPE|BOOKNAME|AUTHOR|MARKTIME|CONTENT')
|
||
self.assertEqual(t_out[1], '--|--|--|--|--')
|
||
self.assertEqual(t_out[2], 'HL|薛兆丰经济学讲义 |薛兆丰|2020/1/13 8:11:05|边际就是“新增”带来的“新增”。\n')
|
||
t_out.clear()
|
||
|
||
|
||
def test_add_note_to_highlight(self):
|
||
t_books = self.cre_tbooks()
|
||
t_books_remove_nt = add_note_to_highlight(t_books)
|
||
for k in t_books_remove_nt.keys():
|
||
bn = k
|
||
self.assertEqual((t_books_remove_nt[bn]['1']['content']).replace('\n',''),\
|
||
'边际就是“新增”带来的“新增”。'+NTPREF+ '山寨 假货 问题')
|
||
|
||
"""
|
||
def test_search_clip(self):
|
||
pass
|
||
def test_statistic(self):
|
||
pass
|
||
def test_dict2json(self):
|
||
pass
|
||
def test_json2dict(self):
|
||
pass
|
||
def test_import_clips(self):
|
||
pass
|
||
"""
|
||
|
||
# clean
|
||
def tearDown(self):
|
||
pass
|
||
|
||
if __name__ == '__main__':
|
||
|
||
"""
|
||
suite = unittest.TestSuite ()
|
||
suite.addTest(TestKman('test_parse_section'))
|
||
suite.addTest(TestKman('test_format_time'))
|
||
suite.addTest(TestKman('test_format_data'))
|
||
suite.addTest(TestKman('test_drop_duplicate'))
|
||
suite.addTest(TestKman('test_add_note_to_highlight'))
|
||
run = unittest.TextTestRunner (verbosity=2)
|
||
run.run (suite)
|
||
"""
|
||
|
||
# not callable sequency
|
||
unittest.main()
|
||
|
||
|