def reset_drafting_query(self, user_id: int, username: str, category: int) -> Ticket: self.redis.clean(user_id=user_id, action='query') self.redis.save(user_id=user_id, action='query', content=Query(category_id=category, user_id=user_id, username=username).to_dict()) query = self.redis.load(user_id=user_id, action='query') event_logger.info(query) return Query(category_id=-1, user_id=user_id, username=username).to_obj(query)
def test_query_init(self): query = Query(user_id=USER_ID, username=USERNAME, category_id=CATEGORY) expect = dict( category=1, dates=list(), prices=list(), quantities=list(), status=1, username=USERNAME, user_id=USER_ID ) self.assertDictEqual(query.to_dict(), expect)
def test_query_to_human_readable(self): sample_query = dict( category=1, dates=[503, 504], prices=[1, 2], quantities=[2, 3], status=1, username=USERNAME, user_id=USER_ID ) query = Query(user_id=USER_ID, username=USERNAME, category_id=CATEGORY).to_obj(sample_query) query_string = query.to_human_readable() assert query_string['category'] == '原價轉讓' assert query_string['dates'] == '5.3(Fri), 5.4(Sat)' assert query_string['prices'] == '$1180座位, $880座位' assert query_string['quantities'] == '2, 3' assert query_string['status'] == '待交易'
def test_query_dict_to_obj(self): query = dict( category=1, dates=[503, 504], prices=[1, 2], quantities=[2, 3], status=1, username=USERNAME, user_id=USER_ID ) obj = Query(user_id=USER_ID, username=USERNAME, category_id=CATEGORY).to_obj(query) assert obj.dates == query['dates'] assert obj.prices == query['prices'] assert obj.quantities == query['quantities'] assert obj.status == query['status'] assert obj.category == query['category'] assert obj.to_dict() == query
def match_my_tickets(self, user_id: int) -> list: query = Query(category_id=2, user_id=user_id) for ticket in query_helper.search_by_user_id(user_id): if ticket.category == 2: for wish_date in ticket.wish_dates: query.update_field('dates', wish_date) for wish_price in ticket.wish_price_ids: query.update_field('prices', wish_price) for wish_quantity in ticket.wish_quantities: query.update_field('quantities', wish_quantity) self.save_drafting_query(user_id, query) return query_helper.search_by_query(query)
def test_drafting_query(self): query = Query(category_id=1, user_id=USER_ID, username=USERNAME) query.update_field('dates', 503) self.helper.save_drafting_query(user_id=USER_ID, query=query) query_in_cache = self.helper.load_drafting_query(user_id=USER_ID) assert query_in_cache.to_dict() == query.to_dict() # Reset Ticket raw_query = Query(category_id=1, user_id=USER_ID, username=USERNAME) query_in_cache = self.helper.reset_drafting_query(user_id=USER_ID, username=USERNAME, category=1) assert query_in_cache.to_dict() == raw_query.to_dict()
def test_get_ticket_by_conditions(self): query = Query(category_id=1, user_id=8081, username='******') query.update_field('prices', 2) results = self.db.get_tickets_by_conditions(query.to_dict()) result = results[0] assert result ticket = Ticket().to_obj(TICKET_1) assert result assert result.id assert result.category == ticket.category assert result.date == ticket.date assert result.price_id == ticket.price_id assert result.quantity == ticket.quantity assert result.section == ticket.section assert result.row == ticket.row assert result.status == ticket.status assert result.wish_dates == ticket.wish_dates assert result.wish_price_ids == ticket.wish_price_ids assert result.wish_quantities == ticket.wish_quantities assert result.user_id == ticket.user_id assert result.username == ticket.username assert result.remarks == ticket.remarks
def test_generate_query(self): redis = RedisController(db_name='search') helper = FeatureHelper(feature='search', redis_controller=redis) query = Query(category_id=1, user_id=USER_ID, username=USERNAME) assert helper.save_drafting_query(USER_ID, query) cached_query = helper.load_drafting_query(USER_ID) assert cached_query.category == 1 assert cached_query.user_id == USER_ID assert cached_query.username == USERNAME # Update Field query = cached_query query.update_field('dates', 504) helper.save_drafting_query(USER_ID, query) cached_query = helper.load_drafting_query(USER_ID) assert cached_query.dates == query.dates # Update Field query = cached_query query.update_field('dates', 505) helper.save_drafting_query(USER_ID, query) cached_query = helper.load_drafting_query(USER_ID) assert cached_query.dates == query.dates assert cached_query.to_human_readable() == dict( category='原價轉讓', dates='5.4(Sat), 5.5(Sun)', prices='', quantities='', status='待交易', user_id=8081, username='******') # Save to Quick Search query_helper = QueryHelper( TicketsModel(mayday.engine, mayday.metadata, role='writer')) quick_search = helper.load_drafting_query(USER_ID) assert query_helper.save_quick_search(quick_search) quick_search_query = query_helper.load_quick_search(user_id=USER_ID) assert query.category == quick_search_query.category assert query.dates == quick_search_query.dates assert query.to_dict() == quick_search_query.to_dict()
def test_search_ticket_by_query(self): helper = QueryHelper(self.db) ticket = helper.search_by_query( Query(category_id=1).to_obj(dict(prices=[2])))[0] print(ticket.to_dict()) assert ticket assert ticket.category == 1 assert ticket.id assert ticket.date == 505 assert ticket.price_id == 2 assert ticket.quantity == 1 assert ticket.section == 'C1' assert ticket.row == '' assert ticket.seat == '' assert ticket.wish_dates == list() assert ticket.wish_price_ids == list() assert ticket.wish_quantities == list() assert ticket.source_id == 1 assert ticket.remarks == '' assert ticket.status == 1 assert ticket.username == 'test_account_1' assert ticket.user_id == 8081
def test_query_update_field(self): query = Query(user_id=USER_ID, username=USERNAME, category_id=CATEGORY) query.update_field('category', 1) assert isinstance(query.category, int) assert query.category == 1 query.update_field('dates', 503) assert isinstance(query.dates, list) assert query.dates == [503] query.update_field('dates', 504) assert isinstance(query.dates, list) assert query.dates == [503, 504] query.update_field('dates', 505) assert isinstance(query.dates, list) assert query.dates == [503, 504, 505] query.update_field('dates', 510) assert isinstance(query.dates, list) assert query.dates == [503, 504, 505, 510] query.update_field('dates', 511) assert isinstance(query.dates, list) assert query.dates == [503, 504, 505, 510, 511] query.update_field('prices', 1) assert isinstance(query.prices, list) assert query.prices == [1] query.update_field('prices', 2) assert isinstance(query.prices, list) assert query.prices == [1, 2] query.update_field('prices', 3) assert isinstance(query.prices, list) assert query.prices == [1, 2, 3] query.update_field('prices', 4) assert isinstance(query.prices, list) assert query.prices == [1, 2, 3, 4] query.update_field('quantities', 1) assert isinstance(query.quantities, list) assert query.quantities == [1] query.update_field('quantities', 2) assert isinstance(query.quantities, list) assert query.quantities == [1, 2] query.update_field('quantities', 3) assert isinstance(query.quantities, list) assert query.quantities == [1, 2, 3] query.update_field('quantities', 4) assert isinstance(query.quantities, list) assert query.quantities == [1, 2, 3, 4] query.update_field('status', 1) assert isinstance(query.status, int) assert query.status == 1 query.update_field('status', 2) assert isinstance(query.status, int) assert query.status == 2 # Remove query.update_field('quantities', 4, remove=True) assert isinstance(query.quantities, list) assert query.quantities == [1, 2, 3] query.update_field('quantities', 3, remove=True) assert isinstance(query.quantities, list) assert query.quantities == [1, 2] query.update_field('quantities', 2, remove=True) assert isinstance(query.quantities, list) assert query.quantities == [1] query.update_field('dates', 511, remove=True) assert isinstance(query.dates, list) assert query.dates == [503, 504, 505, 510] query.update_field('dates', 510, remove=True) assert isinstance(query.dates, list) assert query.dates == [503, 504, 505] query.update_field('dates', 505, remove=True) assert isinstance(query.dates, list) assert query.dates == [503, 504] query.update_field('dates', 504, remove=True) assert isinstance(query.dates, list) assert query.dates == [503] query.update_field('dates', 503, remove=True) assert isinstance(query.dates, list) assert query.dates == list() query.update_field('prices', 4, remove=True) assert isinstance(query.prices, list) assert query.prices == [1, 2, 3] query.update_field('prices', 3, remove=True) assert isinstance(query.prices, list) assert query.prices == [1, 2] query.update_field('prices', 2, remove=True) assert isinstance(query.prices, list) assert query.prices == [1] query.update_field('prices', 1, remove=True) assert isinstance(query.prices, list) assert query.prices == list()
def test_query_validation(self): query = Query(user_id=USER_ID, username=USERNAME, category_id=1) assert query.validate()
def load_drafting_query(self, user_id: int) -> Query: result = self.redis.load(user_id=user_id, action='query') logger.info(result) event_logger.info(result) return Query(category_id=-1, user_id=user_id).to_obj(result)
def save_drafting_query(self, user_id: int, query: Query) -> bool: logger.info(query.to_dict()) event_logger.info(query.to_dict()) return self.redis.save(user_id=user_id, action='query', content=query.to_dict())
def reset_cache(self, user_id: int) -> Query: query = Query(category_id=1, user_id=user_id) self.save_drafting_query(user_id, query) return self.load_drafting_query(user_id)
def search_by_query(self, query: Query) -> list: return self.tickets_table.get_tickets_by_conditions(query.to_dict())
def load_quick_search(self, user_id: int) -> Query: result = self.redis.load(user_id, 'quick_search') logger.debug(result) if result: return Query(category_id=result['category']).to_obj(result) return Query(category_id=-1).to_obj(result)
def save_quick_search(self, query: Query) -> bool: logger.debug(query.to_dict()) return self.redis.save(query.user_id, 'quick_search', query.to_dict(), expiration=2592000)