Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merge changes from v2024.3.4 #6570

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
entities.forEach { entity ->
val existing = if (listExists) {
query(
"\"$list\"",
quote(list),
"${EntitiesTable.COLUMN_ID} = ?",
arrayOf(entity.id)
).first { mapCursorRowToEntity(it, 0) }
Expand All @@ -96,7 +96,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}

update(
"\"$list\"",
quote(list),
contentValues,
"${EntitiesTable.COLUMN_ID} = ?",
arrayOf(entity.id)
Expand All @@ -114,7 +114,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}

insertOrThrow(
"\"$list\"",
quote(list),
null,
contentValues
)
Expand Down Expand Up @@ -180,12 +180,6 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}
}

override fun clear() {
databaseConnection.withConnection {
dropAllTablesFromDB(writableDatabase)
}
}

override fun addList(list: String) {
if (!listExists(list)) {
createList(list)
Expand All @@ -197,7 +191,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
databaseConnection.withConnection {
getLists().forEach {
writableDatabase.delete(
it,
quote(it),
"${EntitiesTable.COLUMN_ID} = ?",
arrayOf(id)
)
Expand Down Expand Up @@ -240,7 +234,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}

val propertyExists = databaseConnection.withConnection {
readableDatabase.doesColumnExist(list, EntitiesTable.getPropertyColumn(property))
readableDatabase.doesColumnExist(quote(list), EntitiesTable.getPropertyColumn(property))
}

return if (propertyExists) {
Expand Down Expand Up @@ -368,7 +362,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep

private fun updatePropertyColumns(list: String, entity: Entity) {
val columnNames = databaseConnection.withConnection {
readableDatabase.getColumnNames("\"$list\"")
readableDatabase.getColumnNames(quote(list))
}

val missingColumns = entity.properties
Expand All @@ -391,7 +385,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep

private fun addPropertiesToContentValues(contentValues: ContentValues, entity: Entity) {
entity.properties.forEach { (name, value) ->
contentValues.put("\"${EntitiesTable.getPropertyColumn(name)}\"", value)
contentValues.put(quote(EntitiesTable.getPropertyColumn(name)), value)
}
}

Expand Down Expand Up @@ -442,6 +436,8 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}
}

private fun quote(text: String) = "\"$text\""

companion object {
private const val DATABASE_VERSION = 2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,27 +314,6 @@ abstract class EntitiesRepositoryTest {
assertThat(repository.getEntities("favourite.wines")[0], sameEntityAs(wine))
}

@Test
fun `#clear deletes all entities`() {
val repository = buildSubject()

val wine = Entity.New("1", "Léoville Barton 2008")
val whisky = Entity.New("2", "Lagavulin 16")
repository.save("wines", wine)
repository.save("whiskys", whisky)

repository.clear()
assertThat(repository.getLists().size, equalTo(0))
assertThat(repository.getEntities("wines").size, equalTo(0))
assertThat(repository.getEntities("whiskys").size, equalTo(0))

repository.addList("wines")
assertThat(repository.getEntities("wines").size, equalTo(0))

repository.addList("whiskys")
assertThat(repository.getEntities("whiskys").size, equalTo(0))
}

@Test
fun `#save can save multiple entities`() {
val repository = buildSubject()
Expand Down Expand Up @@ -429,6 +408,27 @@ abstract class EntitiesRepositoryTest {
)
}

@Test
fun `#delete supports list names with dots and dashes`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")

repository.save("wines.x", leoville)
repository.save("wines-x", leoville)

repository.delete("1")

assertThat(
repository.getEntities("wines.x").isEmpty(),
equalTo(true)
)
assertThat(
repository.getEntities("wines-x").isEmpty(),
equalTo(true)
)
}

@Test
fun `#delete updates index values so that they are always in sequence and start at 0`() {
val repository = buildSubject()
Expand Down Expand Up @@ -517,7 +517,7 @@ abstract class EntitiesRepositoryTest {
}

@Test
fun `#getByAllByProperty returns entities with matching property value`() {
fun `#getAllByProperty returns entities with matching property value`() {
val repository = buildSubject()

val leoville = Entity.New(
Expand All @@ -542,7 +542,7 @@ abstract class EntitiesRepositoryTest {
}

@Test
fun `#getByAllByProperty returns entities without property when searching for empty string`() {
fun `#getAllByProperty returns entities without property when searching for empty string`() {
val repository = buildSubject()

val leoville = Entity.New(
Expand All @@ -566,7 +566,7 @@ abstract class EntitiesRepositoryTest {
}

@Test
fun `#getByAllByProperty returns entities when searching for empty string for property that doesn't exist`() {
fun `#getAllByProperty returns entities when searching for empty string for property that doesn't exist`() {
val repository = buildSubject()

val leoville = Entity.New(
Expand All @@ -580,7 +580,7 @@ abstract class EntitiesRepositoryTest {
}

@Test
fun `#getByAllByProperty returns empty list when searching for non empty string for property that doesn't exist`() {
fun `#getAllByProperty returns empty list when searching for non empty string for property that doesn't exist`() {
val repository = buildSubject()

val leoville = Entity.New(
Expand Down Expand Up @@ -639,6 +639,32 @@ abstract class EntitiesRepositoryTest {
assertThat(repository.getAllByProperty("wines", "vintage", "1983"), equalTo(emptyList()))
}

@Test
fun `#getAllByProperty supports list names with dots and dashes`() {
val repository = buildSubject()

val leoville = Entity.New(
"1",
"Léoville Barton 2008",
properties = listOf("vintage" to "2008")
)

repository.save("favourite-wines", leoville)
repository.save("favourite.wines", leoville)

var wines = repository.getEntities("favourite-wines")
assertThat(
repository.getAllByProperty("favourite-wines", "vintage", "2008"),
containsInAnyOrder(wines.first { it.id == "1" })
)

wines = repository.getEntities("favourite.wines")
assertThat(
repository.getAllByProperty("favourite.wines", "vintage", "2008"),
containsInAnyOrder(wines.first { it.id == "1" })
)
}

@Test
fun `#getCount returns 0 when a list is empty`() {
val repository = buildSubject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,4 @@ class EntitiesViewModel(

return result
}

fun clearAll() {
scheduler.immediate {
entitiesRepository.clear()
_lists.postValue(entitiesRepository.getLists().toList())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface EntitiesRepository {
)
fun getEntities(list: String): List<Entity.Saved>
fun getCount(list: String): Int
fun clear()
fun addList(list: String)
fun delete(id: String)
fun query(list: String, query: Query): List<Entity.Saved>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ class InMemEntitiesRepository : EntitiesRepository {
return getEntities(list).count()
}

override fun clear() {
entities.clear()
lists.clear()
}

override fun addList(list: String) {
lists.add(list)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,6 @@ private class MeasurableEntitiesRepository(private val wrapped: EntitiesReposito
return wrapped.getCount(list)
}

override fun clear() {
accesses += 1
wrapped.clear()
}

override fun addList(list: String) {
accesses += 1
wrapped.addList(list)
Expand Down