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 mockHabits = [ Habit(id: 1, title: 'Habit 1', subtitle: 'Description 1', icon: Icons.star, isComplete: false), Habit(id: 2, title: 'Habit 2', subtitle: 'Description 2', icon: Icons.star_border, isComplete: true), Habit(id: 3, title: 'Habit 3', subtitle: 'Description 3', icon: Icons.favorite, isComplete: true), ]; // Mocking the fetchAll method when(mockToDoInterface.fetchAll()).thenAnswer((_) async => mockHabits); // Act Future> futureHabits = mockToDoInterface.fetchAll(); List habits = await futureHabits; // Assert expect(habits, isA>()); 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', icon: Icons.star, 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); }); }); }