Skip to content

Commit

Permalink
Improve unit tests reliability (#3430)
Browse files Browse the repository at this point in the history
<!--
Note: This checklist is a reminder of our shared engineering
expectations. Feel free to change it, although assigning a GitHub
reviewer and the items in bold are required.

⚠️ If you're an external contributor, please file an issue first before
working on a PR, as we can't guarantee that we will accept your changes
if they haven't been discussed ahead of time. Thanks!
-->

Task/Issue URL:
https://app.asana.com/0/414709148257752/1208526755021935/f
Tech Design URL:
CC:

**Description**:

Improve reliability of test suite.

<!--
If at any point it isn't actively being worked on/ready for
review/otherwise moving forward strongly consider closing it (or not
opening it in the first place). If you decide not to close it, use Draft
PR while work is still in progress or use `DO NOT MERGE` label to
clarify the PRs state and comment with more information.
-->

**Steps to test this PR**:

Validate unit test changes and whether refactoring had any impact on
actual code and production flows.

<!--
Before submitting a PR, please ensure you have tested the combinations
you expect the reviewer to test, then delete configurations you *know*
do not need explicit testing.

Using a simulator where a physical device is unavailable is acceptable.
-->

**Definition of Done (Internal Only)**:

* [ ] Does this PR satisfy our [Definition of
Done](https://app.asana.com/0/1202500774821704/1207634633537039/f)?


---
###### Internal references:
[Software Engineering
Expectations](https://app.asana.com/0/59792373528535/199064865822552)
[Technical Design
Template](https://app.asana.com/0/59792373528535/184709971311943)
  • Loading branch information
bwaresiak authored Oct 11, 2024
1 parent 698f13f commit 0fe6554
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 158 deletions.
35 changes: 20 additions & 15 deletions Core/DailyPixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//

import Foundation
import Persistence

/// A variant of pixel that is fired at most once per day.
///
Expand Down Expand Up @@ -52,6 +53,8 @@ public final class DailyPixel {
error: Swift.Error? = nil,
withAdditionalParameters params: [String: String] = [:],
includedParameters: [Pixel.QueryParameters] = [.appVersion],
pixelFiring: PixelFiring.Type = Pixel.self,
dailyPixelStore: KeyValueStoring = DailyPixel.storage,
onComplete: @escaping (Swift.Error?) -> Void = { _ in }) {
var key: String = pixel.name

Expand All @@ -61,13 +64,13 @@ public final class DailyPixel {
key.append(":\(createSortedStringOfValues(from: errorParams))")
}

if !hasBeenFiredToday(forKey: key, dailyPixelStorage: storage) {
Pixel.fire(pixel: pixel,
error: error,
includedParameters: includedParameters,
withAdditionalParameters: params,
onComplete: onComplete)
updatePixelLastFireDate(forKey: key)
if !hasBeenFiredToday(forKey: key, dailyPixelStore: dailyPixelStore) {
pixelFiring.fire(pixel: pixel,
error: error,
includedParameters: includedParameters,
withAdditionalParameters: params,
onComplete: onComplete)
updatePixelLastFireDate(forKey: key, dailyPixelStore: dailyPixelStore)
} else {
onComplete(Error.alreadyFired)
}
Expand All @@ -80,12 +83,14 @@ public final class DailyPixel {
error: Swift.Error? = nil,
withAdditionalParameters params: [String: String] = [:],
includedParameters: [Pixel.QueryParameters] = [.appVersion],
pixelFiring: PixelFiring.Type = Pixel.self,
dailyPixelStore: KeyValueStoring = DailyPixel.storage,
onDailyComplete: @escaping (Swift.Error?) -> Void = { _ in },
onCountComplete: @escaping (Swift.Error?) -> Void = { _ in }) {
let key: String = pixel.name

if !hasBeenFiredToday(forKey: key, dailyPixelStorage: storage) {
Pixel.fire(
if !hasBeenFiredToday(forKey: key, dailyPixelStore: dailyPixelStore) {
pixelFiring.fire(
pixelNamed: pixel.name + "_d",
withAdditionalParameters: params,
includedParameters: includedParameters,
Expand All @@ -94,25 +99,25 @@ public final class DailyPixel {
} else {
onDailyComplete(Error.alreadyFired)
}
updatePixelLastFireDate(forKey: key)
updatePixelLastFireDate(forKey: key, dailyPixelStore: dailyPixelStore)
var newParams = params
if let error {
newParams.appendErrorPixelParams(error: error)
}
Pixel.fire(
pixelFiring.fire(
pixelNamed: pixel.name + "_c",
withAdditionalParameters: newParams,
includedParameters: includedParameters,
onComplete: onCountComplete
)
}

private static func updatePixelLastFireDate(forKey key: String) {
storage.set(Date(), forKey: key)
private static func updatePixelLastFireDate(forKey key: String, dailyPixelStore: KeyValueStoring) {
dailyPixelStore.set(Date(), forKey: key)
}

private static func hasBeenFiredToday(forKey key: String, dailyPixelStorage: UserDefaults) -> Bool {
if let lastFireDate = dailyPixelStorage.object(forKey: key) as? Date {
private static func hasBeenFiredToday(forKey key: String, dailyPixelStore: KeyValueStoring) -> Bool {
if let lastFireDate = dailyPixelStore.object(forKey: key) as? Date {
return Date().isSameDay(lastFireDate)
}
return false
Expand Down
28 changes: 25 additions & 3 deletions Core/PixelFiring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,46 @@ public protocol PixelFiring {
includedParameters: [Pixel.QueryParameters],
onComplete: @escaping (Error?) -> Void)

static func fire(pixel: Pixel.Event,
error: Error?,
includedParameters: [Pixel.QueryParameters],
withAdditionalParameters params: [String: String],
onComplete: @escaping (Error?) -> Void)

static func fire(_ pixel: Pixel.Event,
withAdditionalParameters params: [String: String])

static func fire(pixelNamed pixelName: String,
withAdditionalParameters params: [String: String],
includedParameters: [Pixel.QueryParameters],
onComplete: @escaping (Error?) -> Void)
}

extension Pixel: PixelFiring {
public static func fire(_ pixel: Pixel.Event,
withAdditionalParameters params: [String: String],
includedParameters: [Pixel.QueryParameters],
onComplete: @escaping (Error?) -> Void) {
Pixel.fire(pixel: pixel,

Self.fire(pixel: pixel,
withAdditionalParameters: params,
includedParameters: includedParameters,
onComplete: onComplete)
}

public static func fire(_ pixel: Pixel.Event,
withAdditionalParameters params: [String: String]) {
Pixel.fire(pixel: pixel, withAdditionalParameters: params)
Self.fire(pixel: pixel, withAdditionalParameters: params)
}

public static func fire(pixelNamed pixelName: String,
withAdditionalParameters params: [String: String],
includedParameters: [Pixel.QueryParameters],
onComplete: @escaping (Error?) -> Void) {
Self.fire(pixelNamed: pixelName,
withAdditionalParameters: params,
allowedQueryReservedCharacters: nil,
includedParameters: includedParameters,
onComplete: onComplete)
}
}
10 changes: 5 additions & 5 deletions DuckDuckGoTests/AdAttributionPixelReporterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class AdAttributionPixelReporterTests: XCTestCase {

let result = await sut.reportAttributionIfNeeded()

XCTAssertEqual(PixelFiringMock.lastPixel, .appleAdAttribution)
XCTAssertEqual(PixelFiringMock.lastPixelName, Pixel.Event.appleAdAttribution.name)
XCTAssertTrue(result)
}

Expand All @@ -55,7 +55,7 @@ final class AdAttributionPixelReporterTests: XCTestCase {
await fetcherStorage.markAttributionReportSuccessful()
let result = await sut.reportAttributionIfNeeded()

XCTAssertNil(PixelFiringMock.lastPixel)
XCTAssertNil(PixelFiringMock.lastPixelName)
XCTAssertFalse(result)
}

Expand All @@ -65,7 +65,7 @@ final class AdAttributionPixelReporterTests: XCTestCase {

let result = await sut.reportAttributionIfNeeded()

XCTAssertEqual(PixelFiringMock.lastPixel?.name, "m_apple-ad-attribution")
XCTAssertEqual(PixelFiringMock.lastPixelName, "m_apple-ad-attribution")
XCTAssertTrue(result)
}

Expand Down Expand Up @@ -130,7 +130,7 @@ final class AdAttributionPixelReporterTests: XCTestCase {

let result = await sut.reportAttributionIfNeeded()

XCTAssertNil(PixelFiringMock.lastPixel)
XCTAssertNil(PixelFiringMock.lastPixelName)
XCTAssertTrue(fetcherStorage.wasAttributionReportSuccessful)
XCTAssertTrue(result)
}
Expand All @@ -141,7 +141,7 @@ final class AdAttributionPixelReporterTests: XCTestCase {

let result = await sut.reportAttributionIfNeeded()

XCTAssertNil(PixelFiringMock.lastPixel)
XCTAssertNil(PixelFiringMock.lastPixelName)
XCTAssertFalse(fetcherStorage.wasAttributionReportSuccessful)
XCTAssertFalse(result)
}
Expand Down
Loading

0 comments on commit 0fe6554

Please sign in to comment.