Пример #1
0
    async def test_book_seat(self, train, coach):
        seat = '19'
        firstname = 'Name'
        lastname = 'Surname'
        data = dict(
            code_station_from=train.source_station.id,
            code_station_to=train.destination_station.id,
            train=train.num,
            date=train.departure_time.timestamp,
            round_trip=0)
        place = dict(
            ord=0,
            coach_num=coach.num,
            coach_class=coach.klass,
            coach_type_id=coach.type_id,
            place_num=seat,
            firstname=firstname,
            lastname=lastname,
            bedding=0,
            child='',
            stud='',
            transp=0,
            reserve=0)
        for key, value in place.items():
            data['places[0][{}]'.format(key)] = value

        expected = response = {'error': False}

        uz = get_uz_client(http_response(response))
        result = await uz.book_seat(train, coach, seat, firstname, lastname)

        assert result == expected
        self.assert_request_call(uz, 'cart/add/', data=data)
Пример #2
0
    async def test_book_seat(self, train, coach):
        seat = '19'
        firstname = 'Name'
        lastname = 'Surname'
        data = dict(
            code_station_from=train.source_station.id,
            code_station_to=train.destination_station.id,
            train=train.num,
            date=train.departure_time.timestamp,
            round_trip=0)
        place = dict(
            ord=0,
            coach_num=coach.num,
            coach_class=coach.klass,
            coach_type_id=coach.type_id,
            place_num=seat,
            firstname=firstname,
            lastname=lastname,
            bedding=0,
            child='',
            stud='',
            transp=0,
            reserve=0)
        for key, value in place.items():
            data['places[0][{}]'.format(key)] = value

        expected = response = {'error': False}

        uz = get_uz_client(http_response(response))
        result = await uz.book_seat(train, coach, seat, firstname, lastname)

        assert result == expected
        self.assert_request_call(uz, 'cart/add/', data=data)
Пример #3
0
    async def test_call_raise(self, status, body, ex):
        endpoint = 'i/am/endpoint'

        uz = get_uz_client(http_response(body, status))
        with pytest.raises(ex):
            await uz.call(endpoint)

        uz.session.request.assert_called_once_with(
            'POST', self.uri(endpoint), headers=self.get_headers())
Пример #4
0
    async def test_call_raise(self, status, body, ex):
        endpoint = 'i/am/endpoint'

        uz = get_uz_client(http_response(body, status))
        with pytest.raises(ex):
            await uz.call(endpoint)

        uz.session.request.assert_called_once_with(
            'POST', self.uri(endpoint), headers=self.get_headers())
Пример #5
0
    async def test_fetch_train(self, train_num, is_found, train_raw, source_station,
                               destination_station):
        date = datetime(2016, 7, 1)
        response = {'value': [train_raw]}
        expected = model.Train.from_dict(train_raw) if is_found else None

        uz = get_uz_client(http_response(response))
        result = await uz.fetch_train(date, source_station, destination_station, train_num)

        assert result == expected
Пример #6
0
    async def test_fetch_train(self, train_num, is_found, train_raw, source_station,
                               destination_station):
        date = datetime(2016, 7, 1)
        response = {'value': [train_raw]}
        expected = model.Train.from_dict(train_raw) if is_found else None

        uz = get_uz_client(http_response(response))
        result = await uz.fetch_train(date, source_station, destination_station, train_num)

        assert result == expected
Пример #7
0
    async def test_search_stations(self, station_raw):
        name = 'lviv'
        endpoint = 'purchase/station/{}/'.format(name)
        response = {'value': [station_raw, station_raw]}
        expected = [model.Station.from_dict(station_raw) for _ in range(2)]

        uz = get_uz_client(http_response(response))
        result = await uz.search_stations(name)

        assert result == expected
        self.assert_request_call(uz, endpoint)
Пример #8
0
    async def test_search_stations(self, station_raw):
        name = 'lviv'
        endpoint = 'purchase/station/{}/'.format(name)
        response = {'value': [station_raw, station_raw]}
        expected = [model.Station.from_dict(station_raw) for _ in range(2)]

        uz = get_uz_client(http_response(response))
        result = await uz.search_stations(name)

        assert result == expected
        self.assert_request_call(uz, endpoint)
Пример #9
0
    async def test_call_ok(self, is_raw):
        body = {'hello': 'world'}
        expected = str(body).encode('utf-8') if is_raw else body
        endpoint = 'i/am/endpoint/ok'

        uz = get_uz_client(http_response(body))
        result = await uz.call(endpoint, method='GET', raw=is_raw)

        assert result == expected
        uz.session.request.assert_called_once_with(
            'GET', self.uri(endpoint), headers=self.get_headers())
