/Source Code/UnitTests/DatabaseTest.vb
Visual Basic | 280 lines | 249 code | 23 blank | 8 comment | 5 complexity | 4c9819938d1f8656298a3a9b80b89a2d MD5 | raw file
- Imports System.Collections.Generic
- Imports PowerSong.SongDatabase.Items
- Imports Microsoft.VisualStudio.TestTools.UnitTesting
- Imports PowerSong.SongDatabase
- Imports System.IO
-
- <TestClass()> _
- Public Class DatabaseTest
-
- Private FRandom As New Random
-
- Private Function CreateTempPath() As String
-
- Dim Result As String = Path.GetTempPath + "PSD" + FRandom.Next(0, 100000).ToString
- If Directory.Exists(Result) Then Throw New Exception("Could not create a temporary path in which to save files.")
- Directory.CreateDirectory(Result)
- Return Result
-
- End Function
-
- Private Function CreateDatabase() As Database
-
- ' Create a database
- Dim DatabaseLocation As String = CreateTempPath()
- Try
-
- Dim Data As Database = Database.Create(DatabaseLocation)
-
- ' Check database version
- Assert.AreEqual(Database.DATABASE_FILE_VERSION, Data.DatabaseVersion, "Incorrect database version.")
- Assert.AreEqual(DatabaseLocation, Data.Location, "Database is in the wrong location.")
-
- ' Check songs
- Assert.AreEqual(0, Data.Songs.GetAllSongFileNames.Count, "Incorrect number of songs.")
- Assert.AreEqual(0, Data.Songs.GetAllSongIDs.Count, "Incorrect number of songs.")
- Assert.AreEqual(0, Data.Songs.GetAllSongTitles.Count, "Incorrect number of songs.")
-
- ' Check styles
- Assert.AreEqual(1, Data.Styles.GetAllStyleNames.Count, "Incorrect number of songs.")
- Assert.AreEqual("Default", Data.Styles.GetAllStyleNames(0), "Incorrect name of the default style.")
- Assert.AreEqual(1, Data.Styles.GetAllStyles.Count, "Incorrect number of songs.")
-
- ' Check settings
- Assert.AreEqual("KJV", Data.Settings.ActiveBible, "Incorrect initial value for a default setting.")
- Assert.AreEqual(Database.DATABASE_FILE_VERSION, Data.Settings.DatabaseVersion, "Incorrect initial value for a default setting.")
- Assert.AreEqual(True, Data.Settings.ShowMainProjection, "Incorrect initial value for a default setting.")
- Assert.AreEqual(True, Data.Settings.ShowPreviewProjection, "Incorrect initial value for a default setting.")
- Assert.AreEqual(True, Data.Settings.ShowSongPreviews, "Incorrect initial value for a default setting.")
-
- Return Data
-
- Catch
- Directory.Delete(DatabaseLocation, True)
- Throw
- End Try
-
- End Function
-
- <TestMethod()> _
- Public Sub CreateDatabaseTest()
-
- Dim Data As Database = CreateDatabase()
- Directory.Delete(Data.Location, True)
-
- End Sub
-
- <TestMethod()> _
- Public Sub AddSongsTest()
-
- Dim Data As Database = CreateDatabase()
- Try
-
- ' Get the default category
- Dim CategoryID As Guid = Data.Categories.GetCategoryID(Database.DEFAULT_CATEGORY_NAME)
-
- ' Define some songs
- Dim Song1 As New Items.SongItem("Song 1", "Part 1", "Part 2", "Part 3")
- Song1.Categories.Add(CategoryID)
- Dim Song2 As New Items.SongItem("Song 2", "Part 1", "Part 2", "Part 3")
- Song2.Categories.Add(CategoryID)
- Dim Song3 As New Items.SongItem("Song 3", "Part 1", "Part 2", "Part 3")
- Song3.Categories.Add(CategoryID)
-
- ' Add songs to the database
- Dim Song1Location As String = Data.Songs.AddSong(Song1)
- Dim Song2Location As String = Data.Songs.AddSong(Song2)
- Dim Song3Location As String = Data.Songs.AddSong(Song3)
- Assert.AreEqual(Data.Location + "\" + Song1.SongID.ToString + ".song", Song1Location, "Incorrect song location.")
- Assert.AreEqual(Data.Location + "\" + Song2.SongID.ToString + ".song", Song2Location, "Incorrect song location.")
- Assert.AreEqual(Data.Location + "\" + Song3.SongID.ToString + ".song", Song3Location, "Incorrect song location.")
-
- ' Perform some assertions
- Assert.AreEqual(3, Data.Songs.GetAllSongTitles.Count, "Incorrect number of songs in the database.")
- Assert.AreEqual("Song 1", Data.Songs.GetSong(Song1.SongID).Title, "Incorrect title for song.")
- Assert.AreEqual("Song 2", Data.Songs.GetSong(Song2.SongID).Title, "Incorrect title for song.")
- Assert.AreEqual("Song 3", Data.Songs.GetSong(Song3.SongID).Title, "Incorrect title for song.")
- Assert.AreEqual(1, Data.Songs.GetSong("Song 1").Categories.Count, "Incorrect number of categories.")
- Assert.AreEqual(1, Data.Songs.GetSong("Song 2").Categories.Count, "Incorrect number of categories.")
- Assert.AreEqual(1, Data.Songs.GetSong("Song 3").Categories.Count, "Incorrect number of categories.")
- Assert.AreEqual(CategoryID, Data.Songs.GetSong(Song1.SongID).Categories(0), "Incorrect category.")
- Assert.AreEqual(CategoryID, Data.Songs.GetSong(Song2.SongID).Categories(0), "Incorrect category.")
- Assert.AreEqual(CategoryID, Data.Songs.GetSong(Song3.SongID).Categories(0), "Incorrect category.")
-
- Finally
- Directory.Delete(Data.Location, True)
- End Try
-
- End Sub
-
- <TestMethod()> _
- Public Sub SimpleExportImportTest()
-
- Dim Data1 As Database = CreateDatabase()
- Dim Data2 As Database = CreateDatabase()
- Try
-
- ' Get the default category
- Dim CategoryID As Guid = Data1.Categories.GetCategoryID(Database.DEFAULT_CATEGORY_NAME)
- AddSong(Data1, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3")
- AddSong(Data1, "Title 2", "(c) 2", New String() {"Person 3", "Person 4"}, New Guid() {CategoryID}, "Part 4", "Part 5", "Part 6")
- AddSong(Data1, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {CategoryID}, "Part 7", "Part 8", "Part 9")
-
- ' Interim checks
- AssertGeneralDatabaseDetails(Data1, 3, 1, 1, 0)
- AssertGeneralDatabaseDetails(Data2, 0, 1, 1, 0)
- AssertSong(Data1, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3")
- AssertSong(Data1, "Title 2", "(c) 2", New String() {"Person 3", "Person 4"}, New Guid() {CategoryID}, "Part 4", "Part 5", "Part 6")
- AssertSong(Data1, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {CategoryID}, "Part 7", "Part 8", "Part 9")
-
- ' Export the database into the second database
- Dim TempPath As String = CreateTempPath()
- Try
- Dim Exporter As New ExportImport.PrimaryFacility(Data1)
- Exporter.Export(TempPath + "\Database.PowerSong", True, True, True, True)
- Dim Importer As New ExportImport.PrimaryFacility(Data2)
- Dim Results As List(Of String) = Importer.Import(TempPath + "\Database.PowerSong", Nothing, True)
- Assert.IsTrue(Results.Count > 0)
- Finally
- Directory.Delete(TempPath, True)
- End Try
-
- ' Final checks
- AssertGeneralDatabaseDetails(Data1, 3, 1, 1, 0)
- AssertGeneralDatabaseDetails(Data2, 3, 1, 1, 0)
- Dim SecondDatabasePrimaryCategoryID As Guid = Data2.Categories.GetCategoryID(Database.DEFAULT_CATEGORY_NAME)
- AssertSong(Data2, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {SecondDatabasePrimaryCategoryID}, "Part 1", "Part 2", "Part 3")
- AssertSong(Data2, "Title 2", "(c) 2", New String() {"Person 3", "Person 4"}, New Guid() {SecondDatabasePrimaryCategoryID}, "Part 4", "Part 5", "Part 6")
- AssertSong(Data2, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {SecondDatabasePrimaryCategoryID}, "Part 7", "Part 8", "Part 9")
-
- Finally
- Directory.Delete(Data1.Location, True)
- Directory.Delete(Data2.Location, True)
- End Try
-
- End Sub
-
- <TestMethod()> _
- Public Sub DeleteSongsTest()
-
- Dim Data As Database = CreateDatabase()
- Try
-
- ' Get the default category
- Dim CategoryID As Guid = Data.Categories.GetCategoryID(Database.DEFAULT_CATEGORY_NAME)
- AssertGeneralDatabaseDetails(Data, 0, 1, 1, 0)
- 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")
- AssertGeneralDatabaseDetails(Data, 1, 1, 1, 0)
- 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")
- AssertGeneralDatabaseDetails(Data, 2, 1, 1, 0)
- 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")
- AssertGeneralDatabaseDetails(Data, 3, 1, 1, 0)
- 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")
- AssertGeneralDatabaseDetails(Data, 4, 1, 1, 0)
- 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")
- AssertGeneralDatabaseDetails(Data, 5, 1, 1, 0)
-
- ' Some checks
- AssertGeneralDatabaseDetails(Data, 5, 1, 1, 0)
- AssertSong(Data, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3")
- AssertSong(Data, "Title 2", "(c) 2", New String() {"Person 3", "Person 4"}, New Guid() {CategoryID}, "Part 4", "Part 5", "Part 6")
- AssertSong(Data, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {CategoryID}, "Part 7", "Part 8", "Part 9")
- AssertSong(Data, "Title 4", "(c) 4", New String() {"Person 7", "Person 8"}, New Guid() {CategoryID}, "Part 10", "Part 11", "Part 12")
- AssertSong(Data, "Title 5", "(c) 5", New String() {"Person 9", "Person 10"}, New Guid() {CategoryID}, "Part 13", "Part 14", "Part 15")
-
- ' Delete some songs
- Data.Songs.DeleteSong(Song2)
- Data.Songs.DeleteSong(Song4)
-
- ' Some checks
- AssertGeneralDatabaseDetails(Data, 3, 1, 1, 0)
- AssertSong(Data, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3")
- AssertSong(Data, "Title 3", "(c) 3", New String() {"Person 5", "Person 6"}, New Guid() {CategoryID}, "Part 7", "Part 8", "Part 9")
- AssertSong(Data, "Title 5", "(c) 5", New String() {"Person 9", "Person 10"}, New Guid() {CategoryID}, "Part 13", "Part 14", "Part 15")
-
- ' Delete some songs
- Data.Songs.DeleteSong(Song3)
-
- ' Some checks
- AssertGeneralDatabaseDetails(Data, 2, 1, 1, 0)
- AssertSong(Data, "Title 1", "(c) 1", New String() {"Person 1", "Person 2"}, New Guid() {CategoryID}, "Part 1", "Part 2", "Part 3")
- AssertSong(Data, "Title 5", "(c) 5", New String() {"Person 9", "Person 10"}, New Guid() {CategoryID}, "Part 13", "Part 14", "Part 15")
-
- ' Delete a song
- Data.Songs.DeleteSong(Song1)
-
- ' Some checks
- AssertGeneralDatabaseDetails(Data, 1, 1, 1, 0)
- AssertSong(Data, "Title 5", "(c) 5", New String() {"Person 9", "Person 10"}, New Guid() {CategoryID}, "Part 13", "Part 14", "Part 15")
-
- ' Delete the last song
- Data.Songs.DeleteSong(Song5)
-
- ' Some checks
- AssertGeneralDatabaseDetails(Data, 0, 1, 1, 0)
-
- Finally
- Directory.Delete(Data.Location, True)
- End Try
-
- End Sub
-
- Private Function AddSong(ByVal database As Database, _
- ByVal title As String, _
- ByVal copyrightLine As String, _
- ByVal authors() As String, _
- ByVal categories As Guid(), _
- ByVal ParamArray parts() As String)
-
- Dim Song As New SongItem(title, parts)
- For Each CategoryID As Guid In categories
- Song.Categories.Add(CategoryID)
- Next
- Song.CopyrightLine = copyrightLine
- Song.Authors = New List(Of String)(authors)
- database.Songs.AddSong(Song)
- Return Song.SongID
-
- End Function
-
- Private Sub AssertGeneralDatabaseDetails(ByVal database As Database, _
- ByVal songCount As Integer, _
- ByVal categoryCount As Integer, _
- ByVal styleCount As Integer, _
- ByVal pluginFileCount As Integer)
-
- Assert.AreEqual(songCount, database.Songs.GetAllSongFileNames.Count, "Incorrect number of songs in database.")
- Assert.AreEqual(songCount, database.Songs.GetAllSongIDs.Count, "Incorrect number of songs in database.")
- Assert.AreEqual(songCount, database.Songs.GetAllSongTitles.Count, "Incorrect number of songs in database.")
- Assert.AreEqual(categoryCount, database.Categories.GetAllCategories.Count, "Incorrect number of categories in database.")
- Assert.AreEqual(styleCount, database.Styles.GetAllStyleNames.Count, "Incorrect number of styles in database.")
- Assert.AreEqual(styleCount, database.Styles.GetAllStyles.Count, "Incorrect number of styles in database.")
- Assert.AreEqual(pluginFileCount, database.PluginFiles.Length, "Incorrect number of plugin files in database.")
-
- End Sub
-
- Private Sub AssertSong(ByVal database As Database, _
- ByVal title As String, _
- ByVal copyrightLine As String, _
- ByVal authors() As String, _
- ByVal categories() As Guid, _
- ByVal ParamArray parts() As String)
-
- Dim Song As SongItem = database.Songs.GetSong(title)
- Assert.IsNotNull(Song, "There is no song with the given title.")
- Assert.AreEqual(title, Song.Title, "Incorrect song title.")
- Assert.AreEqual(copyrightLine, Song.CopyrightLine, "Incorrect song copyright line.")
-
- Assert.AreEqual(authors.Length, Song.Authors.Count, "Incorrect song authors.")
- For Each Author As String In authors
- If Not Song.Authors.Contains(Author) Then Assert.Fail("Expected author not found.")
- Next
-
- Assert.AreEqual(categories.Length, Song.Categories.Count, "Incorrect song categories.")
- For Each CategoryID As Guid In categories
- If Not Song.Categories.Contains(CategoryID) Then Assert.Fail("Expected category ID not found.")
- Next
-
- End Sub
-
- End Class