Skip to content

Commit

Permalink
Add option to run tests bu hitting the actual API
Browse files Browse the repository at this point in the history
  • Loading branch information
lincmba committed Jan 8, 2025
1 parent c52f396 commit 8a83431
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class GeminiModel() {
SafetySetting(HarmCategory.DANGEROUS_CONTENT, BlockThreshold.MEDIUM_AND_ABOVE),
),
)

/**
* Returns the configured GenerativeModel instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,47 @@ class SpeechToTextTest {

private lateinit var speechToText: SpeechToText
private lateinit var mockSpeechClient: SpeechClient
private val useRealApi = System.getProperty("USE_REAL_API")?.toBoolean() ?: false

@Before
fun setUp() {
// Initialize the SpeechToText instance and mock dependencies
mockSpeechClient = mockk(relaxed = true)
mockkStatic(SpeechClient::class)
every { SpeechClient.create() } returns mockSpeechClient

if (!useRealApi) {
mockSpeechClient = mockk(relaxed = true)
mockkStatic(SpeechClient::class)
every { SpeechClient.create() } returns mockSpeechClient
}
speechToText = SpeechToText()
}

@After
fun tearDown() {
unmockkAll()
if (!useRealApi) unmockkAll()
}

@Test
fun transcribeAudioToTextShouldReturnTemporaryFileWithTranscription() {
fun testTranscribeAudioToTextShouldReturnTemporaryFileWithTranscription() {
if (useRealApi) {
testTranscribeAudioToTextRealApi()
} else {
testTranscribeAudioToTextMock()
}
}

private fun testTranscribeAudioToTextRealApi() {
val testFile = File("src/test/resources/sample_audio.wav")
require(testFile.exists()) { "Test audio file not found at ${testFile.absolutePath}" }

val resultFile = speechToText.transcribeAudioToText(testFile)
assertNotNull(resultFile, "Result file should not be null")
assertTrue(resultFile.exists(), "Result file should exist")
println("Transcription result: ${resultFile.readText()}")
}

private fun testTranscribeAudioToTextMock() {
val mockAudioFile = mockk<File>(relaxed = true)
val mockAudioBytes = "test audio bytes".toByteArray()
every { mockAudioFile.readBytes() } returns mockAudioBytes

// Mock SpeechClient response
val mockRecognitionAudio =
RecognitionAudio.newBuilder()
.setContent(com.google.protobuf.ByteString.copyFrom(mockAudioBytes))
Expand All @@ -78,16 +96,9 @@ class SpeechToTextTest {
val mockResponse = RecognizeResponse.newBuilder().addResults(mockResult).build()
every { mockSpeechClient.recognize(mockConfig, mockRecognitionAudio) } returns mockResponse

var resultFile: File? = null
try {
resultFile = speechToText.transcribeAudioToText(mockAudioFile)

assertNotNull(resultFile, "Result file should not be null")
assertTrue(resultFile.exists(), "Result file should exist")
assertEquals("Hello World", resultFile.readText(), "Transcription content should match")
} finally {
resultFile?.delete()
assertTrue(resultFile?.exists() == false, "Result file should be deleted")
}
val resultFile = speechToText.transcribeAudioToText(mockAudioFile)
assertNotNull(resultFile, "Result file should not be null")
assertTrue(resultFile.exists(), "Result file should exist")
assertEquals("Hello World", resultFile.readText(), "Transcription content should match")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,44 @@ class TextToFormTest {

private lateinit var textToForm: TextToForm
private lateinit var mockGenerativeModel: GenerativeModel
private val useRealApi = System.getProperty("USE_REAL_API")?.toBoolean() ?: false

@Before
fun setUp() {
// Initialize the TextToForm instance and mock dependencies
mockGenerativeModel = mockk(relaxed = true)
textToForm = TextToForm(mockGenerativeModel)
if (!useRealApi) {
mockGenerativeModel = mockk(relaxed = true)
textToForm = TextToForm(mockGenerativeModel)
} else {
val geminiModel = GeminiModel()
textToForm = TextToForm(geminiModel.getModel())
}
}

@After
fun tearDown() {
unmockkAll()
if (!useRealApi) unmockkAll()
}

@Test
fun generateQuestionnaireResponseShouldReturnQuestionnaireResponse() = runTest {
fun testGenerateQuestionnaireResponseShouldReturnQuestionnaireResponse() = runTest {
if (useRealApi) {
testGenerateQuestionnaireResponseRealApi()
} else {
testGenerateQuestionnaireResponseMock()
}
}

private suspend fun testGenerateQuestionnaireResponseRealApi() {
val testFile = File("src/test/resources/sample_transcript.txt")
require(testFile.exists()) { "Test transcript file not found at ${testFile.absolutePath}" }
val mockQuestionnaire = Questionnaire()

val result = textToForm.generateQuestionnaireResponse(testFile, mockQuestionnaire)
assertNotNull(result, "QuestionnaireResponse should not be null")
println("Generated QuestionnaireResponse: ${result.id}")
}

private suspend fun testGenerateQuestionnaireResponseMock() {
val mockTranscriptFile = mockk<File>(relaxed = true)
val mockQuestionnaire = mockk<Questionnaire>(relaxed = true)
val mockResponseJson = "{'id': '123'}" // Mock JSON response
Expand All @@ -58,7 +81,6 @@ class TextToFormTest {
mockk { every { text } returns "```json\n$mockResponseJson\n```" }

val result = textToForm.generateQuestionnaireResponse(mockTranscriptFile, mockQuestionnaire)

assertNotNull(result, "QuestionnaireResponse should not be null")
assertEquals("123", result.id, "QuestionnaireResponse ID should match")
}
Expand Down

0 comments on commit 8a83431

Please sign in to comment.