PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/UnitTests/DatabaseTest.vb

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