From a187d3cae879ba0b947f7d5ba10d038e38d68bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=ED=9A=A8=EC=A4=80?= Date: Fri, 29 Nov 2024 21:04:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20?= =?UTF-8?q?CRUD=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CoreDataBookCategoryStorageTests.swift | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 MemorialHouse/MHData/MHDataTests/CoreDataBookCategoryStorageTests.swift diff --git a/MemorialHouse/MHData/MHDataTests/CoreDataBookCategoryStorageTests.swift b/MemorialHouse/MHData/MHDataTests/CoreDataBookCategoryStorageTests.swift new file mode 100644 index 00000000..696a717d --- /dev/null +++ b/MemorialHouse/MHData/MHDataTests/CoreDataBookCategoryStorageTests.swift @@ -0,0 +1,98 @@ +import Testing +@testable import MHCore +@testable import MHData +@testable import MHDomain +@testable import MHFoundation + +struct CoreDataBookCategoryStorageTests { + // MARK: - Properties + private let sut = CoreDataBookCategoryStorage(coreDataStorage: MockCoreDataStorage()) + private static let bookCategories = [ + BookCategoryDTO(order: 0, name: "전체"), + BookCategoryDTO(order: 1, name: "즐겨찾기"), + BookCategoryDTO(order: 2, name: "가족"), + BookCategoryDTO(order: 3, name: "친구"), + ] + + init() async { + for bookCategory in CoreDataBookCategoryStorageTests.bookCategories { + _ = await sut.create(with: bookCategory) + } + } + + @Test func test코어데이터에_새로운_BookCategory_객체를_생성한다() async throws { + // Arrange + let newBookCategory = BookCategoryDTO(order: 4, name: "스티브 임과 함께") + + // Act + let result = await sut.create(with: newBookCategory) + let coreDataBookCategories = try await sut.fetch().get() + + // Assert + switch result { + case .success: + #expect(coreDataBookCategories.contains(where: { $0.name == newBookCategory.name })) + case .failure(let error): + #expect(false, "Create BookCover 실패: \(error.localizedDescription)") + } + } + + @Test func test코어데이터에_저장된_BookCategory_객체들을_모두_불러온다() async { + // Arrange + // Act + let result = await sut.fetch() + + // Assert + switch result { + case .success(let bookCategoriesResult): + bookCategoriesResult.forEach { bookCategory in + #expect(CoreDataBookCategoryStorageTests.bookCategories.contains(where: { + bookCategory.name == $0.name + })) + } + case .failure(let error): + #expect(false, "Fetch BookCover 실패: \(error.localizedDescription)") + } + } + + @Test func test코어데이터에서_특정_이름을_가진_BookCategory_데이터를_업데이트한다() async throws { + // Arrange + let oldBookCategory = CoreDataBookCategoryStorageTests.bookCategories[0] + let newBookCategory = BookCategoryDTO(order: 0, name: "전체(All)") + + // Act + let result = await sut.update(oldName: oldBookCategory.name, with: newBookCategory) + let coreDataBookCategories = try await sut.fetch().get() + + // Assert + switch result { + case .success: + let newBookCategories = coreDataBookCategories.first(where: { $0.name == oldBookCategory.name }) + #expect(newBookCategories?.name != oldBookCategory.name) + case .failure(let error): + #expect(false, "Update BookCover 실패: \(error.localizedDescription)") + } + } + + @Test( + arguments: [ + CoreDataBookCategoryStorageTests.bookCategories[0].name, + CoreDataBookCategoryStorageTests.bookCategories[1].name, + "친구" + ] + ) func test코어데이터에서_특정_이름을_가진_BookCategory_데이터를_삭제한다(_ categoryName: String) async throws { + // Arrange + // Act + let result = await sut.delete(with: categoryName) + let coreDataBookCategories = try await sut.fetch().get() + + // Assert + switch result { + case .success: + #expect(!coreDataBookCategories.contains(where: { $0.name == categoryName })) + case .failure(let error): + #expect(error == MHDataError.findEntityFailure && !coreDataBookCategories.contains(where: { $0.name == categoryName })) + } + } +} +