Skip to content

Commit

Permalink
Merge pull request #396 from rohansen856/models_test
Browse files Browse the repository at this point in the history
tests: added tests for the Models folder
  • Loading branch information
BrawlerXull authored Dec 15, 2024
2 parents f474487 + 5613823 commit d1d4ca2
Show file tree
Hide file tree
Showing 16 changed files with 790 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/models/chart_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:taskwarrior/app/models/chart.dart';

void main() {
group('ChartData', () {
test('should create an instance with correct values', () {
final chartData = ChartData('2024-12-12', 100, 200);

expect(chartData.x, '2024-12-12');
expect(chartData.y1, 100);
expect(chartData.y2, 200);
});

test('should handle null or empty values correctly', () {
final chartData = ChartData('', 0, 0);

expect(chartData.x, '');
expect(chartData.y1, 0);
expect(chartData.y2, 0);
});

test('should handle negative values correctly', () {
final chartData = ChartData('2024-12-12', -100, -200);

expect(chartData.x, '2024-12-12');
expect(chartData.y1, -100);
expect(chartData.y2, -200);
});
});
}
103 changes: 103 additions & 0 deletions test/models/data_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:taskwarrior/app/models/data.dart';
import 'package:taskwarrior/app/models/json/task.dart';
import 'package:taskwarrior/app/services/notification_services.dart';
import 'package:flutter/widgets.dart';
import 'dart:io';

class MockNotificationService extends Mock implements NotificationService {}

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('Data', () {
late Data data;
late Directory home;
late MockNotificationService mockNotificationService;

setUp(() {
WidgetsFlutterBinding.ensureInitialized();
home = Directory.systemTemp.createTempSync();
data = Data(home);
mockNotificationService = MockNotificationService();

when(mockNotificationService.initiliazeNotification())
.thenAnswer((_) async {});
});

test('should update tasks with status "waiting" or "until" correctly',
() async {
final task1 = Task((b) => b
..uuid = '1'
..status = 'pending'
..wait = DateTime.now().toUtc().subtract(const Duration(days: 1))
..description = 'Test Task'
..entry = DateTime.now().toUtc());
final task2 = Task((b) => b
..uuid = '2'
..status = 'deleted'
..until = DateTime.now().toUtc().subtract(const Duration(days: 1))
..description = 'Test Task'
..entry = DateTime.now().toUtc());

final updatedTasks = data.pendingData();
expect(
updatedTasks.any(
(task) => task.uuid == task1.uuid && task.status == task1.status),
isFalse);
expect(
updatedTasks.any(
(task) => task.uuid == task2.uuid && task.status == task2.status),
isFalse);
});

test('should correctly return pending data', () {
final task = Task((b) => b
..uuid = '1'
..status = 'pending'
..description = 'Test Task'
..entry = DateTime.now().toUtc());

data.updateWaitOrUntil([task]);
final tasks = data.pendingData();
expect(tasks.any((t) => t.uuid == '1' && t.description == 'Test Task'),
isFalse);
});

test('should correctly return completed data', () {
final task = Task((b) => b
..uuid = '1'
..status = 'completed'
..description = 'Test Task'
..entry = DateTime.now().toUtc());

data.updateWaitOrUntil([task]);
final tasks = data.completedData();
expect(tasks.any((t) => t.uuid == '1' && t.description == 'Test Task'),
isFalse);
});

test('should correctly return waiting data', () {
final task = Task((b) => b
..uuid = '1'
..status = 'waiting'
..description = 'Test Task'
..entry = DateTime.now().toUtc());

data.updateWaitOrUntil([task]);
final tasks = data.waitingData();
expect(tasks.any((t) => t.uuid == '1' && t.description == 'Test Task'),
isFalse);
});

test('should correctly export data', () {
final exportedData = data.export();
expect(exportedData, isNotNull);
});

tearDown(() {
home.deleteSync(recursive: true);
});
});
}
104 changes: 104 additions & 0 deletions test/models/filters_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:taskwarrior/app/models/filters.dart';
import 'package:taskwarrior/app/services/tag_filter.dart';

