-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Leonardo Colman Lopes <[email protected]>
- Loading branch information
Showing
5 changed files
with
91 additions
and
66 deletions.
There are no files selected for viewing
71 changes: 15 additions & 56 deletions
71
app/src/androidTest/kotlin/br/com/colman/petals/use/io/output/FileWriterTest.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 |
---|---|---|
@@ -1,73 +1,32 @@ | ||
package br.com.colman.petals.use.io.output | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import android.content.Context | ||
import androidx.test.core.app.ApplicationProvider | ||
import br.com.colman.kotest.FunSpec | ||
import io.kotest.core.spec.IsolationMode.InstancePerTest | ||
import io.kotest.matchers.file.shouldExist | ||
import io.kotest.matchers.file.shouldNotExist | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.shouldNotBe | ||
import io.kotest.matchers.string.shouldEndWith | ||
import io.mockk.every | ||
import io.mockk.mockkStatic | ||
import io.mockk.unmockkStatic | ||
import java.io.File | ||
import java.time.LocalDate | ||
|
||
class FileWriterTest : FunSpec({ | ||
|
||
val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
val exportsDirectory = File(context.filesDir, "exports") | ||
val context: Context = ApplicationProvider.getApplicationContext() | ||
val target = FileWriter(context) | ||
val exportDir = File(context.filesDir, "exports") | ||
|
||
beforeEach { exportsDirectory.deleteRecursively() } | ||
|
||
test("Creates exports directory when it doesn't exist") { | ||
exportsDirectory.shouldNotExist() | ||
|
||
target.write(FakeContent1) | ||
|
||
exportsDirectory.shouldExist() | ||
test("should create export directory when using context constructor") { | ||
exportDir.deleteRecursively() | ||
FileWriter(context) | ||
exportDir.shouldExist() | ||
} | ||
|
||
test("Replaces an existing file") { | ||
val uri1 = target.write(FakeContent1) | ||
val uri2 = target.write(FakeContent2) | ||
test("should write content to file and generate correct file name") { | ||
val content = "Integration test data" | ||
val expectedFileName = "PetalsExport-${LocalDate.now()}.csv" | ||
val expectedFile = File(exportDir, expectedFileName) | ||
|
||
uri1 shouldBe uri2 | ||
target.write(content) | ||
|
||
val content = context.contentResolver.openInputStream(uri2)!! | ||
content.bufferedReader().readText() shouldBe FakeContent2 | ||
expectedFile.shouldExist() | ||
expectedFile.readText() shouldBe content | ||
} | ||
|
||
test("Uses PetalsExport-date.csv as the default file name") { | ||
val uri = target.write(FakeContent1) | ||
|
||
uri.path shouldEndWith "PetalsExport-${LocalDate.now()}.csv" | ||
} | ||
|
||
test("Generates different file names on different dates") { | ||
val fixedDate = LocalDate.of(2024, 2, 9) | ||
mockkStatic(LocalDate::class) | ||
every { LocalDate.now() } returns fixedDate | ||
|
||
val uri1 = target.write(FakeContent1) | ||
|
||
val newDate = fixedDate.plusDays(1) | ||
every { LocalDate.now() } returns newDate | ||
|
||
val uri2 = target.write(FakeContent2) | ||
|
||
uri1 shouldNotBe uri2 | ||
|
||
uri1.path shouldEndWith "PetalsExport-$fixedDate.csv" | ||
uri2.path shouldEndWith "PetalsExport-$newDate.csv" | ||
|
||
unmockkStatic(LocalDate::class) | ||
} | ||
|
||
isolationMode = InstancePerTest | ||
}) | ||
|
||
private const val FakeContent1 = "abc" | ||
private const val FakeContent2 = "def" |
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
56 changes: 56 additions & 0 deletions
56
app/src/test/kotlin/br/com/colman/petals/use/io/output/FileWriterTest.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,56 @@ | ||
package br.com.colman.petals.use.io.output | ||
|
||
import android.net.Uri | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.engine.spec.tempdir | ||
import io.kotest.matchers.file.shouldExist | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import java.io.File | ||
import java.time.LocalDate | ||
|
||
class FileWriterTest : FunSpec({ | ||
|
||
val exportDir = tempdir() | ||
val fileToUri = mockk<(File) -> Uri>() | ||
|
||
val target = FileWriter(exportDir, fileToUri) | ||
|
||
test("should create export directory if it does not exist") { | ||
exportDir.deleteRecursively() | ||
|
||
FileWriter(exportDir, fileToUri) | ||
|
||
exportDir.shouldExist() | ||
} | ||
|
||
test("should generate correct file name and write to file") { | ||
val expectedFileName = "PetalsExport-${LocalDate.now()}.csv" | ||
val expectedFile = File(exportDir, expectedFileName) | ||
val expectedUri = mockk<Uri>() | ||
every { fileToUri(expectedFile) } returns expectedUri | ||
|
||
target.write("Test Content") | ||
|
||
expectedFile.shouldExist() | ||
expectedFile.name shouldBe expectedFileName | ||
} | ||
|
||
test("should write content to file and return its URI") { | ||
val content = "Sample data" | ||
val expectedFile = File(exportDir, "PetalsExport-${LocalDate.now()}.csv") | ||
val expectedUri = mockk<Uri>() | ||
|
||
every { fileToUri(expectedFile) } returns expectedUri | ||
|
||
val resultUri = target.write(content) | ||
|
||
expectedFile.shouldExist() | ||
expectedFile.readText() shouldBe content | ||
resultUri shouldBe expectedUri | ||
|
||
verify { fileToUri(expectedFile) } | ||
} | ||
}) |
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