// 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.SQLite; using System.IO; using Core.Common.Base.Data; using Core.Common.TestUtil; using NUnit.Framework; using Rhino.Mocks; namespace Application.Ringtoets.Storage.TestUtil.Test { [TestFixture] public class SqLiteDatabaseHelperTest { private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Storage, "DatabaseFiles"); [Test] public void CreateDatabaseFile_NullPath_ThrowsArgumentNullException() { // Setup const string validScript = ";"; // Call TestDelegate test = () => SqLiteDatabaseHelper.CreateDatabaseFile(null, validScript); // Assert Assert.Throws(test); } [Test] public void CreateDatabaseFile_ValidPathNullScript_ThrowsArgumentNullException() { // Setup const string validPath = "tempFile"; // Call TestDelegate test = () => SqLiteDatabaseHelper.CreateDatabaseFile(validPath, null); // Assert Assert.Throws(test); } [Test] public void CreateDatabaseFile_ValidPathInvalidScript_ThrowsSQLiteException() { // Setup string validPath = Path.Combine(testDataPath, "tempFile.rtd"); const string invalidScript = "SELECT '' FROM *;"; // Call try { SqLiteDatabaseHelper.CreateDatabaseFile(validPath, invalidScript); // Assert Assert.Fail("Should have thrown 'SQLiteException'"); } catch (SQLiteException) {} finally { SqLiteDatabaseHelper.TearDownTempFile(validPath); } } [Test] public void CreateDatabaseFile_ValidPathValidScript_CreatesValidFile() { // Setup string validPath = Path.Combine(testDataPath, "tempFile.rtd"); const string validScript = "select * from sqlite_master;"; // Call try { SqLiteDatabaseHelper.CreateDatabaseFile(validPath, validScript); // Assert File.Exists(validPath); } catch (SQLiteException) { // Assert Assert.Fail("Should not throw 'SQLiteException'"); } finally { SqLiteDatabaseHelper.TearDownTempFile(validPath); } } [Test] public void CreateDatabaseFile_FileAreadyExists_OverwriteFile() { // Setup string validPath = Path.Combine(testDataPath, "tempFile.rtd"); const string validScript = "select * from sqlite_master;"; // Precondition SQLiteConnection.CreateFile(validPath); // Call try { SqLiteDatabaseHelper.CreateDatabaseFile(validPath, validScript); // Assert File.Exists(validPath); } catch (SQLiteException) { // Assert Assert.Fail("Should not throw 'SQLiteException'"); } finally { SqLiteDatabaseHelper.TearDownTempFile(validPath); } } [Test] public void CreateDatabaseFile_FileAreadyExistsAndLocked_Fail() { // Setup string validPath = Path.Combine(testDataPath, "tempFile.rtd"); const string validScript = ";"; // Precondition try { using (File.Create(validPath)) { // Call TestDelegate test = () => SqLiteDatabaseHelper.CreateDatabaseFile(validPath, validScript); // Assert Assert.Throws(test); } } finally { SqLiteDatabaseHelper.TearDownTempFile(validPath); } } [Test] public void CreateValidRingtoetsDatabase_NullPath_ThrowsArgumentException() { // Setup MockRepository mockRepository = new MockRepository(); var projectMock = mockRepository.StrictMock(); // Call TestDelegate test = () => SqLiteDatabaseHelper.CreateValidRingtoetsDatabase(null, projectMock); // Assert Assert.Throws(test); } [Test] public void CreateValidRingtoetsDatabase_NullProject_ThrowsArgumentNullException() { // Setup string validPath = Path.Combine(testDataPath, "tempFile.rtd"); // Call TestDelegate test = () => SqLiteDatabaseHelper.CreateValidRingtoetsDatabase(validPath, null); // Assert Assert.Throws(test); // TearDown SqLiteDatabaseHelper.TearDownTempFile(validPath); } [Test] public void CreateValidRingtoetsDatabase_ValidArguments_SavesProjectToFile() { // Setup string validPath = Path.Combine(testDataPath, "tempFile.rtd"); MockRepository mockRepository = new MockRepository(); var projectMock = mockRepository.StrictMock(); // Call TestDelegate test = () => SqLiteDatabaseHelper.CreateValidRingtoetsDatabase(validPath, projectMock); // Assert Assert.DoesNotThrow(test); Assert.IsTrue(File.Exists(validPath)); // TearDown SqLiteDatabaseHelper.TearDownTempFile(validPath); } [Test] public void GetCorruptSchema_Always_ScriptThatOnlyGeneratesEnoughToPassValidation() { // Setup const string expectedCreateVersionTable = "CREATE TABLE Version "; const string notExpectedCreateProjectEntityTable = "CREATE TABLE ProjectEntity "; // Call string query = SqLiteDatabaseHelper.GetCorruptSchema(); // Assert Assert.IsNotNullOrEmpty(query); Assert.IsTrue(query.Contains(expectedCreateVersionTable)); Assert.IsFalse(query.Contains(notExpectedCreateProjectEntityTable)); } } }