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_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_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_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 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
Exemplo n.º 6
0
 def search_by_query(self, query: Query) -> list:
     return self.tickets_table.get_tickets_by_conditions(query.to_dict())
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 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())