Files
GitJournal/test/flattened_notes_folder_large_test.dart
Vishesh Handa 657721adc6 Update dart-git and stop using the Result class
Instead we're going to move back to standard exceptions.

Using a custom Result class has created far far more problems
- The Stacktraces aren't always right
- Sometimes one forgets to check the Result error
- All other exception throwing code needing to be converted to Results
- Non idiomatic Dart code

I think it's better to just go back to exceptions. They have their
problems, but overall, I think it's a better approach.
2023-11-24 14:03:30 +01:00

107 lines
3.2 KiB
Dart

/*
* SPDX-FileCopyrightText: 2019-2021 Vishesh Handa <me@vhanda.in>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import 'dart:math';
import 'package:dart_git/dart_git.dart';
import 'package:gitjournal/core/file/file_storage.dart';
import 'package:gitjournal/core/folder/flattened_notes_folder.dart';
import 'package:gitjournal/core/folder/notes_folder_config.dart';
import 'package:gitjournal/core/folder/notes_folder_fs.dart';
import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/core/note_storage.dart';
import 'package:gitjournal/core/notes/note.dart';
import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart';
import 'package:universal_io/io.dart' as io;
import 'lib.dart';
void main() {
setUpAll(gjSetupAllTests);
group('Flattened Notes Folder Large Test', () {
late io.Directory tempDir;
late String repoPath;
late NotesFolderFS rootFolder;
late NotesFolderConfig config;
late FileStorage fileStorage;
setUp(() async {
tempDir = await io.Directory.systemTemp.createTemp('__folder_large__');
repoPath = tempDir.path + p.separator;
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
fileStorage = await FileStorage.fake(repoPath);
var random = Random();
for (var i = 0; i < 300; i++) {
// print("Building Note $i");
await _writeRandomNote(random, repoPath, config, fileStorage);
}
var repo = GitRepository.load(repoPath);
repo.commit(
message: "Prepare Test Env",
author: GitAuthor(name: 'Name', email: "name@example.com"),
addAll: true,
);
await fileStorage.reload();
rootFolder = NotesFolderFS.root(config, fileStorage);
await rootFolder.loadRecursively();
});
tearDown(() async {
// print("Cleaning Up TempDir: ${repoPath}");
tempDir.deleteSync(recursive: true);
});
test('Should load all the notes flattened', () async {
var f = FlattenedNotesFolder(rootFolder, title: "");
expect(f.notes.length, 300);
var tempDir = await io.Directory.systemTemp.createTemp('_test_');
var newRepoPath = tempDir.path + p.separator;
var newFileStorage = await FileStorage.fake(newRepoPath);
await _writeRandomNote(Random(), newRepoPath, config, newFileStorage);
var repo = GitRepository.load(newRepoPath);
repo.commit(
message: "Prepare Test Env",
author: GitAuthor(name: 'Name', email: "name@example.com"),
addAll: true,
);
await newFileStorage.reload();
});
});
}
Future<void> _writeRandomNote(Random random, String dirPath,
NotesFolderConfig config, FileStorage fileStorage) async {
String path;
while (true) {
path = p.join(dirPath, "${random.nextInt(10000)}.md");
if (!io.File(path).existsSync()) {
break;
}
}
var parent = NotesFolderFS.root(config, fileStorage);
var note = Note.newNote(parent,
fileName: p.basename(path), fileFormat: NoteFileFormat.Markdown);
note = note.copyWith(
modified: DateTime(2014, 1, 1 + (random.nextInt(2000))),
body: "p1",
);
note = await NoteStorage.save(note);
}