cpd/test/unit_testing/todo_interface_test.dart

149 lines
4.8 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(() {
// Initialisierung von sqflite_ffi für alle Plattformen
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),
];
// Mocking the fetchAll method
when(mockToDoInterface.fetchAll()).thenAnswer((_) async => mockHabits);
// Act
Future<List<Habit>> futureHabits = mockToDoInterface.fetchAll();
List<Habit> habits = await futureHabits;
// Assert
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();
// Mocking the fetchById method
when(mockToDoInterface.fetchById(1)).thenAnswer((_) async => habit);
// Act
Habit? fetchedHabit = await mockToDoInterface.fetchById(1);
// Assert
expect(fetchedHabit, isNotNull);
expect(fetchedHabit!.title, 'Test Habit');
verify(mockToDoInterface.fetchById(1)).called(1);
});
test('create inserts a habit and returns its id', () async {
final mockToDoInterface = MockToDoInterface();
// Mocking the create method
when(mockToDoInterface.create(
title: 'Test Habit',
subtitle: 'Test Subtitle',
icon: Icons.star))
.thenAnswer((_) async => 1);
// Act
final id = await mockToDoInterface.create(
title: 'Test Habit',
subtitle: 'Test Subtitle',
icon: Icons.star,
);
// Assert
expect(id, 1);
verify(mockToDoInterface.create(
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();
// Mocking the update method
when(mockToDoInterface.update(
id: 1, title: 'Updated Habit', subtitle: 'Updated Subtitle', icon: Icons.star))
.thenAnswer((_) async => 1);
// Act
final rows = await mockToDoInterface.update(
id: 1,
title: 'Updated Habit',
subtitle: 'Updated Subtitle',
icon: Icons.star,
);
// Assert
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();
// Mocking the delete method
when(mockToDoInterface.delete(1)).thenAnswer((_) async {});
// Act
await mockToDoInterface.delete(1);
// Assert
verify(mockToDoInterface.delete(1)).called(1);
});
test('updateCompletionStatus updates a habit completion status and returns the number of affected rows', () async {
final mockToDoInterface = MockToDoInterface();
// Mocking the updateCompletionStatus method
when(mockToDoInterface.updateCompletionStatus(1, true)).thenAnswer((_) async => 1);
// Act
final rows = await mockToDoInterface.updateCompletionStatus(1, true);
// Assert
expect(rows, 1);
verify(mockToDoInterface.updateCompletionStatus(1, true)).called(1);
});
});
}