Skip to content

Commit

Permalink
Change upvote from a String to a Long
Browse files Browse the repository at this point in the history
  • Loading branch information
tiembo authored and florina-muntenescu committed Aug 15, 2018
1 parent 5d21c87 commit d66892b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LoggedInUserDaoTest {
firstName = "Loggy",
lastName = "Loggerson",
portraitUrl = "www",
upvotes = listOf("1", "2", "3")
upvotes = listOf(1L, 2L, 3L)
)
}

Expand Down Expand Up @@ -88,7 +88,7 @@ class LoggedInUserDaoTest {
firstName = "Moggy",
lastName = "Moggerson",
portraitUrl = "www",
upvotes = listOf("4", "5", "6")
upvotes = listOf(4L, 5L, 6L)
)
loggedInUserDao.setLoggedInUser(newUser)

Expand Down
13 changes: 8 additions & 5 deletions core/src/main/java/io/plaidapp/core/data/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ import android.arch.persistence.room.TypeConverter
* Type converters to allow Room to reference complex data types.
*/
class Converters {
private val delimiter = ","

@TypeConverter fun csvToStringArray(csvString: String): List<String> {
@TypeConverter fun csvToLongArray(csvString: String): List<Long> {
return if (csvString.isEmpty()) {
emptyList()
} else {
csvString.split(delimiter)
csvString.split(CSV_DELIMITER).map { it.toLong() }
}
}

@TypeConverter fun stringListToCsv(stringList: List<String>): String {
return stringList.joinToString(delimiter)
@TypeConverter fun longListToCsv(longList: List<Long>): String {
return longList.joinToString(CSV_DELIMITER)
}

companion object {
private const val CSV_DELIMITER = ","
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ data class LoggedInUser(

@ColumnInfo(name = "upvotes")
@SerializedName("upvotes")
val upvotes: List<String>
val upvotes: List<Long>
)
24 changes: 12 additions & 12 deletions core/src/test/java/io/plaidapp/core/data/ConvertersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,62 +27,62 @@ class ConvertersTest {
val csv = "1"

// When the string is converted via the type converter
val actualStringList = Converters().csvToStringArray(csv)
val actualLongList = Converters().csvToLongArray(csv)

// Then it should return a list with one element
assertEquals(listOf("1"), actualStringList)
assertEquals(listOf(1L), actualLongList)
}

@Test fun csvToStringArray_multipleValues() {
// Given a non-empty CSV string with multiple values
val csv = "1,2,3"

// When the string is converted via the type converter
val actualStringList = Converters().csvToStringArray(csv)
val actualLongList = Converters().csvToLongArray(csv)

// Then it should return a list of the strings, split by the delimiter
assertEquals(listOf("1", "2", "3"), actualStringList)
assertEquals(listOf(1L, 2L, 3L), actualLongList)
}

@Test fun csvToStringArray_emptyString() {
// Given an empty string
val csv = ""

// When the string is converted via the type converter
val actualStringList = Converters().csvToStringArray(csv)
val actualLongList = Converters().csvToLongArray(csv)

// Then it should return an empty list
assertEquals(emptyList<String>(), actualStringList)
assertEquals(emptyList<String>(), actualLongList)
}

@Test fun stringListToCsv_oneValue() {
// Given a list with one element
val list = listOf("1")
val list = listOf(1L)

// When the list is converted via the type converter
val actualCsv = Converters().stringListToCsv(list)
val actualCsv = Converters().longListToCsv(list)

// Then it should return a CSV string with one value
assertEquals("1", actualCsv)
}

@Test fun stringListToCsv_multipleValues() {
// Given a list with multiple elements
val list = listOf("1", "2", "3")
val list = listOf(1L, 2L, 3L)

// When the list is converted via the type converter
val actualCsv = Converters().stringListToCsv(list)
val actualCsv = Converters().longListToCsv(list)

// Then it should return a CSV string with multiple values
assertEquals("1,2,3", actualCsv)
}

@Test fun stringListToCsv_emptyList() {
// Given an empty list
val list = emptyList<String>()
val list = emptyList<Long>()

// When the list is converted via the type converter
val actualCsv = Converters().stringListToCsv(list)
val actualCsv = Converters().longListToCsv(list)

// Then it should return an empty string
assertEquals("", actualCsv)
Expand Down

0 comments on commit d66892b

Please sign in to comment.