Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseContext/GeneralTableDefinitions.cs =================================================================== diff -u -r7d124cef8960a865cc8d7db24b3359f7ff9958be -r7c7aa064bf7718dcdf9dc373f9eba77418cde957 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseContext/GeneralTableDefinitions.cs (.../GeneralTableDefinitions.cs) (revision 7d124cef8960a865cc8d7db24b3359f7ff9958be) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseContext/GeneralTableDefinitions.cs (.../GeneralTableDefinitions.cs) (revision 7c7aa064bf7718dcdf9dc373f9eba77418cde957) @@ -27,7 +27,8 @@ internal static class GeneralTableDefinitions { internal const string TableName = "General"; - internal const string NameRegion = "NameRegion"; + internal const string RegionName = "NameRegion"; + internal const string RegionId = "GeneralId"; internal const string CreationDate = "CreationDate"; internal const string GeneratedVersion = "GeneratedVersion"; } Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseContext/HydraulicBoundaryDatabaseQueryBuilder.cs =================================================================== diff -u -r07955a5bcecef8840417a57c6a704ef788533581 -r7c7aa064bf7718dcdf9dc373f9eba77418cde957 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseContext/HydraulicBoundaryDatabaseQueryBuilder.cs (.../HydraulicBoundaryDatabaseQueryBuilder.cs) (revision 07955a5bcecef8840417a57c6a704ef788533581) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseContext/HydraulicBoundaryDatabaseQueryBuilder.cs (.../HydraulicBoundaryDatabaseQueryBuilder.cs) (revision 7c7aa064bf7718dcdf9dc373f9eba77418cde957) @@ -34,14 +34,27 @@ { return string.Format( "SELECT ({0} || {1}) as {2} FROM {3} LIMIT 0,1;", - GeneralTableDefinitions.NameRegion, + GeneralTableDefinitions.RegionName, GeneralTableDefinitions.CreationDate, GeneralTableDefinitions.GeneratedVersion, GeneralTableDefinitions.TableName ); } /// + /// Returns the query to get the region id from the database. + /// + /// The query to get the region id from the database. + public static string GetRegionIdQuery() + { + return string.Format( + "SELECT {0} FROM {1} LIMIT 0,1;", + GeneralTableDefinitions.RegionId, + GeneralTableDefinitions.TableName + ); + } + + /// /// Returns the query to get the amount of relevant locations from the database. /// /// The query to get the amount of relevant locations from the database. @@ -70,7 +83,7 @@ HrdLocationsTableDefinitions.YCoordinate, HrdLocationsTableDefinitions.TableName, HrdLocationsTableDefinitions.LocationTypeId // Value > 1 makes it relevant - ); + ); } } } \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseContext/HydraulicBoundarySqLiteDatabaseReader.cs =================================================================== diff -u --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseContext/HydraulicBoundarySqLiteDatabaseReader.cs (revision 0) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseContext/HydraulicBoundarySqLiteDatabaseReader.cs (revision 7c7aa064bf7718dcdf9dc373f9eba77418cde957) @@ -0,0 +1,289 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Data; +using System.Data.SQLite; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.Utils.Builders; +using Ringtoets.HydraRing.Data; +using Ringtoets.HydraRing.IO.Properties; + +namespace Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext +{ + /// + /// This class reads a SqLite database file and constructs + /// instances from this database. + /// + public class HydraulicBoundarySqLiteDatabaseReader : SqLiteDatabaseReaderBase + { + private SQLiteDataReader sqliteDataReader; + + /// + /// Creates a new instance of , + /// which will use the as its source. + /// + /// The path of the database file to open. + /// Thrown when: + /// + /// The contains invalid characters. + /// No file could be found at . + /// + /// + public HydraulicBoundarySqLiteDatabaseReader(string databaseFilePath) : base(databaseFilePath) {} + + /// + /// Gets a value indicating whether or not more hydraulic boundary locations can + /// be read using the . + /// + public bool HasNext { get; private set; } + + /// + /// Reads the next location from the database. + /// + /// New instance of , based on the + /// data read from the database or null if no data is available. + /// Thrown when the database returned incorrect + /// values for required properties. + public void PrepareReadLocation() + { + CloseDataReader(); + HasNext = false; + + string locationsQuery = HydraulicBoundaryDatabaseQueryBuilder.GetRelevantLocationsQuery(); + sqliteDataReader = CreateDataReader(locationsQuery, new SQLiteParameter + { + DbType = DbType.String + }); + MoveNext(); + } + + public HydraulicBoundaryLocation ReadLocation() + { + if (!HasNext) + { + return null; + } + + try + { + return ReadHydraulicBoundaryLocation(); + } + catch (InvalidCastException e) + { + var message = new FileReaderErrorMessageBuilder(Path). + Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + throw new LineParseException(message, e); + } + } + + /// + /// Gets the database version from the metadata table. + /// + /// The version found in the database, or an empty string if the version + /// cannot be found. + /// Thrown when the database returned incorrect + /// values for required properties. + public string GetVersion() + { + string versionQuery = HydraulicBoundaryDatabaseQueryBuilder.GetVersionQuery(); + var sqliteParameter = new SQLiteParameter + { + DbType = DbType.String + }; + using (SQLiteDataReader dataReader = CreateDataReader(versionQuery, sqliteParameter)) + { + if (!dataReader.Read()) + { + return ""; + } + + try + { + return (string) dataReader[GeneralTableDefinitions.GeneratedVersion]; + } + catch (InvalidCastException e) + { + var message = new FileReaderErrorMessageBuilder(Path). + Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + throw new LineParseException(message, e); + } + } + } + + /// + /// Gets the region id from the metadata table. + /// + /// The region id found in the database, or -1 if the region id + /// cannot be found. + /// Thrown when the database returned incorrect + /// values for required properties. + public int GetRegionId() + { + string versionQuery = HydraulicBoundaryDatabaseQueryBuilder.GetRegionIdQuery(); + var sqliteParameter = new SQLiteParameter + { + DbType = DbType.String + }; + using (SQLiteDataReader dataReader = CreateDataReader(versionQuery, sqliteParameter)) + { + if (!dataReader.Read()) + { + return -1; + } + + try + { + return Convert.ToInt32(dataReader[GeneralTableDefinitions.RegionId]); + } + catch (InvalidCastException e) + { + var message = new FileReaderErrorMessageBuilder(Path). + Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + throw new LineParseException(message, e); + } + } + } + + /// + /// Gets the amount of locations that can be read from the database. + /// + /// Thrown when the database returned incorrect + /// values for required properties. + public int GetLocationCount() + { + string locationCountQuery = HydraulicBoundaryDatabaseQueryBuilder.GetRelevantLocationsCountQuery(); + var sqliteParameter = new SQLiteParameter + { + DbType = DbType.String + }; + using (SQLiteDataReader dataReader = CreateDataReader(locationCountQuery, sqliteParameter)) + { + if (!dataReader.Read()) + { + return 0; + } + + try + { + return Convert.ToInt32(dataReader[HrdLocationsTableDefinitions.Count]); + } + catch (InvalidCastException e) + { + var message = new FileReaderErrorMessageBuilder(Path). + Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + throw new LineParseException(message, e); + } + } + } + + public override void Dispose() + { + CloseDataReader(); + base.Dispose(); + } + + /// + /// Moves the reader to the next record in the database. + /// + private void MoveNext() + { + HasNext = sqliteDataReader.Read() || (sqliteDataReader.NextResult() && sqliteDataReader.Read()); + } + + /// + /// Reads a value at column from the database. + /// + /// The expected type of value in the column with name . + /// The name of the column to read from. + /// The read value from the column with name . + /// Thrown when the value in the column was not of type + /// . + private T Read(string columnName) + { + return (T) sqliteDataReader[columnName]; + } + + /// + /// Reads the current row into a new instance of . + /// + /// A new instance of , based upon the current row. + /// Thrown when the database returned incorrect values for + /// required properties. + private HydraulicBoundaryLocation ReadHydraulicBoundaryLocation() + { + try + { + var id = Read(HrdLocationsTableDefinitions.HrdLocationId); + var name = Read(HrdLocationsTableDefinitions.Name); + var x = Read(HrdLocationsTableDefinitions.XCoordinate); + var y = Read(HrdLocationsTableDefinitions.YCoordinate); + MoveNext(); + return new HydraulicBoundaryLocation(id, name, x, y); + } + catch (InvalidCastException) + { + MoveNext(); + throw; + } + } + + /// + /// Creates a new data reader to use in this class. + /// + /// Thrown when + /// + /// Amount of locations in database could not be read. + /// A query could not be executed on the database schema. + /// + /// + private SQLiteDataReader CreateDataReader(string queryString, params SQLiteParameter[] parameters) + { + using (var query = new SQLiteCommand(Connection) + { + CommandText = queryString + }) + { + query.Parameters.AddRange(parameters); + + try + { + return query.ExecuteReader(); + } + catch (SQLiteException exception) + { + Dispose(); + var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.Error_HydraulicBoundaryLocation_read_from_database); + throw new CriticalFileReadException(message, exception); + } + } + } + + private void CloseDataReader() + { + if (sqliteDataReader != null) + { + sqliteDataReader.Dispose(); + } + } + } +} \ No newline at end of file Fisheye: Tag 7c7aa064bf7718dcdf9dc373f9eba77418cde957 refers to a dead (removed) revision in file `Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundarySqLiteDatabaseReader.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/Ringtoets.HydraRing.IO.csproj =================================================================== diff -u -r7d124cef8960a865cc8d7db24b3359f7ff9958be -r7c7aa064bf7718dcdf9dc373f9eba77418cde957 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/Ringtoets.HydraRing.IO.csproj (.../Ringtoets.HydraRing.IO.csproj) (revision 7d124cef8960a865cc8d7db24b3359f7ff9958be) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/Ringtoets.HydraRing.IO.csproj (.../Ringtoets.HydraRing.IO.csproj) (revision 7c7aa064bf7718dcdf9dc373f9eba77418cde957) @@ -50,7 +50,7 @@ - + True Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicBoundaryDatabaseContext/HydraulicBoundaryDatabaseQueryBuilderTest.cs =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicBoundaryDatabaseContext/HydraulicBoundaryDatabaseQueryBuilderTest.cs (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicBoundaryDatabaseContext/HydraulicBoundaryDatabaseQueryBuilderTest.cs (revision 7c7aa064bf7718dcdf9dc373f9eba77418cde957) @@ -0,0 +1,82 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using NUnit.Framework; +using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext; + +namespace Ringtoets.HydraRing.IO.Test.HydraulicBoundaryDatabaseContext +{ + [TestFixture] + public class HydraulicBoundaryDatabaseQueryBuilderTest + { + [Test] + public void GetVersionQuery_Always_ReturnsExpectedValues() + { + // Setup + string expectedQuery = "SELECT (NameRegion || CreationDate) as GeneratedVersion FROM General LIMIT 0,1;"; + + // Call + string query = HydraulicBoundaryDatabaseQueryBuilder.GetVersionQuery(); + + // Assert + Assert.AreEqual(expectedQuery, query); + } + + [Test] + public void GetRegionIdQuery_Always_ReturnsExpectedValues() + { + // Setup + string expectedQuery = "SELECT GeneralId FROM General LIMIT 0,1;"; + + // Call + string query = HydraulicBoundaryDatabaseQueryBuilder.GetRegionIdQuery(); + + // Assert + Assert.AreEqual(expectedQuery, query); + } + + [Test] + public void GetRelevantLocationsCountQuery_Always_ReturnsExpectedValues() + { + // Setup + string expectedQuery = "SELECT count(HRDLocationId) as nrOfRows FROM HRDLocations WHERE LocationTypeId > 1 ;"; + + // Call + string query = HydraulicBoundaryDatabaseQueryBuilder.GetRelevantLocationsCountQuery(); + + // Assert + Assert.AreEqual(expectedQuery, query); + } + + [Test] + public void GetRelevantLocationsQuery_Always_ReturnsExpectedValues() + { + // Setup + string expectedQuery = "SELECT HRDLocationId, Name, XCoordinate, YCoordinate FROM HRDLocations WHERE LocationTypeId > 1;"; + + // Call + string query = HydraulicBoundaryDatabaseQueryBuilder.GetRelevantLocationsQuery(); + + // Assert + Assert.AreEqual(expectedQuery, query); + } + } +} \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicBoundaryDatabaseContext/HydraulicBoundaryDatabaseReaderTest.cs =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicBoundaryDatabaseContext/HydraulicBoundaryDatabaseReaderTest.cs (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicBoundaryDatabaseContext/HydraulicBoundaryDatabaseReaderTest.cs (revision 7c7aa064bf7718dcdf9dc373f9eba77418cde957) @@ -0,0 +1,296 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Collections.Generic; +using System.IO; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.TestUtil; +using Core.Common.Utils.Builders; +using NUnit.Framework; +using Ringtoets.HydraRing.Data; +using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext; +using Ringtoets.HydraRing.IO.Properties; +using UtilsResources = Core.Common.Utils.Properties.Resources; + +namespace Ringtoets.HydraRing.IO.Test.HydraulicBoundaryDatabaseContext +{ + [TestFixture] + public class HydraulicBoundaryDatabaseReaderTest + { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.HydraRing.IO, "HydraulicBoundaryLocationReader"); + + [Test] + public void Constructor_NonExistingPath_ThrowsCriticalFileReadException() + { + // Setup + var testFile = Path.Combine(testDataPath, "none.sqlite"); + var expectedMessage = new FileReaderErrorMessageBuilder(testFile).Build(UtilsResources.Error_File_does_not_exist); + + // Call + TestDelegate test = () => new HydraulicBoundarySqLiteDatabaseReader(testFile).Dispose(); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + [TestCase(null)] + [TestCase("")] + public void Constructor_FileNullOrEmpty_ThrowsCriticalFileReadException(string fileName) + { + // Setup + var expectedMessage = String.Format("Fout bij het lezen van bestand '{0}': {1}", fileName, UtilsResources.Error_Path_must_be_specified); + + // Call + TestDelegate test = () => new HydraulicBoundarySqLiteDatabaseReader(fileName).Dispose(); + + // Assert + var exception = Assert.Throws(test); + + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void GetVersion_InvalidColumns_ThrowsLineParseException() + { + // Setup + var dbFile = Path.Combine(testDataPath, "corruptschema.sqlite"); + var expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + + // Precondition + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); + using (HydraulicBoundarySqLiteDatabaseReader hydraulicBoundarySqLiteDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + // Call + TestDelegate test = () => { hydraulicBoundarySqLiteDatabaseReader.GetVersion(); }; + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void Constructor_ValidFile_ExpectedValues() + { + // Setup + var dbFile = Path.Combine(testDataPath, "complete.sqlite"); + + // Call + using (HydraulicBoundarySqLiteDatabaseReader hydraulicBoundarySqLiteDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + Assert.IsFalse(hydraulicBoundarySqLiteDatabaseReader.HasNext); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void GetLocationCount_ValidFile_ExpectedValues() + { + // Setup + const int nrOfLocations = 18; + var dbFile = Path.Combine(testDataPath, "complete.sqlite"); + + // Call + using (HydraulicBoundarySqLiteDatabaseReader hydraulicBoundarySqLiteDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + Assert.AreEqual(nrOfLocations, hydraulicBoundarySqLiteDatabaseReader.GetLocationCount()); + Assert.IsFalse(hydraulicBoundarySqLiteDatabaseReader.HasNext); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void GetVersion_ValidFile_ExpectedValues() + { + // Setup + const string version = "Dutch coast South19-11-2015 12:00"; + var dbFile = Path.Combine(testDataPath, "complete.sqlite"); + + // Call + using (HydraulicBoundarySqLiteDatabaseReader hydraulicBoundarySqLiteDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + Assert.AreEqual(version, hydraulicBoundarySqLiteDatabaseReader.GetVersion()); + Assert.IsFalse(hydraulicBoundarySqLiteDatabaseReader.HasNext); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void GetRegionId_ValidFile_ExpectedValues() + { + // Setup + const int version = 13; + var dbFile = Path.Combine(testDataPath, "complete.sqlite"); + + // Call + using (HydraulicBoundarySqLiteDatabaseReader hydraulicBoundarySqLiteDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + Assert.AreEqual(version, hydraulicBoundarySqLiteDatabaseReader.GetRegionId()); + Assert.IsFalse(hydraulicBoundarySqLiteDatabaseReader.HasNext); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadLocation_InvalidColumns_ThrowsLineParseException() + { + // Setup + var dbFile = Path.Combine(testDataPath, "corruptschema.sqlite"); + var expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + + // Precondition + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); + + // Call + using (HydraulicBoundarySqLiteDatabaseReader hydraulicBoundarySqLiteDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + hydraulicBoundarySqLiteDatabaseReader.PrepareReadLocation(); + TestDelegate test = () => hydraulicBoundarySqLiteDatabaseReader.ReadLocation(); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadLocation_ValidFileReadOneLocation_ExpectedValues() + { + // Setup + var dbFile = Path.Combine(testDataPath, "complete.sqlite"); + + // Call + using (HydraulicBoundarySqLiteDatabaseReader hydraulicBoundarySqLiteDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + hydraulicBoundarySqLiteDatabaseReader.PrepareReadLocation(); + HydraulicBoundaryLocation location = hydraulicBoundarySqLiteDatabaseReader.ReadLocation(); + + // Assert + Assert.IsInstanceOf(location); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadLocation_ValidFilereadAllLocations_ExpectedValues() + { + // Setup + const int nrOfLocations = 18; + var dbFile = Path.Combine(testDataPath, "complete.sqlite"); + var boundaryLocations = new List(); + CollectionAssert.IsEmpty(boundaryLocations); + + // Call + using (HydraulicBoundarySqLiteDatabaseReader hydraulicBoundarySqLiteDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + hydraulicBoundarySqLiteDatabaseReader.PrepareReadLocation(); + for (int i = 0; i < nrOfLocations; i++) + { + boundaryLocations.Add(hydraulicBoundarySqLiteDatabaseReader.ReadLocation()); + } + + Assert.IsFalse(hydraulicBoundarySqLiteDatabaseReader.HasNext); + Assert.IsNull(hydraulicBoundarySqLiteDatabaseReader.ReadLocation()); + } + + // Assert + CollectionAssert.AllItemsAreInstancesOfType(boundaryLocations, typeof(HydraulicBoundaryLocation)); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ParameteredConstructor_PathToExistingFile_ExpectedValues() + { + // Setup + string dbFile = Path.Combine(testDataPath, "emptyschema.sqlite"); + + // Call + using (var hydraulicBoundaryDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + // Assert + Assert.AreEqual(dbFile, hydraulicBoundaryDatabaseReader.Path); + Assert.IsInstanceOf(hydraulicBoundaryDatabaseReader); + } + } + + [Test] + public void Constructor_EmptyDatabase_HasNextFalse() + { + // Setup + var dbFile = Path.Combine(testDataPath, "emptyschema.sqlite"); + + // Call + using (var hydraulicBoundaryDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile)) + { + // Assert + Assert.IsFalse(hydraulicBoundaryDatabaseReader.HasNext); + } + } + + [Test] + public void Dispose_AfterConstruction_CorrectlyReleasesFile() + { + // Setup + var dbFile = Path.Combine(testDataPath, "complete.sqlite"); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition failed: The file should be writable to begin with."); + + // Call + new HydraulicBoundarySqLiteDatabaseReader(dbFile).Dispose(); + + // Assert + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void Dispose_WhenReadLocation_CorrectlyReleasesFile() + { + // Setup + var dbFile = Path.Combine(testDataPath, "complete.sqlite"); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition failed: The file should be writable to begin with."); + + HydraulicBoundarySqLiteDatabaseReader hydraulicBoundarySqLiteDatabaseReader = null; + HydraulicBoundaryLocation boundaryLocation; + try + { + hydraulicBoundarySqLiteDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(dbFile); + hydraulicBoundarySqLiteDatabaseReader.PrepareReadLocation(); + boundaryLocation = hydraulicBoundarySqLiteDatabaseReader.ReadLocation(); + } + finally + { + // Call + if (hydraulicBoundarySqLiteDatabaseReader != null) + { + hydraulicBoundarySqLiteDatabaseReader.Dispose(); + } + } + + // Assert + Assert.NotNull(boundaryLocation); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + } +} \ No newline at end of file Fisheye: Tag 7c7aa064bf7718dcdf9dc373f9eba77418cde957 refers to a dead (removed) revision in file `Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicBoundaryDatabaseReaderTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj =================================================================== diff -u -r7d124cef8960a865cc8d7db24b3359f7ff9958be -r7c7aa064bf7718dcdf9dc373f9eba77418cde957 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj (.../Ringtoets.HydraRing.IO.Test.csproj) (revision 7d124cef8960a865cc8d7db24b3359f7ff9958be) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj (.../Ringtoets.HydraRing.IO.Test.csproj) (revision 7c7aa064bf7718dcdf9dc373f9eba77418cde957) @@ -53,7 +53,8 @@ - + + Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/HydraulicBoundaryDatabaseImporter.cs =================================================================== diff -u -r0e801c8485332bd9cdd1a907619f1f93e495ba4c -r7c7aa064bf7718dcdf9dc373f9eba77418cde957 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/HydraulicBoundaryDatabaseImporter.cs (.../HydraulicBoundaryDatabaseImporter.cs) (revision 0e801c8485332bd9cdd1a907619f1f93e495ba4c) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/HydraulicBoundaryDatabaseImporter.cs (.../HydraulicBoundaryDatabaseImporter.cs) (revision 7c7aa064bf7718dcdf9dc373f9eba77418cde957) @@ -20,15 +20,11 @@ // All rights reserved. using System; - using Core.Common.IO.Exceptions; - using log4net; - using Ringtoets.Common.Data; using Ringtoets.HydraRing.Data; -using Ringtoets.HydraRing.IO; -using Ringtoets.Integration.Data; +using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext; using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Plugin.Properties;