Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vegaro committed Dec 4, 2024
1 parent 440c861 commit 9340e05
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 95 deletions.
3 changes: 2 additions & 1 deletion Tests/UnitTests/Paywalls/Events/PaywallEventStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ private extension StoredEvent {
return .init(event: event,
userID: UUID().uuidString,
feature: .paywalls,
appSessionID: UUID())!
appSessionID: UUID(),
eventDiscriminator: "impression")!
}

}
23 changes: 11 additions & 12 deletions Tests/UnitTests/Paywalls/Events/PaywallEventsBackendTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ class BackendPaywallEventTests: BaseBackendTests {

func testPostPaywallEventsWithOneEvent() throws {
let event = PaywallEvent.impression(Self.eventCreation1, Self.eventData1)
let storedEvent: StoredEvent = try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
let storedEvent: StoredEvent = try Self.createStoredEvent(from: event)

let error = waitUntilValue { completion in
self.internalAPI.postPaywallEvents(events: [storedEvent], completion: completion)
Expand All @@ -55,15 +52,9 @@ class BackendPaywallEventTests: BaseBackendTests {

func testPostPaywallEventsWithMultipleEvents() throws {
let event1 = PaywallEvent.impression(Self.eventCreation1, Self.eventData1)
let storedEvent1: StoredEvent = try XCTUnwrap(.init(event: event1,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
let storedEvent1: StoredEvent = try Self.createStoredEvent(from: event1)
let event2 = PaywallEvent.close(Self.eventCreation2, Self.eventData2)
let storedEvent2: StoredEvent = try XCTUnwrap(.init(event: event2,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
let storedEvent2: StoredEvent = try Self.createStoredEvent(from: event2)

let error = waitUntilValue { completion in
self.internalAPI.postPaywallEvents(events: [storedEvent1, storedEvent2],
Expand Down Expand Up @@ -108,4 +99,12 @@ private extension BackendPaywallEventTests {
darkMode: false
)

static func createStoredEvent(from event: PaywallEvent) throws -> StoredEvent {
return try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID(),
eventDiscriminator: "impression"))
}

}
63 changes: 19 additions & 44 deletions Tests/UnitTests/Paywalls/Events/PaywallEventsManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ class PaywallEventsManagerTests: TestCase {

let events = await self.store.storedEvents
expect(events) == [
try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
try createStoredEvent(from: event)
]
}

Expand All @@ -66,14 +63,8 @@ class PaywallEventsManagerTests: TestCase {

let events = await self.store.storedEvents
expect(events) == [
try XCTUnwrap(.init(event: event1,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID())),
try XCTUnwrap(.init(event: event2,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
try createStoredEvent(from: event1),
try createStoredEvent(from: event2)
]
}

Expand All @@ -92,10 +83,7 @@ class PaywallEventsManagerTests: TestCase {
expect(result) == 1

expect(self.api.invokedPostPaywallEvents) == true
expect(self.api.invokedPostPaywallEventsParameters) == [[try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))]]
expect(self.api.invokedPostPaywallEventsParameters) == [[try createStoredEvent(from: event)]]

await self.verifyEmptyStore()
}
Expand All @@ -112,25 +100,16 @@ class PaywallEventsManagerTests: TestCase {

expect(self.api.invokedPostPaywallEvents) == true
expect(self.api.invokedPostPaywallEventsParameters) == [
[try XCTUnwrap(.init(event: event1,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))],
[try XCTUnwrap(.init(event: event2,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))]
[try createStoredEvent(from: event1)],
[try createStoredEvent(from: event2)]
]

await self.verifyEmptyStore()
}

func testFlushOnlyOneEventPostsFirstOne() async throws {
let event = await self.storeRandomEvent()
let storedEvent: StoredEvent = try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
let storedEvent = try createStoredEvent(from: event)

_ = await self.storeRandomEvent()
_ = await self.storeRandomEvent()
Expand All @@ -148,10 +127,7 @@ class PaywallEventsManagerTests: TestCase {

func testFlushWithUnsuccessfulPostError() async throws {
let event = await self.storeRandomEvent()
let storedEvent: StoredEvent = try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
let storedEvent = try createStoredEvent(from: event)
let expectedError: NetworkError = .offlineConnection()

self.api.stubbedPostPaywallEventsCompletionResult = .networkError(expectedError)
Expand Down Expand Up @@ -209,16 +185,10 @@ class PaywallEventsManagerTests: TestCase {
}

expect(self.api.invokedPostPaywallEvents) == true
let expectedEvent: StoredEvent = try XCTUnwrap(.init(event: event1,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
let expectedEvent = try createStoredEvent(from: event1)
expect(self.api.invokedPostPaywallEventsParameters) == [[expectedEvent]]

await self.verifyEvents([try XCTUnwrap(.init(event: event2,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))])
await self.verifyEvents([try createStoredEvent(from: event2)])
}

#if swift(>=5.9)
Expand Down Expand Up @@ -259,10 +229,7 @@ class PaywallEventsManagerTests: TestCase {
expect(self.api.invokedPostPaywallEvents) == true
expect(self.api.invokedPostPaywallEventsParameters).to(haveCount(1))
expect(self.api.invokedPostPaywallEventsParameters.onlyElement) == [
try XCTUnwrap(.init(event: event1,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
try createStoredEvent(from: event1)
]

self.logger.verifyMessageWasLogged(
Expand Down Expand Up @@ -321,6 +288,14 @@ private extension PaywallEventsManagerTests {
expect(file: file, line: line, events) == expected
}

func createStoredEvent(from event: PaywallEvent) throws -> StoredEvent {
return try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID(),
eventDiscriminator: "impression"))
}

}

// MARK: - MockPaywallEventStore
Expand Down
42 changes: 23 additions & 19 deletions Tests/UnitTests/Paywalls/Events/PaywallEventsRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,23 @@ class PaywallEventsRequestTests: TestCase {

func testImpressionEvent() throws {
let event = PaywallEvent.impression(Self.eventCreationData, Self.eventData)
let storedEvent: StoredEvent = try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: Self.appSessionID))
let storedEvent = try Self.createStoredEvent(from: event)
let requestEvent: EventsRequest.PaywallEvent = try XCTUnwrap(.init(storedEvent: storedEvent))

assertSnapshot(matching: requestEvent, as: .formattedJson)
}

func testCancelEvent() throws {
let event = PaywallEvent.cancel(Self.eventCreationData, Self.eventData)
let storedEvent: StoredEvent = try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: Self.appSessionID))
let storedEvent = try Self.createStoredEvent(from: event)
let requestEvent: EventsRequest.PaywallEvent = try XCTUnwrap(.init(storedEvent: storedEvent))

assertSnapshot(matching: requestEvent, as: .formattedJson)
}

func testCloseEvent() throws {
let event = PaywallEvent.close(Self.eventCreationData,
Self.eventData)
let storedEvent: StoredEvent = try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: Self.appSessionID))
let event = PaywallEvent.close(Self.eventCreationData, Self.eventData)
let storedEvent = try Self.createStoredEvent(from: event)
let requestEvent: EventsRequest.PaywallEvent = try XCTUnwrap(.init(storedEvent: storedEvent))

assertSnapshot(matching: requestEvent, as: .formattedJson)
Expand All @@ -79,7 +69,8 @@ class PaywallEventsRequestTests: TestCase {
let storedEvent = try XCTUnwrap(StoredEvent(event: paywallEvent,
userID: expectedUserID,
feature: .paywalls,
appSessionID: Self.appSessionID))
appSessionID: Self.appSessionID,
eventDiscriminator: "impression"))
let serializedEvent = try StoredEventSerializer.encode(storedEvent)
let deserializedEvent = try StoredEventSerializer.decode(serializedEvent)
expect(deserializedEvent.userID) == expectedUserID
Expand All @@ -92,12 +83,25 @@ class PaywallEventsRequestTests: TestCase {

// MARK: -

private static let eventCreationData: PaywallEvent.CreationData = .init(
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
private extension PaywallEventsRequestTests {

static func createStoredEvent(from event: PaywallEvent) throws -> StoredEvent {
return try XCTUnwrap(.init(event: event,
userID: Self.userID,
feature: .paywalls,
appSessionID: Self.appSessionID,
eventDiscriminator: "impression"))
}

static let eventCreationData: PaywallEvent.CreationData = .init(
id: .init(uuidString: "72164C05-2BDC-4807-8918-A4105F727DEB")!,
date: .init(timeIntervalSince1970: 1694029328)
)

private static let eventData: PaywallEvent.Data = .init(
static let eventData: PaywallEvent.Data = .init(
offeringIdentifier: "offering",
paywallRevision: 0,
sessionID: .init(uuidString: "98CC0F1D-7665-4093-9624-1D7308FFF4DB")!,
Expand All @@ -106,8 +110,8 @@ class PaywallEventsRequestTests: TestCase {
darkMode: true
)

private static let userID = "Jack Shepard"
static let userID = "Jack Shepard"

private static let appSessionID = UUID(uuidString: "83164C05-2BDC-4807-8918-A4105F727DEB")
static let appSessionID = UUID(uuidString: "83164C05-2BDC-4807-8918-A4105F727DEB")

}
36 changes: 17 additions & 19 deletions Tests/UnitTests/Paywalls/Events/StoredEventSerializerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,20 @@ class StoredEventSerializerTests: TestCase {

func testEncodeImpressionEvent() throws {
let originalEvent = PaywallEvent.impression(.random(), .random())
let event: StoredEvent = try XCTUnwrap(.init(event: originalEvent,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))

let event = try Self.createStoredEvent(from: originalEvent)
expect(try event.encodeAndDecode()) == event
}

func testDecodeCancelEvent() throws {
let originalEvent = PaywallEvent.cancel(.random(), .random())
let event: StoredEvent = try XCTUnwrap(.init(event: originalEvent,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
let event = try Self.createStoredEvent(from: originalEvent)

expect(try event.encodeAndDecode()) == event
}

func testDecodeCloseEvent() throws {
let originalEvent = PaywallEvent.close(.random(), .random())
let event: StoredEvent = try XCTUnwrap(.init(event: originalEvent,
userID: Self.userID,
feature: .paywalls,
appSessionID: UUID()))
let event = try Self.createStoredEvent(from: originalEvent)

expect(try event.encodeAndDecode()) == event
}
Expand All @@ -71,10 +61,7 @@ class StoredEventSerializerTests: TestCase {
)
let paywallEvent = PaywallEvent.impression(paywallEventCreationData, paywallEventData)

let storedEvent = try XCTUnwrap(StoredEvent(event: paywallEvent,
userID: expectedUserID,
feature: .paywalls,
appSessionID: UUID()))
let storedEvent = try Self.createStoredEvent(from: paywallEvent, expectedUserID: expectedUserID)
let serializedEvent = try StoredEventSerializer.encode(storedEvent)
let deserializedEvent = try StoredEventSerializer.decode(serializedEvent)
expect(deserializedEvent.userID) == expectedUserID
Expand All @@ -85,9 +72,20 @@ class StoredEventSerializerTests: TestCase {
expect(decodedPaywallEvent) == paywallEvent
}

// MARK: -
}

@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
private extension StoredEventSerializerTests {

static let userID = UUID().uuidString

private static let userID = UUID().uuidString
static func createStoredEvent(from event: PaywallEvent, expectedUserID: String = userID) throws -> StoredEvent {
return try XCTUnwrap(.init(event: event,
userID: expectedUserID,
feature: .paywalls,
appSessionID: UUID(),
eventDiscriminator: "impression"))
}

}

Expand Down

0 comments on commit 9340e05

Please sign in to comment.