Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock list all #33

Merged
merged 5 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ build/
doc/api/

.flutter-plugins
.flutter-plugins-dependencies
.flutter-plugins-dependencies

coverage/
24 changes: 24 additions & 0 deletions lib/src/mock_list_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:firebase_storage/firebase_storage.dart';
import 'package:firebase_storage_mocks/firebase_storage_mocks.dart';
import 'package:firebase_storage_mocks/src/mock_storage_reference.dart';

class MockListResult implements ListResult {
@override
MockFirebaseStorage storage;

@override
List<MockReference> items;

@override
List<MockReference> prefixes;

@override
String? nextPageToken;

MockListResult({
required this.storage,
required this.items,
required this.prefixes,
this.nextPageToken,
});
}
28 changes: 28 additions & 0 deletions lib/src/mock_storage_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:typed_data';

import 'package:firebase_storage/firebase_storage.dart';
import 'package:firebase_storage_mocks/firebase_storage_mocks.dart';
import 'package:firebase_storage_mocks/src/mock_list_result.dart';

class MockReference implements Reference {
final MockFirebaseStorage _storage;
Expand Down Expand Up @@ -89,6 +90,33 @@ class MockReference implements Reference {
return Future.value(_storage.storedDataMap[_path]);
}

@override
Future<ListResult> listAll() {
final normalizedPath = _path.endsWith('/') ? _path : _path + '/';
final prefixes = <String>[], items = <String>[];
final allPaths = <String>[
..._storage.storedDataMap.keys,
..._storage.storedFilesMap.keys
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
];
for (var child in allPaths) {
if (!child.startsWith(normalizedPath)) continue;
final relativeChild = child.substring(normalizedPath.length);
if (relativeChild.contains('/')) {
var prefix = normalizedPath + relativeChild.split('/')[0];
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
if (!prefixes.contains(prefix)) prefixes.add(prefix);
} else {
items.add(child);
}
}
prefixes.sort();
items.sort();
return Future.value(MockListResult(
items: items.map((item) => MockReference(_storage, item)).toList(),
prefixes:
prefixes.map((prefix) => MockReference(_storage, prefix)).toList(),
storage: _storage));
}

@override
Future<FullMetadata> updateMetadata(SettableMetadata metadata) {
final nonNullMetadata = metadata.asMap()
Expand Down
44 changes: 42 additions & 2 deletions test/firebase_storage_mocks_test.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';

import 'package:file/memory.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:firebase_storage_mocks/firebase_storage_mocks.dart';
import 'package:firebase_storage_mocks/src/mock_storage_reference.dart';
import 'package:test/test.dart';

final filename = 'someimage.png';
final random = Random.secure();

void main() {
group('MockFirebaseStorage Tests', () {
Expand All @@ -24,7 +27,7 @@ void main() {
test('Puts Data', () async {
final storage = MockFirebaseStorage();
final storageRef = storage.ref().child(filename);
final imageData = Uint8List(256);
final imageData = randomData(256);
final task = storageRef.putData(imageData);
await task;

Expand All @@ -36,7 +39,7 @@ void main() {
group('Gets Data', () {
late MockFirebaseStorage storage;
late Reference reference;
final imageData = Uint8List(256);
final imageData = randomData(256);
setUp(() async {
storage = MockFirebaseStorage();
reference = storage.ref().child(filename);
Expand Down Expand Up @@ -115,9 +118,46 @@ void main() {
expect(downloadUrl.contains('some-bucket/o/someimage.png'), isTrue);
});
});

test('Ref listAll', () async {
final basePath = 'this/is/basic';
final otherPath = 'another/path';
final storage = MockFirebaseStorage();
await storage.ref(basePath + '/data2').putData(randomData(255));
await storage.ref(basePath + '/subdir1/data1').putData(randomData(255));
await storage.ref(basePath + '/data2').putData(randomData(255));
await storage.ref(otherPath + '/data3').putData(randomData(255));
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
await storage.ref(basePath + '/file3').putFile(getFakeImageFile());
await storage.ref(basePath + '/data3').putData(randomData(255));
await storage
.ref(basePath + '/subdir2/file2')
.putFile(getFakeImageFile());
await storage
.ref(basePath + '/subdir1/file1')
.putFile(getFakeImageFile());

final listResult = await storage.ref(basePath).listAll();
expect(listResult.prefixes.length, 2);
expectRef(listResult.prefixes[0], name: 'subdir1');
expectRef(listResult.prefixes[1], name: 'subdir2');
expect(listResult.items.length, 3);
expectRef(listResult.items[0], name: 'data2');
expectRef(listResult.items[1], name: 'data3');
expectRef(listResult.items[2], name: 'file3');
});
});
}

void expectRef(Reference actualReference, {required String name}) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

expect(actualReference,
isA<MockReference>().having((ref) => ref.name, 'Right name', name));
}

Uint8List randomData(int n) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

final elements = List.generate(n, (index) => random.nextInt(255));
return Uint8List.fromList(elements);
}

File getFakeImageFile() {
var fs = MemoryFileSystem();
final image = fs.file(filename);
Expand Down