feat: even more dart tests

This commit is contained in:
2025-01-08 20:33:59 +00:00
parent 4324d22bf8
commit 582b2aeef4
10 changed files with 716 additions and 6 deletions
@@ -0,0 +1,199 @@
import 'package:backend_dart/application/repository/project_repository_impl.dart';
import 'package:backend_dart/domain/entities/project.dart';
import 'package:backend_dart/domain/repository/project_repository.dart';
import 'package:fpdart/fpdart.dart';
import 'package:test/test.dart';
import '../mocks/mock_database.dart';
void main() {
late MockDatabase database;
late ProjectRepository projectRepository;
setUpAll(() {
database = MockDatabase();
projectRepository = ProjectRepositoryImpl(database);
});
setUp(() {
database.clear(); // Clear the database before each test
});
test('create project', () async {
final projectCreate = ProjectCreate(
userId: 'user123',
name: 'Test Project',
description: 'This is a test project',
clientId: 'client123',
);
final project = await projectRepository.create(projectCreate).run();
expect(project.isRight(), true, reason: 'Result should be right');
project.match(
(_) => fail('Result should be right'),
(project) {
expect(project.userId, projectCreate.userId,
reason: 'User ID should be ${projectCreate.userId}');
expect(project.name, projectCreate.name,
reason: 'Name should be ${projectCreate.name}');
expect(project.description, projectCreate.description,
reason: 'Description should be ${projectCreate.description}');
expect(project.clientId, projectCreate.clientId,
reason: 'Client ID should be ${projectCreate.clientId}');
},
);
});
test('find project by id', () async {
final projectCreate = ProjectCreate(
userId: 'user123',
name: 'Test Project',
description: 'This is a test project',
clientId: 'client123',
);
final createdProject = await projectRepository.create(projectCreate).run();
createdProject.match(
(_) => fail('Result should be right'),
(project) async {
final foundProject = await projectRepository.findById(project.id).run();
foundProject.match(
(_) => fail('Result should be right'),
(project) {
expect(project.name, projectCreate.name,
reason: 'Name should be ${projectCreate.name}');
},
);
},
);
});
test('find projects by user id', () async {
final project1 = ProjectCreate(
userId: 'user123',
name: 'Project 1',
description: 'Description 1',
clientId: 'client123',
);
final project2 = ProjectCreate(
userId: 'user123',
name: 'Project 2',
description: 'Description 2',
clientId: 'client123',
);
await projectRepository.create(project1).run();
await projectRepository.create(project2).run();
final projects = await projectRepository.findByUserId('user123').run();
projects.match(
(_) => fail('Result should be right'),
(projects) {
expect(projects.length, 2, reason: 'Should return two projects');
expect(
projects.map((p) => p.name).toList(),
containsAll([project1.name, project2.name]),
);
},
);
});
test('update project', () async {
final projectCreate = ProjectCreate(
userId: 'user123',
name: 'Old Name',
description: 'Old Description',
clientId: 'client123',
);
final createdProject = await projectRepository.create(projectCreate).run();
createdProject.match(
(_) => fail('Result should be right'),
(project) async {
final projectUpdate = ProjectUpdate(
id: project.id,
name: 'Updated Name',
description: 'Updated Description',
);
final updatedProject =
await projectRepository.update(projectUpdate).run();
updatedProject.match(
(_) => fail('Result should be right'),
(project) {
expect(project.name, 'Updated Name',
reason: 'Name should be Updated Name');
expect(project.description, 'Updated Description',
reason: 'Description should be Updated Description');
},
);
},
);
});
test('delete project', () async {
final projectCreate = ProjectCreate(
userId: 'user123',
name: 'Test Project',
description: 'This is a test project',
clientId: 'client123',
);
final createdProject = await projectRepository.create(projectCreate).run();
createdProject.match(
(_) => fail('Result should be right'),
(project) async {
final deletedProject = await projectRepository.delete(project.id).run();
deletedProject.match(
(_) => fail('Result should be right'),
(project) {
expect(project.id, createdProject.match((e) => null, identity)?.id,
reason: 'Deleted project ID should match created project ID');
},
);
final result = await projectRepository.findById(project.id).run();
expect(result.isLeft(), true, reason: 'Project should no longer exist');
},
);
});
test('find all projects', () async {
final project1 = ProjectCreate(
userId: 'user123',
name: 'Project 1',
description: 'Description 1',
clientId: 'client123',
);
final project2 = ProjectCreate(
userId: 'user456',
name: 'Project 2',
description: 'Description 2',
clientId: 'client123',
);
await projectRepository.create(project1).run();
await projectRepository.create(project2).run();
final projects = await projectRepository.findAll().run();
projects.match(
(_) => fail('Result should be right'),
(projects) {
expect(projects.length, 2, reason: 'Should return all projects');
expect(
projects.map((p) => p.name).toList(),
containsAll([project1.name, project2.name]),
);
},
);
});
}
@@ -0,0 +1,191 @@
import 'package:backend_dart/application/repository/project_task_repository_impl.dart';
import 'package:backend_dart/domain/entities/project_task.dart';
import 'package:backend_dart/domain/repository/project_task_repository.dart';
import 'package:fpdart/fpdart.dart';
import 'package:test/test.dart';
import '../mocks/mock_database.dart';
void main() {
late MockDatabase database;
late ProjectTaskRepository projectTaskRepository;
setUpAll(() {
database = MockDatabase();
projectTaskRepository = ProjectTaskRepositoryImpl(database);
});
setUp(() {
database.clear(); // Clear the database before each test
});
test('create project task', () async {
final projectTaskCreate = ProjectTaskCreate(
projectId: 'project123',
name: 'Test Task',
description: 'This is a test task',
);
final projectTask =
await projectTaskRepository.create(projectTaskCreate).run();
expect(projectTask.isRight(), true, reason: 'Result should be right');
projectTask.match(
(_) => fail('Result should be right'),
(task) {
expect(task.projectId, projectTaskCreate.projectId,
reason: 'Project ID should match');
expect(task.name, projectTaskCreate.name,
reason: 'Task name should match');
expect(task.description, projectTaskCreate.description,
reason: 'Description should match');
},
);
});
test('find project task by id', () async {
final projectTaskCreate = ProjectTaskCreate(
projectId: 'project123',
name: 'Test Task',
description: 'This is a test task',
);
final createdTask =
await projectTaskRepository.create(projectTaskCreate).run();
createdTask.match(
(_) => fail('Result should be right'),
(task) async {
final foundTask = await projectTaskRepository.findById(task.id).run();
foundTask.match(
(_) => fail('Result should be right'),
(task) {
expect(task.id, createdTask.match((e) => null, identity)?.id,
reason: 'Task ID should match');
},
);
},
);
});
test('find tasks by project id', () async {
final task1 = ProjectTaskCreate(
projectId: 'project123',
name: 'Task 1',
description: 'First task',
);
final task2 = ProjectTaskCreate(
projectId: 'project123',
name: 'Task 2',
description: 'Second task',
);
await projectTaskRepository.create(task1).run();
await projectTaskRepository.create(task2).run();
final tasks =
await projectTaskRepository.findByProjectId('project123').run();
tasks.match(
(_) => fail('Result should be right'),
(tasks) {
expect(tasks.length, 2, reason: 'Should return two tasks');
expect(
tasks.map((t) => t.name).toList(),
containsAll([task1.name, task2.name]),
);
},
);
});
test('update project task', () async {
final projectTaskCreate = ProjectTaskCreate(
projectId: 'project123',
name: 'Initial Task',
description: 'Initial Description',
);
final createdTask =
await projectTaskRepository.create(projectTaskCreate).run();
createdTask.match(
(_) => fail('Result should be right'),
(task) async {
final taskUpdate = ProjectTaskUpdate(
id: task.id,
name: 'Updated Task',
);
final updatedTask =
await projectTaskRepository.update(taskUpdate).run();
updatedTask.match(
(_) => fail('Result should be right'),
(task) {
expect(task.name, 'Updated Task',
reason: 'Task name should be updated');
},
);
},
);
});
test('delete project task', () async {
final projectTaskCreate = ProjectTaskCreate(
projectId: 'project123',
name: 'Task to delete',
description: 'Task description',
);
final createdTask =
await projectTaskRepository.create(projectTaskCreate).run();
createdTask.match(
(_) => fail('Result should be right'),
(task) async {
final deletedTask = await projectTaskRepository.delete(task.id).run();
deletedTask.match(
(_) => fail('Result should be right'),
(task) {
expect(task.id, createdTask.match((e) => null, identity)?.id,
reason: 'Deleted task ID should match created task ID');
},
);
final result = await projectTaskRepository.findById(task.id).run();
expect(result.isLeft(), true, reason: 'Task should no longer exist');
},
);
});
test('find all project tasks', () async {
final task1 = ProjectTaskCreate(
projectId: 'project123',
name: 'Task 1',
description: 'Description 1',
);
final task2 = ProjectTaskCreate(
projectId: 'project456',
name: 'Task 2',
description: 'Description 2',
);
await projectTaskRepository.create(task1).run();
await projectTaskRepository.create(task2).run();
final tasks = await projectTaskRepository.findAll().run();
tasks.match(
(_) => fail('Result should be right'),
(tasks) {
expect(tasks.length, 2, reason: 'Should return all tasks');
expect(
tasks.map((t) => t.name),
containsAll([task1.name, task2.name]),
);
},
);
});
}
@@ -0,0 +1,239 @@
import 'package:backend_dart/application/repository/time_entry_repository_impl.dart';
import 'package:backend_dart/domain/entities/time_entry.dart';
import 'package:backend_dart/domain/repository/time_entry_repository.dart';
import 'package:fpdart/fpdart.dart';
import 'package:test/test.dart';
import '../mocks/mock_database.dart';
void main() {
late MockDatabase database;
late TimeEntryRepository timeEntryRepository;
setUpAll(() {
database = MockDatabase();
timeEntryRepository = TimeEntryRepositoryImpl(database);
});
setUp(() {
database.clear(); // Clear the database before each test
});
test('create time entry', () async {
final timeEntryCreate = TimeEntryCreate(
userId: 'user123',
projectId: 'project123',
startTime: DateTime.now(),
endTime: DateTime.now().add(Duration(hours: 1)),
description: 'Working on project',
);
final timeEntry = await timeEntryRepository.create(timeEntryCreate).run();
expect(timeEntry.isRight(), true, reason: 'Result should be right');
timeEntry.match(
(_) => fail('Result should be right'),
(timeEntry) {
expect(timeEntry.userId, timeEntryCreate.userId,
reason: 'User ID should match');
expect(timeEntry.projectId, timeEntryCreate.projectId,
reason: 'Project ID should match');
expect(timeEntry.startTime, timeEntryCreate.startTime,
reason: 'Start time should match');
expect(timeEntry.endTime, timeEntryCreate.endTime,
reason: 'End time should match');
expect(timeEntry.description, timeEntryCreate.description,
reason: 'Description should match');
},
);
});
test('find time entry by id', () async {
final timeEntryCreate = TimeEntryCreate(
userId: 'user123',
projectId: 'project123',
startTime: DateTime.now(),
endTime: DateTime.now().add(Duration(hours: 1)),
description: 'Working on project',
);
final createdEntry =
await timeEntryRepository.create(timeEntryCreate).run();
createdEntry.match(
(_) => fail('Result should be right'),
(entry) async {
final foundEntry = await timeEntryRepository.findById(entry.id).run();
foundEntry.match(
(_) => fail('Result should be right'),
(entry) {
expect(entry.id, createdEntry.match((e) => null, identity)?.id,
reason: 'ID should match the created entry');
},
);
},
);
});
test('find time entries by user id', () async {
final timeEntry1 = TimeEntryCreate(
userId: 'user123',
projectId: 'project123',
startTime: DateTime.now(),
endTime: DateTime.now().add(Duration(hours: 1)),
description: 'Task 1',
);
final timeEntry2 = TimeEntryCreate(
userId: 'user123',
projectId: 'project456',
startTime: DateTime.now().add(Duration(hours: 2)),
endTime: DateTime.now().add(Duration(hours: 3)),
description: 'Task 2',
);
await timeEntryRepository.create(timeEntry1).run();
await timeEntryRepository.create(timeEntry2).run();
final entries = await timeEntryRepository.findByUserId('user123').run();
entries.match(
(_) => fail('Result should be right'),
(entries) {
expect(entries.length, 2, reason: 'Should return two entries');
expect(entries.map((e) => e.description),
containsAll([timeEntry1.description, timeEntry2.description]));
},
);
});
test('find time entries by project id', () async {
final timeEntry1 = TimeEntryCreate(
userId: 'user123',
projectId: 'project123',
startTime: DateTime.now(),
endTime: DateTime.now().add(Duration(hours: 1)),
description: 'Task 1',
);
final timeEntry2 = TimeEntryCreate(
userId: 'user456',
projectId: 'project123',
startTime: DateTime.now().add(Duration(hours: 2)),
endTime: DateTime.now().add(Duration(hours: 3)),
description: 'Task 2',
);
await timeEntryRepository.create(timeEntry1).run();
await timeEntryRepository.create(timeEntry2).run();
final entries =
await timeEntryRepository.findByProjectId('project123').run();
entries.match(
(_) => fail('Result should be right'),
(entries) {
expect(entries.length, 2, reason: 'Should return two entries');
expect(entries.map((e) => e.description),
containsAll([timeEntry1.description, timeEntry2.description]));
},
);
});
test('update time entry', () async {
final timeEntryCreate = TimeEntryCreate(
userId: 'user123',
projectId: 'project123',
startTime: DateTime.now(),
endTime: DateTime.now().add(Duration(hours: 1)),
description: 'Initial Task',
);
final createdEntry =
await timeEntryRepository.create(timeEntryCreate).run();
createdEntry.match(
(_) => fail('Result should be right'),
(entry) async {
final timeEntryUpdate = TimeEntryUpdate(
id: entry.id,
description: 'Updated Task',
);
final updatedEntry =
await timeEntryRepository.update(timeEntryUpdate).run();
updatedEntry.match(
(_) => fail('Result should be right'),
(entry) {
expect(entry.description, 'Updated Task',
reason: 'Description should be updated');
},
);
},
);
});
test('delete time entry', () async {
final timeEntryCreate = TimeEntryCreate(
userId: 'user123',
projectId: 'project123',
startTime: DateTime.now(),
endTime: DateTime.now().add(Duration(hours: 1)),
description: 'Task to delete',
);
final createdEntry =
await timeEntryRepository.create(timeEntryCreate).run();
createdEntry.match(
(_) => fail('Result should be right'),
(entry) async {
final deletedEntry = await timeEntryRepository.delete(entry.id).run();
deletedEntry.match(
(_) => fail('Result should be right'),
(entry) {
expect(entry.id, createdEntry.match((e) => null, identity)?.id,
reason: 'Deleted entry ID should match created entry ID');
},
);
final result = await timeEntryRepository.findById(entry.id).run();
expect(result.isLeft(), true, reason: 'Entry should no longer exist');
},
);
});
test('find all time entries', () async {
final timeEntry1 = TimeEntryCreate(
userId: 'user123',
projectId: 'project123',
startTime: DateTime.now(),
endTime: DateTime.now().add(Duration(hours: 1)),
description: 'Task 1',
);
final timeEntry2 = TimeEntryCreate(
userId: 'user456',
projectId: 'project456',
startTime: DateTime.now().add(Duration(hours: 2)),
endTime: DateTime.now().add(Duration(hours: 3)),
description: 'Task 2',
);
await timeEntryRepository.create(timeEntry1).run();
await timeEntryRepository.create(timeEntry2).run();
final entries = await timeEntryRepository.findAll().run();
entries.match(
(_) => fail('Result should be right'),
(entries) {
expect(entries.length, 2, reason: 'Should return all entries');
expect(
entries.map((e) => e.description),
containsAll([timeEntry1.description, timeEntry2.description]),
);
},
);
});
}
@@ -0,0 +1,202 @@
import 'package:backend_dart/application/repository/user_repository_impl.dart';
import 'package:backend_dart/common/secure_hash.dart';
import 'package:backend_dart/domain/entities/user.dart';
import 'package:backend_dart/domain/repository/user_repository.dart';
import 'package:fpdart/fpdart.dart';
import 'package:test/test.dart';
import '../mocks/mock_database.dart';
void main() {
late MockDatabase database;
late UserRepository userRepository;
setUpAll(() {
database = MockDatabase();
userRepository = UserRepositoryImpl(database);
});
setUp(() {
database.clear(); // Für jeden Test die Datenbank zurücksetzen
});
test('generateSecureHash', () {
final password = 'password';
final hash = generateSecureHash(password);
final expected =
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8';
expect(hash, expected, reason: 'Hash should be $expected');
});
test('create user', () async {
database.clear();
final userCreate = UserCreate(
email: 'user@example.com',
password: 'password',
name: 'John Doe1',
);
final user = await userRepository.create(userCreate).run();
expect(user.isRight(), true, reason: 'Result should be right');
user.match(
(_) => fail('Result should be right'),
(user) {
final hashedPassword = generateSecureHash(userCreate.password);
expect(user.email, userCreate.email,
reason: 'Email should be ${userCreate.email}');
expect(user.name, userCreate.name,
reason: 'Name should be ${userCreate.name}');
expect(user.passwordHash, hashedPassword,
reason: 'Password hash should be $hashedPassword');
},
);
});
test('find user by email', () async {
database.clear();
final userCreate = UserCreate(
email: 'user@example.com',
password: 'password',
name: 'John Doe2',
);
await userRepository.create(userCreate).run();
final foundUser = await userRepository.findByEmail(userCreate.email).run();
foundUser.match(
(_) => fail('Result should be right'),
(user) {
final hashedPassword = generateSecureHash(userCreate.password);
expect(user.email, userCreate.email,
reason: 'Email should be ${userCreate.email}');
expect(user.name, userCreate.name,
reason: 'Name should be ${userCreate.name}');
expect(user.passwordHash, hashedPassword,
reason: 'Password hash should be $hashedPassword');
},
);
});
test('find user by id', () async {
database.clear();
final userCreate = UserCreate(
email: 'user@example.com',
password: 'password',
name: 'John Doe3',
);
final createdUser = await userRepository.create(userCreate).run();
createdUser.match(
(_) => fail('Result should be right'),
(user) async {
final foundUser = await userRepository.findById(user.id).run();
foundUser.match(
(_) => fail('Result should be right'),
(user) {
final hashedPassword = generateSecureHash(userCreate.password);
expect(user.email, userCreate.email,
reason: 'Email should be ${userCreate.email}');
expect(user.name, userCreate.name,
reason: 'Name should be ${userCreate.name}');
expect(user.passwordHash, hashedPassword,
reason: 'Password hash should be $hashedPassword');
},
);
},
);
});
test('update user', () async {
database.clear();
final userCreate = UserCreate(
email: 'user@example.com',
password: 'password',
name: 'John Doe4',
);
final createdUser = await userRepository.create(userCreate).run();
createdUser.match(
(_) => fail('Result should be right'),
(user) async {
final userUpdate = UserUpdate(
id: user.id,
name: 'Jane Doe',
);
final updatedUser = await userRepository.update(userUpdate).run();
updatedUser.match(
(_) => fail('Result should be right'),
(user) {
expect(user.name, 'Jane Doe',
reason: 'Updated name should be Jane Doe');
},
);
},
);
});
test('delete user', () async {
database.clear();
final userCreate = UserCreate(
email: 'user@example.com',
password: 'password',
name: 'John Doe5',
);
final createdUser = await userRepository.create(userCreate).run();
createdUser.match(
(_) => fail('Result should be right'),
(user) async {
final deletedUser = await userRepository.delete(user.id).run();
deletedUser.match(
(_) => fail('Result should be right'),
(user) {
expect(user.id, createdUser.match((e) => null, identity)?.id,
reason: 'Deleted user ID should match created user ID');
},
);
final result = await userRepository.findById(user.id).run();
expect(result.isLeft(), true, reason: 'User should no longer exist');
},
);
});
test('find all users', () async {
database.clear();
final user1 = UserCreate(
email: 'user1@example.com',
password: 'password1',
name: 'User One',
);
final user2 = UserCreate(
email: 'user2@example.com',
password: 'password2',
name: 'User Two',
);
await userRepository.create(user1).run();
await userRepository.create(user2).run();
final result = await userRepository.findAll().run();
result.match(
(_) => fail('Result should be right'),
(users) {
print(users);
expect(users.length, 2, reason: 'There should be two users');
expect(users.map((u) => u.email).toList(),
containsAll([user1.email, user2.email]));
},
);
});
}