Skip to content

Commit

Permalink
Improve speed of tests + add test with a discount card
Browse files Browse the repository at this point in the history
  • Loading branch information
tducret committed Jul 6, 2019
1 parent 99f6f52 commit cabca74
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 110 deletions.
2 changes: 1 addition & 1 deletion pytest.ini
@@ -1,2 +1,2 @@
[pytest]
addopts = --doctest-modules --cov trainline --ignore build_station_index.py
addopts = --doctest-modules -vs --cov trainline --ignore build_station_index.py
156 changes: 47 additions & 109 deletions tests/test_trainline.py
Expand Up @@ -70,6 +70,11 @@
_TOMORROW = tommorow_obj.strftime("%d/%m/%Y")


def test_class_Trainline():
t = Trainline()
assert t is not None


def test_class_ComfortClass():
cc = ComfortClass(mydict=_DEFAULT_COMFORT_CLASS_DICT)
assert cc.id == "ae9ba138a7c211e88f35afa2c1b6c287"
Expand Down Expand Up @@ -147,22 +152,24 @@ def test_class_Passenger():
"label": id_p1
}

p2 = Passenger(birthdate="01/01/2006",
cards=[trainline.JEUNE, trainline.WEEK_END])
p2 = Passenger(
birthdate="01/01/2006",
cards=[trainline.AVANTAGE_JEUNE, trainline.AVANTAGE_WEEK_END]
)
print(p2)
assert p2.birthdate == "01/01/2006"
assert p2.cards == [trainline.JEUNE, trainline.WEEK_END]
assert p2.cards == [trainline.AVANTAGE_JEUNE, trainline.AVANTAGE_WEEK_END]
age_p2 = date.today().year - 2018 + 12
assert p2.age == age_p2
id_p2 = p2.id
assert len(id_p2) == 36
assert p2.get_dict() == {
"id": id_p2,
"age": age_p2,
"cards": [{"reference": trainline.JEUNE},
{"reference": trainline.WEEK_END}],
"label": id_p2
}
"id": id_p2,
"age": age_p2,
"cards": [{"reference": trainline.AVANTAGE_JEUNE},
{"reference": trainline.AVANTAGE_WEEK_END}],
"label": id_p2
}


def test_class_Passenger_errors():
Expand Down Expand Up @@ -219,49 +226,9 @@ def test_basic_search():
display_trips(results)


def test_basic_search_Carcassonne():
from_date = "{} 18:00".format(_TOMORROW)
to_date = "{} 23:00".format(_TOMORROW)
departure_station = "Toulouse Matabiau"
arrival_station = "Carcassonne"

results = trainline.search(
departure_station=departure_station,
arrival_station=arrival_station,
from_date=from_date,
to_date=to_date)
print()
print("Search trips for {} to {}, between {} and {}".format(
departure_station, arrival_station, from_date, to_date))
print("{} results".format(len(results)))
assert len(results) > 0

display_trips(results)


def test_basic_search_Paris():
from_date = "{} 08:00".format(_TOMORROW)
to_date = "{} 23:00".format(_TOMORROW)
departure_station = "Toulouse Matabiau"
arrival_station = "Paris"

results = trainline.search(
departure_station=departure_station,
arrival_station=arrival_station,
from_date=from_date,
to_date=to_date)
print()
print("Search trips for {} to {}, between {} and {}".format(
departure_station, arrival_station, from_date, to_date))
print("{} results".format(len(results)))
assert len(results) > 0

display_trips(results)


def test_search_only_bus():
from_date = "{} 09:00".format(_TOMORROW)
to_date = "{} 15:00".format(_TOMORROW)
to_date = "{} 11:00".format(_TOMORROW)
departure_station = "Toulouse Matabiau"
arrival_station = "Bordeaux St-Jean"

Expand All @@ -287,7 +254,7 @@ def test_search_only_bus():

def test_basic_search_with_bicyle():
from_date = "{} 08:00".format(_TOMORROW)
to_date = "{} 23:00".format(_TOMORROW)
to_date = "{} 12:00".format(_TOMORROW)
departure_station = "Toulouse Matabiau"
arrival_station = "Narbonne"

Expand All @@ -308,7 +275,7 @@ def test_basic_search_with_bicyle():

