// Copyright (C) Stichting Deltares 2025. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI 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.Linq; using Deltares.Dam.Data; using NUnit.Framework; namespace Deltares.Dam.Tests { [TestFixture] public class TimeSerieCollectionTest { private TimeSerieCollection timeSerieCollection; #region Setup/Teardown [SetUp] public void TestSetup() { timeSerieCollection = new TimeSerieCollection(); timeSerieCollection.TimeZone = 4.75; timeSerieCollection.Version = "1.2"; var timeSerie = new TimeSerie(); timeSerie.ParameterId = "Cat speed"; timeSerie.LocationId = "LOC-04"; timeSerieCollection.Series.Add(timeSerie); timeSerie = new TimeSerie(); timeSerie.ParameterId = "Cat speed"; timeSerie.LocationId = "LOC-27"; timeSerieCollection.Series.Add(timeSerie); timeSerie = new TimeSerie(); timeSerie.ParameterId = "Cat speed"; timeSerie.LocationId = "LOC-18"; timeSerieCollection.Series.Add(timeSerie); timeSerie = new TimeSerie(); timeSerie.ParameterId = "Dog speed"; timeSerie.LocationId = "LOC-27"; timeSerieCollection.Series.Add(timeSerie); timeSerie = new TimeSerie(); timeSerie.ParameterId = "Dog speed"; timeSerie.LocationId = "LOC-04"; timeSerieCollection.Series.Add(timeSerie); timeSerie = new TimeSerie(); timeSerie.ParameterId = "Cow speed"; timeSerie.LocationId = "LOC-27"; timeSerie = new TimeSerie(); timeSerie.ParameterId = "OvertoppingRate"; timeSerie.LocationId = "LOC-27"; timeSerieCollection.Series.Add(timeSerie); } #endregion [Test] public void TestCannotFindSeries() { TimeSerie timeSerie = timeSerieCollection.FindSerie("Bird speed", "LOC-36"); Assert.That(timeSerie, Is.Null, "Series found incorrectly"); } [Test] public void TestFindSeries() { TimeSerie timeSerie = timeSerieCollection.FindSerie("Cat speed", "LOC-04"); Assert.That(timeSerie.ParameterId.Equals("Cat speed", StringComparison.InvariantCultureIgnoreCase), Is.True); Assert.That(timeSerie.LocationId.Equals("LOC-04", StringComparison.InvariantCultureIgnoreCase), Is.True); Assert.That(timeSerieCollection.Series.IndexOf(timeSerie), Is.EqualTo(0)); timeSerie = timeSerieCollection.FindSerie("Dog speed", "LOC-27"); Assert.That(timeSerie.ParameterId.Equals("Dog speed", StringComparison.InvariantCultureIgnoreCase), Is.True); Assert.That(timeSerie.LocationId.Equals("LOC-27", StringComparison.InvariantCultureIgnoreCase), Is.True); Assert.That(timeSerieCollection.Series.IndexOf(timeSerie), Is.EqualTo(3)); timeSerie = timeSerieCollection.FindSerie("OvertoppingRate", "LOC-27"); Assert.That(timeSerie.ParameterId.Equals("OvertoppingRate", StringComparison.InvariantCultureIgnoreCase), Is.True); Assert.That(timeSerie.LocationId.Equals("LOC-27", StringComparison.InvariantCultureIgnoreCase), Is.True); } [Test] public void TestSeriesAtLocation() { IEnumerable serieCollection = timeSerieCollection.GetSeriesByLocationId("LOC-04"); Assert.That(serieCollection.Count(), Is.EqualTo(2)); serieCollection = timeSerieCollection.GetSeriesByLocationId("LOC-27"); Assert.That(serieCollection.Count(), Is.EqualTo(3)); serieCollection = timeSerieCollection.GetSeriesByLocationId("LOC-36"); Assert.That(serieCollection.Count(), Is.EqualTo(0)); } [Test] public void TestSeriesOfParameter() { IEnumerable serieCollection = timeSerieCollection.GetSeriesByParameterId("Cat Speed"); Assert.That(serieCollection.Count(), Is.EqualTo(3)); serieCollection = timeSerieCollection.GetSeriesByParameterId("Dog Speed"); Assert.That(serieCollection.Count(), Is.EqualTo(2)); serieCollection = timeSerieCollection.GetSeriesByParameterId("Bird Speed"); Assert.That(serieCollection.Count(), Is.EqualTo(0)); } [Test] public void UpdateAllEntriesWorksCorrectly() { var collection = new TimeSerieCollection(); var timeSerie1 = new TimeSerie { ParameterId = "test" }; var timeSerieEntry1 = new TimeSerieEntry { Value = 1 }; var timeSerieEntry2 = new TimeSerieEntry { Value = 2 }; timeSerie1.Entries.Add(timeSerieEntry1); timeSerie1.Entries.Add(timeSerieEntry2); collection.Series.Add(timeSerie1); collection.UpdateAllEntryValues("test", x => x + 1); Assert.That(collection.Series[0].Entries[0].Value, Is.EqualTo(2)); Assert.That(collection.Series[0].Entries[1].Value, Is.EqualTo(3)); } [Test] public void UpdateAllEntriesDoesntThrowExceptionWhenParameterDoesNotExist() { Assert.DoesNotThrow(() => new TimeSerieCollection().UpdateAllEntryValues("somevalue", x => x + 1)); } [Test] public void MapWorksCorrectly() { const string locationId = "locationId"; const string parameterId = "waterlevel"; var collection = new TimeSerieCollection(); var timeSerie1 = new TimeSerie { ParameterId = parameterId, LocationId = locationId }; var timeSerieEntry1 = new TimeSerieEntry { Value = 1 }; var timeSerieEntry2 = new TimeSerieEntry { Value = 2 }; timeSerie1.Entries.Add(timeSerieEntry1); timeSerie1.Entries.Add(timeSerieEntry2); collection.Series.Add(timeSerie1); IEnumerable newCollection = collection.Map(locationId, parameterId, timeSerie => timeSerie.Map(timeSerieEntry => timeSerieEntry.Map(entry => entry.Value * 2))); Assert.That(newCollection.Count(), Is.EqualTo(1)); TimeSerie newTimeSerie = newCollection.ElementAt(0); Assert.That(newTimeSerie.Entries.Count, Is.EqualTo(2)); Assert.That(newTimeSerie.Entries[0].Value, Is.EqualTo(2)); Assert.That(newTimeSerie.Entries[1].Value, Is.EqualTo(4)); } [Test] public void MapWorksCorrectlyWithMultipeParametersAndLocations() { const string locationId1 = "location1"; const string locationId2 = "location2"; const string parameterId = "waterlevel"; var collection = new TimeSerieCollection(); var timeSerie1 = new TimeSerie { ParameterId = parameterId, LocationId = locationId1 }; var timeSerie2 = new TimeSerie { ParameterId = parameterId, LocationId = locationId2 }; var timeSerieEntry1 = new TimeSerieEntry { Value = 1 }; var timeSerieEntry2 = new TimeSerieEntry { Value = 2 }; timeSerie1.Entries.Add(timeSerieEntry1); timeSerie2.Entries.Add(timeSerieEntry2); collection.Series.Add(timeSerie1); IEnumerable newCollection = collection.Map(locationId1, parameterId, timeSerie => timeSerie.Map(timeSerieEntry => timeSerieEntry.Map(entry => entry.Value * 2))); Assert.That(newCollection.Count(), Is.EqualTo(1)); TimeSerie newTimeSerie = newCollection.ElementAt(0); Assert.That(newTimeSerie.Entries.Count, Is.EqualTo(1)); Assert.That(newTimeSerie.Entries[0].Value, Is.EqualTo(2)); } } }