void main() {
group('Filters', () {
late Filters filters;
late bool pendingFilter;
late bool waitingFilter;
late String projectFilter;
late bool tagUnion;
late Map<String, TagFilterMetadata> tags;

setUp(() {
pendingFilter = false;
waitingFilter = false;
projectFilter = 'TestProject';
tagUnion = false;
tags = {
'tag1': const TagFilterMetadata(display: 'Tag 1', selected: false),
'tag2': const TagFilterMetadata(display: 'Tag 2', selected: true),
};

filters = Filters(
pendingFilter: pendingFilter,
waitingFilter: waitingFilter,
togglePendingFilter: () {
pendingFilter = !pendingFilter;
},
toggleWaitingFilter: () {
waitingFilter = !waitingFilter;
},
tagFilters: TagFilters(
tagUnion: tagUnion,
toggleTagUnion: () {
tagUnion = !tagUnion;
},
tags: tags,
toggleTagFilter: (String tag) {
tags[tag] = TagFilterMetadata(
display: tags[tag]!.display,
selected: !tags[tag]!.selected,
);
},
),
projects: [],
projectFilter: projectFilter,
toggleProjectFilter: (String project) {
projectFilter = project;
},
);
});

test('should correctly initialize with given parameters', () {
expect(filters.pendingFilter, pendingFilter);
expect(filters.waitingFilter, waitingFilter);
expect(filters.projectFilter, projectFilter);
expect(filters.tagFilters.tagUnion, tagUnion);
expect(filters.tagFilters.tags, tags);
});

test('should correctly toggle pending filter', () {
filters.togglePendingFilter();
expect(pendingFilter, isTrue);

filters.togglePendingFilter();
expect(pendingFilter, isFalse);
});

test('should correctly toggle waiting filter', () {
filters.toggleWaitingFilter();
expect(waitingFilter, isTrue);

filters.toggleWaitingFilter();
expect(waitingFilter, isFalse);
});

test('should correctly toggle project filter', () {
const newProject = 'NewProject';
filters.toggleProjectFilter(newProject);
expect(projectFilter, newProject);

const anotherProject = 'AnotherProject';
filters.toggleProjectFilter(anotherProject);
expect(projectFilter, anotherProject);
});

test('should correctly toggle tag union', () {
filters.tagFilters.toggleTagUnion();
expect(tagUnion, isTrue);

filters.tagFilters.toggleTagUnion();
expect(tagUnion, isFalse);
});

test('should correctly toggle tag filter', () {
filters.tagFilters.toggleTagFilter('tag1');
expect(tags['tag1']!.selected, isTrue);

filters.tagFilters.toggleTagFilter('tag1');
expect(tags['tag1']!.selected, isFalse);
});
});
}
44 changes: 44 additions & 0 deletions test/models/json/annotation_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// test/annotation_test.dart

import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart';
import 'package:taskwarrior/app/models/json/annotation.dart';

void main() {
group('Annotation', () {
late Annotation annotation;
late DateTime entry;
late String description;

setUp(() {
entry = DateTime.now().toUtc(); // Ensure the DateTime is in UTC
description = 'Test description';

annotation = Annotation((b) => b
..entry = entry
..description = description);
});

test('should correctly initialize with given parameters', () {
expect(annotation.entry, entry);
expect(annotation.description, description);
});

test('should correctly convert to JSON', () {
final json = annotation.toJson();
expect(DateFormat("yyyyMMdd'T'HH").format(DateTime.parse(json['entry'])),
DateFormat("yyyyMMdd'T'HH").format(entry));
expect(json['description'], description);
});

test('should correctly create from JSON', () {
final json = {
'entry': entry.toIso8601String(),
'description': description,
};
final newAnnotation = Annotation.fromJson(json);
expect(newAnnotation.entry, entry);
expect(newAnnotation.description, description);
});
});
}
30 changes: 30 additions & 0 deletions test/models/json/iso_8601_basic_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart';
import 'package:taskwarrior/app/models/json/iso_8601_basic.dart';
import 'package:taskwarrior/app/models/json/serializers.dart';

void main() {
group('Iso8601BasicDateTimeSerializer', () {
late Iso8601BasicDateTimeSerializer serializer;
late DateTime dateTime;
late DateFormat dateFormat;

setUp(() {
serializer = Iso8601BasicDateTimeSerializer();
dateTime = DateTime.utc(2024, 12, 12, 13, 30, 40, 495);
dateFormat = DateFormat('yMMddTHHmmss\'Z\'');
});

test('should throw ArgumentError if dateTime is not in UTC', () {
expect(
() => serializer.serialize(serializers, dateTime.toLocal()),
throwsArgumentError,
);
});

test('should correctly serialize UTC dateTime', () {
final serialized = serializer.serialize(serializers, dateTime);
expect(serialized, dateFormat.format(dateTime));
});
});
}
42 changes: 42 additions & 0 deletions test/models/json/serializer_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart';
import 'package:taskwarrior/app/models/json/serializers.dart';
import 'package:taskwarrior/app/models/json/annotation.dart';
import 'package:taskwarrior/app/models/json/task.dart';

void main() {
group('Serializers', () {
test('should correctly serialize and deserialize Annotation', () {
final annotation = Annotation((b) => b
..entry = DateTime.now().toUtc()
..description = 'Test description');

final serialized =
serializers.serializeWith(Annotation.serializer, annotation);
final deserialized =
serializers.deserializeWith(Annotation.serializer, serialized!);

expect(DateFormat("yyyyMMdd'T'HH").format(deserialized!.entry),
DateFormat("yyyyMMdd'T'HH").format(annotation.entry));
expect(deserialized.description, annotation.description);
});

test('should correctly serialize and deserialize Task', () {
final task = Task((b) => b
..uuid = '1'
..status = 'pending'
..description = 'Test Task'
..entry = DateTime.now().toUtc());

final serialized = serializers.serializeWith(Task.serializer, task);
final deserialized =
serializers.deserializeWith(Task.serializer, serialized!);

expect(DateFormat("yyyyMMdd'T'HH").format(deserialized!.entry),
DateFormat("yyyyMMdd'T'HH").format(task.entry));
expect(deserialized.uuid, task.uuid);
expect(deserialized.status, task.status);
expect(deserialized.description, task.description);
});
});
}
Loading

0 comments on commit d1d4ca2

Please sign in to comment.