-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Telemetry can't serialize traits anymore (#477)
* fix: Telemetry can't serialize traits anymore * Add tests
- Loading branch information
1 parent
9666590
commit 67dc425
Showing
6 changed files
with
88 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ plugins { | |
|
||
dependencies { | ||
implementation(libs.analytics) | ||
implementation(libs.gson) | ||
} |
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
67 changes: 67 additions & 0 deletions
67
...lin/ee/carlrobert/codegpt/telemetry/core/service/segment/IdentifyTraitsPersistenceTest.kt
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package ee.carlrobert.codegpt.telemetry.core.service.segment | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.JsonSyntaxException | ||
import com.intellij.util.io.write | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.createTempFile | ||
import kotlin.io.path.readText | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertNull | ||
|
||
private const val NOT_JSON = "}NOT]:JSON{" | ||
|
||
class IdentifyTraitsPersistenceTest { | ||
private val gson = Gson() | ||
private val persistence = IdentifyTraitsPersistence.INSTANCE | ||
private val identifyTraits = IdentifyTraits("locale", "timezone", "os", "version", "distribution") | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
persistence.identifyTraits = null | ||
IdentifyTraitsPersistence.FILE = createTempFile() | ||
} | ||
|
||
@Test | ||
fun `get returns null when file does not exist`() { | ||
IdentifyTraitsPersistence.FILE = Path(" ") | ||
assertNull(persistence.get()) | ||
} | ||
|
||
@Test | ||
fun `get throws JsonSyntaxException when file contains malformed JSON`() { | ||
IdentifyTraitsPersistence.FILE.write(NOT_JSON) | ||
assertFailsWith<JsonSyntaxException> { | ||
persistence.get() | ||
} | ||
} | ||
|
||
@Test | ||
fun `set saves the event to the file overwriting it`() { | ||
IdentifyTraitsPersistence.FILE.write(NOT_JSON) | ||
persistence.set(identifyTraits) | ||
assertEquals(IdentifyTraitsPersistence.FILE.readText(), gson.toJson(identifyTraits)) | ||
} | ||
|
||
@Test | ||
fun `set saves the event to the file when file does not exist`() { | ||
persistence.set(identifyTraits) | ||
assertEquals(IdentifyTraitsPersistence.FILE.readText(), gson.toJson(identifyTraits)) | ||
} | ||
|
||
@Test | ||
fun `get returns the deserialized event`() { | ||
IdentifyTraitsPersistence.FILE.write(gson.toJson(identifyTraits)) | ||
assertEquals(identifyTraits, persistence.get()) | ||
} | ||
|
||
@Test | ||
fun `set throws IOException when file cannot be written and returns false`() { | ||
IdentifyTraitsPersistence.FILE = IdentifyTraitsPersistence.FILE.resolve(" xyz ") | ||
assertEquals(persistence.set(identifyTraits), false) | ||
} | ||
|
||
} |