单元测试与TMK项目复盘
第一个问题:如何处理接口结果的落表以及读取
def parse_json(self, data):
res = []
for i in data:
df_dict = {}
json_dict = json.loads(i[1])
content_words, context_time = "", 0
if not json_dict.get('results'):
pass
elif not json_dict.get('results').get('check'):
pass
else:
for context in json_dict['results']['check']:
t = context['sent_end'] - context['sent_beg']
content_words += context['text']
context_time += t
if start_time == self.start_time:
df_dict['request_id'] = i[0]
df_dict['start_time'] = start_time
df_dict['content'] = content_words
df_dict['context_time'] = context_time
res.append(df_dict)
return res
一般接口会返回request_id以及接口返回的结果。如果接口服务没有处理该request_id的请求,如图所示,接口返回的字典就没有results 与check 的key,我们需要对这些case进行处理,返回的是空字符串以及0。
设计模式如何应用的?
class ObeyedWord(object):
"""梅赛德斯
"""
def __init__(self):
self.dict = {}
self.dict['捆绑销售'] = ['学而思', '新东方', '猿辅导', '作业帮', '清北网校', '掌门']
self.dict['不合法不合规'] = ['提升', '提分', '提高', '出题人', '专业', '专家', '原价', '国家级', '最高级', '顶级', '最佳', '专利', '高考状元', '名师', '名校',
'资深老师', '班主任', '大师', '突破', '逆袭', '薪资', '轻松', '快人一倍', '一次成功', '无忧', '保障', '金牌', '提前预习', '轻松掌握',
'轻松取得', '轻松提分', '快速提升', '快速提分', '提前学', '预习', '正价课', '正价课程', '金牌', '平均教龄', '冲刺满分', '弯道超车']
self.dict['过度承诺'] = ['清华', '北大', '一对一', '保证', '承诺', '哈佛', '一线品牌', '一线在线品牌', '必考', '教学经验', '以上教学经验', '以上的教学经验',
'清北老师', '985211', '北大清华的名师', '以上的教育经验', '清北', '北京大学', '清华大学']
self.dict['自称高途'] = ['高途课堂老师', '高途课堂的老师', '高途课堂员工', '高途课堂的员工', '高途的老师', '高途老师']
def __repr__(self):
return json.dumps(self.dict)
def GetDict(self):
return self.dict
class RecallWord(object):
"""宝马
"""
def __init__(self):
self.dict = {}
self.dict['名师'] = ['老师']
self.dict['清北'] = ['老师']
self.dict['掌门'] = ['一对一']
def __repr__(self):
return json.dumps(self.dict)
def GetDict(self):
return self.dict
class SimpleWordFactory(object):
"""简单工厂
"""
@staticmethod
def product_object(name):
if name == 'obeyed_words':
return ObeyedWord()
elif name == 'recall_words':
return RecallWord()
单元测试怎么做的?
import unittest
import pandas as pd
from contrib.match import *
from experiments.bin.SendMail import SimpleWordFactory
from experiments.bin.SendMail import identify_obeyed_words
factory = SimpleWordFactory()
obeyed_words_dict = factory.product_object('obeyed_words').GetDict()
recall_words_dict = factory.product_object('recall_words').GetDict()
ah = ac_automation()
ah.parse(dict(obeyed_words_dict, **recall_words_dict))
content = '啊,家长您好,我们这边是您是课堂的老师'
class UnitTest(unittest.TestCase):
def test_unit(self):
obeyed_word_str, obeyed_type, score = identify_obeyed_words(content, ah, obeyed_words_dict, recall_words_dict)
self.assertEqual(obeyed_word_str, '名师')
if __name__ == '__main__':
unittest.main()
|