2024-05-30 14:28:21 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:mockito/annotations.dart';
|
|
|
|
import 'package:mockito/mockito.dart';
|
|
|
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
|
|
import 'package:cpd/database/habit.dart';
|
|
|
|
import 'package:cpd/database/todo_interface.dart';
|
|
|
|
import 'todo_interface_test.mocks.dart';
|
|
|
|
|
|
|
|
@GenerateMocks([ToDoInterface])
|
|
|
|
void main() {
|
|
|
|
setUpAll(() {
|
|
|
|
sqfliteFfiInit();
|
|
|
|
databaseFactory = databaseFactoryFfi;
|
|
|
|
});
|
|
|
|
|
|
|
|
group('ToDoInterface', () {
|
|
|
|
test('fetchAll returns list of habits', () async {
|
|
|
|
final mockToDoInterface = MockToDoInterface();
|
|
|
|
|
|
|
|
List<Habit> mockHabits = [
|
2024-06-09 17:45:17 +02:00
|
|
|
Habit(id: 1,
|
|
|
|
title: 'Habit 1',
|
|
|
|
subtitle: 'Description 1',
|
|
|
|
iconCodePoint: Icons.star.codePoint,
|
|
|
|
iconFontFamily: Icons.star.fontFamily ?? 'MaterialIcons',
|
|
|
|
isComplete: false),
|
|
|
|
Habit(id: 2,
|
|
|
|
title: 'Habit 2',
|
|
|
|
subtitle: 'Description 2',
|
|
|
|
iconCodePoint: Icons.star_border.codePoint,
|
|
|
|
iconFontFamily: Icons.star_border.fontFamily ?? 'MaterialIcons',
|
|
|
|
isComplete: true),
|
|
|
|
Habit(id: 3,
|
|
|
|
title: 'Habit 3',
|
|
|
|
subtitle: 'Description 3',
|
|
|
|
iconCodePoint: Icons.favorite.codePoint,
|
|
|
|
iconFontFamily: Icons.favorite.fontFamily ?? 'MaterialIcons',
|
|
|
|
isComplete: true),
|
2024-05-30 14:28:21 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
when(mockToDoInterface.fetchAll()).thenAnswer((_) async => mockHabits);
|
|
|
|
|
|
|
|
Future<List<Habit>> futureHabits = mockToDoInterface.fetchAll();
|
|
|
|
List<Habit> habits = await futureHabits;
|
|
|
|
|
|
|
|
expect(habits, isA<List<Habit>>());
|
|
|
|
expect(habits.length, 3);
|
|
|
|
expect(habits[0].title, 'Habit 1');
|
|
|
|
expect(habits[1].title, 'Habit 2');
|
|
|
|
expect(habits[2].title, 'Habit 3');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('fetchById returns a habit when found', () async {
|
2024-06-09 17:45:17 +02:00
|
|
|
final habit = Habit(id: 1, title: 'Test Habit', subtitle: 'Test Subtitle', iconCodePoint: Icons.star.codePoint,
|
|
|
|
iconFontFamily: Icons.star.fontFamily ?? 'MaterialIcons',isComplete: false);
|
2024-05-30 14:28:21 +02:00
|
|
|
final mockToDoInterface = MockToDoInterface();
|
|
|
|
|
|
|
|
when(mockToDoInterface.fetchById(1)).thenAnswer((_) async => habit);
|
|
|
|
|
|
|
|
Habit? fetchedHabit = await mockToDoInterface.fetchById(1);
|
|
|
|
|
|
|
|
expect(fetchedHabit, isNotNull);
|
|
|
|
expect(fetchedHabit!.title, 'Test Habit');
|
|
|
|
verify(mockToDoInterface.fetchById(1)).called(1);
|
|
|
|
});
|
|
|
|
|
2024-06-16 17:12:28 +02:00
|
|
|
test('insert inserts a habit and returns its id', () async {
|
2024-05-30 14:28:21 +02:00
|
|
|
|
|
|
|
final mockToDoInterface = MockToDoInterface();
|
2024-06-16 17:12:28 +02:00
|
|
|
when(mockToDoInterface.insert(
|
2024-05-30 14:28:21 +02:00
|
|
|
title: 'Test Habit',
|
|
|
|
subtitle: 'Test Subtitle',
|
|
|
|
icon: Icons.star))
|
|
|
|
.thenAnswer((_) async => 1);
|
|
|
|
|
2024-06-16 17:12:28 +02:00
|
|
|
final id = await mockToDoInterface.insert(
|
2024-05-30 14:28:21 +02:00
|
|
|
title: 'Test Habit',
|
|
|
|
subtitle: 'Test Subtitle',
|
|
|
|
icon: Icons.star,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(id, 1);
|
2024-06-16 17:12:28 +02:00
|
|
|
verify(mockToDoInterface.insert(
|
2024-05-30 14:28:21 +02:00
|
|
|
title: 'Test Habit',
|
|
|
|
subtitle: 'Test Subtitle',
|
|
|
|
icon: Icons.star))
|
|
|
|
.called(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('update updates a habit and returns the number of affected rows', () async {
|
|
|
|
final mockToDoInterface = MockToDoInterface();
|
|
|
|
when(mockToDoInterface.update(
|
|
|
|
id: 1, title: 'Updated Habit', subtitle: 'Updated Subtitle', icon: Icons.star))
|
|
|
|
.thenAnswer((_) async => 1);
|
|
|
|
|
|
|
|
final rows = await mockToDoInterface.update(
|
|
|
|
id: 1,
|
|
|
|
title: 'Updated Habit',
|
|
|
|
subtitle: 'Updated Subtitle',
|
|
|
|
icon: Icons.star,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(rows, 1);
|
|
|
|
verify(mockToDoInterface.update(
|
|
|
|
id: 1, title: 'Updated Habit', subtitle: 'Updated Subtitle', icon: Icons.star))
|
|
|
|
.called(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('delete deletes a habit', () async {
|
|
|
|
final mockToDoInterface = MockToDoInterface();
|
|
|
|
when(mockToDoInterface.delete(1)).thenAnswer((_) async {});
|
|
|
|
|
|
|
|
await mockToDoInterface.delete(1);
|
|
|
|
|
|
|
|
verify(mockToDoInterface.delete(1)).called(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('updateCompletionStatus updates a habit completion status and returns the number of affected rows', () async {
|
|
|
|
final mockToDoInterface = MockToDoInterface();
|
|
|
|
when(mockToDoInterface.updateCompletionStatus(1, true)).thenAnswer((_) async => 1);
|
|
|
|
|
|
|
|
final rows = await mockToDoInterface.updateCompletionStatus(1, true);
|
|
|
|
|
|
|
|
expect(rows, 1);
|
|
|
|
verify(mockToDoInterface.updateCompletionStatus(1, true)).called(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|