From c5e32e4576ee11b7aeff76be8102d6775840b988 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 13 Jun 2020 14:50:50 +0200 Subject: Add tests for DB backend --- lib/Travel/Status/DE/HAFAS.pm | 17 ++-- t/20-db.t | 112 +++++++++++++++++++++++++++ t/30-invalid-xml.t | 21 +++++ "t/in/DB.Berlin Jannowitzbr\303\274cke.xml" | 1 + t/in/NAHSH.Flensburg.xml | 40 ++++++++++ t/in/NASA.Wernigerode Hbf.xml | 18 +++++ t/in/NVV.Kassel Hauptbahnhof.xml | 67 ++++++++++++++++ t/in/RSAG.Rostock Hbf.xml | 98 +++++++++++++++++++++++ "t/in/VBB.Berlin Jannowitzbr\303\274cke.xml" | 75 ++++++++++++++++++ t/in/VBN.Diepholz.xml | 8 ++ "t/in/\303\226BB.Wien Praterstern.xml" | 101 ++++++++++++++++++++++++ 11 files changed, 552 insertions(+), 6 deletions(-) create mode 100755 t/20-db.t create mode 100755 t/30-invalid-xml.t create mode 100644 "t/in/DB.Berlin Jannowitzbr\303\274cke.xml" create mode 100644 t/in/NAHSH.Flensburg.xml create mode 100644 t/in/NASA.Wernigerode Hbf.xml create mode 100644 t/in/NVV.Kassel Hauptbahnhof.xml create mode 100644 t/in/RSAG.Rostock Hbf.xml create mode 100644 "t/in/VBB.Berlin Jannowitzbr\303\274cke.xml" create mode 100644 t/in/VBN.Diepholz.xml create mode 100644 "t/in/\303\226BB.Wien Praterstern.xml" diff --git a/lib/Travel/Status/DE/HAFAS.pm b/lib/Travel/Status/DE/HAFAS.pm index 010b1f9..4a74692 100644 --- a/lib/Travel/Status/DE/HAFAS.pm +++ b/lib/Travel/Status/DE/HAFAS.pm @@ -145,14 +145,19 @@ sub new { my $url = ( $conf{url} // $hafas_instance{$service}{url} ) . "/${lang}n"; - $reply = $ua->post( $url, $ref->{post} ); - - if ( $reply->is_error ) { - $ref->{errstr} = $reply->status_line; - return $ref; + if ( $conf{xml} ) { + $ref->{raw_xml} = $conf{xml}; } + else { + $reply = $ua->post( $url, $ref->{post} ); + + if ( $reply->is_error ) { + $ref->{errstr} = $reply->status_line; + return $ref; + } - $ref->{raw_xml} = $reply->content; + $ref->{raw_xml} = $reply->content; + } # the interface often does not return valid XML (but it's close!) if ( substr( $ref->{raw_xml}, 0, 5 ) ne ' 46; + +use Travel::Status::DE::HAFAS; + +my $xml = read_file('t/in/DB.Berlin Jannowitzbrücke.xml'); + +my $status = Travel::Status::DE::HAFAS->new( + service => 'DB', + station => 'Berlin Jannowitzbrücke', + xml => $xml +); + +is( $status->errcode, undef, 'no error code' ); +is( $status->errstr, undef, 'no error string' ); + +is( + $status->get_active_service->{name}, + 'Deutsche Bahn', + 'active service name' +); + +is( scalar $status->results, 73, 'number of results' ); + +my @results = $status->results; + +# Result 0: S-Bahn + +is( $results[0]->date, '13.06.2020', 'result 0: date' ); +is( + $results[0]->datetime->strftime('%Y%m%d %H%M%S'), + '20200613 141700', + 'result 0: datetime' +); +is( $results[0]->delay, 2, 'result 0: delay' ); +is( $results[0]->info, undef, 'result 0: no info' ); +ok( !$results[0]->is_cancelled, 'result 0: not cancelled' ); +ok( !$results[0]->is_changed_platform, 'result 0: platform not changed' ); +is( scalar $results[0]->messages, 0, 'result 0: no messages' ); + +for my $res ( $results[0]->line, $results[0]->train ) { + is( $res, 'S 5', 'result 0: line/train' ); +} +for my $res ( $results[0]->line_no, $results[0]->train_no ) { + is( $res, 5, 'result 0: line/train number' ); +} + +is( $results[0]->operator, undef, 'result 0: no operator' ); +is( $results[0]->platform, '4', 'result 0: platform' ); + +for my $res ( $results[0]->route_end, $results[0]->destination, + $results[0]->origin ) +{ + is( $res, 'Berlin Westkreuz', 'result 0: route start/end' ); +} + +is( $results[0]->sched_date, '13.06.2020', 'result 0: sched_date' ); +is( + $results[0]->sched_datetime->strftime('%Y%m%d %H%M%S'), + '20200613 141500', + 'result 0: sched_datetime' +); +is( $results[0]->sched_time, '14:15', 'result 0: sched_time' ); +is( $results[0]->time, '14:17', 'result 0: time' ); +is( $results[0]->type, 'S', 'result 0: type' ); + +# Result 2: Bus + +is( $results[2]->date, '13.06.2020', 'result 2: date' ); +is( + $results[2]->datetime->strftime('%Y%m%d %H%M%S'), + '20200613 141700', + 'result 2: datetime' +); +is( $results[2]->delay, 0, 'result 2: delay' ); +is( $results[2]->info, undef, 'result 2: no info' ); +ok( !$results[2]->is_cancelled, 'result 2: not cancelled' ); +ok( !$results[2]->is_changed_platform, 'result 2: platform not changed' ); +is( scalar $results[2]->messages, 0, 'result 2: no messages' ); + +for my $res ( $results[2]->line, $results[2]->train ) { + is( $res, 'Bus 300', 'result 2: line/train' ); +} +for my $res ( $results[2]->line_no, $results[2]->train_no ) { + is( $res, 300, 'result 2: line/train number' ); +} + +is( $results[2]->operator, undef, 'result 2: no operator' ); +is( $results[2]->platform, undef, 'result 2: no platform' ); + +for my $res ( $results[2]->route_end, $results[2]->destination, + $results[2]->origin ) +{ + is( $res, 'Warschauer Str. (S+U), Berlin', 'result 2: route start/end' ); +} + +is( $results[2]->sched_date, '13.06.2020', 'result 2: sched_date' ); +is( + $results[2]->sched_datetime->strftime('%Y%m%d %H%M%S'), + '20200613 141700', + 'result 2: sched_datetime' +); +is( $results[2]->sched_time, '14:17', 'result 2: sched_time' ); +is( $results[2]->time, '14:17', 'result 2: time' ); +is( $results[2]->type, 'Bus', 'result 2: type' ); diff --git a/t/30-invalid-xml.t b/t/30-invalid-xml.t new file mode 100755 index 0000000..cb83a4c --- /dev/null +++ b/t/30-invalid-xml.t @@ -0,0 +1,21 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.020; + +use utf8; + +use File::Slurp qw(read_file); +use Test::More tests => 1; + +use Travel::Status::DE::HAFAS; + +my $xml = 'lol'; + +my $status = Travel::Status::DE::HAFAS->new( + service => 'DB', + station => 'Berlin Jannowitzbrücke', + xml => $xml +); + +is (scalar $status->results, 0, 'no results on invalid input'); diff --git "a/t/in/DB.Berlin Jannowitzbr\303\274cke.xml" "b/t/in/DB.Berlin Jannowitzbr\303\274cke.xml" new file mode 100644 index 0000000..b5d7ed7 --- /dev/null +++ "b/t/in/DB.Berlin Jannowitzbr\303\274cke.xml" @@ -0,0 +1 @@ + diff --git a/t/in/NAHSH.Flensburg.xml b/t/in/NAHSH.Flensburg.xml new file mode 100644 index 0000000..aeaf89b --- /dev/null +++ b/t/in/NAHSH.Flensburg.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/t/in/NASA.Wernigerode Hbf.xml b/t/in/NASA.Wernigerode Hbf.xml new file mode 100644 index 0000000..0803d15 --- /dev/null +++ b/t/in/NASA.Wernigerode Hbf.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/t/in/NVV.Kassel Hauptbahnhof.xml b/t/in/NVV.Kassel Hauptbahnhof.xml new file mode 100644 index 0000000..4c5a1d8 --- /dev/null +++ b/t/in/NVV.Kassel Hauptbahnhof.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/t/in/RSAG.Rostock Hbf.xml b/t/in/RSAG.Rostock Hbf.xml new file mode 100644 index 0000000..ae03be1 --- /dev/null +++ b/t/in/RSAG.Rostock Hbf.xml @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/t/in/VBB.Berlin Jannowitzbr\303\274cke.xml" "b/t/in/VBB.Berlin Jannowitzbr\303\274cke.xml" new file mode 100644 index 0000000..698d0a4 --- /dev/null +++ "b/t/in/VBB.Berlin Jannowitzbr\303\274cke.xml" @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/t/in/VBN.Diepholz.xml b/t/in/VBN.Diepholz.xml new file mode 100644 index 0000000..c431830 --- /dev/null +++ b/t/in/VBN.Diepholz.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git "a/t/in/\303\226BB.Wien Praterstern.xml" "b/t/in/\303\226BB.Wien Praterstern.xml" new file mode 100644 index 0000000..b9a455e --- /dev/null +++ "b/t/in/\303\226BB.Wien Praterstern.xml" @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3