Skip to content

Commit

Permalink
Cleanup tests (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas34 authored May 28, 2020
1 parent 351b183 commit 6f874bb
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 174 deletions.
45 changes: 18 additions & 27 deletions Tests/SwiftQueueTests/BackgroundTasksTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ import Dispatch
@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
class BackgroundTasksTest {

let serializers: [JobInfoSerializer] = [
V1Serializer(),
DecodableSerializer()
]

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
public func testBackgroundOperationShouldNotRun() {
let (type, job) = (UUID().uuidString, TestJob())
Expand All @@ -51,32 +46,28 @@ class BackgroundTasksTest {

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
public func testBuilderPeriodicLimited() throws {
for serializer in serializers {
let type = UUID().uuidString
let limited: Double = 123
let interval: Double = 12342
let executor = Executor.any

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).periodic(limit: .limited(limited), interval: interval, executor: .any))
XCTAssertEqual(jobInfo?.maxRun, Limit.limited(limited))
XCTAssertEqual(jobInfo?.interval, interval)
XCTAssertEqual(jobInfo?.executor, executor)
}
let type = UUID().uuidString
let limited: Double = 123
let interval: Double = 12342
let executor = Executor.any

let jobInfo = JobBuilder(type: type).periodic(limit: .limited(limited), interval: interval, executor: .any).info
XCTAssertEqual(jobInfo.maxRun, Limit.limited(limited))
XCTAssertEqual(jobInfo.interval, interval)
XCTAssertEqual(jobInfo.executor, executor)
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
public func testBuilderPeriodicBackground() throws {
for serializer in serializers {
let type = UUID().uuidString
let limited: Double = 123
let interval: Double = 12342
let executor = Executor.background

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).periodic(limit: .limited(limited), interval: interval, executor: .background))
XCTAssertEqual(jobInfo?.maxRun, Limit.limited(limited))
XCTAssertEqual(jobInfo?.interval, interval)
XCTAssertEqual(jobInfo?.executor, executor)
}
let type = UUID().uuidString
let limited: Double = 123
let interval: Double = 12342
let executor = Executor.background

let jobInfo = JobBuilder(type: type).periodic(limit: .limited(limited), interval: interval, executor: .background).info
XCTAssertEqual(jobInfo.maxRun, Limit.limited(limited))
XCTAssertEqual(jobInfo.interval, interval)
XCTAssertEqual(jobInfo.executor, executor)
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
Expand Down
11 changes: 11 additions & 0 deletions Tests/SwiftQueueTests/PersisterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ class PersisterTests: XCTestCase {
// for (_, task) in tasks {
// task.assertSingleCompletion()
// }
// }

// public func testBuilderWith() throws {
// for serializer in serializers {
// try assertUnicode(serializer, expected: UUID().uuidString)
// try assertUnicode(serializer, expected: "Hello world")
// try assertUnicode(serializer, expected: "Powerلُلُصّبُلُلصّبُررً ॣ ॣh ॣ ॣ冗")
// try assertUnicode(serializer, expected: "🏳0🌈")
// try assertUnicode(serializer, expected: "🤪🤯🧐")
// try assertUnicode(serializer, expected: "జ్ఞ‌ా")
// }
// }

func testCustomSerializer() {
Expand Down
201 changes: 69 additions & 132 deletions Tests/SwiftQueueTests/SwiftQueueBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,193 +28,130 @@ import Dispatch

class SwiftQueueBuilderTests: XCTestCase {

let serializers: [JobInfoSerializer] = [
V1Serializer(),
DecodableSerializer()
]

public func testBuilderJobType() throws {
for serializer in serializers {
let type = UUID().uuidString
let type = UUID().uuidString

let jobInfo = JobBuilder(type: type).info
XCTAssertEqual(jobInfo.type, type)

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type))
XCTAssertEqual(jobInfo?.type, type)
}
}

public func testBuilderSingleInstance() throws {
for serializer in serializers {
let type = UUID().uuidString
let uuid = UUID().uuidString

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).singleInstance(forId: uuid))
XCTAssertEqual(jobInfo?.uuid, uuid)
XCTAssertEqual(jobInfo?.override, false)
}
}
let type = UUID().uuidString
let uuid = UUID().uuidString

public func testBuilderSingleInstanceOverride() throws {
for serializer in serializers {
let type = UUID().uuidString
let uuid = UUID().uuidString
let jobInfo = JobBuilder(type: type).singleInstance(forId: uuid).info
XCTAssertEqual(jobInfo.uuid, uuid)
XCTAssertEqual(jobInfo.override, false)

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).singleInstance(forId: uuid, override: true))
XCTAssertEqual(jobInfo?.uuid, uuid)
XCTAssertEqual(jobInfo?.override, true)
}
}

