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
@Test | |
fun `correct test`() = runTest { | |
val argumentCaptor = slot<BlackBoxSDK.Callback>() | |
val sdk = mockk<BlackBoxSDK> { | |
every { init(capture(argumentCaptor)) } returns Unit | |
} | |
CallbackToFlow(sdk).flow.test { | |
argumentCaptor.captured.callback("hello world") |
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
//Terrible Refactor 1 | |
class CallbackToFlow(sdk: BlackBoxSDK): BlackBoxSDK.Callback { | |
private val _flow = MutableSharedFlow<String>(extraBufferCapacity = 1) | |
val flow: Flow<String> = _flow | |
override fun callback(something: String) { | |
_flow.tryEmit(something) | |
} |
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
@Test | |
fun `terrible test 1`() = runTest { | |
val context = RuntimeEnvironment.getApplication() | |
val sdk = BlackBoxSDK(context) | |
val subject = CallbackToFlow(sdk) | |
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager | |
//TODO: location manager has to do something I think? |
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
class CallbackToFlow(sdk: BlackBoxSDK) { | |
private val _flow = MutableSharedFlow<String>(extraBufferCapacity = 1) | |
val flow: Flow<String> = _flow | |
private val callback = object : BlackBoxSDK.Callback { | |
override fun callback(something: String) { | |
_flow.tryEmit(something) | |
} | |
} |
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
interface BlackBoxSDK { | |
fun init(callback: Callback) | |
interface Callback { | |
fun callback(something: String) | |
} | |
} |
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
@Test | |
fun `verify deleting favorite characters results in getIds flow responses`() = runTest { | |
val items = (0 until 3).map { id -> | |
FavoriteCharacter( | |
id = "id_$id", | |
name = "name_$id", | |
status = "status_$id", | |
species = "species_$id", | |
image = "image_$id", | |
) |
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
@Test | |
fun `verify inserting favorite characters results in getAll flow responses`() = runTest { | |
val items = (0 until 3).map { id -> | |
FavoriteCharacter( | |
id = "id_$id", | |
name = "name_$id", | |
status = "status_$id", | |
species = "species_$id", | |
image = "image_$id", | |
) |
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
@RunWith(RobolectricTestRunner::class) | |
class FavoriteCharacterDaoTest { | |
private val context: Context by lazy { RuntimeEnvironment.getApplication() } | |
private lateinit var subject: FavoriteCharactersDao | |
private lateinit var db: AppDatabase | |
@Before | |
fun setUp() { |
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
@Database( | |
entities = [ | |
FavoriteCharacter::class, | |
], | |
version = 1, | |
) | |
abstract class AppDatabase : RoomDatabase() { | |
abstract fun favoriteCharactersDao(): FavoriteCharactersDao | |
} |
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
@Dao | |
interface FavoriteCharactersDao { | |
@Query("SELECT * FROM favorite_character") | |
fun getAll(): Flow<List<FavoriteCharacter>> | |
@Query("SELECT id FROM favorite_character") | |
fun getIds(): Flow<List<String>> | |
@Insert | |
suspend fun insert(favoriteCharacter: FavoriteCharacter) |
NewerOlder