-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUnitTest.cs
More file actions
101 lines (83 loc) · 2.6 KB
/
Copy pathUnitTest.cs
File metadata and controls
101 lines (83 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
namespace Api.Test
{
public class UnitTest
{
[Fact]
public void Test()
{
Assert.True(true);
}
}
public class TodoRepositoryTests
{
private static TodoRepository CreateRepo() => new();
[Fact]
public void GetAll_ReturnsEmpty_WhenNoTodosAdded()
{
var repo = CreateRepo();
Assert.Empty(repo.GetAll());
}
[Fact]
public void Add_ReturnsTodoWithGeneratedId()
{
var repo = CreateRepo();
var result = repo.Add(new CreateTodoRequest("Buy milk", "From the store", false));
Assert.NotNull(result.Id);
Assert.NotEmpty(result.Id);
Assert.Equal("Buy milk", result.Title);
Assert.Equal("From the store", result.Description);
Assert.False(result.Completed);
}
[Fact]
public void Add_MultipleTodos_AreAllReturned()
{
var repo = CreateRepo();
repo.Add(new CreateTodoRequest("First", "", false));
repo.Add(new CreateTodoRequest("Second", "", false));
Assert.Equal(2, repo.GetAll().Count());
}
[Fact]
public void Update_ExistingTodo_ReturnsUpdated()
{
var repo = CreateRepo();
var todo = repo.Add(new CreateTodoRequest("Original", "", false));
var updated = repo.Update(todo.Id, new UpdateTodoRequest("Updated", null, true));
Assert.NotNull(updated);
Assert.Equal("Updated", updated!.Title);
Assert.True(updated.Completed);
Assert.Equal("", updated.Description); // unchanged
}
[Fact]
public void Update_NonExistentId_ReturnsNull()
{
var repo = CreateRepo();
var result = repo.Update("missing-id", new UpdateTodoRequest("x", null, null));
Assert.Null(result);
}
[Fact]
public void Update_OnlyPatchesProvidedFields()
{
var repo = CreateRepo();
var todo = repo.Add(new CreateTodoRequest("Title", "Desc", false));
var updated = repo.Update(todo.Id, new UpdateTodoRequest(null, null, true));
Assert.Equal("Title", updated!.Title);
Assert.Equal("Desc", updated.Description);
Assert.True(updated.Completed);
}
[Fact]
public void Remove_ExistingTodo_ReturnsTrueAndRemoves()
{
var repo = CreateRepo();
var todo = repo.Add(new CreateTodoRequest("Delete me", "", false));
var removed = repo.Remove(todo.Id);
Assert.True(removed);
Assert.Empty(repo.GetAll());
}
[Fact]
public void Remove_NonExistentId_ReturnsFalse()
{
var repo = CreateRepo();
Assert.False(repo.Remove("missing-id"));
}
}
}