From 81446c51d4d14dce8ec4bf241513f91f10b6c46e Mon Sep 17 00:00:00 2001 From: jsmarr Date: Wed, 12 May 2021 14:58:20 -0700 Subject: [PATCH 1/3] Throw if timezone database isn't initialized --- lib/src/location_database.dart | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/src/location_database.dart b/lib/src/location_database.dart index 812564e..d3c0151 100644 --- a/lib/src/location_database.dart +++ b/lib/src/location_database.dart @@ -28,6 +28,13 @@ class LocationDatabase { /// Finds [Location] by its name. Location get(String name) { + if (!isInitialized) { + // Before you can get a location, you need to manually initialize the + // timezone location database by calling initializeDatabase or similar. + throw LocationNotFoundException( + 'Tried to get location before initializing timezone database'); + } + final loc = _locations[name]; if (loc == null) { throw LocationNotFoundException( From e9299795ccd0309d5df71630ea2c8295102dfe6b Mon Sep 17 00:00:00 2001 From: jsmarr Date: Wed, 12 May 2021 14:59:21 -0700 Subject: [PATCH 2/3] Test for initialization exception --- test/datetime_test_no_database.dart | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/datetime_test_no_database.dart b/test/datetime_test_no_database.dart index a2bb837..4323083 100644 --- a/test/datetime_test_no_database.dart +++ b/test/datetime_test_no_database.dart @@ -1,11 +1,21 @@ @TestOn('vm') import 'package:test/test.dart'; import 'package:timezone/standalone.dart'; +import 'package:timezone/timezone.dart'; void main() { group('Without initializing timezone database', () { test('Can still construct TZDateTime.utc', () { TZDateTime.utc(2019); }); + + test( + 'getLocation throws $LocationNotFoundException with message about the database not being initialized', + () { + expect( + () => getLocation('America/New_York'), + throwsA(TypeMatcher() + .having((e) => e.msg, 'msg', contains('database')))); + }); }); } From e3363e9d1afa7c5f5d18c66bedb1a7d2905a5db0 Mon Sep 17 00:00:00 2001 From: jsmarr Date: Wed, 12 May 2021 15:00:31 -0700 Subject: [PATCH 3/3] Test for unrecognized timezone --- test/datetime_test.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/datetime_test.dart b/test/datetime_test.dart index 032b29d..d705282 100644 --- a/test/datetime_test.dart +++ b/test/datetime_test.dart @@ -154,5 +154,14 @@ Future main() async { }); }); }); + }); + + group('Timezones', () { + test( + 'getLocation throws $LocationNotFoundException for unrecognized timezone', + () { + expect(() => getLocation('non-existent-location'), + throwsA(TypeMatcher())); + }); }); }