-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2161c35
commit b73c4b3
Showing
5 changed files
with
174 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,9 +1,100 @@ | ||
package com.posthog | ||
|
||
import com.posthog.internal.PostHogBatchEvent | ||
import com.posthog.internal.PostHogSerializer | ||
import com.posthog.internal.PostHogThreadFactory | ||
import org.junit.Rule | ||
import org.junit.rules.TemporaryFolder | ||
import java.util.concurrent.Executors | ||
import kotlin.test.AfterTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
|
||
internal class PostHogTest { | ||
|
||
@get:Rule | ||
val tmpDir = TemporaryFolder() | ||
|
||
private val queueExecutor = Executors.newSingleThreadScheduledExecutor(PostHogThreadFactory("queueExecutor")) | ||
private val serializer = PostHogSerializer(PostHogConfig(apiKey)) | ||
|
||
fun getSut( | ||
host: String = "", | ||
storagePrefix: String = tmpDir.newFolder().absolutePath, | ||
optOut: Boolean = false, | ||
preloadFeatureFlags: Boolean = true, | ||
): PostHogInterface { | ||
val config = PostHogConfig(apiKey, host).apply { | ||
// for testing | ||
flushAt = 1 | ||
this.storagePrefix = storagePrefix | ||
this.optOut = optOut | ||
this.preloadFeatureFlags = preloadFeatureFlags | ||
} | ||
return PostHog.withInternal(config, queueExecutor) | ||
} | ||
|
||
@AfterTest | ||
fun `set down`() { | ||
tmpDir.root.deleteRecursively() | ||
} | ||
|
||
@Test | ||
fun `optOut is disabled by default`() { | ||
val sut = getSut() | ||
|
||
assertFalse(sut.isOptOut()) | ||
|
||
sut.close() | ||
} | ||
|
||
@Test | ||
fun `deserializes json to date`() { | ||
fun `optOut is enabled if given`() { | ||
val sut = getSut(optOut = true) | ||
|
||
assertTrue(sut.isOptOut()) | ||
|
||
sut.close() | ||
} | ||
|
||
@Test | ||
fun `captures an event`() { | ||
val http = mockHttp() | ||
val url = http.url("/") | ||
|
||
val sut = getSut(url.toString(), preloadFeatureFlags = false) | ||
|
||
sut.capture( | ||
event, | ||
distinctId, | ||
props, | ||
userProperties = userProps, | ||
userPropertiesSetOnce = userPropsOnce, | ||
groupProperties = groupProps, | ||
) | ||
|
||
queueExecutor.shutdownAndAwaitTermination() | ||
|
||
val request = http.takeRequest() | ||
|
||
assertEquals(1, http.requestCount) | ||
val content = request.body.unGzip() | ||
val batch = serializer.deserialize<PostHogBatchEvent>(content.reader()) | ||
|
||
assertEquals(apiKey, batch.apiKey) | ||
assertNotNull(batch.sentAt) | ||
|
||
val theEvent = batch.batch.first() | ||
assertEquals(event, theEvent.event) | ||
assertEquals(distinctId, theEvent.distinctId) | ||
assertNotNull(theEvent.timestamp) | ||
assertNotNull(theEvent.uuid) | ||
assertEquals("value", theEvent.properties!!["prop"] as String) | ||
assertEquals(userProps, theEvent.properties!!["\$set"]) | ||
assertEquals(userPropsOnce, theEvent.properties!!["\$set_once"]) | ||
assertEquals(groupProps, theEvent.properties!!["\$groups"]) | ||
} | ||
} |
This file contains 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