Skip to content

Commit

Permalink
Issue mozilla-mobile#2883: Introduce TabCollectionWithTabs data type …
Browse files Browse the repository at this point in the history
…for joining over collections and tabs.
  • Loading branch information
pocmo committed May 9, 2019
1 parent 28d7082 commit c49faef
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class TabCollectionDaoTest {

assertEquals(2, pagedList.size)

assertEquals("Collection Two", pagedList[0]!!.title)
assertEquals("Collection One", pagedList[1]!!.title)
assertEquals("Collection Two", pagedList[0]!!.collection.title)
assertEquals("Collection One", pagedList[1]!!.collection.title)
}

@Test
Expand All @@ -81,8 +81,8 @@ class TabCollectionDaoTest {

assertEquals(2, pagedList.size)

assertEquals("Updated collection", pagedList[0]!!.title)
assertEquals("Collection Two", pagedList[1]!!.title)
assertEquals("Updated collection", pagedList[0]!!.collection.title)
assertEquals("Collection Two", pagedList[1]!!.collection.title)
}

@Test
Expand All @@ -107,7 +107,7 @@ class TabCollectionDaoTest {

assertEquals(2, pagedList.size)

assertEquals("Collection Three", pagedList[0]!!.title)
assertEquals("Collection One", pagedList[1]!!.title)
assertEquals("Collection Three", pagedList[0]!!.collection.title)
assertEquals("Collection One", pagedList[1]!!.collection.title)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mozilla.components.feature.tab.collections.db

import android.content.Context
import androidx.paging.PagedList
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import org.junit.After
Expand Down Expand Up @@ -58,10 +59,18 @@ class TabDaoTest {
it.id = tabDao.insertTab(it)
}

val tabs = tabDao.getTabsOfCollection(collection.id!!)
assertEquals(2, tabs.size)
assertEquals(tab2, tabs[0])
assertEquals(tab1, tabs[1])
val dataSource = tabCollectionDao.getTabCollectionsPaged()
.create()

val pagedList = PagedList.Builder(dataSource, 10)
.setNotifyExecutor(executor)
.setFetchExecutor(executor)
.build()

assertEquals(1, pagedList.size)
assertEquals(2, pagedList[0]!!.tabs.size)
assertEquals(tab1, pagedList[0]!!.tabs[0])
assertEquals(tab2, pagedList[0]!!.tabs[1])
}

@Test
Expand Down Expand Up @@ -92,9 +101,17 @@ class TabDaoTest {

tabDao.deleteTab(tab1)

val tabs = tabDao.getTabsOfCollection(collection.id!!)
assertEquals(1, tabs.size)
assertEquals(tab2, tabs[0])
val dataSource = tabCollectionDao.getTabCollectionsPaged()
.create()

val pagedList = PagedList.Builder(dataSource, 10)
.setNotifyExecutor(executor)
.setFetchExecutor(executor)
.build()

assertEquals(1, pagedList.size)
assertEquals(1, pagedList[0]!!.tabs.size)
assertEquals(tab2, pagedList[0]!!.tabs[0])
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ internal interface TabCollectionDao {
@Update
fun updateTabCollection(collection: TabCollectionEntity)

@Query("SELECT * FROM tab_collections ORDER BY created_at DESC")
fun getTabCollectionsPaged(): DataSource.Factory<Int, TabCollectionEntity>
@Query("""
SELECT *
FROM tab_collections LEFT JOIN tabs ON tab_collections.id = tab_collection_id
GROUP BY tab_collections.id
ORDER BY created_at DESC
""")
fun getTabCollectionsPaged(): DataSource.Factory<Int, TabCollectionWithTabs>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.feature.tab.collections.db

import androidx.room.Embedded
import androidx.room.Relation

/**
* Class representing a [TabCollectionEntity] joined with its [TabEntity] instances.
*/
internal class TabCollectionWithTabs {
@Embedded
lateinit var collection: TabCollectionEntity

@Relation(parentColumn = "id", entityColumn = "tab_collection_id")
lateinit var tabs: List<TabEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package mozilla.components.feature.tab.collections.db
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query

/**
* Internal DAO for accessing [TabEntity] instances.
Expand All @@ -19,7 +18,4 @@ internal interface TabDao {

@Delete
fun deleteTab(tab: TabEntity)

@Query("SELECT * FROM tabs WHERE tab_collection_id = :tabCollectionId ORDER BY created_at ASC")
fun getTabsOfCollection(tabCollectionId: Long): List<TabEntity>
}

0 comments on commit c49faef

Please sign in to comment.