-
Notifications
You must be signed in to change notification settings - Fork 71
test: SDKE-535 Implement Test Coverage for iOS SDK - Part 1 #456
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
Merged
denischilik
merged 14 commits into
main
from
feat/SDKE-535-Implement-Test-Coverage-for-iOS-SDK
Dec 1, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4225754
Add integration test for logEvent with complex nested attributes
denischilik 19f6ab2
Improve extract_request_body.py to sanitize API keys
denischilik b2c02f8
- create new script for mapping
denischilik 7d5557a
- create new script for mapping
denischilik 4f5aed1
- update script
denischilik cac06ec
Update script to ignore new dynamic fields
denischilik 65f4cac
Add integration test for logEvent with custom attributes and flags
denischilik 1201c79
Add integration test for logScreen
denischilik 92746b1
Add integration test for commerce event purchase
denischilik 304bb50
test: Add integration test for Rokt selectPlacements
denischilik 6c2f51c
Replace Russian comments with English in main.swift
denischilik 995469a
Rename MyUploadListener to EventUploadWaiter
denischilik 31bd5fe
Rename listener variable to uploadWaiter
denischilik 51d2d27
Refactor: Extract integration tests into separate functions
denischilik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,142 @@ | ||
| import Foundation | ||
| import mParticle_Apple_SDK | ||
|
|
||
| // Listener for tracking upload events | ||
| class EventUploadWaiter: NSObject, MPListenerProtocol { | ||
| private var uploadCompletedSemaphore: DispatchSemaphore? | ||
| var mparticle = MParticle.sharedInstance() | ||
|
|
||
| @discardableResult | ||
| func wait(timeout: Int = 10) -> Bool { | ||
| mparticle.upload() | ||
| let semaphore = DispatchSemaphore(value: 0) | ||
| uploadCompletedSemaphore = semaphore | ||
|
|
||
| let timeoutTime = DispatchTime.now() + .seconds(timeout) | ||
| let result = semaphore.wait(timeout: timeoutTime) | ||
|
|
||
| uploadCompletedSemaphore = nil | ||
|
|
||
| return result == .success | ||
| } | ||
|
|
||
| func onNetworkRequestFinished(_ type: MPEndpoint, | ||
| url: String, | ||
| body: NSObject, | ||
| responseCode: Int) { | ||
| if type == .events { | ||
| uploadCompletedSemaphore?.signal() | ||
| } | ||
| } | ||
|
|
||
| func onNetworkRequestStarted(_ type: MPEndpoint, url: String, body: NSObject) {} | ||
| } | ||
|
|
||
| // Test 1: Simple Event | ||
| func testSimpleEvent(mparticle: MParticle, uploadWaiter: EventUploadWaiter) { | ||
| mparticle.logEvent("Simple Event Name", eventType: .other, eventInfo: ["SimpleKey": "SimpleValue"]) | ||
| uploadWaiter.wait() | ||
| } | ||
|
|
||
| // Test 2: Log Event with Custom Attributes and Custom Flags | ||
| // Based on ViewController.m logEvent method (lines 131-147) | ||
| func testEventWithCustomAttributesAndFlags(mparticle: MParticle, uploadWaiter: EventUploadWaiter) { | ||
| let event = MPEvent(name: "Event Name", type: .transaction) | ||
|
|
||
| // Use static date instead of Date() for deterministic testing | ||
| let staticDate = Date(timeIntervalSince1970: 1700000000) // Fixed timestamp: 2023-11-14 22:13:20 UTC | ||
|
|
||
| // Add custom attributes including string, number, date, and nested dictionary | ||
| event?.customAttributes = [ | ||
| "A_String_Key": "A String Value", | ||
| "A Number Key": 42, | ||
| "A Date Key": staticDate, | ||
| "test Dictionary": [ | ||
| "test1": "test", | ||
| "test2": 2, | ||
| "test3": staticDate | ||
| ] | ||
| ] | ||
|
|
||
| // Custom flags - sent to mParticle but not forwarded to other providers | ||
| event?.addCustomFlag("Top Secret", withKey: "Not_forwarded_to_providers") | ||
|
|
||
| // Log the event | ||
| if let event = event { | ||
| mparticle.logEvent(event) | ||
| } | ||
| uploadWaiter.wait() | ||
| } | ||
|
|
||
| // Test 3: Log Screen | ||
| // Based on ViewController.m logScreen method (lines 149-151) | ||
| func testLogScreen(mparticle: MParticle, uploadWaiter: EventUploadWaiter) { | ||
| mparticle.logScreen("Home Screen", eventInfo: nil) | ||
| uploadWaiter.wait() | ||
| } | ||
|
|
||
| // Test 4: Log Commerce Event with Product and Transaction | ||
| // Based on ViewController.m logCommerceEvent method (lines 153-180) | ||
| func testCommerceEvent(mparticle: MParticle, uploadWaiter: EventUploadWaiter) { | ||
| let product = MPProduct( | ||
| name: "Awesome Book", | ||
| sku: "1234567890", | ||
| quantity: NSNumber(value: 1), | ||
| price: NSNumber(value: 9.99) | ||
| ) | ||
| product.brand = "A Publisher" | ||
| product.category = "Fiction" | ||
| product.couponCode = "XYZ123" | ||
| product.position = 1 | ||
| product["custom key"] = "custom value" // Product may contain custom key/value pairs | ||
|
|
||
| // Create a commerce event with purchase action | ||
| let commerceEvent = MPCommerceEvent(action: .purchase, product: product) | ||
| commerceEvent.checkoutOptions = "Credit Card" | ||
| commerceEvent.screenName = "Timeless Books" | ||
| commerceEvent.checkoutStep = 4 | ||
| commerceEvent.customAttributes = ["an_extra_key": "an_extra_value"] // Commerce event may contain custom key/value pairs | ||
|
|
||
| // Create transaction attributes | ||
| let transactionAttributes = MPTransactionAttributes() | ||
| transactionAttributes.affiliation = "Book seller" | ||
| transactionAttributes.shipping = NSNumber(value: 1.23) | ||
| transactionAttributes.tax = NSNumber(value: 0.87) | ||
| transactionAttributes.revenue = NSNumber(value: 12.09) | ||
| transactionAttributes.transactionId = "zyx098" | ||
| commerceEvent.transactionAttributes = transactionAttributes | ||
|
|
||
| // Log the commerce event | ||
| mparticle.logEvent(commerceEvent) | ||
| uploadWaiter.wait() | ||
| } | ||
|
|
||
| // Test 5: Rokt Select Overlay Placement | ||
| // Based on ViewController.m selectOverlayPlacement method (lines 182-192) | ||
| // Tests Rokt SDK integration through mParticle for selecting placements with custom attributes | ||
| func testRoktSelectPlacement(mparticle: MParticle, uploadWaiter: EventUploadWaiter) { | ||
| let roktAttributes: [String: String] = [ | ||
| "email": "[email protected]", | ||
| "firstname": "Jenny", | ||
| "lastname": "Smith", | ||
| "sandbox": "true", | ||
| "mobile": "(555)867-5309" | ||
| ] | ||
|
|
||
| // Select Rokt placement with identifier and attributes | ||
| mparticle.rokt.selectPlacements("RoktLayout", attributes: roktAttributes) | ||
| uploadWaiter.wait() | ||
| } | ||
|
|
||
| var options = MParticleOptions( | ||
| key: "", // Put your key | ||
| secret: "" // Put your secret | ||
| key: "", | ||
| secret: "" | ||
| ) | ||
|
|
||
| var identityRequest = MPIdentityApiRequest.withEmptyUser() | ||
| identityRequest.email = "[email protected]"; | ||
| identityRequest.customerId = "123456"; | ||
| options.identifyRequest = identityRequest; | ||
| identityRequest.email = "[email protected]" | ||
| identityRequest.customerId = "123456" | ||
| options.identifyRequest = identityRequest | ||
|
|
||
| options.onIdentifyComplete = { apiResult, error in | ||
| if let apiResult { | ||
|
|
@@ -20,17 +146,25 @@ options.onIdentifyComplete = { apiResult, error in | |
| options.logLevel = .verbose | ||
|
|
||
| var networkOptions = MPNetworkOptions() | ||
| networkOptions.configHost = "127.0.0.1"; // config2.mparticle.com | ||
| networkOptions.eventsHost = "127.0.0.1"; // nativesdks.mparticle.com | ||
| networkOptions.identityHost = "127.0.0.1"; // identity.mparticle.com | ||
| networkOptions.configHost = "127.0.0.1" // config2.mparticle.com | ||
| networkOptions.eventsHost = "127.0.0.1" // nativesdks.mparticle.com | ||
| networkOptions.identityHost = "127.0.0.1" // identity.mparticle.com | ||
| networkOptions.pinningDisabled = true; | ||
|
|
||
| options.networkOptions = networkOptions; | ||
| options.networkOptions = networkOptions | ||
|
|
||
| // Register listener for tracking upload events | ||
| let uploadWaiter = EventUploadWaiter() | ||
| MPListenerController.sharedInstance().addSdkListener(uploadWaiter) | ||
|
|
||
| let mparticle = MParticle.sharedInstance() | ||
| mparticle.start(with: options) | ||
|
|
||
| sleep(1) | ||
|
|
||
| mparticle.logEvent("Simple Event Name", eventType: .other, eventInfo: ["SimpleKey": "SimpleValue"]) | ||
|
|
||
| sleep(7) | ||
| // Run tests | ||
| testSimpleEvent(mparticle: mparticle, uploadWaiter: uploadWaiter) | ||
| testEventWithCustomAttributesAndFlags(mparticle: mparticle, uploadWaiter: uploadWaiter) | ||
| testLogScreen(mparticle: mparticle, uploadWaiter: uploadWaiter) | ||
| testCommerceEvent(mparticle: mparticle, uploadWaiter: uploadWaiter) | ||
| testRoktSelectPlacement(mparticle: mparticle, uploadWaiter: uploadWaiter) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: Line numbers here and other comments with line numbers are highly likely to change separately to the comment
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.
Thanks will update