From 104d54efc37cd0df469b505638ec4b091b7b41b4 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sun, 3 Jun 2018 20:19:20 +0200 Subject: [PATCH] Fix tests --- lib/state_container.dart | 3 +-- lib/storage/file_storage.dart | 16 ++++++++++++++++ test/file_storage_test.dart | 17 +++++++++++------ test/serializers_test.dart | 2 +- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lib/state_container.dart b/lib/state_container.dart index 27bbd96f..18226096 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -89,8 +89,7 @@ class StateContainerState extends State { // FIXME: Implement this! void updateNote(Note note) { setState(() { - //appState.notes. - //appState.notes.remove(note); + noteRepo.updateNote(note); }); } diff --git a/lib/storage/file_storage.dart b/lib/storage/file_storage.dart index e0a4cda7..da9c0e84 100644 --- a/lib/storage/file_storage.dart +++ b/lib/storage/file_storage.dart @@ -78,4 +78,20 @@ class FileStorage implements NoteRepository { Future sync() async { return false; } + + Future saveNotes(List notes) async { + final dir = await getDirectory(); + await dir.delete(recursive: true); + await dir.create(); + + for (var note in notes) { + var filePath = p.join(dir.path, fileNameGenerator(note)); + + var file = new File(filePath); + var contents = noteSerializer.encode(note); + await file.writeAsString(contents); + } + + return dir; + } } diff --git a/test/file_storage_test.dart b/test/file_storage_test.dart index b5fc6756..58d53184 100644 --- a/test/file_storage_test.dart +++ b/test/file_storage_test.dart @@ -3,18 +3,23 @@ import 'dart:io'; import 'package:test/test.dart'; import 'package:path/path.dart' as p; -import '../lib/note.dart'; -import '../lib/file_storage.dart'; +import 'package:journal/note.dart'; +import 'package:journal/storage/file_storage.dart'; +import 'package:journal/storage/serializers.dart'; main() { group('FileStorage', () { var notes = [ - Note(id: "1", body: "test", createdAt: new DateTime.now()), - Note(id: "2", body: "test2", createdAt: new DateTime.now()), + Note(id: "1", body: "test", created: new DateTime.now()), + Note(id: "2", body: "test2", created: new DateTime.now()), ]; final directory = Directory.systemTemp.createTemp('__storage_test__'); - final storage = FileStorage(getDirectory: () => directory); + final storage = FileStorage( + getDirectory: () => directory, + noteSerializer: new JsonNoteSerializer(), + fileNameGenerator: (Note note) => note.id, + ); tearDownAll(() async { final tempDirectory = await directory; @@ -30,7 +35,7 @@ main() { }); test('Should load Notes from disk', () async { - var loadedNotes = await storage.loadNotes(); + var loadedNotes = await storage.listNotes(); loadedNotes.sort(); notes.sort(); diff --git a/test/serializers_test.dart b/test/serializers_test.dart index 5e691a35..a681ed92 100644 --- a/test/serializers_test.dart +++ b/test/serializers_test.dart @@ -1,7 +1,7 @@ import 'dart:io'; import 'package:journal/note.dart'; -import 'package:journal/serializers.dart'; +import 'package:journal/storage/serializers.dart'; import 'package:test/test.dart';