Skip to content

Commit

Permalink
Refactored the api, middleware and tests for shows to use the Show da…
Browse files Browse the repository at this point in the history
…rt models.
  • Loading branch information
Iiro Krankka committed Apr 6, 2018
1 parent 397e638 commit 04667f8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
21 changes: 10 additions & 11 deletions lib/data/models/show.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import 'package:meta/meta.dart';
import 'package:xml/xml.dart' as xml;
import 'package:inkino/utils/xml_utils.dart';
import 'package:xml/xml.dart' as xml;

class Show {
Show({
@required this.id,
@required this.eventId,
@required this.title,
@required this.originalTitle,
@required this.url,
@required this.presentationMethod,
@required this.theaterAndAuditorium,
@required this.start,
@required this.end,
this.id,
this.eventId,
this.title,
this.originalTitle,
this.url,
this.presentationMethod,
this.theaterAndAuditorium,
this.start,
this.end,
});

final String id;
Expand Down
8 changes: 5 additions & 3 deletions lib/data/networking/finnkino_api.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:inkino/data/models/show.dart';
import 'package:inkino/data/models/theater.dart';
import 'package:inkino/data/networking/http_utils.dart';
import 'package:intl/intl.dart';
Expand All @@ -12,15 +13,16 @@ class FinnkinoApi {
static final Uri kEventsBaseUrl =
new Uri.https('www.finnkino.fi', '/en/xml/Events');

Future<String> getSchedule(Theater theater, DateTime date) async {
Future<List<Show>> getSchedule(Theater theater, DateTime date) async {
var dt = ddMMyyyy.format(date ?? new DateTime.now());

return getRequest(
var response = await getRequest(
kScheduleBaseUrl.replace(queryParameters: {
'area': theater.id,
'dt': dt,
}),
);

return Show.parseAll(response);
}

Future<String> getNowInTheatersEvents(Theater theater) async {
Expand Down
3 changes: 1 addition & 2 deletions lib/redux/show/show_middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class ShowMiddleware extends MiddlewareClass<AppState> {
DateTime currentDate,
NextDispatcher next,
) async {
var showsXml = await api.getSchedule(newTheater, currentDate);
var shows = Show.parseAll(showsXml);
var shows = await api.getSchedule(newTheater, currentDate);
var now = Clock.getCurrentTime();

// Return only show times that haven't started yet.
Expand Down
27 changes: 15 additions & 12 deletions test/redux/show/show_middleware_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:io';

import 'package:inkino/data/models/show.dart';
import 'package:inkino/data/models/theater.dart';
import 'package:inkino/redux/app/app_state.dart';
import 'package:inkino/redux/common_actions.dart';
Expand All @@ -15,7 +16,7 @@ import '../../mocks.dart';

void main() {
group('ShowMiddleware', () {
final DateTime fakeDateTime = new DateTime(2018);
final DateTime startOf2018 = new DateTime(2018);
final Theater theater = new Theater(id: 'abc123', name: 'Test Theater');
final List<dynamic> actionLog = <dynamic>[];
final Function(dynamic) next = (action) => actionLog.add(action);
Expand All @@ -32,18 +33,13 @@ void main() {
);
}

Future<String> _scheduleXml() =>
new File('test_assets/schedule.xml').readAsString();

setUp(() {
mockFinnkinoApi = new MockFinnkinoApi();
mockStore = new MockStore();
sut = new ShowMiddleware(mockFinnkinoApi);

// Given
when(mockStore.state).thenReturn(_theaterState(currentTheater: theater));
when(mockFinnkinoApi.getSchedule(theater, any))
.thenReturn(_scheduleXml());
});

tearDown(() {
Expand All @@ -57,7 +53,12 @@ void main() {
// The middleware filters shows based on if the showtime has already
// passed. As new DateTime(2018) will mean the very first hour and minute
// in January, all the show times in test assets will be after this date.
Clock.getCurrentTime = () => fakeDateTime;
Clock.getCurrentTime = () => startOf2018;
when(mockFinnkinoApi.getSchedule(theater, any)).thenReturn(<Show>[
new Show(start: new DateTime(2018, 02, 21)),
new Show(start: new DateTime(2018, 02, 21)),
new Show(start: new DateTime(2018, 03, 21)),
]);

// When
await sut.call(mockStore, new InitCompleteAction(null, theater), next);
Expand All @@ -79,21 +80,23 @@ void main() {
() async {
// Given
Clock.getCurrentTime = () => new DateTime(2018, 3);
when(mockFinnkinoApi.getSchedule(theater, any)).thenReturn(<Show>[
new Show(start: new DateTime(2018, 02, 21)),
new Show(start: new DateTime(2018, 02, 21)),
new Show(start: new DateTime(2018, 03, 21)),
]);

// When
await sut.call(
mockStore, new ChangeCurrentDateAction(fakeDateTime), next);
mockStore, new ChangeCurrentDateAction(startOf2018), next);

// Then
verify(mockFinnkinoApi.getSchedule(theater, fakeDateTime));
verify(mockFinnkinoApi.getSchedule(theater, startOf2018));

expect(actionLog.length, 3);
expect(actionLog[0], new isInstanceOf<ChangeCurrentDateAction>());
expect(actionLog[1], new isInstanceOf<RequestingShowsAction>());

// As there's only show in test_assets/schedule.xml that has a start
// time in 3rd month (or May) of 2018, the ReceivedShowsAction should
// be dispatched with only that show.
final ReceivedShowsAction receivedShowsAction = actionLog[2];
expect(receivedShowsAction.shows.length, 1);
},
Expand Down

0 comments on commit 04667f8

Please sign in to comment.