/Source Code/UnitTests/DatabaseTest.vb
Visual Basic | 280 lines | 249 code | 23 blank | 8 comment | 5 complexity | 4c9819938d1f8656298a3a9b80b89a2d MD5 | raw file
1Imports System.Collections.Generic 2Imports PowerSong.SongDatabase.Items 3Imports Microsoft.VisualStudio.TestTools.UnitTesting 4Imports PowerSong.SongDatabase 5Imports System.IO 6 7<TestClass()> _ 8Public Class DatabaseTest 9 10 Private FRandom As New Random 11 12 Private Function CreateTempPath() As String 13 14 Dim Result As String = Path.GetTempPath + "PSD" + FRandom.Next(0, 100000).ToString 15 If Directory.Exists(Result) Then Throw New Exception("Could not create a temporary path in which to save files.") 16 Directory.CreateDirectory(Result) 17 Return Result 18 19 End Function 20 21 Private Function CreateDatabase() As Database 22 23 ' Create a database 24 Dim DatabaseLocation As String = CreateTempPath() 25 Try 26 27 Dim Data As Database = Database.Create(DatabaseLocation) 28 29 ' Check database version 30 Assert.AreEqual(Database.DATABASE_FILE_VERSION, Data.DatabaseVersion, "Incorrect database version.") 31 Assert.AreEqual(DatabaseLocation, Data.Location, "Database is in the wrong location.") 32 33 ' Check songs 34 Assert.AreEqual(0, Data.Songs.GetAllSongFileNames.Count, "Incorrect number of songs.") 35 Assert.AreEqual(0, Data.Songs.GetAllSongIDs.Count, "Incorrect number of songs.") 36 Assert.AreEqual(0, Data.Songs.GetAllSongTitles.Count, "Incorrect number of songs.") 37 38 ' Check styles 39 Assert.AreEqual(1, Data.Styles.GetAllStyleNames.Count, "Incorrect number of songs.") 40 Assert.AreEqual("Default", Data.Styles.GetAllStyleNames(0), "Incorrect name of the default style.") 41 Assert.AreEqual(1, Data.Styles.GetAllStyles.Count, "Incorrect number of songs.") 42 43 ' Check settings 44 Assert.AreEqual("KJV", Data.Settings.ActiveBible, "Incorrect initial value for a default setting.") 45 Assert.AreEqual(Database.DATABASE_FILE_VERSION, Data.Settings.DatabaseVersion, "Incorrect initial value for a default setting.") 46 Assert.AreEqual(True, Data.Settings.ShowMainProjection, "Incorrect initial value for a default setting.") 47 Assert.AreEqual(True, Data.Settings.ShowPreviewProjection, "Incorrect initial value for a default setting.") 48 Assert.AreEqual(True, Data.Settings.ShowSongPreviews, "Incorrect initial value for a default setting.") 49 50 Return Data 51 52 Catch 53 Directory.Delete(DatabaseLocation, True) 54 Throw 55 End Try 56 57 End Function 58 59 <TestMethod()> _ 60 Public Sub CreateDatabaseTest() 61 62 Dim Data As Database = CreateDatabase() 63 Directory.Delete(Data.Location, True) 64 65 End Sub 66 67 <TestMethod()> _ 68 Public Sub AddSongsTest() 69 70 Dim Data As Database = CreateDatabase() 71 Try 72 73 ' Get the default category 74 Dim CategoryID As Guid = Data.Categories.GetCategoryID(Database.DEFAULT_CATEGORY_NAME) 75 76 ' Define some songs 77 Dim Song1 As New Items.SongItem("Song 1", "Part 1", "Part 2", "Part 3") 78 Song1.Categories.Add(CategoryID) 79 Dim Song2 As New Items.SongItem("Song 2", "Part 1", "Part 2", "Part 3") 80 Song2.Categories.Add(CategoryID) 81 Dim Song3 As New Items.SongItem("Song 3", "Part 1", "Part 2", "Part 3") 82 Song3.Categories.Add(CategoryID) 83 84 ' Add songs to the database 85 Dim Song1Location As String = Data.Songs.AddSong(Song1) 86 Dim Song2Location As String = Data.Songs.AddSong(Song2) 87 Dim Song3Location As String = Data.Songs.AddSong(Song3) 88 Assert.AreEqual(Data.Location + "\" + Song1.SongID.ToString + ".song", Song1Location, "Incorrect song location.") 89 Assert.AreEqual(Data.Location + "\" + Song2.SongID.ToString + ".song", Song2Location, "Incorrect song location.") 90 Assert.AreEqual(Data.Location + "\" + Song3.SongID.ToString + ".song", Song3Location, "Incorrect song location.") 91 92 ' Perform some assertions 93 Assert.AreEqual(3, Data.Songs.GetAllSongTitles.Count, "Incorrect number of songs in the database.") 94 Assert.AreEqual("Song 1", Data.Songs.GetSong(Song1.SongID).Title, "Incorrect title for song.") 95 Assert.AreEqual("Song 2", Data.Songs.GetSong(Song2.SongID).Title, "Incorrect title for song.") 96 Assert.AreEqual("Song 3", Data.Songs.GetSong(Song3.SongID).Title, "Incorrect title for song.") 97 Assert.AreEqual(1, Data.Songs.GetSong("Song 1").Categories.Count, "Incorrect number of categories.") 98 Assert.AreEqual(1, Data.Songs.GetSong("Song 2").Categories.Count, "Incorrect number of categories.") 99 Assert.AreEqual(1, Data.Songs.GetSong("Song 3").Categories.Count, "Incorrect number of categories.") 100 Assert.AreEqual(CategoryID, Data.Songs.GetSong(Song1.SongID).Categories(0), "Incorrect category.") 101 Assert.AreEqual(CategoryID, Data.Songs.GetSong(Song2.SongID).Categories(0), "Incorrect category.") 102 Assert.AreEqual(CategoryID, Data.Songs.GetSong(Song3.SongID).Categories(0), "Incorrect category.") 103 104 Finally 105 Directory.Delete(Data.Location, True) 106 End Try 107 108 End Sub 109 110 <TestMethod()> _ 111 Public Sub SimpleExportImportTest() 112 113 Dim Data1 As Database = CreateDatabase() 114 Dim Data2 As Database = CreateDatabase() 115 Try 116 117 ' Get the default category 118 Dim CategoryID As Guid = Data1.Categories.GetCategoryID(Database.DEFAULT_CATEGORY_NAME) 119 AddSong(Data1, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3") 120 AddSong(Data1, "Title 2", "(c) 2", New String() {"Person 3", "Person 4"}, New Guid() {CategoryID}, "Part 4", "Part 5", "Part 6") 121 AddSong(Data1, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {CategoryID}, "Part 7", "Part 8", "Part 9") 122 123 ' Interim checks 124 AssertGeneralDatabaseDetails(Data1, 3, 1, 1, 0) 125 AssertGeneralDatabaseDetails(Data2, 0, 1, 1, 0) 126 AssertSong(Data1, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3") 127 AssertSong(Data1, "Title 2", "(c) 2", New String() {"Person 3", "Person 4"}, New Guid() {CategoryID}, "Part 4", "Part 5", "Part 6") 128 AssertSong(Data1, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {CategoryID}, "Part 7", "Part 8", "Part 9") 129 130 ' Export the database into the second database 131 Dim TempPath As String = CreateTempPath() 132 Try 133 Dim Exporter As New ExportImport.PrimaryFacility(Data1) 134 Exporter.Export(TempPath + "\Database.PowerSong", True, True, True, True) 135 Dim Importer As New ExportImport.PrimaryFacility(Data2) 136 Dim Results As List(Of String) = Importer.Import(TempPath + "\Database.PowerSong", Nothing, True) 137 Assert.IsTrue(Results.Count > 0) 138 Finally 139 Directory.Delete(TempPath, True) 140 End Try 141 142 ' Final checks 143 AssertGeneralDatabaseDetails(Data1, 3, 1, 1, 0) 144 AssertGeneralDatabaseDetails(Data2, 3, 1, 1, 0) 145 Dim SecondDatabasePrimaryCategoryID As Guid = Data2.Categories.GetCategoryID(Database.DEFAULT_CATEGORY_NAME) 146 AssertSong(Data2, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {SecondDatabasePrimaryCategoryID}, "Part 1", "Part 2", "Part 3") 147 AssertSong(Data2, "Title 2", "(c) 2", New String() {"Person 3", "Person 4"}, New Guid() {SecondDatabasePrimaryCategoryID}, "Part 4", "Part 5", "Part 6") 148 AssertSong(Data2, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {SecondDatabasePrimaryCategoryID}, "Part 7", "Part 8", "Part 9") 149 150 Finally 151 Directory.Delete(Data1.Location, True) 152 Directory.Delete(Data2.Location, True) 153 End Try 154 155 End Sub 156 157 <TestMethod()> _ 158 Public Sub DeleteSongsTest() 159 160 Dim Data As Database = CreateDatabase() 161 Try 162 163 ' Get the default category 164 Dim CategoryID As Guid = Data.Categories.GetCategoryID(Database.DEFAULT_CATEGORY_NAME) 165 AssertGeneralDatabaseDetails(Data, 0, 1, 1, 0) 166 Dim Song1 As Guid = AddSong(Data, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3") 167 AssertGeneralDatabaseDetails(Data, 1, 1, 1, 0) 168 Dim Song2 As Guid = AddSong(Data, "Title 2", "(c) 2", New String() {"Person 3", "Person 4"}, New Guid() {CategoryID}, "Part 4", "Part 5", "Part 6") 169 AssertGeneralDatabaseDetails(Data, 2, 1, 1, 0) 170 Dim Song3 As Guid = AddSong(Data, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {CategoryID}, "Part 7", "Part 8", "Part 9") 171 AssertGeneralDatabaseDetails(Data, 3, 1, 1, 0) 172 Dim Song4 As Guid = AddSong(Data, "Title 4", "(c) 4", New String() {"Person 7", "Person 8"}, New Guid() {CategoryID}, "Part 10", "Part 11", "Part 12") 173 AssertGeneralDatabaseDetails(Data, 4, 1, 1, 0) 174 Dim Song5 As Guid = AddSong(Data, "Title 5", "(c) 5", New String() {"Person 9", "Person 10"}, New Guid() {CategoryID}, "Part 13", "Part 14", "Part 15") 175 AssertGeneralDatabaseDetails(Data, 5, 1, 1, 0) 176 177 ' Some checks 178 AssertGeneralDatabaseDetails(Data, 5, 1, 1, 0) 179 AssertSong(Data, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3") 180 AssertSong(Data, "Title 2", "(c) 2", New String() {"Person 3", "Person 4"}, New Guid() {CategoryID}, "Part 4", "Part 5", "Part 6") 181 AssertSong(Data, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {CategoryID}, "Part 7", "Part 8", "Part 9") 182 AssertSong(Data, "Title 4", "(c) 4", New String() {"Person 7", "Person 8"}, New Guid() {CategoryID}, "Part 10", "Part 11", "Part 12") 183 AssertSong(Data, "Title 5", "(c) 5", New String() {"Person 9", "Person 10"}, New Guid() {CategoryID}, "Part 13", "Part 14", "Part 15") 184 185 ' Delete some songs 186 Data.Songs.DeleteSong(Song2) 187 Data.Songs.DeleteSong(Song4) 188 189 ' Some checks 190 AssertGeneralDatabaseDetails(Data, 3, 1, 1, 0) 191 AssertSong(Data, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3") 192 AssertSong(Data, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {CategoryID}, "Part 7", "Part 8", "Part 9") 193 AssertSong(Data, "Title 5", "(c) 5", New String() {"Person 9", "Person 10"}, New Guid() {CategoryID}, "Part 13", "Part 14", "Part 15") 194 195 ' Delete some songs 196 Data.Songs.DeleteSong(Song3) 197 198 ' Some checks 199 AssertGeneralDatabaseDetails(Data, 2, 1, 1, 0) 200 AssertSong(Data, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3") 201 AssertSong(Data, "Title 5", "(c) 5", New String() {"Person 9", "Person 10"}, New Guid() {CategoryID}, "Part 13", "Part 14", "Part 15") 202 203 ' Delete a song 204 Data.Songs.DeleteSong(Song1) 205 206 ' Some checks 207 AssertGeneralDatabaseDetails(Data, 1, 1, 1, 0) 208 AssertSong(Data, "Title 5", "(c) 5", New String() {"Person 9", "Person 10"}, New Guid() {CategoryID}, "Part 13", "Part 14", "Part 15") 209 210 ' Delete the last song 211 Data.Songs.DeleteSong(Song5) 212 213 ' Some checks 214 AssertGeneralDatabaseDetails(Data, 0, 1, 1, 0) 215 216 Finally 217 Directory.Delete(Data.Location, True) 218 End Try 219 220 End Sub 221 222 Private Function AddSong(ByVal database As Database, _ 223 ByVal title As String, _ 224 ByVal copyrightLine As String, _ 225 ByVal authors() As String, _ 226 ByVal categories As Guid(), _ 227 ByVal ParamArray parts() As String) 228 229 Dim Song As New SongItem(title, parts) 230 For Each CategoryID As Guid In categories 231 Song.Categories.Add(CategoryID) 232 Next 233 Song.CopyrightLine = copyrightLine 234 Song.Authors = New List(Of String)(authors) 235 database.Songs.AddSong(Song) 236 Return Song.SongID 237 238 End Function 239 240 Private Sub AssertGeneralDatabaseDetails(ByVal database As Database, _ 241 ByVal songCount As Integer, _ 242 ByVal categoryCount As Integer, _ 243 ByVal styleCount As Integer, _ 244 ByVal pluginFileCount As Integer) 245 246 Assert.AreEqual(songCount, database.Songs.GetAllSongFileNames.Count, "Incorrect number of songs in database.") 247 Assert.AreEqual(songCount, database.Songs.GetAllSongIDs.Count, "Incorrect number of songs in database.") 248 Assert.AreEqual(songCount, database.Songs.GetAllSongTitles.Count, "Incorrect number of songs in database.") 249 Assert.AreEqual(categoryCount, database.Categories.GetAllCategories.Count, "Incorrect number of categories in database.") 250 Assert.AreEqual(styleCount, database.Styles.GetAllStyleNames.Count, "Incorrect number of styles in database.") 251 Assert.AreEqual(styleCount, database.Styles.GetAllStyles.Count, "Incorrect number of styles in database.") 252 Assert.AreEqual(pluginFileCount, database.PluginFiles.Length, "Incorrect number of plugin files in database.") 253 254 End Sub 255 256 Private Sub AssertSong(ByVal database As Database, _ 257 ByVal title As String, _ 258 ByVal copyrightLine As String, _ 259 ByVal authors() As String, _ 260 ByVal categories() As Guid, _ 261 ByVal ParamArray parts() As String) 262 263 Dim Song As SongItem = database.Songs.GetSong(title) 264 Assert.IsNotNull(Song, "There is no song with the given title.") 265 Assert.AreEqual(title, Song.Title, "Incorrect song title.") 266 Assert.AreEqual(copyrightLine, Song.CopyrightLine, "Incorrect song copyright line.") 267 268 Assert.AreEqual(authors.Length, Song.Authors.Count, "Incorrect song authors.") 269 For Each Author As String In authors 270 If Not Song.Authors.Contains(Author) Then Assert.Fail("Expected author not found.") 271 Next 272 273 Assert.AreEqual(categories.Length, Song.Categories.Count, "Incorrect song categories.") 274 For Each CategoryID As Guid In categories 275 If Not Song.Categories.Contains(CategoryID) Then Assert.Fail("Expected category ID not found.") 276 Next 277 278 End Sub 279 280End Class