public func testBuilderGroup() throws {
for serializer in serializers {
let type = UUID().uuidString
let groupName = UUID().uuidString

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).parallel(queueName: groupName))
XCTAssertEqual(jobInfo?.queueName, groupName)
}
}
public func testBuilderSingleInstanceOverride() throws {
let type = UUID().uuidString
let uuid = UUID().uuidString

public func testBuilderDelay() throws {
for serializer in serializers {
let type = UUID().uuidString
let delay: Double = 1234
let jobInfo = JobBuilder(type: type).singleInstance(forId: uuid, override: true).info
XCTAssertEqual(jobInfo.uuid, uuid)
XCTAssertEqual(jobInfo.override, true)

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).delay(time: delay))
XCTAssertEqual(jobInfo?.delay, delay)
}
}

public func testBuilderDeadlineCodable() throws {
public func testBuilderGroup() throws {
let type = UUID().uuidString
let deadline = Date(timeIntervalSinceNow: TimeInterval(30))
let groupName = UUID().uuidString

let jobInfo = try toJobInfo(DecodableSerializer(), type: type, JobBuilder(type: type).deadline(date: deadline))
XCTAssertEqual(jobInfo?.deadline, deadline)
let jobInfo = JobBuilder(type: type).parallel(queueName: groupName).info
XCTAssertEqual(jobInfo.queueName, groupName)
}

public func testBuilderDeadlineV1() throws {
public func testBuilderDelay() throws {
let type = UUID().uuidString
let deadline = Date(timeIntervalSinceNow: TimeInterval(30))
let delay: Double = 1234

let v1Serializer = V1Serializer()
let dateFormatter = v1Serializer.dateFormatter
let jobInfo = JobBuilder(type: type).delay(time: delay).info
XCTAssertEqual(jobInfo.delay, delay)

let jobInfo = try toJobInfo(v1Serializer, type: type, JobBuilder(type: type).deadline(date: deadline))
/// V1 have a precision loss
XCTAssertEqual(jobInfo?.deadline.map(dateFormatter.string), Optional(deadline).map(dateFormatter.string))
}

public func testBuilderPeriodicUnlimited() throws {
for serializer in serializers {
let type = UUID().uuidString
let interval: Double = 12341
let executor = Executor.foreground
let type = UUID().uuidString
let interval: Double = 12341
let executor = Executor.foreground

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).periodic(limit: .unlimited, interval: interval))
XCTAssertEqual(jobInfo?.maxRun, Limit.unlimited)
XCTAssertEqual(jobInfo?.interval, interval)
XCTAssertEqual(jobInfo?.executor, executor)
}
let jobInfo = JobBuilder(type: type).periodic(limit: .unlimited, interval: interval).info
XCTAssertEqual(jobInfo.maxRun, Limit.unlimited)
XCTAssertEqual(jobInfo.interval, interval)
XCTAssertEqual(jobInfo.executor, executor)
}

public func testBuilderInternetAny() throws {
for serializer in serializers {

let type = UUID().uuidString
let network: NetworkType = .any
let type = UUID().uuidString
let network: NetworkType = .any

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).internet(atLeast: network))
XCTAssertEqual(jobInfo?.requireNetwork, network)
}
let jobInfo = JobBuilder(type: type).internet(atLeast: network).info
XCTAssertEqual(jobInfo.requireNetwork, network)
}

public func testBuilderInternetCellular() throws {
for serializer in serializers {
let type = UUID().uuidString
let network: NetworkType = .cellular
let type = UUID().uuidString
let network: NetworkType = .cellular

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).internet(atLeast: network))
XCTAssertEqual(jobInfo?.requireNetwork, network)
}
let jobInfo = JobBuilder(type: type).internet(atLeast: network).info
XCTAssertEqual(jobInfo.requireNetwork, network)
}

public func testBuilderInternetWifi() throws {
for serializer in serializers {
let type = UUID().uuidString
let network: NetworkType = .wifi
let type = UUID().uuidString
let network: NetworkType = .wifi

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).internet(atLeast: network))
XCTAssertEqual(jobInfo?.requireNetwork, network)
}
let jobInfo = JobBuilder(type: type).internet(atLeast: network).info
XCTAssertEqual(jobInfo.requireNetwork, network)
}

