def test_ticket_init(self):
     ticket = Ticket(USER_ID, USERNAME)
     self.assertDictEqual(
         ticket.to_dict(),
         dict(
             id=0,
             category=int(),
             date=int(),
             price_id=int(),
             quantity=int(),
             section='',
             row='',
             seat='',
             wish_dates=list(),
             wish_price_ids=list(),
             wish_quantities=list(),
             source_id=0,
             remarks='',
             status=1,
             username=USERNAME,
             user_id=USER_ID,
             created_at=ticket.created_at,
             updated_at=ticket.updated_at
         )
     )
 def test_ticket_to_human_readable(self):
     sample_ticket = dict(
         category=1,
         date=503,
         price_id=1,
         quantity=2,
         section='Yellow',
         row='32',
         seat='59',
         wish_dates=[504, 505],
         wish_price_ids=[1],
         wish_quantities=[1, 2],
         source_id=1,
         remarks='',
         status=1,
         username=USERNAME,
         user_id=USER_ID
     )
     ticket = Ticket(USER_ID, USERNAME).to_obj(sample_ticket)
     ticket_string = ticket.to_human_readable()
     assert ticket_string['category'] == '原價轉讓'
     assert ticket_string['date'] == '5.3(Fri)'
     assert ticket_string['price'] == '$1180座位'
     assert ticket_string['quantity'] == 2
     assert ticket_string['section'] == 'Yellow'
     assert ticket_string['row'] == '32'
     assert ticket_string['seat'] == '59'
     assert ticket_string['wish_dates'] == '5.4(Sat), 5.5(Sun)'
     assert ticket_string['wish_prices'] == '$1180座位'
     assert ticket_string['wish_quantities'] == '1, 2'
     assert ticket_string['status'] == '待交易'
     assert ticket_string['remarks'] == ''
示例#3
0
 def reset_drafting_ticket(self, user_id: int, username: str) -> Ticket:
     self.redis.clean(user_id=user_id, action='ticket')
     self.redis.save(user_id=user_id,
                     action='ticket',
                     content=Ticket(user_id=user_id,
                                    username=username).to_dict())
     ticket = self.redis.load(user_id=user_id, action='ticket')
     event_logger.info(ticket)
     return Ticket(user_id=user_id, username=username).to_obj(ticket)
示例#4
0
    def test_validator_error_message(self):
        ticket = Ticket(username=USERNAME, user_id=USER_ID)
        expected = '門票類別未填喔\n日期未填喔\n價錢未填喔\n數量未填喔'
        result = ticket.validate()
        assert result['status'] is False
        assert result['info'] == expected

        validator = ItemValidator(ticket.to_dict())
        result = validator.check_ticket()
        assert result['status'] is False
        assert result['info'] == expected
 def test_ticket_dict_to_obj(self):
     ticket = {
         'id': 0,
         'category': 1,
         'date': 503,
         'price_id': 1,
         'quantity': 1,
         'section': 'Yellow',
         'row': '32',
         'seat': '59',
         'wish_dates': list(),
         'wish_price_ids': list(),
         'wish_quantities': list(),
         'source_id': 0,
         'remarks': '',
         'status': 1,
         'username': USERNAME,
         'user_id': USER_ID
     }
     obj = Ticket(USER_ID, USERNAME).to_obj(ticket)
     assert obj.date == ticket['date']
     assert obj.price_id == ticket['price_id']
     assert obj.quantity == ticket['quantity']
     assert obj.status == ticket['status']
     assert obj.category == ticket['category']
     assert obj.id == 0
    def test_save_formal_ticket(self):
        ticket = Ticket(user_id=USER_ID, username=USERNAME)
        assert self.helper.save_ticket(ticket)

        tickets_in_db = self.db.get_tickets_by_user_id(USER_ID)
        assert tickets_in_db

        ticket_in_db = tickets_in_db[0]

        assert ticket.category == ticket_in_db.category
        assert ticket.date == ticket_in_db.date
        assert ticket.price_id == ticket_in_db.price_id
        assert ticket.quantity == ticket_in_db.quantity
        assert ticket.section == ticket_in_db.section
        assert ticket.row == ticket_in_db.row
        assert ticket.seat == ticket_in_db.seat
        assert ticket.status == ticket_in_db.status
        assert ticket.remarks == ticket_in_db.remarks
        assert ticket.wish_dates == ticket_in_db.wish_dates
        assert ticket.wish_price_ids == ticket_in_db.wish_price_ids
        assert ticket.wish_quantities == ticket_in_db.wish_quantities
        assert ticket.user_id == ticket_in_db.user_id
        assert ticket.username
        assert ticket.username == ticket_in_db.username
        assert ticket_in_db.id  # can not know the ticket if before insert
        assert ticket_in_db.created_at  # can not know the created ts before create
        assert ticket_in_db.updated_at  # always change
