Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alyssa.Yu authored and Alyssa.Yu committed Dec 8, 2022
1 parent 7e6775c commit 806e1d2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
8 changes: 2 additions & 6 deletions Sources/Amplitude/Amplitude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,8 @@ public class Amplitude {

func onEnterForeground(timestamp: Int64) {
_inForeground = true


let dummySessionStartEvent = BaseEvent(timestamp: timestamp, sessionId: -1, eventType: Constants.AMP_SESSION_START_EVENT)
timeline.process(event: dummySessionStartEvent)


let dummySessionStartEvent = BaseEvent(timestamp: timestamp, sessionId: -1, eventType: Constants.AMP_SESSION_START_EVENT)
timeline.process(event: dummySessionStartEvent)
}

func onExitForeground() {
Expand Down
16 changes: 8 additions & 8 deletions Sources/Amplitude/Utilities/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ extension Amplitude {
}

public func startOrContinueSession(timestamp: Int64) -> Array<BaseEvent>? {
if (_sessionId >= 0) {
// if with in the same session extend the session and update the session time
if self.isWithinMinTimeBetweenSessions(timestamp: timestamp) == true {
self.refreshSessionTime(timestamp: timestamp)
return nil
}
if (_sessionId >= 0) {
// if with in the same session extend the session and update the session time
if self.isWithinMinTimeBetweenSessions(timestamp: timestamp) == true {
self.refreshSessionTime(timestamp: timestamp)
return nil
}
}

return startNewSession(timestamp: timestamp)
}
Expand All @@ -70,8 +70,8 @@ extension Amplitude {

if (self.configuration.trackingSessionEvents == true) {
let lastEventTime: Int64? = self.storage.read(key: .LAST_EVENT_TIME) ?? nil
let sessionStartEnd = BaseEvent(timestamp: lastEventTime, sessionId: _sessionId, eventType: Constants.AMP_SESSION_START_EVENT)
sessionEvents.append(sessionStartEnd)
let sessionStartEvent = BaseEvent(timestamp: lastEventTime, sessionId: _sessionId, eventType: Constants.AMP_SESSION_START_EVENT)
sessionEvents.append(sessionStartEvent)
}

return sessionEvents
Expand Down
39 changes: 39 additions & 0 deletions Tests/AmplitudeTests/AmplitudeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,43 @@ final class AmplitudeTests: XCTestCase {
XCTAssertEqual(lastEvent?.platform!.isEmpty, false)
XCTAssertEqual(lastEvent?.language!.isEmpty, false)
}

func testNewSessionStartEvent() {
let amplitude = Amplitude(configuration: configuration)
let sessionReader = SessionReaderPlugin()
amplitude.add(plugin: sessionReader)
let timestamp = Int64(NSDate().timeIntervalSince1970 * 1000)
amplitude.onEnterForeground(timestamp: timestamp)

let sessionEvents = sessionReader.sessionEvents
let sessionEvent = sessionEvents?[0]

XCTAssertEqual(sessionEvents?.count, 1)
XCTAssertEqual(sessionEvent?.eventType, Constants.AMP_SESSION_START_EVENT)
XCTAssertEqual(sessionEvent?.timestamp, timestamp)
XCTAssertNotNil(sessionEvent?.eventId)
}

func testSessionEventNotInTheSameSession() {
let previousSessionTimestamp = Int64(NSDate().timeIntervalSince1970 * 1000)
let amplitude = Amplitude(configuration: Configuration(apiKey: "testApiKey", minTimeBetweenSessionsMillis: 1))
amplitude._sessionId = previousSessionTimestamp
let sessionReader = SessionReaderPlugin()
amplitude.add(plugin: sessionReader)
let currentSessionTimestamp = Int64(NSDate().timeIntervalSince1970 * 1000)
amplitude.onEnterForeground(timestamp: currentSessionTimestamp)

let sessionEvents = sessionReader.sessionEvents
let sessionEndEvent = sessionEvents?[0]
let sessionStartEvent = sessionEvents?[1]

XCTAssertNotNil(sessionStartEvent)
XCTAssertNotNil(sessionEndEvent)
XCTAssertEqual(sessionStartEvent?.eventType, Constants.AMP_SESSION_START_EVENT)
XCTAssertEqual(sessionStartEvent?.sessionId, currentSessionTimestamp)
XCTAssertNotNil(sessionStartEvent?.eventId)
XCTAssertEqual(sessionEndEvent?.eventType, Constants.AMP_SESSION_END_EVENT)
XCTAssertEqual(sessionEndEvent?.sessionId, previousSessionTimestamp)
XCTAssertNotNil(sessionEndEvent?.eventId)
}
}
21 changes: 21 additions & 0 deletions Tests/AmplitudeTests/Supports/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ class OutputReaderPlugin: Plugin {
}
}

class SessionReaderPlugin: Plugin {
var type: PluginType
var amplitude: Amplitude?
var sessionEvents: Array<BaseEvent>? = Array()

init() {
self.type = .destination
}

func setup(amplitude: Amplitude) {
self.amplitude = amplitude
}

func execute(event: BaseEvent?) -> BaseEvent? {
guard let eventType = event?.eventType, (eventType == Constants.AMP_SESSION_START_EVENT || eventType == Constants.AMP_SESSION_END_EVENT) else { return event }
sessionEvents?.append(event!)
return event
}
}


class FakeInMemoryStorage: Storage {
typealias EventBlock = URL

Expand Down

0 comments on commit 806e1d2

Please sign in to comment.