Index: Application/Ringtoets/src/Application.Ringtoets.Storage/SqLiteStorageConnection.cs =================================================================== diff -u -rafc121510f818769b49ae9fd3a7297524aff1d65 -r7ab3572b4460666f30900c9a974161ed38d901c5 --- Application/Ringtoets/src/Application.Ringtoets.Storage/SqLiteStorageConnection.cs (.../SqLiteStorageConnection.cs) (revision afc121510f818769b49ae9fd3a7297524aff1d65) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/SqLiteStorageConnection.cs (.../SqLiteStorageConnection.cs) (revision 7ab3572b4460666f30900c9a974161ed38d901c5) @@ -1,4 +1,5 @@ -using System.Data.Entity.Core.EntityClient; +using System; +using System.Data.Entity.Core.EntityClient; using System.Data.SQLite; namespace Application.Ringtoets.Storage @@ -13,13 +14,18 @@ /// /// Location of the storage file. /// A new connection string. + /// Thrown when is null. public static string BuildSqLiteEntityConnectionString(string filePath) { + if (string.IsNullOrWhiteSpace(filePath)) + { + throw new ArgumentNullException(); + } return new EntityConnectionStringBuilder { Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "DbContext.RingtoetsEntities"), Provider = @"System.Data.SQLite.EF6", - ProviderConnectionString = new SQLiteConnectionStringBuilder() + ProviderConnectionString = new SQLiteConnectionStringBuilder { FailIfMissing = true, DataSource = filePath, @@ -36,15 +42,21 @@ /// /// Location of the storage file. /// A new connection string. + /// Thrown when is null. public static string BuildSqLiteConnectionString(string filePath) { - return new SQLiteConnectionStringBuilder() + if (string.IsNullOrWhiteSpace(filePath)) { + throw new ArgumentNullException(); + } + return new SQLiteConnectionStringBuilder + { FailIfMissing = true, DataSource = filePath, ReadOnly = false, ForeignKeys = true, - Version = 3 + Version = 3, + Pooling = true }.ConnectionString; } } Index: Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs =================================================================== diff -u -r85053cd7be8aa42587cc2b0e25d6b98b5a5c2893 -r7ab3572b4460666f30900c9a974161ed38d901c5 --- Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision 85053cd7be8aa42587cc2b0e25d6b98b5a5c2893) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision 7ab3572b4460666f30900c9a974161ed38d901c5) @@ -57,6 +57,23 @@ } } + /// + /// Converts to an existing in the database. + /// + /// Path to database file. + /// to save. + /// Returns the number of changes that were saved in . + /// Thrown when is null. + /// is invalid. + /// Thrown when the database does not contain the table version. + /// Thrown when does not exist. + /// Thrown when + /// + /// Saving the to the database failed. + /// The connection to the database file failed. + /// The related entity was not found in the database. Therefore, no update was possible. + /// + /// public int SaveProject(string databaseFilePath, Project project) { Connect(databaseFilePath); Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/SqLiteStorageConnectionTest.cs =================================================================== diff -u -rbcbae879035d5472efff973990ba1194e38b2ea5 -r7ab3572b4460666f30900c9a974161ed38d901c5 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/SqLiteStorageConnectionTest.cs (.../SqLiteStorageConnectionTest.cs) (revision bcbae879035d5472efff973990ba1194e38b2ea5) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/SqLiteStorageConnectionTest.cs (.../SqLiteStorageConnectionTest.cs) (revision 7ab3572b4460666f30900c9a974161ed38d901c5) @@ -1,29 +1,75 @@ -using NUnit.Framework; +using System; +using NUnit.Framework; namespace Application.Ringtoets.Storage.Test { [TestFixture] - class SqLiteStorageConnectionTest + public class SqLiteStorageConnectionTest { [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + public void BuildSqLiteEntityConnectionString_InvalidPath_ThrowsArgumentNullException(string invalidPath) + { + // Call + TestDelegate test = () => SqLiteStorageConnection.BuildSqLiteEntityConnectionString(invalidPath); + + // Assert + Assert.Throws(test); + } + + [Test] public void BuildSqLiteEntityConnectionString_ValidFile_ValidConnectionString() { - const string validFile = "validFile"; + // Call var connectionString = SqLiteStorageConnection.BuildSqLiteEntityConnectionString(validFile); + + // Assert Assert.IsNotNullOrEmpty(connectionString); StringAssert.Contains(validFile, connectionString); - StringAssert.Contains("System.Data.SQLite.EF6", connectionString); - StringAssert.Contains("DbContext.RingtoetsEntities", connectionString); + StringAssert.Contains(String.Format("metadata=res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl;", "DbContext.RingtoetsEntities"), connectionString); + StringAssert.Contains("provider=System.Data.SQLite.EF6", connectionString); + StringAssert.Contains("failifmissing=True", connectionString); + StringAssert.Contains(String.Format("data source={0}", validFile), connectionString); + StringAssert.Contains("read only=False", connectionString); + StringAssert.Contains("foreign keys=True", connectionString); + StringAssert.Contains("version=3", connectionString); + StringAssert.Contains("pooling=True", connectionString); } - + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + public void BuildSqLiteConnectionString_InvalidPath_ThrowsArgumentNullException(string invalidPath) + { + // Call + TestDelegate test = () => SqLiteStorageConnection.BuildSqLiteConnectionString(invalidPath); + + // Assert + Assert.Throws(test); + } + + [Test] public void BuildSqLiteConnectionString_ValidFile_ValidConnectionString() { - const string validFile = "validFile"; + // Call var connectionString = SqLiteStorageConnection.BuildSqLiteConnectionString(validFile); + + // Assert Assert.IsNotNullOrEmpty(connectionString); StringAssert.Contains(validFile, connectionString); + StringAssert.DoesNotContain("metadata=", connectionString); StringAssert.DoesNotContain("System.Data.SQLite.EF6", connectionString); + StringAssert.Contains("failifmissing=True", connectionString); + StringAssert.Contains(String.Format("data source={0}", validFile), connectionString); + StringAssert.Contains("read only=False", connectionString); + StringAssert.Contains("foreign keys=True", connectionString); + StringAssert.Contains("version=3", connectionString); + StringAssert.Contains("pooling=True", connectionString); } + + private const string validFile = "validFile"; } -} +} \ No newline at end of file Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/StorageSqLiteTest.cs =================================================================== diff -u -re98b20963d45f14d250039cb5261cea9c050b5c9 -r7ab3572b4460666f30900c9a974161ed38d901c5 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/StorageSqLiteTest.cs (.../StorageSqLiteTest.cs) (revision e98b20963d45f14d250039cb5261cea9c050b5c9) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/StorageSqLiteTest.cs (.../StorageSqLiteTest.cs) (revision 7ab3572b4460666f30900c9a974161ed38d901c5) @@ -12,7 +12,14 @@ public class StorageSqLiteTest { private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Storage, "DatabaseFiles"); + private readonly string tempRingtoetsFile = Path.Combine(TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Storage, "DatabaseFiles"), "tempProjectFile.rtd"); + [TestFixtureTearDown] + public void TearDownTempRingtoetsFile() + { + TearDownTempRingtoetsFile(tempRingtoetsFile); + } + [Test] [TestCase(null)] [TestCase("")] @@ -271,28 +278,36 @@ } [Test] - public void SaveProject_ValidPathToSetFilePath_ValidContentsFromDatabase() + public void SaveProjectLoadProject_ValidPathToSetFilePath_ValidContentsFromDatabase() { // Setup long projectId = 1234L; var tempFile = Path.Combine(testDataPath, "tempProjectFile.rtd"); var project = new Project() { - StorageId = projectId + StorageId = projectId, + Name = "test", + Description = "description" }; var storage = new StorageSqLite(); TestDelegate precondition = () => storage.SaveProjectAs(tempFile, project); Assert.DoesNotThrow(precondition, String.Format("Precondition: file '{0}' must be a valid Ringtoets database file.", tempFile)); + // Call TestDelegate test = () => storage.SaveProject(tempFile, project); + + // Assert Assert.DoesNotThrow(test, String.Format("Precondition: failed to save project to file '{0}'.", tempFile)); // Call Project loadedProject = storage.LoadProject(tempFile); // Assert Assert.IsInstanceOf(loadedProject); + Assert.AreNotEqual(project, loadedProject); Assert.AreEqual(project.StorageId, loadedProject.StorageId); + Assert.AreEqual(project.Name, loadedProject.Name); + Assert.AreEqual(project.Description, loadedProject.Description); // TearDown TearDownTempRingtoetsFile(tempFile); @@ -311,7 +326,10 @@ { GC.Collect(); GC.WaitForPendingFinalizers(); - File.Delete(filePath); + if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath)) + { + File.Delete(filePath); + } } } } \ No newline at end of file