Skip to content

Commit

Permalink
add testcase doc comment
Browse files Browse the repository at this point in the history
  • Loading branch information
rex committed Oct 25, 2024
1 parent 2f7696f commit 73d594d
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 61 deletions.
14 changes: 12 additions & 2 deletions mobile-client/ToDoListTests/AddToDoReducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,67 @@ import Testing
@testable import ToDoList

struct AddToDoReducerTests {
/// Tests the successful saving of a new ToDo item.
@Test
func testSaveToDoSuccess() async {
// Arrange: Create a new ToDo item and a mock service.
let newTodo = ToDoItem(id: 0, title: "New Task", deadline: nil, status: "pending", tags: [], createdAt: "", updatedAt: "")

let mockService = MockToDoRemoteService()

// Create a test store with an initial state and dependencies.
let store = await TestStore(initialState: AddToDoReducer.State(todo: ToDoItem(id: 0, title: "", deadline: nil, status: "", tags: [], createdAt: "", updatedAt: ""))) {
AddToDoReducer()
} withDependencies: {
$0.toDoService = mockService
}

// Act: Set the title and status of the ToDo item.
await store.send(.setTitle("New Task")) {
$0.todo.title = "New Task"
}
await store.send(.setStatus("pending")) {
$0.todo.status = "pending"
}

// Trigger the save action.
await store.send(.saveButtonTapped) {
$0.isSaving = true
}

// Assert: Receive the success response and check the state.
await store.receive(.saveResponse(.success(newTodo))) {
$0.isSaving = false
}
}

/// Tests the failure case when saving a new ToDo item.
@Test
func testSaveToDoFailure() async {
// Set up mock remote service
// Arrange: Set up a mock remote service that simulates an error.
let mockService = MockToDoRemoteService()
mockService.error = NSError(domain: "Fail", code: 0)

// Create a test store with an initial state and dependencies.
let store = await TestStore(initialState: AddToDoReducer.State(todo: ToDoItem(id: 0, title: "", deadline: nil, status: "", tags: [], createdAt: "", updatedAt: ""))) {
AddToDoReducer()
} withDependencies: {
$0.toDoService = mockService
}

// Act: Set the title and status of the ToDo item.
await store.send(.setTitle("New Task")) {
$0.todo.title = "New Task"
}
await store.send(.setStatus("pending")) {
$0.todo.status = "pending"
}

// Trigger the save action.
await store.send(.saveButtonTapped) {
$0.isSaving = true
}

// Assert: Receive the failure response and check the state for the error message.
await store.receive(.saveResponse(.failure(.networkError(mockService.error!)))) {
$0.isSaving = false
$0.saveError = "Network error: The operation couldn’t be completed. (Fail error 0.)"
Expand Down
117 changes: 68 additions & 49 deletions mobile-client/ToDoListTests/ToDoLocalServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,91 +6,100 @@ struct ToDoLocalServiceTests {
private var context: ModelContext
private var service: ToDoLocalService

/// Initializes a new instance of `ToDoLocalServiceTests`.
init() {
// Configure the model to be stored in memory only for testing purposes.
let config = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try! ModelContainer(for: ToDoItemData.self, configurations: config)
context = ModelContext(container)
service = ToDoLocalService(context: context)
}

/// Tests saving and fetching ToDo items.
@Test
func testSaveAndFetchTodos() {
let todo =
ToDoItemData(id: 0,
title: "Grocery Shopping",
deadline: nil,
status: "todo",
tags: [Tag(name: "groceries"), Tag(name: "shopping")],
createdAt: "now",
updatedAt: "now")
// Arrange: Create a new ToDo item to save.
let todo = ToDoItemData(id: 0,
title: "Grocery Shopping",
deadline: nil,
status: "todo",
tags: [Tag(name: "groceries"), Tag(name: "shopping")],
createdAt: "now",
updatedAt: "now")
try! service.save(todo: todo)

// Act: Fetch the saved ToDo items.
let todos = try! service.fetchTodos()

// Assert: Check that the count and the title match expectations.
#expect(todos.count == 1)

let fetchedTodo = todos.first
#expect(fetchedTodo?.title == "Grocery Shopping")
}

/// Tests saving multiple ToDo items and their fetch order.
@Test
func testSaveAndFetchTodosFetchOrder() {
let todo1 =
ToDoItemData(id: 0,
title: "Prepare Presentation",
deadline: "2023-05-13T20:49:00.000Z",
status: "todo",
tags: [],
createdAt: "2023-12-01T12:22:30.356Z",
updatedAt: "2023-12-08T12:22:30.356Z")
let todo2 =
ToDoItemData(id: 1,
title: "Visit Doctor",
deadline: "2023-05-13T20:49:00.000+08:00",
status: "todo",
tags: [Tag(name: "health")],
createdAt: "2023-12-02T12:22:30.356Z",
updatedAt: "2023-12-10T12:22:30.356Z")
let todo3 =
ToDoItemData(id: 2,
title: "Finish Book",
deadline: "2024-12-30T15:12:26.676Z",
status: "todo",
tags: [Tag(name: "reading")],
createdAt: "2023-12-03T12:22:30.356Z",
updatedAt: "2023-12-09T12:22:30.356Z")

let todo4 =
ToDoItemData(id: 3,
title: "Finish Book",
deadline: nil,
status: "todo",
tags: [Tag(name: "reading")],
createdAt: "2023-12-03T12:22:30.356Z",
updatedAt: "2023-12-09T12:22:30.356Z")
// Arrange: Create multiple ToDo items with varying attributes.
let todo1 = ToDoItemData(id: 0,
title: "Prepare Presentation",
deadline: "2023-05-13T20:49:00.000Z",
status: "todo",
tags: [],
createdAt: "2023-12-01T12:22:30.356Z",
updatedAt: "2023-12-08T12:22:30.356Z")
let todo2 = ToDoItemData(id: 1,
title: "Visit Doctor",
deadline: "2023-05-13T20:49:00.000+08:00",
status: "todo",
tags: [Tag(name: "health")],
createdAt: "2023-12-02T12:22:30.356Z",
updatedAt: "2023-12-10T12:22:30.356Z")
let todo3 = ToDoItemData(id: 2,
title: "Finish Book",
deadline: "2024-12-30T15:12:26.676Z",
status: "todo",
tags: [Tag(name: "reading")],
createdAt: "2023-12-03T12:22:30.356Z",
updatedAt: "2023-12-09T12:22:30.356Z")
let todo4 = ToDoItemData(id: 3,
title: "Finish Book",
deadline: nil,
status: "todo",
tags: [Tag(name: "reading")],
createdAt: "2023-12-03T12:22:30.356Z",
updatedAt: "2023-12-09T12:22:30.356Z")

// Act: Save all ToDo items.
try! service.save(todo: todo1)
try! service.save(todo: todo2)
try! service.save(todo: todo3)
try! service.save(todo: todo4)

// Assert: Fetch the items and check their order.
let todos = try! service.fetchTodos()
#expect(todos.count == 4)
#expect(todos.first!.id == 1)
#expect(todos[1].id == 0)
#expect(todos[2].id == 2)
#expect(todos[3].id == 3)
#expect(todos.first!.id == 1) // Visit Doctor
#expect(todos[1].id == 0) // Prepare Presentation
#expect(todos[2].id == 2) // Finish Book
#expect(todos[3].id == 3) // Finish Book
}

/// Tests updating an existing ToDo item.
@Test
func testUpdateTodo() {
// Arrange: Create and save a ToDo item.
let todo = ToDoItemData(id: 1,
title: "Draft Report",
deadline: nil,
status: "todo",
tags: [Tag(name: "work")],
createdAt: "now", updatedAt: "now")
createdAt: "now",
updatedAt: "now")
try! service.save(todo: todo)

// Act: Update the ToDo item.
let updatedTodo = ToDoItem(id: 1,
title: "Draft Final Report",
deadline: nil,
Expand All @@ -100,6 +109,7 @@ struct ToDoLocalServiceTests {
updatedAt: "now")
try! service.update(todoId: 1, newToDo: updatedTodo)

// Assert: Fetch the updated item and verify its properties.
let todos = try! service.fetchTodos()
let fetchedTodo = todos.first!

Expand All @@ -108,25 +118,31 @@ struct ToDoLocalServiceTests {
#expect(fetchedTodo.tags.count == 0)
}

/// Tests deleting a ToDo item.
@Test
func testDeleteTodo() async throws {
// Arrange: Create and save two ToDo items.
let todo1 = ToDoItemData(id: 0,
title: "Walk the Dog",
deadline: nil, status: "todo",
deadline: nil,
status: "todo",
tags: [Tag(name: "pets")],
createdAt: "now",
updatedAt: "now")
let todo2 = ToDoItemData(id: 1,
title: "Walk the Dog",
deadline: nil, status: "todo",
deadline: nil,
status: "todo",
tags: [Tag(name: "pets")],
createdAt: "now",
updatedAt: "now")
try service.save(todo: todo1)
try service.save(todo: todo2)

// Act: Delete the second ToDo item.
try service.delete(todo: todo2)

// Delete non-existent todo, expect no change
// Attempt to delete a non-existent ToDo item, expecting no change.
let todo3 = ToDoItemData(id: 2,
title: "Walk the Dog",
deadline: nil,
Expand All @@ -136,7 +152,10 @@ struct ToDoLocalServiceTests {
updatedAt: "now")
try service.delete(todo: todo3)

// Assert: Check the remaining ToDo items.
let todos = try service.fetchTodos()

// Only one ToDo should remain.
#expect(todos.count == 1)
}
}
Loading

0 comments on commit 73d594d

Please sign in to comment.