Skip to content

Commit

Permalink
Unit tests for DecisionTreeClassifier refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed May 6, 2020
1 parent ab820fa commit 9348dbc
Showing 1 changed file with 22 additions and 21 deletions.
@@ -1,4 +1,3 @@
import 'dart:convert';
import 'dart:io';

import 'package:ml_algo/src/classifier/decision_tree_classifier/decision_tree_classifier.dart';
Expand Down Expand Up @@ -28,14 +27,6 @@ void main() {

final testFileName = 'test/classifier/decision_tree_classifier/serialized_classifier.json';

tearDownAll(() async {
final file = await File(testFileName);
if (!await file.exists()) {
return;
}
await file.delete();
});

test('should create classifier', () {
expect(classifier, isA<DecisionTreeClassifierImpl>());
});
Expand Down Expand Up @@ -169,23 +160,33 @@ void main() {
expect(json[treeRootNodeJsonKey], majorityTreeDataMock);
});

test('should save to file as json', () async {
await classifier.saveAsJson(testFileName);
group('saveAsJson', () {
tearDown(() async {
final file = await File(testFileName);
if (!await file.exists()) {
return;
}
await file.delete();
});

final file = await File(testFileName);
final fileExists = await file.exists();
test('should save to file as json', () async {
await classifier.saveAsJson(testFileName);

expect(fileExists, isTrue);
});
final file = await File(testFileName);
final fileExists = await file.exists();

expect(fileExists, isTrue);
});

test('should save to a restorable json', () async {
await classifier.saveAsJson(testFileName);
test('should save to a restorable json', () async {
await classifier.saveAsJson(testFileName);

final file = await File(testFileName);
final json = await file.readAsString();
final restoredClassifier = DecisionTreeClassifier.fromJson(json);
final file = await File(testFileName);
final json = await file.readAsString();
final restoredClassifier = DecisionTreeClassifier.fromJson(json);

expect(restoredClassifier.toJson(), classifier.toJson());
expect(restoredClassifier.toJson(), classifier.toJson());
});
});
});
}

0 comments on commit 9348dbc

Please sign in to comment.