def test_basic_search_with_bicyle_without_reservation():
from_date = "{} 08:00".format(_TOMORROW)
to_date = "{} 23:00".format(_TOMORROW)
to_date = "{} 12:00".format(_TOMORROW)
departure_station = "Toulouse Matabiau"
arrival_station = "Carcassonne"

Expand All @@ -329,10 +296,10 @@ def test_basic_search_with_bicyle_without_reservation():


def test_basic_search_with_bicyle_with_reservation():
from_date = "{} 08:00".format(_TOMORROW)
to_date = "{} 23:00".format(_TOMORROW)
from_date = "{} 20:00".format(_TOMORROW)
to_date = "{} 21:00".format(_TOMORROW)
departure_station = "Toulouse Matabiau"
arrival_station = "Bordeaux St-Jean"
arrival_station = "Bordeaux"

results = trainline.search(
departure_station=departure_station,
Expand All @@ -348,56 +315,6 @@ def test_basic_search_with_bicyle_with_reservation():

display_trips(results)


def display_trips(folder_list):
print(folder_list.csv())
# for folder in folder_list:
# print()
# print("-----------------------")
# print(folder)
# for trip in folder.trips:
# print('\t', end='')
# print(trip)
# for segment in trip.segments:
# print('\t\t', end='')
# print(segment)
# for comfort_class in segment.comfort_classes:
# print('\t\t\t', end='')
# print(comfort_class)
# for extra in comfort_class.extras:
# print('\t\t\t\t', end='')
# print("{} : {} {}".format(
# extra.get("title"),
# float(extra.get("cents"))/100,
# extra.get("currency")))


def test_search_3_passengers_and_bicyles():
Pierre = Passenger(birthdate="01/01/1980")
Sophie = Passenger(birthdate="01/02/1981")
Enzo = Passenger(birthdate="01/03/2012", cards=[trainline.ENFANT_PLUS])

from_date = "{} 08:00".format(_TOMORROW)
to_date = "{} 19:00".format(_TOMORROW)
departure_station = "Toulouse Matabiau"
arrival_station = "Bordeaux St-Jean"

results = trainline.search(
passengers=[Pierre, Sophie, Enzo],
departure_station=departure_station,
arrival_station=arrival_station,
from_date=from_date,
to_date=to_date,
bicycle_with_or_without_reservation=True)

print()
print("Search trips for {} to {}, between {} and {}".format(
departure_station, arrival_station, from_date, to_date))
print("{} results".format(len(results)))
assert len(results) > 0

display_trips(results)

csv_header = results.csv().split("\n")[0]
assert csv_header == "departure_date;arrival_date;duration;\
number_of_segments;price;currency;transportation_mean;bicycle_reservation"
Expand All @@ -410,17 +327,16 @@ def test_search_3_passengers_and_bicyles():
assert _TOMORROW in last_result.split(";")[0]


def test_class_Trainline():
t = Trainline()
assert t is not None
def display_trips(folder_list):
print(folder_list.csv())


def test_with_benerail():
# Added this test to check that "benerail.default" comfort class
# is handled properly ("options" field is missing in this case)
# that was causing the issue #1
from_date = "{} 08:00".format(_TOMORROW)
to_date = "{} 23:00".format(_TOMORROW)
to_date = "{} 10:00".format(_TOMORROW)
departure_station = "Paris"
arrival_station = "Antwerpen-Centraal"

Expand All @@ -436,3 +352,25 @@ def test_with_benerail():
assert len(results) > 0

display_trips(results)

def test_basic_search_with_card():
from_date = "{} 10:00".format(_TOMORROW)
to_date = "{} 12:00".format(_TOMORROW)
departure_station = "Toulouse Matabiau"
arrival_station = "Bordeaux St-Jean"

p1 = Passenger(birthdate="01/01/1980", cards=[trainline.AVANTAGE_FAMILLE])

results = trainline.search(
departure_station=departure_station,
arrival_station=arrival_station,
from_date=from_date,
to_date=to_date
)
print()
print("Search trips for {} to {}, between {} and {}".format(
departure_station, arrival_station, from_date, to_date))
print("{} results".format(len(results)))
assert len(results) > 0

display_trips(results)

0 comments on commit cabca74

Please sign in to comment.