Skip to content

Commit

Permalink
refactor(storage): migrate example and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pr-Mais committed Nov 22, 2021
1 parent 90bbe7b commit 8dad3a5
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 102 deletions.
Expand Up @@ -2,7 +2,7 @@ name: firebase_storage_example
description: Demonstrates how to use the firebase_storage plugin.

environment:
sdk: ">=2.12.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
firebase_core:
Expand All @@ -29,7 +29,7 @@ dependency_overrides:
path: ../../firebase_storage_web

dev_dependencies:
drive: 0.1.0
drive: ^1.0.0-1.0.nullsafety.1
flutter_driver:
sdk: flutter
test: any
Expand Down
@@ -1,12 +1,7 @@
// ignore_for_file: require_trailing_commas
// @dart = 2.9

// Copyright 2020, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'package:drive/drive.dart' as drive;
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_storage/firebase_storage.dart';
Expand All @@ -24,7 +19,8 @@ bool useEmulator = true;

void testsMain() {
setUpAll(() async {
await Firebase.initializeApp();
await Firebase.initializeApp(options: testDefaultOptions);

if (useEmulator) {
await FirebaseStorage.instance
.useStorageEmulator(testEmulatorHost, testEmulatorPort);
Expand Down
@@ -1,12 +1,7 @@
// ignore_for_file: require_trailing_commas
// @dart = 2.9

// Copyright 2020, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'package:drive/drive_driver.dart' as drive;

void main() => drive.main();
@@ -1,6 +1,3 @@
// ignore_for_file: require_trailing_commas
// @dart = 2.9

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -9,12 +6,12 @@ import 'test_utils.dart';

void runInstanceTests() {
group('$FirebaseStorage', () {
/*late*/ FirebaseStorage storage;
/*late*/ FirebaseApp secondaryApp;
/*late*/ FirebaseApp secondaryAppWithoutBucket;
late FirebaseStorage storage;
late FirebaseApp secondaryApp;
late FirebaseApp secondaryAppWithoutBucket;

setUpAll(() async {
await Firebase.initializeApp();
await Firebase.initializeApp(options: testDefaultOptions);
storage = FirebaseStorage.instance;
secondaryApp = await testInitializeSecondaryApp();
});
Expand Down Expand Up @@ -146,16 +143,20 @@ void runInstanceTests() {
});

test('throws an error if https url could not be parsed', () async {
try {
storage.refFromURL('https://invertase.io');
fail('Did not throw an Error.');
} catch (error) {
expect(
error.message,
expect(
() {
storage.refFromURL('https://invertase.io');
fail('Did not throw an Error.');
},
throwsA(
isA<AssertionError>().having(
(p0) => p0.message,
'assertion message',
contains(
"url could not be parsed, ensure it's a valid storage url"));
return;
}
"url could not be parsed, ensure it's a valid storage url"),
),
),
);
});

test('accepts a gs url without a fullPath', () async {
Expand All @@ -167,23 +168,33 @@ void runInstanceTests() {

test('throws an error if url does not start with gs:// or https://',
() async {
try {
storage.refFromURL('bs://foo/bar/cat.gif');
fail('Should have thrown an [AssertionError]');
} catch (error) {
expect(error.message,
contains("a url must start with 'gs://' or 'https://'"));
}
expect(
() {
storage.refFromURL('bs://foo/bar/cat.gif');
fail('Should have thrown an [AssertionError]');
},
throwsA(
isA<AssertionError>().having(
(p0) => p0.message,
'assertion message',
contains("a url must start with 'gs://' or 'https://'"),
),
),
);
});
});

group('setMaxOperationRetryTime', () {
test('should set', () async {
expect(storage.maxOperationRetryTime,
const Duration(milliseconds: 120000));
expect(
storage.maxOperationRetryTime,
const Duration(milliseconds: 120000),
);
storage.setMaxOperationRetryTime(const Duration(milliseconds: 100000));
expect(storage.maxOperationRetryTime,
const Duration(milliseconds: 100000));
expect(
storage.maxOperationRetryTime,
const Duration(milliseconds: 100000),
);
});
});

Expand Down
@@ -1,13 +1,10 @@
// ignore_for_file: require_trailing_commas
// @dart = 2.9

import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter_test/flutter_test.dart';

void runListResultTests() {
group('$ListResult', () {
/*late*/ FirebaseStorage storage;
/*late*/ ListResult result;
late FirebaseStorage storage;
late ListResult result;

setUpAll(() async {
storage = FirebaseStorage.instance;
Expand Down
@@ -1,6 +1,3 @@
// ignore_for_file: require_trailing_commas
// @dart = 2.9

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
Expand All @@ -14,7 +11,7 @@ import './test_utils.dart';

void runReferenceTests() {
group('$Reference', () {
/*late*/ FirebaseStorage storage;
late FirebaseStorage storage;

setUpAll(() async {
storage = FirebaseStorage.instance;
Expand Down Expand Up @@ -46,7 +43,7 @@ void runReferenceTests() {

group('parent', () {
test('returns the parent directory as a reference', () async {
expect(storage.ref('/foo/uploadNope.jpeg').parent.fullPath, 'foo');
expect(storage.ref('/foo/uploadNope.jpeg').parent?.fullPath, 'foo');
});

test('returns null if already at root', () async {
Expand Down Expand Up @@ -99,11 +96,14 @@ void runReferenceTests() {
Reference ref = storage.ref('/uploadNope.jpeg');

await expectLater(
() => ref.delete(),
throwsA(isA<FirebaseException>()
() => ref.delete(),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'unauthorized')
.having((e) => e.message, 'message',
'User is not authorized to perform the desired action.')));
'User is not authorized to perform the desired action.'),
),
);
});
});

Expand Down Expand Up @@ -196,8 +196,8 @@ void runReferenceTests() {
contentLanguage: 'en',
));

expect(complete.metadata.size, kTestString.length);
expect(complete.metadata.contentLanguage, 'en');
expect(complete.metadata?.size, kTestString.length);
expect(complete.metadata?.contentLanguage, 'en');
});

test('errors if permission denied', () async {
Expand Down Expand Up @@ -249,9 +249,9 @@ void runReferenceTests() {
),
);

expect(complete.metadata.size, kTestString.length);
expect(complete.metadata.contentLanguage, 'en');
expect(complete.metadata.customMetadata['activity'], 'test');
expect(complete.metadata?.size, kTestString.length);
expect(complete.metadata?.contentLanguage, 'en');
expect(complete.metadata?.customMetadata!['activity'], 'test');
});

test('errors if permission denied', () async {
Expand Down Expand Up @@ -279,11 +279,14 @@ void runReferenceTests() {
final Reference ref = storage.ref('uploadNope.jpeg');

await expectLater(
() => ref.putString('data'),
throwsA(isA<FirebaseException>()
() => ref.putString('data'),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'unauthorized')
.having((e) => e.message, 'message',
'User is not authorized to perform the desired action.')));
'User is not authorized to perform the desired action.'),
),
);
});
});

Expand All @@ -292,7 +295,7 @@ void runReferenceTests() {
Reference ref = storage.ref('flutter-tests').child('flt-ok.txt');
FullMetadata fullMetadata = await ref
.updateMetadata(SettableMetadata(customMetadata: {'foo': 'bar'}));
expect(fullMetadata.customMetadata['foo'], 'bar');
expect(fullMetadata.customMetadata!['foo'], 'bar');
});

test('errors if property does not exist', () async {
Expand Down

0 comments on commit 8dad3a5

Please sign in to comment.