-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
기록소 등록 화면에서 홈 화면으로 전환 간의 엔티티 설계 변경 & 테스트 코드 작성 #110
Changes from all commits
ccf8744
997eb78
71c7e35
681e092
69b83dd
9e23665
5adeea7
68c69f6
bb67f3a
8070e62
37df6b9
74e107d
e1730ce
5d9f83e
087a233
771bbda
16a1471
d3d77e6
9d9a807
ad4e810
d4574b3
c612cfb
2f291f1
1b2bf7a
a755670
77127df
b3b6b15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import MHFoundation | ||
import MHCore | ||
|
||
// TODO: 기록소 이름 변경 | ||
Check warning on line 4 in MemorialHouse/MHData/MHData/LocalStorage/MemorialHouseNameStorage.swift GitHub Actions / SwiftLint
|
||
public protocol MemorialHouseNameStorage: Sendable { | ||
func create(with memorialHouseName: String) async -> Result<Void, MHDataError> | ||
func fetch() async -> Result<String, MHDataError> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import MHCore | ||
import MHFoundation | ||
|
||
public struct UserDefaultsMemorialHouseNameStorage: MemorialHouseNameStorage { | ||
private nonisolated(unsafe) let userDefaults: UserDefaults | ||
|
||
public init(userDefaults: UserDefaults = .standard) { | ||
self.userDefaults = userDefaults | ||
} | ||
|
||
public func create(with memorialHouseName: String) async -> Result<Void, MHDataError> { | ||
userDefaults.set(memorialHouseName, forKey: Constant.houseNameUserDefaultKey) | ||
|
||
if userDefaults.string(forKey: Constant.houseNameUserDefaultKey) == memorialHouseName { | ||
return .success(()) | ||
} else { | ||
return .failure(.setUserDefaultFailure) | ||
} | ||
} | ||
|
||
public func fetch() async -> Result<String, MHDataError> { | ||
guard let memorialHouseName = userDefaults.string(forKey: Constant.houseNameUserDefaultKey) else { | ||
MHLogger.error("MemorialHouseName을 찾을 수 없습니다: \(Constant.houseNameUserDefaultKey)") | ||
return .failure(.noSuchEntity(key: Constant.houseNameUserDefaultKey)) | ||
} | ||
return .success(memorialHouseName) | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import MHCore | ||
import MHDomain | ||
|
||
public struct LocalMemorialHouseNameRepository: MemorialHouseNameRepository { | ||
private let storage: MemorialHouseNameStorage | ||
|
||
public init(storage: MemorialHouseNameStorage) { | ||
self.storage = storage | ||
} | ||
|
||
public func createMemorialHouseName(with name: String) async -> Result<Void, MHDataError> { | ||
return await storage.create(with: name) | ||
} | ||
|
||
public func fetchMemorialHouseName() async -> Result<String, MHDataError> { | ||
return await storage.fetch() | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: 해당 파일은 UserDefault에서 불러오기 및 삭제 등이 실행되는지에 대한 테스트코드인 것 같은데, repository와 storage를 하나로 합치고 둘 중 하나의 테스트만 존재하면 되지 않을까.. 라는 생각이 드네용 .. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Testing | ||
@testable import MHData | ||
@testable import MHCore | ||
@testable import MHFoundation | ||
|
||
struct UserDefaultsMemorialHouseNameStorageTest { | ||
@Test func test저장소에_기록소_이름을_저장한다() async throws { | ||
// Arrange | ||
let suiteName = UUID().uuidString | ||
let userDefaults = UserDefaults(suiteName: suiteName)! | ||
let storage = UserDefaultsMemorialHouseNameStorage(userDefaults: userDefaults) | ||
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: UserDefault를 UUID를 넣어 생성해주는 로직이 들어가게된 이유가 무엇인지 궁금합니다 .ᐟ.ᐟ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트 메소드마다 독립된 UserDefaults를 제공해주고 싶었는데, |
||
let testName = "테스트 기록소" | ||
|
||
// Act | ||
let result = await storage.create(with: testName) | ||
|
||
// Assert | ||
switch result { | ||
case .success: | ||
// 비동기 작업 후 일정 시간 대기 (필요 시) | ||
let savedName = userDefaults.string(forKey: Constant.houseNameUserDefaultKey) | ||
#expect(savedName == testName) | ||
case .failure: | ||
throw MHDataError.noSuchEntity(key: suiteName) | ||
} | ||
|
||
userDefaults.removePersistentDomain(forName: suiteName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 팁: class를 통해 init deinit에 테스트 전 / 테스트 후 코드를 자동화 가능합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오오 감사합니다 !! |
||
} | ||
|
||
@Test func test_fetch_저장소에서_기록소_이름을_불러온다() async throws { | ||
// Arrange | ||
let suiteName = UUID().uuidString | ||
let userDefaults = UserDefaults(suiteName: suiteName)! | ||
let storage = UserDefaultsMemorialHouseNameStorage(userDefaults: userDefaults) | ||
let testName = "테스트 기록소" | ||
userDefaults.set(testName, forKey: Constant.houseNameUserDefaultKey) | ||
|
||
// Act | ||
let result = await storage.fetch() | ||
|
||
// Assert | ||
switch result { | ||
case .success(let fetchedName): | ||
#expect(fetchedName == testName) | ||
case .failure: | ||
throw MHDataError.noSuchEntity(key: suiteName) | ||
} | ||
|
||
userDefaults.removePersistentDomain(forName: suiteName) | ||
} | ||
|
||
@Test func test_fetch_기록소_이름이_없을때_에러를_반환한다() async throws { | ||
// Arrange | ||
let suiteName = UUID().uuidString | ||
let userDefaults = UserDefaults(suiteName: suiteName)! | ||
userDefaults.removePersistentDomain(forName: suiteName) | ||
let storage = UserDefaultsMemorialHouseNameStorage(userDefaults: userDefaults) | ||
|
||
// Act | ||
let result = await storage.fetch() | ||
|
||
// Assert | ||
switch result { | ||
case .success: | ||
throw MHDataError.noSuchEntity(key: suiteName) | ||
case .failure(let error): | ||
#expect(error == .noSuchEntity(key: Constant.houseNameUserDefaultKey)) | ||
} | ||
|
||
userDefaults.removePersistentDomain(forName: suiteName) | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import MHCore | ||
|
||
public protocol MemorialHouseNameRepository: Sendable { | ||
func createMemorialHouseName(with name: String) async -> Result<Void, MHDataError> | ||
func fetchMemorialHouseName() async -> Result<String, MHDataError> | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: 개인적인 생각인데 MemorialHouseName은 UserDefault에만 저장될게 분명해서 굳이 한 번 추상화를 해줄 필요가 있나..? 라는 생각입니다. 그냥 Storage를 만들지 않고 바로 repository에서 처리해도 되지 않을까..? 라는 생각?? 입니당