Skip to content

Commit

Permalink
test and options helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jun 12, 2024
1 parent 7b15baf commit 41060f3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 15 deletions.
39 changes: 38 additions & 1 deletion storage/lib/src/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,50 @@ class GetFilesOptions {
'autoPaginate': autoPaginate,
if (pageToken != null) 'pageToken': pageToken
}.toString();

// Copy options
GetFilesOptions copyWith({
int? maxResults,
String? prefix,
bool? autoPaginate,
String? pageToken,
}) {
return GetFilesOptions(
maxResults: maxResults ?? this.maxResults,
prefix: prefix ?? this.prefix,
autoPaginate: autoPaginate ?? this.autoPaginate,
pageToken: pageToken ?? this.pageToken,
);
}
}

/// GetFiles response
abstract class GetFilesResponse {
List<File> get files;

GetFilesOptions? get nextQuery;

/// Default implementation
factory GetFilesResponse(
{required List<File> files, GetFilesOptions? nextQuery}) {
return _GetFilesResponse(files: files, nextQuery: nextQuery);
}
}

class _GetFilesResponse implements GetFilesResponse {
@override
final List<File> files;

@override
final GetFilesOptions? nextQuery;

_GetFilesResponse({required this.files, required this.nextQuery});

@override
String toString() => {
'files': files.length,
if (nextQuery != null) 'nextQuery': nextQuery
}.toString();
}

mixin StorageMixin implements Storage {
Expand Down Expand Up @@ -117,7 +154,7 @@ abstract class File {
/// The bucket instance the is attached to.
Bucket get bucket;

/// Available when listed through getFiles
/// Available when listed through getFiles (not on flutter though...)
FileMetadata? get metadata;

/// Read meatada
Expand Down
2 changes: 1 addition & 1 deletion storage_fs/test/storage_fs_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() {
var firebase = FirebaseLocal();

group('storage_fs_io', () {
run(
runStorageTests(
firebase: firebase,
storageService: storageServiceIo,
storageOptions: TestStorageOptions(bucket: _bucketName));
Expand Down
2 changes: 1 addition & 1 deletion storage_fs/test/storage_fs_memory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
var storage = storageServiceMemory.storage(app);
await storage.bucket(_bucketName).create();
});
runApp(app,
runStorageAppTests(app,
storageService: storageServiceMemory,
storageOptions: TestStorageOptions(bucket: _bucketName));

Expand Down
2 changes: 1 addition & 1 deletion storage_rest/test/storage_rest_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Future main() async {
if (context != null) {
var firebase = firebaseRest;
group('all', () {
run(
runStorageTests(
firebase: firebase,
storageService: storageServiceRest,
options: context.options,
Expand Down
54 changes: 43 additions & 11 deletions storage_test/lib/storage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ class TestStorageOptions {
String toString() => {'bucket': bucket, 'rootPath': rootPath}.toString();
}

@Deprecated('Use runStorageTests')
void run(
{required Firebase firebase,
required StorageService storageService,
AppOptions? options,
required TestStorageOptions storageOptions}) =>
runStorageTests(
firebase: firebase,
storageService: storageService,
options: options,
storageOptions: storageOptions);

void runStorageTests(
{required Firebase firebase,
required StorageService storageService,
AppOptions? options,
Expand All @@ -26,10 +38,18 @@ void run(
return app.delete();
});

runApp(app, storageService: storageService, storageOptions: storageOptions);
runStorageAppTests(app,
storageService: storageService, storageOptions: storageOptions);
}

@Deprecated('Use runStorageAppTests')
void runApp(App app,
{required StorageService storageService,
required TestStorageOptions storageOptions}) =>
runStorageAppTests(app,
storageService: storageService, storageOptions: storageOptions);

void runStorageAppTests(App app,
{required StorageService storageService,
required TestStorageOptions storageOptions}) {
String filePath(String path) {
Expand Down Expand Up @@ -140,17 +160,23 @@ void runApp(App app,
await bucket
.file(filePath('test/list_files/yes/other_sub/sub/file3.txt'))
.writeAsString(content);

var files = <File>[];
var listFilePath = filePath('test/list_files/yes');

var query = GetFilesOptions(
maxResults: 2,
prefix: filePath('test/list_files/yes'),
autoPaginate: false);
maxResults: 2, prefix: listFilePath, autoPaginate: false);
var response = await bucket.getFiles(query);
files.addAll(response.files);

String? lastFirstPath;
while (response.nextQuery != null) {
response = await bucket.getFiles(response.nextQuery);
var firstPath = response.files.firstOrNull?.name;
if (firstPath != null) {
expect(firstPath, isNot(lastFirstPath));
}
lastFirstPath = firstPath;
files.addAll(response.files);
}
var names = files.map((e) => e.name).toList()..sort();
Expand All @@ -169,10 +195,13 @@ void runApp(App app,
// Check meta
var file = files.firstWhere((element) =>
element.name == filePath('test/list_files/yes/file1.txt'));
expect(file.metadata!.dateUpdated.isBefore(now), isFalse);
expect(file.metadata!.md5Hash,
isNotEmpty); // 'abd848eb171be7fa03d8e29223fcbe78');
expect(file.metadata!.size, 23);
// This happens on flutter.
if (file.metadata != null) {
expect(file.metadata!.dateUpdated.isBefore(now), isFalse);
expect(file.metadata!.md5Hash,
isNotEmpty); // 'abd848eb171be7fa03d8e29223fcbe78');
expect(file.metadata!.size, 23);
}
});

test('list_files_meta', () async {
Expand All @@ -188,9 +217,12 @@ void runApp(App app,

response = await bucket.getFiles(query);
var file2 = response.files.first;
expect(file1.metadata!.size, file2.metadata!.size);
expect(file1.metadata!.dateUpdated, file2.metadata!.dateUpdated);
expect(file1.metadata!.md5Hash, file2.metadata!.md5Hash);
// This happens on flutter.
if (file1.metadata != null) {
expect(file1.metadata!.size, file2.metadata!.size);
expect(file1.metadata!.dateUpdated, file2.metadata!.dateUpdated);
expect(file1.metadata!.md5Hash, file2.metadata!.md5Hash);
}
});

test('file_get_meta', () async {
Expand Down

0 comments on commit 41060f3

Please sign in to comment.