Пример #10
0
    async def test_call_ok(self, is_raw):
        body = {'hello': 'world'}
        expected = str(body).encode('utf-8') if is_raw else body
        endpoint = 'i/am/endpoint/ok'

        uz = get_uz_client(http_response(body))
        result = await uz.call(endpoint, method='GET', raw=is_raw)

        assert result == expected
        uz.session.request.assert_called_once_with(
            'GET', self.uri(endpoint), headers=self.get_headers())
Пример #11
0
    async def test_get_token(self, index_page):
        expected = '33107f87dadad37307f93da538b73138'

        uz = get_uz_client(http_response(index_page))
        uz._token_date = 0  # reset mocked value from previous call

        result = await uz.get_token()

        assert result == expected
        assert uz._token == expected
        assert uz._token_date

        uz.session.request.assert_called_once_with('POST', self.uri(''), headers=None)
async def test_deserializer(station_raw, another_station_raw):
    uz = get_uz_client()
    uz.session.request.side_effect = (http_response({'value': [station_raw]}),
                                      http_response({'value': [another_station_raw]}))
    date = datetime(2016, 10, 21)

    result = await serializer.Deserializer(uz).load(dict(
        date=date.strftime('%Y-%m-%d'),
        source=station_raw['title'],
        destination=another_station_raw['title']))
    assert result == (date, Station.from_dict(station_raw), Station.from_dict(another_station_raw))
    calls = [mock.call('POST', uz.uri('purchase/station/{}/'.format(i['title'])), headers=mock.ANY)
             for i in (station_raw, another_station_raw)]
    assert uz.session.request.call_args_list == calls
Пример #13
0
    async def test_get_token(self, index_page):
        expected = '33107f87dadad37307f93da538b73138'

        uz = get_uz_client(http_response(index_page))
        uz._token_date = 0  # reset mocked value from previous call

        result = await uz.get_token()

        assert result == expected
        assert uz._token == expected
        assert uz._token_date

        uz.session.cookies.clear.assert_called_once_with()
        uz.session.request.assert_called_once_with(
            'POST', self.uri(''), headers={'User-Agent': 'user_agent'})
Пример #14
0
    async def test_fetch_first_station(self, is_found, station_raw):
        name = 'lviv'
        endpoint = 'purchase/station/{}/'.format(name)

        if is_found:
            response = {'value': [station_raw]}
            expected = model.Station.from_dict(station_raw)
        else:
            response = {'value': []}
            expected = None

        uz = get_uz_client(http_response(response))
        result = await uz.fetch_first_station(name)

        assert result == expected
        self.assert_request_call(uz, endpoint)
Пример #15
0
    async def test_fetch_first_station(self, is_found, station_raw):
        name = 'lviv'
        endpoint = 'purchase/station/{}/'.format(name)

        if is_found:
            response = {'value': [station_raw]}
            expected = model.Station.from_dict(station_raw)
        else:
            response = {'value': []}
            expected = None

        uz = get_uz_client(http_response(response))
        result = await uz.fetch_first_station(name)

        assert result == expected
        self.assert_request_call(uz, endpoint)
Пример #16
0
    async def test_list_seats(self, train, coach, seats_raw):
        data = dict(
            station_id_from=train.source_station.id,
            station_id_till=train.destination_station.id,
            train=train.num,
            coach_num=coach.num,
            coach_class=coach.klass,
            coach_type_id=coach.type_id,
            date_dep=train.departure_time.timestamp
        )
        response = {'value': seats_raw}
        expected = {'6', '9', '10', '14', '16', '18'}

        uz = get_uz_client(http_response(response))
        result = await uz.list_seats(train, coach)

        assert result == expected
        self.assert_request_call(uz, 'purchase/coach/', data=data)
Пример #17
0
    async def test_list_seats(self, train, coach, seats_raw):
        data = dict(
            station_id_from=train.source_station.id,
            station_id_till=train.destination_station.id,
            train=train.num,
            coach_num=coach.num,
            coach_class=coach.klass,
            coach_type_id=coach.type_id,
            date_dep=train.departure_time.timestamp
        )
        response = {'value': seats_raw}
        expected = {'6', '9', '10', '14', '16', '18'}

        uz = get_uz_client(http_response(response))
        result = await uz.list_seats(train, coach)

        assert result == expected
        self.assert_request_call(uz, 'purchase/coach/', data=data)
