Skip to content

Commit

Permalink
Merge pull request #12 from tmconsulting/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
roquie authored May 16, 2018
2 parents fc4dfd0 + 1f936e6 commit f2e5297
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
7 changes: 6 additions & 1 deletion onelya_sdk/railway/reservation/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, category: RailwayPassengerCategory, order_customer_index: int
'category': self.category,
'order_customer_index': self.order_customer_index,
'preferred_adult_tariff_type': self.preferred_adult_tariff_type,
'railway_bonus_cards': self.railway_bonus_cards,
'railway_bonus_cards': [item.json_data for item in self.railway_bonus_cards] if self.railway_bonus_cards is not None else [],
'is_invalid': self.is_invalid
}

Expand All @@ -29,6 +29,11 @@ def __init__(self, card_number: str, car_type: RzhdCardTypes=None):
self.card_number = card_number
self.car_type = car_type

self.json_data = {
'card_number': self.card_number,
'car_type': self.car_type
}


class OrderFullCustomerRequest(object):
def __init__(self, document_number: str, document_type: DocumentType, first_name: str, last_name: str, sex: Sex,
Expand Down
15 changes: 13 additions & 2 deletions onelya_sdk/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
from datetime import datetime


def get_array(items, item_class):
if items is not None:
def get_array(items, item_class=str):
if type(items) is list:
return [item_class(item) for item in items]
return None


def get_array_from_str(items):
if items not in ['', 'Unknown', 'NoValue', None]:
if ', ' in items:
items = items.split(', ')
else:
items = items.split(',')
if all([item.isdigit() for item in items]):
return [int(item) for item in items]
return [str(item) for item in items]


def get_item(item, item_class):
if item is not None:
return item_class(item)
Expand Down
20 changes: 10 additions & 10 deletions onelya_sdk/wrapper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ..utils import get_array, get_item, get_datetime, get_datetime_array, get_bool_item
from ..utils import get_array, get_item, get_datetime, get_datetime_array, get_bool_item, get_array_from_str


class Discount(object):
Expand Down Expand Up @@ -115,7 +115,7 @@ def __init__(self, json_data):
class FreePlacesByCompartments(object):
def __init__(self, json_data):
self.compartment_number = json_data.get('CompartmentNumber')
self.places = get_array(json_data.get('Places'), int)
self.places = get_array_from_str(json_data.get('Places'))


class CarPriceInfo(object):
Expand All @@ -128,7 +128,7 @@ def __init__(self, json_data):
self.international_service_class = json_data.get('InternationalServiceClass')
self.car_description = json_data.get('CarDescription')
self.service_class_transcript = json_data.get('ServiceClassTranscript')
self.free_places = json_data.get('FreePlaces')
self.free_places = get_array_from_str(json_data.get('FreePlaces'))
self.place_quantity = get_item(json_data.get('PlaceQuantity'), int)
self.is_two_storey = json_data.get('IsTwoStorey')
self.services = json_data.get('Services')
Expand Down Expand Up @@ -342,9 +342,9 @@ def __init__(self, json_data):
class PassengerResponse(object):
def __init__(self, json_data):
self.category = json_data.get('Category')
self.places = get_array(json_data.get('Places'), int)
self.place_tiers = get_array(json_data.get('PlaceTiers'), str)
self.places_with_type = get_item(json_data.get('PlacesWithType'), PlaceWithType)
self.places = get_array_from_str(json_data.get('Places'))
self.place_tiers = get_array_from_str(json_data.get('PlaceTiers'))
self.places_with_type = get_array(json_data.get('PlacesWithType'), PlaceWithType)
self.tariff_type = json_data.get('TariffType')
self.first_name = json_data.get('FirstName')
self.middle_name = json_data.get('MiddleName')
Expand Down Expand Up @@ -382,9 +382,9 @@ def __init__(self, json_data):

class PrepaidMealInfo(object):
def __init__(self, json_data):
self.v = json_data.get('MealOptionCode')
self.v = json_data.get('MealName')
self.v = json_data.get('Description')
self.meal_option_code = json_data.get('MealOptionCode')
self.meal_name = json_data.get('MealName')
self.description = json_data.get('Description')

self.json_data = json_data

Expand Down Expand Up @@ -611,7 +611,7 @@ class RailwayOrderItemCustomerInfo(object):
def __init__(self, json_data):
self.type = json_data.get('$type')
self.order_item_blank_id = json_data.get('OrderItemBlankId')
self.places = json_data.get('Places')
self.places = get_array_from_str(json_data.get('Places'))
self.place_quantity = json_data.get('PlaceQuantity')
self.transit_document = json_data.get('TransitDocument')
self.category = json_data.get('Category')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

__version__ = '1.2.5'
__version__ = '1.3.0'

setup(
version=__version__,
Expand Down
12 changes: 10 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mock

from onelya_sdk.api import API
from onelya_sdk import utils
from onelya_sdk.exceptions import OnelyaAPIError
from onelya_sdk.railway import (OrderFullCustomerRequest, RailwayReservationRequest,
RailwayPassengerRequest, ServiceAddUpsaleRequest, ProductRequest, AdditionalMeal)
Expand Down Expand Up @@ -105,14 +106,14 @@ def railway_api(self):
def aeroexpress_api(self):
return API(self.username, self.password, self.pos)

def test_json_railway_train_pricing(self):
def test_railway_train_pricing(self):
train_pricing = self.railway_api.railway_search.train_pricing('Москва', '2004000', self.datetime, 12, 24, CarGrouping.GROUP)

input_data = json.loads(open('tests/data/Railway/Search/TrainPricing.in.json', 'r', encoding='utf8').read())
self.assertEquals(input_data, self.railway_api.last_request)
self.assert_json_with_class(train_pricing)

def test_json_railway_car_pricing(self):
def test_railway_car_pricing(self):
car_pricing = self.railway_api.railway_search.car_pricing('2000000', '2004000', self.datetime, '054Ч', None, PricingTariffType.FULL)

input_data = json.loads(open('tests/data/Railway/Search/CarPricing.in.json', 'r', encoding='utf8').read())
Expand Down Expand Up @@ -434,6 +435,13 @@ def test_empty_message_params(self):
error_data = {'Code': 1, 'Message': 'Message'}
self.assertTrue(OnelyaAPIError('Test/Test', error_data, {}).message_params is None)

def test_get_array(self):
self.assertEqual(['1L', '2P'], utils.get_array_from_str('1L, 2P'))
self.assertEqual(['1L', '2P'], utils.get_array_from_str('1L,2P'))

self.assertEqual([1, 2], utils.get_array_from_str('1, 2'))
self.assertEqual([1, 2], utils.get_array_from_str('1,2'))

def assert_json_with_class(self, wrapper):
for key in wrapper.json_data.keys():
var = wrapper.__getattribute__(self.get_var_name(key))
Expand Down

0 comments on commit f2e5297

Please sign in to comment.