1+ /*
2+ * Copyright (c) 2018 DuckDuckGo
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com.duckduckgo.app.migration
18+
19+ import android.arch.lifecycle.LiveData
20+ import android.arch.persistence.room.Room
21+ import android.content.ContentValues
22+ import android.support.test.InstrumentationRegistry
23+ import com.duckduckgo.app.bookmarks.db.BookmarkEntity
24+ import com.duckduckgo.app.bookmarks.db.BookmarksDao
25+ import com.duckduckgo.app.browser.DuckDuckGoRequestRewriter
26+ import com.duckduckgo.app.browser.DuckDuckGoUrlDetector
27+ import com.duckduckgo.app.browser.omnibar.QueryUrlConverter
28+ import com.duckduckgo.app.global.db.AppDatabase
29+ import com.duckduckgo.app.migration.legacy.LegacyDb
30+ import com.duckduckgo.app.migration.legacy.LegacyDbContracts
31+ import org.junit.After
32+ import org.junit.Assert.*
33+ import org.junit.Test
34+
35+ class LegacyMigrationTest {
36+
37+ // target context else we can't write a db file
38+ val context = InstrumentationRegistry .getTargetContext()
39+ val urlConverter = QueryUrlConverter (DuckDuckGoRequestRewriter (DuckDuckGoUrlDetector ()))
40+
41+ var appDatabase: AppDatabase = Room .inMemoryDatabaseBuilder(context, AppDatabase ::class .java).build()
42+ var bookmarksDao = MockBookmarksDao ()
43+
44+ @After
45+ fun after () {
46+ deleteLegacyDb()
47+ }
48+
49+ @Test
50+ fun whenNoLegacyDbExistsThenMigrationCompletesWithZeroMigratedItems () {
51+
52+ deleteLegacyDb()
53+
54+ val migration = LegacyMigration (appDatabase, bookmarksDao, context, urlConverter);
55+
56+ migration.start { favourites, searches ->
57+ assertEquals(0 , favourites)
58+ assertEquals(0 , searches)
59+ }
60+
61+ assertEquals(0 , bookmarksDao.bookmarks.size)
62+
63+ }
64+
65+ @Test
66+ fun whenLegacyDbExistsThenMigrationCompletesWithCorrectNumberOfMigratedItems () {
67+
68+ populateLegacyDB()
69+
70+ // migrate
71+ val migration = LegacyMigration (appDatabase, bookmarksDao, context, urlConverter);
72+
73+ migration.start { favourites, searches ->
74+ assertEquals(1 , favourites)
75+ assertEquals(1 , searches)
76+ }
77+
78+ assertEquals(2 , bookmarksDao.bookmarks.size)
79+
80+ migration.start { favourites, searches ->
81+ assertEquals(0 , favourites)
82+ assertEquals(0 , searches)
83+ }
84+
85+ }
86+
87+ private fun deleteLegacyDb () {
88+ context.getDatabasePath(LegacyDbContracts .DATABASE_NAME ).delete()
89+ }
90+
91+ private fun populateLegacyDB () {
92+ val db = LegacyDb (context)
93+
94+ assertNotEquals(- 1 , db.sqLiteDB.insert(LegacyDbContracts .SAVED_SEARCH_TABLE .TABLE_NAME , null , searchEntry()))
95+ assertNotEquals(- 1 , db.sqLiteDB.insert(LegacyDbContracts .FEED_TABLE .TABLE_NAME , null , favouriteEntry()))
96+ assertNotEquals(- 1 , db.sqLiteDB.insert(LegacyDbContracts .FEED_TABLE .TABLE_NAME , null , notFavouriteEntry()))
97+
98+ db.close()
99+ }
100+
101+ private fun notFavouriteEntry (): ContentValues {
102+ val values = ContentValues ()
103+ values.put(LegacyDbContracts .FEED_TABLE ._ID , " oops id" )
104+ values.put(LegacyDbContracts .FEED_TABLE .COLUMN_TITLE , " oops title" )
105+ values.put(LegacyDbContracts .FEED_TABLE .COLUMN_URL , " oops url" )
106+ values.put(LegacyDbContracts .FEED_TABLE .COLUMN_FAVORITE , " OOPS" )
107+ return values
108+ }
109+
110+ private fun favouriteEntry (): ContentValues {
111+ val values = ContentValues ()
112+ values.put(LegacyDbContracts .FEED_TABLE ._ID , " favourite id" )
113+ values.put(LegacyDbContracts .FEED_TABLE .COLUMN_TITLE , " favourite title" )
114+ values.put(LegacyDbContracts .FEED_TABLE .COLUMN_URL , " favourite url" )
115+ values.put(LegacyDbContracts .FEED_TABLE .COLUMN_FAVORITE , " F" )
116+ return values
117+ }
118+
119+ private fun searchEntry (): ContentValues {
120+ val values = ContentValues ()
121+ values.put(LegacyDbContracts .SAVED_SEARCH_TABLE .COLUMN_TITLE , " search title" )
122+ values.put(LegacyDbContracts .SAVED_SEARCH_TABLE .COLUMN_QUERY , " search query" )
123+ return values
124+ }
125+
126+ class MockBookmarksDao (): BookmarksDao {
127+
128+ var bookmarks = mutableListOf<BookmarkEntity >()
129+
130+ override fun insert (bookmark : BookmarkEntity ) {
131+ bookmarks.add(bookmark)
132+ }
133+
134+ override fun bookmarks (): LiveData <List <BookmarkEntity >> {
135+ throw UnsupportedOperationException ()
136+ }
137+
138+ override fun delete (bookmark : BookmarkEntity ) {
139+ throw UnsupportedOperationException ()
140+ }
141+
142+ }
143+
144+ }
0 commit comments