示例#7
0
    def test_validator_success(self):
        ticket = Ticket(username=USERNAME, user_id=USER_ID)
        ticket.category = 1
        ticket.date = 508
        ticket.price_id = 1
        ticket.quantity = 2
        ticket.status = 1
        result = ticket.validate()
        assert result['status']

        validator = ItemValidator(ticket.to_dict())
        result = validator.check_ticket()
        assert result['status']
    def test_split_tickets_traits(self):
        ticket = Ticket()
        tickets = list()
        for i in range(0, 9):
            tickets.append(ticket)

        traits = QueryHelper.split_tickets_traits(tickets)
        assert len(traits) == 2
        assert len(traits[0]) == 5
        assert len(traits[1]) == 4
示例#9
0
 def get_ticket_by_ticket_id(self, ticket_id: int) -> Ticket:
     stmt = select(['*']).where(and_(self.table.c.id == ticket_id))
     row = self.execute(stmt).fetchone()
     if row:
         ticket = dict()
         for key, value in dict(zip([col.key for col in self.table.columns], row)).items():
             if 'wish' in key and isinstance(value, str):
                 value = json.loads(value)
             ticket[key] = value
         return Ticket().to_obj(ticket)
     return None
 def test_ticket_check_wishlist(self):
     sample_ticket = dict(
         category=2,
         date=503,
         price_id=1,
         quantity=2,
         section='Yellow',
         row='32',
         seat='59',
         wish_dates=[],
         wish_price_ids=[],
         wish_quantities=[],
         source_id=1,
         remarks='',
         status=1,
         username=USERNAME,
         user_id=USER_ID
     )
     ticket = Ticket(USER_ID, USERNAME).to_obj(sample_ticket)
     assert ticket.validate_wishlist()['status'] is False
     assert ticket.validate_wishlist()['info'] == '希望交換的日期未填喔\n希望交換的價格種類未填喔\n希望交換的數量未填喔'
示例#11
0
 def get_tickets_by_conditions(self, conditions: dict) -> list:
     stmt = text(SQL.SEARCH_BY_CONDITIONS.format(self._trim_where_stmt(conditions)))
     cursor = self.execute(stmt)
     tickets = list()
     for row in cursor.fetchall():
         ticket = dict()
         for key, value in dict(zip(SQL.SEARCH_BY_CONDITIONS_KEYS, row)).items():
             if 'wish' in key and isinstance(value, str):
                 value = json.loads(value)
             ticket[key] = value
         tickets.append(Ticket().to_obj(ticket))
     return tickets
示例#12
0
 def get_tickets_by_user_id(self, user_id: int) -> list:
     stmt = select(['*']).where(and_(self.table.c.user_id == user_id)).order_by(desc(self.table.c.updated_at))
     cursor = self.execute(stmt)
     tickets = list()
     for row in cursor.fetchall():
         ticket = dict()
         for key, value in dict(zip([col.key for col in self.table.columns], row)).items():
             if 'wish' in key and isinstance(value, str):
                 value = json.loads(value)
             ticket[key] = value
         tickets.append(Ticket().to_obj(ticket))
     return tickets
