def test_get_OF_matrix(self): mock_word_info = mock_info.word_info mock_OF_matrix = mock_info.OF_matrix with session_scope() as session: session.query(Word).filter_by( word_name=mock_word_info['word_name']).delete() word_id = word_store.create( mock_word_info['word_name'], mock_word_info['ph'], mock_word_info['tran_means'], mock_word_info['mp3_url'], '', ) with session_scope() as session: word_factor_id = session.query(WordFactor).filter_by( word_id=word_id).first().id OF_objs = [ OptimumFactorMatrix(word_factor_id=word_factor_id, number=item[0], OF=item[1]) for item in mock_OF_matrix ] session.add_all(OF_objs) assert word_factor_store.get_OF_matrix(word_id) == mock_OF_matrix with session_scope() as session: session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.query(Word).filter_by(id=word_id).delete() session.query(OptimumFactorMatrix).delete()
def test_reduce_repeat_count(self): mock_word_info = mock_info.word_info with session_scope() as session: session.query(Word).filter_by( word_name=mock_word_info['word_name']).delete() word_obj = Word( word_name=mock_word_info['word_name'], ph=mock_word_info['ph'], ) session.add(word_obj) session.flush() word_id = word_obj.id review_list_store.del_word_in_list(word_id) review_list_store.add_word_in_list(word_id, new=False, repeat_count=2) review_list_store.reduce_repeat_count(word_id) with session_scope() as session: assert session.query(ReviewList).filter_by( word_id=word_id).first().repeat_count == 1 review_list_store.reduce_repeat_count(word_id) with session_scope() as session: assert session.query(ReviewList).filter_by( word_id=word_id).first() is None session.query(Mp3).delete() session.query(ReviewList).delete() session.query(Word).delete() session.query(OptimumFactorMatrix).delete()
def test_update_mp3(self): mock_word_info = mock_info.word_info with session_scope() as session: session.query(Word).filter_by( word_name=mock_word_info['word_name']).delete() word_id = word_store.create( mock_word_info['word_name'], mock_word_info['ph'], mock_word_info['tran_means'], mock_word_info['mp3_url'], '', ) mock_path = '/data/mp3/mock.mp3' mock_url = mock_word_info['mp3_url'] + 'mock' word_store.update_mp3(id=word_id, path=mock_path, url=mock_url) assert word_store.get_word(id=word_id)['mp3']['path'] == mock_path assert word_store.get_word(id=word_id)['mp3']['url'] == mock_url with session_scope() as session: session.query(Mp3).delete() session.query(ReviewList).delete() session.query(Word).delete() session.query(OptimumFactorMatrix).delete()
def test_review_a_word(self): # TODO I don't know how to test it. # SO, smoke testing. with session_scope() as session: session.query(Word).filter_by(word_name=mock_info.word_info['word_name']).delete() word_id = word_store.create( mock_info.word_info['word_name'], mock_info.word_info['ph'], mock_info.word_info['tran_means'], mock_info.word_info['mp3_url'], '', ) review_hander.review_a_word(word_id, 2) for i in range(3): review_hander.review_a_word(word_id, 5) with session_scope() as session: assert session.query(ReviewList).filter_by(word_id=word_id).first() is None review_list_store.add_word_in_list(word_id) review_hander.review_a_word(word_id, 5) with session_scope() as session: session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.query(Word).filter_by(id=word_id).delete()
def test_add_words(self): mock_words = [mock_info.word_info['word_name'], ] with session_scope() as session: session.query(models.Word).filter_by(word_name=mock_words[0]).delete() assert word_hander.add_words(mock_words)[0] == 0 with session_scope() as session: word = session.query(models.Word).filter_by(word_name=mock_words[0]).first() assert word.word_name == mock_info.word_info['word_name'] word_id = word.id session.query(models.Mp3).filter_by(word_id=word_id).delete() session.query(models.ReviewList).filter_by(word_id=word_id).delete() session.query(models.Word).filter_by(id=word_id).delete()
def get_OF_matrix(self, word_id): """Return historical optimum factor matrix of the word""" with session_scope() as session: word_factor = session.query(WordFactor).filter_by(word_id=word_id).first() OF_objs = word_factor.OF_matrix OF_matrix = [(OF_i.number, OF_i.OF) for OF_i in OF_objs] return OF_matrix
def get_EF(self, word_id): """Return word's EF""" with session_scope() as session: word_factor = session.query(WordFactor).filter_by( word_id=word_id).first() EF = word_factor.EF return EF
def set_EF(self, word_id, EF, session=None): """Set EF for word""" with session_scope(session=session) as session: word_factor = session.query(WordFactor).filter_by( word_id=word_id).first() word_factor.EF = EF return EF
def set_next_review_time(self, word_id, next_review_time, session=None): """set next review time for word""" with session_scope(session=session) as session: word_factor = session.query(WordFactor).filter_by( word_id=word_id).first() word_factor.next_review_time = next_review_time return next_review_time
def test_create(self): mock_word_info = mock_info.word_info with session_scope() as session: session.query(Word).filter_by( word_name=mock_word_info['word_name']).delete() session.commit() word_id = word_store.create( mock_word_info['word_name'], mock_word_info['ph'], mock_word_info['tran_means'], mock_word_info['mp3_url'], '', ) word_obj = session.query(Word).filter_by(id=word_id).first() assert word_obj.word_name == mock_word_info['word_name'] assert word_obj.ph == mock_word_info['ph'] tran_mean = word_obj.tran_means[0] assert { 'part': tran_mean.part, 'means': json.loads(tran_mean.means) } == mock_word_info['tran_means'][0] assert word_obj.mp3.url == mock_word_info['mp3_url'] assert isinstance(word_obj.word_factor, WordFactor) session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.delete(word_obj) session.query(OptimumFactorMatrix).delete()
def reduce_repeat_count(self, word_id, session=None): """reduce repeat_count for word""" with session_scope(session=session) as session: item = session.query(ReviewList).filter_by(word_id=word_id).first() if item.repeat_count > 1: item.repeat_count -= 1 else: session.delete(item) return True
def set_OF_matrix(self, word_id, OF_matrix, session=None): """set OF matrix for word""" with session_scope(session=session) as session: word_factor = session.query(WordFactor).filter_by(word_id=word_id).first() session.query(OptimumFactorMatrix).filter_by(word_factor_id=word_factor.id).delete() OF_objs = [OptimumFactorMatrix(word_factor_id=word_factor.id, number=item[0], OF=item[1]) for item in OF_matrix] session.add_all(OF_objs) return OF_matrix
def test_get_EF(self): mock_word_info = mock_info.word_info with session_scope() as session: session.query(Word).filter_by( word_name=mock_word_info['word_name']).delete() word_id = word_store.create( mock_word_info['word_name'], mock_word_info['ph'], mock_word_info['tran_means'], mock_word_info['mp3_url'], '', ) assert word_factor_store.get_EF(word_id) == 1.3 with session_scope() as session: session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.query(Word).filter_by(id=word_id).delete()
def get_today_word_ids(self, session=None): """get the words next_review_time in today""" now = datetime.datetime.now() end = now.replace(hour=0, minute=0, second=0) + datetime.timedelta(days=1) with session_scope(session=session) as session: word_Factor_objs = session.query(WordFactor).filter( WordFactor.next_review_time < end ).all() word_ids = [word_factor.word_id for word_factor in word_Factor_objs] return word_ids
def init_review_list(self): """put today' words need review in review list""" # TODO unit test if sys_info_store.get_review_list_time() != datetime.datetime.now().strftime("%Y-%m-%d"): with session_scope() as session: word_ids = word_factor_store.get_today_word_ids(session=session) for word_id in word_ids: review_list_store.add_word_in_list(word_id, session=session) sys_info_store.set_review_list_time(session=session) console_hander.msg("已添加{}个单词到今日学习计划".format(len(word_ids)))
def test_get_factors(self): with session_scope() as session: session.query(Word).filter_by(word_name=mock_info.word_info['word_name']).delete() word_id = word_store.create( mock_info.word_info['word_name'], mock_info.word_info['ph'], mock_info.word_info['tran_means'], mock_info.word_info['mp3_url'], '', ) IF, EF, OF_matrix = review_hander.get_factors(word_id, 0) assert IF == 1 assert EF == MAX_EF assert OF_matrix == [(0, MIN_EF)] # TODO Here need 'else' test with session_scope() as session: session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.query(Word).filter_by(id=word_id).delete()
def test_get_words_count(self): with session_scope() as session: session.query(Word).delete() word_store.create( mock_info.word_info['word_name'], mock_info.word_info['ph'], mock_info.word_info['tran_means'], mock_info.word_info['mp3_url'], '', ) assert word_store.get_words_count() == 1 with session_scope() as session: session.query(Mp3).delete() session.query(TranMean).delete() session.query(ReviewList).delete() session.query(Word).delete() session.query(WordFactor).delete() session.query(OptimumFactorMatrix).delete()
def get_a_word_id(self): """Return a word_id in review list.""" with session_scope() as session: learn_list = session.query(ReviewList).all() if not learn_list: raise exception.LearnListEmptyException item = random.choice(learn_list) word_id = item.word_id return word_id
def test_set_OF_matrix(self): mock_word_info = mock_info.word_info with session_scope() as session: session.query(Word).filter_by( word_name=mock_word_info['word_name']).delete() word_id = word_store.create( mock_word_info['word_name'], mock_word_info['ph'], mock_word_info['tran_means'], mock_word_info['mp3_url'], '', ) word_factor_store.set_OF_matrix(word_id, mock_info.OF_matrix) assert word_factor_store.get_OF_matrix(word_id) == mock_info.OF_matrix with session_scope() as session: session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.query(Word).filter_by(id=word_id).delete() session.query(OptimumFactorMatrix).delete()
def test_get_word_type(self): mock_word_info = mock_info.word_info with session_scope() as session: session.query(Word).filter_by( word_name=mock_word_info['word_name']).delete() word_obj = Word( word_name=mock_word_info['word_name'], ph=mock_word_info['ph'], ) session.add(word_obj) session.flush() word_id = word_obj.id review_list_store.add_word_in_list(word_id) assert review_list_store.get_word_type( word_id) is ReviewList.WordType.review with session_scope() as session: session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.query(Word).filter_by(id=word_id).delete()
def test_del_word_in_list(self): mock_word_info = mock_info.word_info with session_scope() as session: session.query(Word).filter_by( word_name=mock_word_info['word_name']).delete() word_obj = Word( word_name=mock_word_info['word_name'], ph=mock_word_info['ph'], ) session.add(word_obj) session.flush() word_id = word_obj.id review_list_store.del_word_in_list(word_id) with session_scope() as session: assert session.query(ReviewList).filter_by( word_id=word_id).first() is None session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.query(Word).filter_by(id=word_id).delete()
def test_set_next_review_time(self): with session_scope() as session: session.query(Word).filter_by( word_name=mock_info.word_info['word_name']).delete() word_id = word_store.create( mock_info.word_info['word_name'], mock_info.word_info['ph'], mock_info.word_info['tran_means'], mock_info.word_info['mp3_url'], '', ) word_factor_store.set_next_review_time( word_id=word_id, next_review_time=mock_info.next_review_time) with session_scope() as session: assert session.query(WordFactor).filter_by(word_id=word_id).first( ).next_review_time == mock_info.next_review_time session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.query(Word).filter_by(id=word_id).delete()
def test_get_today_word_ids(self): with session_scope() as session: session.query(Word).filter_by( word_name=mock_info.word_info['word_name']).delete() word_id = word_store.create( mock_info.word_info['word_name'], mock_info.word_info['ph'], mock_info.word_info['tran_means'], mock_info.word_info['mp3_url'], '', session=session, ) word_factor = session.query(WordFactor).filter_by( word_id=word_id).first() word_factor.next_review_time = datetime.datetime.now() assert word_factor_store.get_today_word_ids() == [word_id] with session_scope() as session: session.query(Mp3).filter_by(word_id=word_id).delete() session.query(ReviewList).filter_by(word_id=word_id).delete() session.query(WordFactor).filter_by(word_id=word_id).delete() session.query(Word).filter_by(id=word_id).delete()
def test_get_words(self): with session_scope() as session: session.query(Word).filter_by( word_name=mock_info.word_info['word_name']).delete() test_word_ids = [] for i in range(10): word_id = word_store.create( mock_info.word_info['word_name'] + str(i), mock_info.word_info['ph'], mock_info.word_info['tran_means'], mock_info.word_info['mp3_url'], '', ) test_word_ids.append(word_id) word_infos = word_store.get_words(ids=test_word_ids, start=2, end=6) assert len(word_infos) == 4 with session_scope() as session: session.query(Mp3).delete() session.query(ReviewList).delete() session.query(Word).delete() session.query(OptimumFactorMatrix).delete()
def get_word(self, id=None, word=None, session=None): """Return a Word obj """ with session_scope(session=session) as session: kwargs = {} if id: kwargs['id'] = id elif word: kwargs['word_name'] = word else: return None word_obj = session.query(Word).filter_by(**kwargs).first() word_info = dumps_alchemy(word_obj, ['mp3', 'tran_means']) return word_info
def update_mp3(self, id=None, word=None, path=None, url=None, session=None): """Update mp3 Info""" kwargs = {} if id: kwargs['id'] = id elif word: kwargs['word'] = word with session_scope(session=session) as session: word_obj = session.query(Word).filter_by(id=id).first() mp3_obj = word_obj.mp3 if path: mp3_obj.path = path if url: mp3_obj.url = url return True
def create(self, word_name, ph, tran_means, mp3_url, mp3_path, session=None): """create word in memory store""" with session_scope(session=session) as session: if session.query(Word).filter_by(word_name=word_name).first(): raise exception.WordRepeatAddException(word_name) # create word_obj tran_mean_objs = [] for tran_mean in tran_means: mean_obj = TranMean( part=tran_mean['part'], means=json.dumps(tran_mean['means']), ) tran_mean_objs.append(mean_obj) mp3_obj = Mp3( url=mp3_url, path=mp3_path, ) tomorrow = datetime.datetime.now() + datetime.timedelta(days=1) word_factor = WordFactor( EF=1.3, number=0, next_review_time=tomorrow, ) word_obj = Word( word_name=word_name, ph=ph, tran_means=tran_mean_objs, mp3=mp3_obj, word_factor=word_factor, ) session.add(word_obj) session.flush() review_list_store.add_word_in_list(word_obj.id, new=True, session=session) word_obj_id = word_obj.id return word_obj_id
def test_get_a_word_id(self): word_id = word_store.create( mock_info.word_info['word_name'], mock_info.word_info['ph'], mock_info.word_info['tran_means'], mock_info.word_info['mp3_url'], '', ) assert isinstance(review_list_store.get_a_word_id(), int) with session_scope() as session: session.query(Mp3).delete() session.query(ReviewList).delete() session.query(Word).delete() session.query(TranMean).delete() session.query(OptimumFactorMatrix).delete()
def add_word_in_list(self, word_id, new=False, repeat=False, repeat_count=1, session=None): """Add new word to review list.""" with session_scope(session=session) as session: if new: w_type = ReviewList.WordType.new elif repeat: w_type = ReviewList.WordType.repeat else: w_type = ReviewList.WordType.review review_list_obj = ReviewList( word_id=word_id, w_type=w_type, repeat_count=repeat_count, ) session.add(review_list_obj) return True
def get_words(self, ids=None, start=None, end=None, session=None): """Return word_infos""" # TODO: unit test with session_scope(session=session) as session: query = session.query(Word) if ids: query = query.filter(Word.id.in_(ids)) if start or end: query = query.slice(start, end) word_objs = query.all() expand_fields = ['mp3', 'tran_means', 'word_factor'] word_infos = [ dumps_alchemy(word_obj, expand_fields) for word_obj in word_objs ] return word_infos