自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android O 遷移測試:Room

企業(yè)動態(tài)
在之前的一篇文章中,我們介紹了一下構件組件,其中就提到了使用 Room 來進行數(shù)據庫操作。以下是您需要了解的有關 Room 遷移測試的內容。

之前的一篇文章中,我們介紹了一下構件組件,其中就提到了使用 Room 來進行數(shù)據庫操作。但我們也有注意到,如果您錯誤地實施遷移,最嚴重的結果可能會導致您的應用崩潰或丟失用戶數(shù)據。

[[204036]]

除此之外,Migration.migrate 在編譯時不會檢查在方法中執(zhí)行的 SQL 語句,這導致了更多的問題。了解到這個情況之后,對遷移進行測試就成了一項必做任務。 Room 提供了 MigrationTestHelper 測試工具,這一工具允許您:

  • 在給定的版本中創(chuàng)建一個數(shù)據庫
  • 在數(shù)據庫上運行一組給定的遷移
  • 驗證數(shù)據庫 schema

當然,Room 不會驗證數(shù)據庫中的數(shù)據本身。這是您需要親自去實現(xiàn)的東西。

以下是您需要了解的有關 Room 遷移測試的內容。

工作原理

[[204037]]

為了進行遷移測試,Room 需要知道您當前數(shù)據庫版本的幾個要素:版本號、Entity、Identity Hash,以及為創(chuàng)建和更新 room_master_table 而做出的查詢請求。所有這些都由 Room 在編譯時自動生成,并存儲在schema JSON 文件中。

在 build.gradle 文件中指定一個文件夾,來放置這些生成的 schema JSON 文件。在更新 schema 時,最終會出現(xiàn)一些 JSON 文件,每個版本都將有一個對應的文件。確保將每個生成的文件提交給源代碼管理工具。下次再次增加版本號碼時,Room 可以使用 JSON 文件進行測試。

先決條件

要生成 JSON 文件,請使用以下內容更新 build.gradle 文件:

1. 定義 schema 位置

  1. defaultConfig { 
  2.   javaCompileOptions { 
  3.             annotationProcessorOptions { 
  4.                 arguments = ["room.schemaLocation":  
  5.                                 "$projectDir/schemas".toString()] 
  6.             } 
  7.         } 

2. 將 schema 位置添加到源碼目錄

  1. android { 
  2.      
  3.     sourceSets { 
  4.         androidTest.assets.srcDirs +=   
  5.                            files("$projectDir/schemas".toString()) 
  6.     } 

3. 將 Room 測試庫添加到依賴列表中

  1. dependencies { 
  2. androidTestImplementation     
  3.                 “android.arch.persistence.room:testing:1.0.0-alpha5” 

遷移測試規(guī)則

創(chuàng)建數(shù)據庫和 schema,打開和關閉數(shù)據庫,運行遷移 —— 您幾乎需要為每個測試編寫大量這樣的樣板代碼。為了避免過度重復勞動,請在遷移測試類中使用 MigrationTestHelper 測試工具。

為了創(chuàng)建數(shù)據庫以及驗證遷移,MigrationTestHelper 很大程度上依賴于生成的 JSON 文件。

  1. @Rule 
  2. public MigrationTestHelper testHelper = 
  3.         new MigrationTestHelper( 
  4.                 InstrumentationRegistry.getInstrumentation(), 
  5.                 <your_database_class>.class.getCanonicalName(), 
  6.                 new FrameworkSQLiteOpenHelperFactory() 

您可以在特定版本中創(chuàng)建數(shù)據庫:

  1. // Create the database with version 2 
  2. SupportSQLiteDatabase db =  
  3.                          testHelper.createDatabase(TEST 

您可以運行一組遷移,并自動驗證 schema 是否更新無誤:

  1. db = testHelper.runMigrationsAndValidate(TEST_DB_NAME, 4, validateDroppe 

實施測試

測試策略很簡單:

  1. 在特定版本中打開數(shù)據庫;
  2. 插入一些數(shù)據;
  3. 運行遷移并驗證 schema;
  4. 檢查數(shù)據庫中是否有正確的數(shù)據。

例如,數(shù)據庫的版本 3 添加了一個新列:date 。因此,當測試從版本 2 到版本 3 的遷移時,我們檢查插入到版本 2 的數(shù)據的有效性,也是我們新列的默認值。我們的 AndroidJUnitTest 看起來是這樣的:

  1. @Test 
  2. public void migrationFrom2To3_containsCorrectData() throws  
  3.                                                        IOException { 
  4.     // Create the database in version 2 
  5.     SupportSQLiteDatabase db =  
  6.                          testHelper.createDatabase(TEST_DB_NAME, 2); 
  7.     // Insert some data 
  8.     insertUser(USER.getId(), USER.getUserName(), db); 
  9.     //Prepare for the next version 
  10.     db.close(); 
  11.  
  12.     // Re-open the database with version 3 and provide MIGRATION_1_2   
  13.     // and MIGRATION_2_3 as the migration process. 
  14.     testHelper.runMigrationsAndValidate(TEST_DB_NAME, 3,    
  15.               validateDroppedTables, MIGRATION_1_2, MIGRATION_2_3); 
  16.  
  17.     // MigrationTestHelper automatically verifies the schema   
  18.     //changes, but not the data validity 
  19.     // Validate that the data was migrated properly. 
  20.     User dbUser = getMigratedRoomDatabase().userDao().getUser(); 
  21.     assertEquals(dbUser.getId(), USER.getId()); 
  22.     assertEquals(dbUser.getUserName(), USER.getUserName()); 
  23.     // The date was missing in version 2, so it should be null in  
  24.     //version 3 
  25.     assertEquals(dbUser.getDate(), null); 

測試從 SQLiteDatabase 到 Room 的遷移

從標準 SQLiteDatabase 到 Room 的步驟雖然乍一看很直觀,但我們覺得有必要詳細介紹如何測試遷移實現(xiàn)。

因為原本的數(shù)據庫沒有使用 Room 實現(xiàn),自然我們就沒有相應的 JSON 文件,因此我們無法使用 MigrationTestHelper 創(chuàng)建數(shù)據庫。

我們需要這么做:

  1. 擴展 SQLiteOpenHelper 類,并在 onCreate 執(zhí)行創(chuàng)建數(shù)據庫表的 SQL 查詢操作;
  2. 在 @Before 測試方法中,創(chuàng)建數(shù)據庫;
  3. 在 @After 測試方法中,清除數(shù)據庫;
  4. 使用 SQLiteOpenHelper ,來插入測試所需的數(shù)據,檢查從SQLiteDatabase 版本遷移到使用 Room 的版本;
  5. 使用 MigrationTestHelper 運行遷移和驗證 schema;
  6. 檢查數(shù)據庫數(shù)據的有效性。

數(shù)據庫版本 1 使用 SQLiteDatabase 實現(xiàn),然后在版本 2 中,我們遷移到了 Room,而在版本 3 中,我們添加了一個新的列。檢查從版本 1 到 3 的遷移測試如下所示:

  1. @Test 
  2. public void migrationFrom1To3_containsCorrectData() throws IOException { 
  3.     // Create the database with the initial version 1 schema and     
  4.     //insert a user 
  5.     SqliteDatabaseTestHelper.insertUser(1, USER.getUserName(), sqliteTestDbHelper); 
  6.  
  7.     // Re-open the database with version 3 and provide MIGRATION_1_2  
  8.     // and MIGRATION_2_3 as the migration process. 
  9.     testHelper.runMigrationsAndValidate(TEST_DB_NAME, 3, true, 
  10.             MIGRATION_1_2, MIGRATION_2_3); 
  11.  
  12.     // Get the latest, migrated, version of the database 
  13.     // Check that the correct data is in the database 
  14.     User dbUser = getMigratedRoomDatabase().userDao().getUser(); 
  15.     assertEquals(dbUser.getId(), 1); 
  16.     assertEquals(dbUser.getUserName(), USER.getUserName()); 
  17.     // The date was missing in version 2, so it should be null in    
  18.     //version 3 
  19.     assertEquals(dbUser.getDate(), null); 

挽起袖子試試吧!

[[204038]]

這里有一個示例 App:

https://github.com/googlesamples/android-architecture-components/tree/master/PersistenceMigrationsSample

您可以在此示例應用中查看實現(xiàn)。為了簡化比較,每個數(shù)據庫版本都是以自己的風格實現(xiàn)的,相信看完之后您已經能玩轉典型的遷移路徑了:

  1. sqlite:使用 SQLiteOpenHelper 和傳統(tǒng)的 SQLite 界面;
  2. room :使用 Room 替換實現(xiàn),并提供到版本 2 的遷移;
  3. room2:將數(shù)據庫更新為新 schema,版本 3;
  4. room3: 將數(shù)據庫更新為新的版本 4。提供從版本 2 到 3,版本 3 到 4,以及版本 1 到 4 的遷移路徑。

使用 Room,您可以很容易地實施和測試遷移。MigrationTestHelper 允許您在任何版本打開數(shù)據庫、運行遷移,并且只需幾行代碼就可以驗證 schema。

【本文是51CTO專欄機構“谷歌開發(fā)者”的原創(chuàng)稿件,轉載請聯(lián)系原作者(微信公眾號:Google_Developers)】

戳這里,看該作者更多好文

責任編輯:趙寧寧 來源: 51CTO專欄
相關推薦

2017-06-30 09:36:10

Android OAPI兼容

2015-08-28 09:12:44

云測O2O

2022-12-29 08:57:34

Android本地數(shù)據存儲

2022-01-19 12:23:36

云遷移云端

2019-03-25 12:20:29

數(shù)據MySQL性能測試

2017-09-18 10:26:05

Android OAndroid應用安裝

2013-05-09 10:51:44

2011-12-14 10:06:55

UnixLinux服務器

2017-08-16 14:08:46

Android O圖標視覺

2025-04-09 08:21:10

2013-01-15 13:28:24

盈利模式移動互聯(lián)網O2O

2015-10-15 09:57:08

光合資本

2014-06-13 11:25:04

Android 5.0

2017-09-18 22:55:46

GoogleAndroidRTDB

2013-01-15 13:59:14

2013O2O細分領域

2011-12-14 09:46:14

LinuxUnix遷移

2015-10-15 17:44:15

O2O

2015-01-06 10:24:23

O2OO2O項目

2014-11-07 14:47:33

微訪談

2015-01-26 16:02:02

O2O世博會
點贊
收藏

51CTO技術棧公眾號