Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
forgotvas committed Dec 2, 2024
1 parent a17ffd7 commit 6dbca78
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 36 deletions.
13 changes: 13 additions & 0 deletions Authorization/AuthorizationTests/AuthorizationMock.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,12 @@ open class CoreAnalyticsMock: CoreAnalytics, Mock {
// MARK: - CorePersistenceProtocol

open class CorePersistenceProtocolMock: CorePersistenceProtocol, Mock {
open func deleteDownloadDataTasks(ids: [String]) {
addInvocation(.m_deleteDownloadDataTasks__ids_ids(Parameter<[String]>.value(`ids`)))
let perform = methodPerformValue(.m_deleteDownloadDataTasks__ids_ids(Parameter<[String]>.value(`ids`))) as? ([String]) -> Void
perform?(`ids`)
}

public init(sequencing sequencingPolicy: SequencingPolicy = .lastWrittenResolvedFirst, stubbing stubbingPolicy: StubbingPolicy = .wrap, file: StaticString = #file, line: UInt = #line) {
SwiftyMockyTestObserver.setup()
self.sequencingPolicy = sequencingPolicy
Expand Down Expand Up @@ -2973,6 +2979,7 @@ open class CorePersistenceProtocolMock: CorePersistenceProtocol, Mock {
case m_downloadDataTask__for_blockId(Parameter<String>)
case m_getDownloadDataTasks
case m_getDownloadDataTasksForCourse__courseId(Parameter<String>)
case m_deleteDownloadDataTasks__ids_ids(Parameter<[String]>)

static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Matcher.ComparisonResult {
switch (lhs, rhs) {
Expand Down Expand Up @@ -3045,6 +3052,10 @@ open class CorePersistenceProtocolMock: CorePersistenceProtocol, Mock {
var results: [Matcher.ParameterComparisonResult] = []
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsCourseid, rhs: rhsCourseid, with: matcher), lhsCourseid, rhsCourseid, "_ courseId"))
return Matcher.ComparisonResult(results)
case (.m_deleteDownloadDataTasks__ids_ids(let lhsIds), .m_deleteDownloadDataTasks__ids_ids(let rhsIds)):
var results: [Matcher.ParameterComparisonResult] = []
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsIds, rhs: rhsIds, with: matcher), lhsIds, rhsIds, "ids"))
return Matcher.ComparisonResult(results)
default: return .none
}
}
Expand All @@ -3068,6 +3079,7 @@ open class CorePersistenceProtocolMock: CorePersistenceProtocol, Mock {
case let .m_downloadDataTask__for_blockId(p0): return p0.intValue
case .m_getDownloadDataTasks: return 0
case let .m_getDownloadDataTasksForCourse__courseId(p0): return p0.intValue
case let .m_deleteDownloadDataTasks__ids_ids(p0): return p0.intValue
}
}
func assertionName() -> String {
Expand All @@ -3089,6 +3101,7 @@ open class CorePersistenceProtocolMock: CorePersistenceProtocol, Mock {
case .m_downloadDataTask__for_blockId: return ".downloadDataTask(for:)"
case .m_getDownloadDataTasks: return ".getDownloadDataTasks()"
case .m_getDownloadDataTasksForCourse__courseId: return ".getDownloadDataTasksForCourse(_:)"
case .m_deleteDownloadDataTasks__ids_ids: return ".deleteDownloadDataTasks(ids:)"
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions Core/Core/Data/Persistence/CorePersistenceProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public protocol CorePersistenceProtocol: Sendable {
func deleteAllProgress() async

func addToDownloadQueue(blocks: [CourseBlock], downloadQuality: DownloadQuality)
func nextBlockForDownloading() async -> DownloadDataTask?
func updateDownloadState(id: String, state: DownloadState, resumeData: Data?)
func saveDownloadDataTask(_ task: DownloadDataTask) async
func downloadDataTask(for blockId: String) async -> DownloadDataTask?
Expand All @@ -38,7 +37,6 @@ public final class CorePersistenceMock: CorePersistenceProtocol, @unchecked Send
public func publisher() -> AnyPublisher<Int, Never> { Just(0).eraseToAnyPublisher() }
public func addToDownloadQueue(blocks: [CourseBlock], downloadQuality: DownloadQuality) {}
public func addToDownloadQueue(tasks: [DownloadDataTask]) {}
public func nextBlockForDownloading() async -> DownloadDataTask? { nil }
public func updateDownloadState(id: String, state: DownloadState, resumeData: Data?) {}
public func downloadDataTask(for blockId: String) async -> DownloadDataTask? { nil }
public func saveOfflineProgress(progress: OfflineProgress) async {}
Expand Down
32 changes: 16 additions & 16 deletions Core/CoreTests/DownloadManager/DownloadManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ final class DownloadManagerTests: XCTestCase {
override func setUp() {
super.setUp()
persistence = CorePersistenceProtocolMock()
let mockTask = createMockDownloadTask()
storage = CoreStorageMock()
connectivity = ConnectivityProtocolMock()
Given(persistence, .getDownloadDataTasks(willReturn: [mockTask]))
Given(connectivity, .isInternetAvaliable(getter: true))
Given(connectivity, .internetReachableSubject(getter: .init(.reachable)))
Given(storage, .user(getter: .init(id: 19, username: "username", email: "email", name: "name")))
}

// MARK: - Test Add to Queue
Expand All @@ -46,8 +51,7 @@ final class DownloadManagerTests: XCTestCase {
let blocks = [createMockCourseBlock()]

// When
try await downloadManager.addToDownloadQueue(blocks: blocks)

try downloadManager.addToDownloadQueue(blocks: blocks)
// Then
Verify(persistence, 1, .addToDownloadQueue(blocks: .value(blocks), downloadQuality: .value(.auto)))
}
Expand All @@ -60,6 +64,7 @@ final class DownloadManagerTests: XCTestCase {
downloadQuality: .auto
)))
Given(connectivity, .isInternetAvaliable(getter: true))
Given(connectivity, .internetReachableSubject(getter: .init(.reachable)))
Given(connectivity, .isMobileData(getter: true))

let downloadManager = DownloadManager(
Expand All @@ -72,7 +77,7 @@ final class DownloadManagerTests: XCTestCase {

// When/Then
do {
try await downloadManager.addToDownloadQueue(blocks: blocks)
try downloadManager.addToDownloadQueue(blocks: blocks)
XCTFail("Should throw NoWiFiError")
} catch is NoWiFiError {
// Success
Expand Down Expand Up @@ -101,11 +106,7 @@ final class DownloadManagerTests: XCTestCase {
// When
try await downloadManager.resumeDownloading()

// Wait a bit for async operations to complete
await Task.yield()

// Then
Verify(persistence, 1, .nextBlockForDownloading())
XCTAssertEqual(downloadManager.currentDownloadTask?.id, mockTask.id)
}

Expand All @@ -116,7 +117,6 @@ final class DownloadManagerTests: XCTestCase {
let task = createMockDownloadTask()
Given(connectivity, .isInternetAvaliable(getter: true))
Given(connectivity, .isMobileData(getter: false))
Given(persistence, .deleteDownloadDataTask(id: .value(task.id), willProduce: { _ in }))

let downloadManager = DownloadManager(
persistence: persistence,
Expand All @@ -125,10 +125,10 @@ final class DownloadManagerTests: XCTestCase {
)

// When
try await downloadManager.cancelDownloading(task: task)
try downloadManager.cancelDownloading(task: task)

// Then
Verify(persistence, 1, .deleteDownloadDataTask(id: .value(task.id)))
Verify(persistence, 1, .deleteDownloadDataTasks(ids: .value([task.id])))
}

func testCancelDownloading_ForCourse_ShouldCancelAllTasksForCourse() async throws {
Expand All @@ -140,7 +140,6 @@ final class DownloadManagerTests: XCTestCase {
Given(connectivity, .isInternetAvaliable(getter: true))
Given(connectivity, .isMobileData(getter: false))
Given(persistence, .getDownloadDataTasksForCourse(.value(courseId), willReturn: tasks))
Given(persistence, .deleteDownloadDataTask(id: .value(task.id), willProduce: { _ in }))

let downloadManager = DownloadManager(
persistence: persistence,
Expand All @@ -153,17 +152,18 @@ final class DownloadManagerTests: XCTestCase {

// Then
Verify(persistence, 1, .getDownloadDataTasksForCourse(.value(courseId)))
Verify(persistence, 1, .deleteDownloadDataTask(id: .value(task.id)))
Verify(persistence, 1, .deleteDownloadDataTasks(ids: .value([task.id])))
}

// MARK: - Test File Management

func testDeleteFile_ShouldRemoveFileAndTask() async {
// Given
let block = createMockCourseBlock()
let task = createMockDownloadTask()
Given(connectivity, .isInternetAvaliable(getter: true))
Given(connectivity, .isMobileData(getter: false))
Given(persistence, .deleteDownloadDataTask(id: .value(block.id), willProduce: { _ in }))
Given(persistence, .getDownloadDataTasksForCourse(.value(block.courseId), willReturn: [task]))

let downloadManager = DownloadManager(
persistence: persistence,
Expand All @@ -172,10 +172,10 @@ final class DownloadManagerTests: XCTestCase {
)

// When
await downloadManager.deleteFile(blocks: [block])
await downloadManager.delete(blocks: [block], courseId: block.courseId)

// Then
Verify(persistence, 1, .deleteDownloadDataTask(id: .value(block.id)))
Verify(persistence, 1, .deleteDownloadDataTasks(ids: .value([task.id])))
}

func testFileUrl_ForFinishedTask_ShouldReturnCorrectUrl() async {
Expand Down Expand Up @@ -286,7 +286,7 @@ final class DownloadManagerTests: XCTestCase {
) -> DownloadDataTask {
DownloadDataTask(
id: id,
blockId: "block123",
blockId: "test123",
courseId: courseId,
userId: 1,
url: "https://test.com/video.mp4",
Expand Down
18 changes: 0 additions & 18 deletions OpenEdX/Data/CorePersistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,24 +226,6 @@ public final class CorePersistence: CorePersistenceProtocol {
}
}

public func nextBlockForDownloading() async -> DownloadDataTask? {
let userId = getUserId32()
return await container.performBackgroundTask { context in
let data = try? CorePersistenceHelper.fetchCDDownloadData(
predicate: .state(DownloadState.finished.rawValue),
fetchLimit: 1,
context: context,
userId: userId
)

guard let downloadData = data?.first else {
return nil
}

return DownloadDataTask(sourse: downloadData)
}
}

public func updateDownloadState(
id: String,
state: DownloadState,
Expand Down

0 comments on commit 6dbca78

Please sign in to comment.