示例#13
0
    def test_validator_missing_quantity(self):
        ticket = Ticket(username=USERNAME, user_id=USER_ID)
        ticket.category = 1
        ticket.date = 508
        ticket.price_id = 1
        ticket.status = 1
        result = ticket.validate()
        assert result['status'] is False
        assert result['info'] == '數量未填喔'

        validator = ItemValidator(ticket.to_dict())
        result = validator.check_ticket()
        assert result['status'] is False
        assert result['info'] == '數量未填喔'
    def test_ticket_fill_full_wishlist(self):
        sample_ticket = dict(
            category=2,
            date=503,
            price_id=1,
            quantity=2,
            section='Yellow',
            row='32',
            seat='59',
            wish_dates=[],
            wish_price_ids=[],
            wish_quantities=[],
            source_id=1,
            remarks='',
            status=1,
            username=USERNAME,
            user_id=USER_ID
        )
        ticket = Ticket(USER_ID, USERNAME).to_obj(sample_ticket)
        ticket.fill_full_wishlist()

        assert ticket.wish_dates == [503, 504, 505, 510, 511, 512]
        assert ticket.wish_price_ids == [1, 2, 3, 4, 5, 6, 7]
        assert ticket.wish_quantities == [1, 2, 3, 4]
    def test_drafting_ticket(self):
        ticket = Ticket(user_id=USER_ID, username=USERNAME)
        ticket.update_field('date', 503)
        self.helper.save_drafting_ticket(user_id=USER_ID, ticket=ticket)
        ticket_in_cache = self.helper.load_drafting_ticket(user_id=USER_ID)
        assert ticket_in_cache.user_id
        assert ticket_in_cache.user_id == ticket.user_id
        assert ticket_in_cache.username
        assert ticket_in_cache.username == ticket.username
        assert ticket_in_cache.to_dict() == ticket.to_dict()

        # Reset Ticket
        raw_ticket = Ticket(user_id=USER_ID, username=USERNAME)
        ticket_in_cache = self.helper.reset_drafting_ticket(user_id=USER_ID,
                                                            username=USERNAME)
        assert ticket_in_cache.to_dict() == raw_ticket.to_dict()
        assert ticket_in_cache.username == raw_ticket.username
    def test_get_tickets_by_user_id(self):
        results = self.db.get_tickets_by_user_id(user_id=8081)

        assert results
        result = results[0]
        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
示例#17
0
 def test_create_ticket(self):
     helper = TicketHelper(self.db)
     dumpy_ticket = dict(category=1,
                         date=504,
                         price_id=1,
                         quantity=1,
                         section='A1',
                         row='',
                         seat='',
                         wish_dates=list(),
                         wish_price_ids=list(),
                         wish_quantities=list(),
                         source=1,
                         remarks='',
                         status=1,
                         username=__name__,
                         user_id=USER_ID)
     ticket = Ticket(user_id=USER_ID,
                     username=__name__).to_obj(dumpy_ticket)
     assert helper.save_ticket(ticket)
     tickets_in_db = self.db.get_tickets_by_user_id(USER_ID)
     assert tickets_in_db
     ticket_in_db = tickets_in_db[0]
     assert ticket_in_db.section == ticket.section
    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
import pytest
import sqlalchemy
from sqlalchemy import MetaData

from mayday.db.tables.tickets import TicketsModel
from mayday.objects.ticket import Ticket

SAMPLES = [
    Ticket().to_obj(
        dict(id=1,
             category=2,
             date=512,
             price_id=4,
             quantity=2,
             section='',
             row='',
             status=1,
             source=1,
             remarks='test',
             wish_dates=[503, 504, 505, 510, 511, 512],
             wish_price_ids=[1, 2, 3, 4, 5],
             wish_quantities=[1, 2],
             user_id=123456789,
             username='******'))
]