Пример #18
0
    async def test_list_coaches(self, train, coach_type, coach_raw):
        data = dict(
            station_id_from=train.source_station.id,
            station_id_till=train.destination_station.id,
            train=train.num,
            model=train.model,
            date_dep=train.departure_time.timestamp,
            round_trip=0,
            another_ec=0,
            coach_type=coach_type.letter
        )
        response = {'coaches': [coach_raw, coach_raw]}
        expected = [model.Coach.from_dict(coach_raw) for _ in range(2)]

        uz = get_uz_client(http_response(response))
        result = await uz.list_coaches(train, coach_type)

        assert result == expected
        self.assert_request_call(uz, 'purchase/coaches/', data=data)
Пример #19
0
    async def test_list_trains(self, source_station, destination_station, train_raw):
        date = datetime(2016, 7, 1)
        data = dict(
            station_id_from=source_station.id,
            station_id_till=destination_station.id,
            date_dep='07.01.2016',
            time_dep='00:00',
            time_dep_till='',
            another_ec=0,
            search='')

        response = {'value': [train_raw, train_raw]}
        expected = [model.Train.from_dict(train_raw) for _ in range(2)]

        uz = get_uz_client(http_response(response))
        result = await uz.list_trains(date, source_station, destination_station)

        assert result == expected
        self.assert_request_call(uz, 'purchase/search/', data=data)
Пример #20
0
    async def test_list_trains(self, source_station, destination_station, train_raw):
        date = datetime(2016, 7, 1)
        data = dict(
            station_id_from=source_station.id,
            station_id_till=destination_station.id,
            date_dep='07.01.2016',
            time_dep='00:00',
            time_dep_till='',
            another_ec=0,
            search='')

        response = {'value': [train_raw, train_raw]}
        expected = [model.Train.from_dict(train_raw) for _ in range(2)]

        uz = get_uz_client(http_response(response))
        result = await uz.list_trains(date, source_station, destination_station)

        assert result == expected
        self.assert_request_call(uz, 'purchase/search/', data=data)
Пример #21
0
    async def test_list_coaches(self, train, coach_type, coach_raw):
        data = dict(
            station_id_from=train.source_station.id,
            station_id_till=train.destination_station.id,
            train=train.num,
            model=train.model,
            date_dep=train.departure_time.timestamp,
            round_trip=0,
            another_ec=0,
            coach_type=coach_type.letter
        )
        response = {'coaches': [coach_raw, coach_raw]}
        expected = [model.Coach.from_dict(coach_raw) for _ in range(2)]

        uz = get_uz_client(http_response(response))
        result = await uz.list_coaches(train, coach_type)

        assert result == expected
        self.assert_request_call(uz, 'purchase/coaches/', data=data)
Пример #22
0
async def test_deserializer(station_raw, another_station_raw):
    uz = get_uz_client()
    uz.session.request.side_effect = (http_response(
        {'value':
         [station_raw]}), http_response({'value': [another_station_raw]}))
    date = datetime(2016, 10, 21)

    result = await serializer.Deserializer(uz).load(
        dict(date=date.strftime('%Y-%m-%d'),
             source=station_raw['title'],
             destination=another_station_raw['title']))
    assert result == (date, Station.from_dict(station_raw),
                      Station.from_dict(another_station_raw))
    calls = [
        mock.call('POST',
                  uz.uri('purchase/station/{}/'.format(i['title'])),
                  headers=mock.ANY) for i in (station_raw, another_station_raw)
    ]
    assert uz.session.request.call_args_list == calls
async def test_deserializer_bad_date():
    uz = get_uz_client()
    with pytest.raises(serializer.SerializerException):
        await serializer.Deserializer(uz).load(dict(date='bad format'))
async def test_deserializer_empty():
    uz = get_uz_client()
    result = await serializer.Deserializer(uz).load(dict())
    assert result == (None, None, None)
Пример #25
0
    async def test_get_token_fail(self):
        uz = get_uz_client(http_response(''))
        uz._token_date = 0  # reset mocked value from previous call

        with pytest.raises(client.FailedObtainToken):
            await uz.get_token()
Пример #26
0
async def test_deserializer_bad_date():
    uz = get_uz_client()
    with pytest.raises(serializer.SerializerException):
        await serializer.Deserializer(uz).load(dict(date='bad format'))
Пример #27
0
async def test_deserializer_empty():
    uz = get_uz_client()
    result = await serializer.Deserializer(uz).load(dict())
    assert result == (None, None, None)
Пример #28
0
    async def test_get_token_fail(self):
        uz = get_uz_client(http_response(''))
        uz._token_date = 0  # reset mocked value from previous call

        with pytest.raises(client.FailedObtainToken):
            await uz.get_token()