Added tests + bump version

This commit is contained in:
Tobe O 2018-07-12 16:29:16 -04:00
parent 1f20ed5e02
commit 3e157caa6c
6 changed files with 95 additions and 14 deletions

4
.travis.yml Normal file
View file

@ -0,0 +1,4 @@
language: dart
dart:
- dev
- stable

View file

@ -1,3 +1,6 @@
# 1.1.2
* Added tests, because tests.
# 1.1.1
* Dart 2 fixes.

View file

@ -1,4 +1,7 @@
# file_service
[![Pub](https://img.shields.io/pub/v/angel_file_service.svg)](https://pub.dartlang.org/packages/angel_file_service)
[![build status](https://travis-ci.org/angel-dart/file_service.svg)](https://travis-ci.org/angel-dart/file_service)
Angel service that persists data to a file on disk, stored as a JSON list. It uses a simple
mutex to prevent race conditions, and caches contents in memory until changes
are made.

View file

@ -19,6 +19,11 @@ class JsonFileService extends Service {
allowQuery: allowQuery != false);
}
Map<String, dynamic> _coerceStringDynamic(Map m) {
return m.keys.fold<Map<String, dynamic>>(
<String, dynamic>{}, (out, k) => out..[k.toString()] = m[k]);
}
Future _load() {
return _mutex.withResource(() async {
if (!await file.exists()) await file.writeAsString(json.encode([]));
@ -32,18 +37,10 @@ class JsonFileService extends Service {
var contents = await file.readAsString();
try {
var list = json.decode(contents) as Iterable;
_store.items.clear(); // Clear exist in-memory copy
_store.items.addAll(list.map((x) =>
_revive(x) as Map<String, dynamic>)); // Insert all new entries
} catch (e) {
if (_store.app is Angel) {
(_store.app as Angel)
.logger
.warning('WARNING: Failed to reload contents of ${file.path}.');
}
}
var list = json.decode(contents) as Iterable;
_store.items.clear(); // Clear exist in-memory copy
_store.items.addAll(list.map((x) =>
_coerceStringDynamic(_revive(x) as Map))); // Insert all new entries
}
});
}

View file

@ -1,5 +1,5 @@
name: angel_file_service
version: 1.1.1
version: 1.1.2
description: Angel service that persists data to a file on disk.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/file_service
@ -9,4 +9,6 @@ dependencies:
angel_framework: ^1.1.0-alpha
dart2_constant: ^1.0.0
file: ^2.0.0
pool: ^1.0.0
pool: ^1.0.0
dev_dependencies:
test:

72
test/all_test.dart Normal file
View file

@ -0,0 +1,72 @@
import 'package:angel_file_service/angel_file_service.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:test/test.dart';
main() {
MemoryFileSystem fs;
File dbFile;
JsonFileService service;
setUp(() async {
fs = new MemoryFileSystem();
dbFile = fs.file('db.json');
service = new JsonFileService(dbFile);
await dbFile.writeAsString('''
[
{"id": "0", "foo": "bar"},
{"id": "1", "foo": "baz"},
{"id": "2", "foo": "quux"}
]
''');
});
tearDown(() => service.close());
test('index no params', () async {
expect(await service.index(), [
{"id": "0", "foo": "bar"},
{"id": "1", "foo": "baz"},
{"id": "2", "foo": "quux"}
]);
});
test('index with query', () async {
expect(
await service.index({
'query': {'foo': 'bar'}
}),
[
{"id": "0", "foo": "bar"}
],
);
});
test('read', () async {
expect(
await service.read('2'),
{"id": "2", "foo": "quux"},
);
});
test('modify', () async {
await service.modify('2', {'baz': 'quux'});
expect(await service.read('2'), containsPair('baz', 'quux'));
});
test('update', () async {
await service.update('2', {'baz': 'quux'});
expect(await service.read('2'), containsPair('baz', 'quux'));
expect(await service.read('2'), isNot(containsPair('foo', 'quux')));
});
test('delete', () async {
await service.remove('2');
expect(await service.index(), [
{"id": "0", "foo": "bar"},
{"id": "1", "foo": "baz"}
]);
});
}