129 lines
4.4 KiB
Dart
129 lines
4.4 KiB
Dart
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 = [
|
|
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),
|
|
];
|
|
|
|
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 {
|
|
final habit = Habit(id: 1, title: 'Test Habit', subtitle: 'Test Subtitle', iconCodePoint: Icons.star.codePoint,
|
|
iconFontFamily: Icons.star.fontFamily ?? 'MaterialIcons',isComplete: false);
|
|
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);
|
|
});
|
|
|
|
test('insert inserts a habit and returns its id', () async {
|
|
|
|
final mockToDoInterface = MockToDoInterface();
|
|
when(mockToDoInterface.insert(
|
|
title: 'Test Habit',
|
|
subtitle: 'Test Subtitle',
|
|
icon: Icons.star))
|
|
.thenAnswer((_) async => 1);
|
|
|
|
final id = await mockToDoInterface.insert(
|
|
title: 'Test Habit',
|
|
subtitle: 'Test Subtitle',
|
|
icon: Icons.star,
|
|
);
|
|
|
|
expect(id, 1);
|
|
verify(mockToDoInterface.insert(
|
|
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);
|
|
});
|
|
});
|
|
} |