@pytest.mark.usefixtures()
class TestCase(unittest.TestCase):
    @pytest.fixture(autouse=True, scope='function')
    def before_all(self):
        engine = sqlalchemy.create_engine('sqlite://')
    def test_ticket_update_field(self):
        ticket = Ticket(USER_ID, USERNAME)

        ticket.update_field('category', 1)
        assert isinstance(ticket.category, int)
        assert ticket.category == 1
        assert ticket.username == USERNAME

        ticket.update_field('category', 0)
        assert isinstance(ticket.category, int)
        assert ticket.category == 0
        assert ticket.username == USERNAME

        ticket.update_field('id', 1)
        assert isinstance(ticket.id, int)
        assert ticket.id == 1
        assert ticket.username == USERNAME

        ticket.update_field('date', 1)
        assert isinstance(ticket.date, int)
        assert ticket.date == 1
        assert ticket.username == USERNAME

        ticket.update_field('price_id', 1)
        assert isinstance(ticket.price_id, int)
        assert ticket.price_id == 1
        assert ticket.username == USERNAME

        ticket.update_field('quantity', 1)
        assert isinstance(ticket.quantity, int)
        assert ticket.quantity == 1
        assert ticket.username == USERNAME

        ticket.update_field('status', 1)
        assert isinstance(ticket.status, int)
        assert ticket.status == 1
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 503)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == [503]
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 504)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == [503, 504]
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 505)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == [503, 504, 505]
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 510)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == [503, 504, 505, 510]
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 511)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == [503, 504, 505, 510, 511]
        assert ticket.username == USERNAME

        ticket.update_field('wish_price_ids', 1)
        assert isinstance(ticket.wish_price_ids, list)
        assert ticket.wish_price_ids == [1]
        assert ticket.username == USERNAME

        ticket.update_field('wish_price_ids', 2)
        assert isinstance(ticket.wish_price_ids, list)
        assert ticket.wish_price_ids == [1, 2]
        assert ticket.username == USERNAME

        ticket.update_field('wish_price_ids', 3)
        assert isinstance(ticket.wish_price_ids, list)
        assert ticket.wish_price_ids == [1, 2, 3]
        assert ticket.username == USERNAME

        ticket.update_field('wish_price_ids', 4)
        assert isinstance(ticket.wish_price_ids, list)
        assert ticket.wish_price_ids == [1, 2, 3, 4]
        assert ticket.username == USERNAME

        ticket.update_field('wish_quantities', 1)
        assert isinstance(ticket.wish_quantities, list)
        assert ticket.wish_quantities == [1]
        assert ticket.username == USERNAME

        ticket.update_field('wish_quantities', 2)
        assert isinstance(ticket.wish_quantities, list)
        assert ticket.wish_quantities == [1, 2]
        assert ticket.username == USERNAME

        ticket.update_field('wish_quantities', 3)
        assert isinstance(ticket.wish_quantities, list)
        assert ticket.wish_quantities == [1, 2, 3]
        assert ticket.username == USERNAME

        ticket.update_field('wish_quantities', 4)
        assert isinstance(ticket.wish_quantities, list)
        assert ticket.wish_quantities == [1, 2, 3, 4]
        assert ticket.username == USERNAME

        ticket.update_field('status', 0)
        assert isinstance(ticket.status, int)
        assert ticket.status == 0
        assert ticket.username == USERNAME

        ticket.update_field('status', 1)
        assert isinstance(ticket.status, int)
        assert ticket.status == 1
        assert ticket.username == USERNAME

        ticket.update_field('source_id', 1)
        assert isinstance(ticket.status, int)
        assert ticket.status == 1
        assert ticket.username == USERNAME

        # Remove
        ticket.update_field('wish_quantities', 4, remove=True)
        assert isinstance(ticket.wish_quantities, list)
        assert ticket.wish_quantities == [1, 2, 3]
        assert ticket.username == USERNAME

        ticket.update_field('wish_quantities', 3, remove=True)
        assert isinstance(ticket.wish_quantities, list)
        assert ticket.wish_quantities == [1, 2]
        assert ticket.username == USERNAME

        ticket.update_field('wish_quantities', 2, remove=True)
        assert isinstance(ticket.wish_quantities, list)
        assert ticket.wish_quantities == [1]
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 511, remove=True)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == [503, 504, 505, 510]
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 510, remove=True)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == [503, 504, 505]
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 505, remove=True)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == [503, 504]
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 504, remove=True)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == [503]
        assert ticket.username == USERNAME

        ticket.update_field('wish_dates', 503, remove=True)
        assert isinstance(ticket.wish_dates, list)
        assert ticket.wish_dates == list()
        assert ticket.username == USERNAME

        ticket.update_field('wish_price_ids', 4, remove=True)
        assert isinstance(ticket.wish_price_ids, list)
        assert ticket.wish_price_ids == [1, 2, 3]
        assert ticket.username == USERNAME

        ticket.update_field('wish_price_ids', 3, remove=True)
        assert isinstance(ticket.wish_price_ids, list)
        assert ticket.wish_price_ids == [1, 2]
        assert ticket.username == USERNAME

        ticket.update_field('wish_price_ids', 2, remove=True)
        assert isinstance(ticket.wish_price_ids, list)
        assert ticket.wish_price_ids == [1]
        assert ticket.username == USERNAME

        ticket.update_field('wish_price_ids', 1, remove=True)
        assert isinstance(ticket.wish_price_ids, list)
        assert ticket.wish_price_ids == list()
        assert ticket.username == USERNAME
示例#21
0
 def create_ticket(self, ticket: Ticket):
     ticket = ticket.to_dict()
     del ticket['id']
     return bool(self.raw_insert(ticket).rowcount)
示例#22
0
 def update_ticket(self, ticket: Ticket) -> bool:
     return bool(self.raw_update(where=and_(self.table.c.id == ticket.id), row=ticket.to_dict()).rowcount)
示例#23
0
 def load_drafting_ticket(self, user_id: int) -> Ticket:
     result = self.redis.load(user_id=user_id, action='ticket')
     logger.info(result)
     event_logger.info(result)
     return Ticket(user_id=user_id).to_obj(result)
示例#24
0
 def save_drafting_ticket(self, user_id: int, ticket: Ticket) -> bool:
     logger.info(ticket.to_str())
     event_logger.info(ticket.to_str())
     return self.redis.save(user_id=user_id,
                            action='ticket',
                            content=ticket.to_dict())