public func testBuilderRetryUnlimited() throws {
for serializer in serializers {
let type = UUID().uuidString
let type = UUID().uuidString

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).retry(limit: .unlimited))
XCTAssertEqual(jobInfo?.retries, Limit.unlimited)
}
let jobInfo = JobBuilder(type: type).retry(limit: .unlimited).info
XCTAssertEqual(jobInfo.retries, Limit.unlimited)
}

public func testBuilderRetryLimited() throws {
for serializer in serializers {
let type = UUID().uuidString
let limited: Double = 123
let type = UUID().uuidString
let limited: Double = 123

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).retry(limit: .limited(limited)))
XCTAssertEqual(jobInfo?.retries, Limit.limited(limited))
}
let jobInfo = JobBuilder(type: type).retry(limit: .limited(limited)).info
XCTAssertEqual(jobInfo.retries, Limit.limited(limited))
}

public func testBuilderAddTag() throws {
for serializer in serializers {
let type = UUID().uuidString
let tag1 = UUID().uuidString
let tag2 = UUID().uuidString

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).addTag(tag: tag1).addTag(tag: tag2))
XCTAssertEqual(jobInfo?.tags.contains(tag1), true)
XCTAssertEqual(jobInfo?.tags.contains(tag2), true)
}
}
let type = UUID().uuidString
let tag1 = UUID().uuidString
let tag2 = UUID().uuidString

public func testBuilderWith() throws {
for serializer in serializers {
try assertUnicode(serializer, expected: UUID().uuidString)
try assertUnicode(serializer, expected: "Hello world")
try assertUnicode(serializer, expected: "Powerلُلُصّبُلُلصّبُررً ॣ ॣh ॣ ॣ冗")
try assertUnicode(serializer, expected: "🏳0🌈")
try assertUnicode(serializer, expected: "🤪🤯🧐")
try assertUnicode(serializer, expected: "జ్ఞ‌ా")
}
let jobInfo = JobBuilder(type: type).addTag(tag: tag1).addTag(tag: tag2).info
XCTAssertEqual(jobInfo.tags.contains(tag1), true)
XCTAssertEqual(jobInfo.tags.contains(tag2), true)
}

public func testBuilderWithFreeArgs() {
for serializer in serializers {
let type = UUID().uuidString
let params: [String: Any] = [UUID().uuidString: [UUID().uuidString: self]]
let type = UUID().uuidString
let params: [String: Any] = [UUID().uuidString: [UUID().uuidString: self]]

let creator = TestCreator([type: TestJob()])
let manager = SwiftQueueManagerBuilder(creator: creator)
.set(persister: NoSerializer.shared)
.set(serializer: serializer)
.build()
let creator = TestCreator([type: TestJob()])
let manager = SwiftQueueManagerBuilder(creator: creator)
.set(persister: NoSerializer.shared)
.build()

// No assert expected
// This is just to test if the serialization failed on self
JobBuilder(type: type).with(params: params).schedule(manager: manager)
}
// No assert expected
// This is just to test if the serialization failed on self
JobBuilder(type: type).with(params: params).schedule(manager: manager)
}

public func testBuilderRequireCharging() throws {
for serializer in serializers {

let type = UUID().uuidString
let type = UUID().uuidString

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).requireCharging())
XCTAssertEqual(jobInfo?.requireCharging, true)
}
let jobInfo = JobBuilder(type: type).requireCharging().info
XCTAssertEqual(jobInfo.requireCharging, true)
}

func testCopyBuilder() {
Expand All @@ -233,8 +170,8 @@ class SwiftQueueBuilderTests: XCTestCase {

let params: [String: Any] = [UUID().uuidString: expected]

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).with(params: params))
XCTAssertTrue(NSDictionary(dictionary: params).isEqual(to: jobInfo?.params), file: file, line: line)
let jobInfo = JobBuilder(type: type).with(params: params).info
XCTAssertTrue(NSDictionary(dictionary: params).isEqual(to: jobInfo.params), file: file, line: line)
}

}
15 changes: 0 additions & 15 deletions Tests/SwiftQueueTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,3 @@ extension JobBuilder {
}

}

func toJobInfo(_ serializer: JobInfoSerializer, type: String, _ builder: JobBuilder) throws -> JobInfo? {
let creator = TestCreator([type: TestJob()])

let persister = PersisterTracker(key: UUID().uuidString)

let manager = SwiftQueueManagerBuilder(creator: creator)
.set(persister: persister)
.set(serializer: serializer)
.build()

builder.persist().schedule(manager: manager)

return try serializer.deserialize(json: persister.putData[0])
}

0 comments on commit 6f874bb

Please sign in to comment.