// Copyright (C) Stichting Deltares 2018. 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
{
#region Setup/Teardown
[SetUp]
public void TestSetup()
{
this.timeSerieCollection = new TimeSerieCollection();
timeSerieCollection.TimeZone = 4.75;
timeSerieCollection.Version = "1.2";
TimeSerie 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
private TimeSerieCollection timeSerieCollection;
[Test]
public void TestCannotFindSeries()
{
TimeSerie timeSerie = this.timeSerieCollection.FindSerie("Bird speed", "LOC-36");
Assert.IsNull(timeSerie, "Series found incorrectly");
}
[Test]
public void TestFindSeries()
{
TimeSerie timeSerie = this.timeSerieCollection.FindSerie("Cat speed", "LOC-04");
Assert.IsTrue(timeSerie.ParameterId.Equals("Cat speed", StringComparison.InvariantCultureIgnoreCase));
Assert.IsTrue(timeSerie.LocationId.Equals("LOC-04", StringComparison.InvariantCultureIgnoreCase));
Assert.AreEqual(0, this.timeSerieCollection.Series.IndexOf(timeSerie));
timeSerie = this.timeSerieCollection.FindSerie("Dog speed", "LOC-27");
Assert.IsTrue(timeSerie.ParameterId.Equals("Dog speed", StringComparison.InvariantCultureIgnoreCase));
Assert.IsTrue(timeSerie.LocationId.Equals("LOC-27", StringComparison.InvariantCultureIgnoreCase));
Assert.AreEqual(3, this.timeSerieCollection.Series.IndexOf(timeSerie));
timeSerie = this.timeSerieCollection.FindSerie("OvertoppingRate", "LOC-27");
Assert.IsTrue(timeSerie.ParameterId.Equals("OvertoppingRate", StringComparison.InvariantCultureIgnoreCase));
Assert.IsTrue(timeSerie.LocationId.Equals("LOC-27", StringComparison.InvariantCultureIgnoreCase));
}
[Test]
public void TestSeriesAtLocation()
{
IEnumerable serieCollection = this.timeSerieCollection.GetSeriesByLocationId("LOC-04");
Assert.AreEqual(2, serieCollection.Count());
serieCollection = this.timeSerieCollection.GetSeriesByLocationId("LOC-27");
Assert.AreEqual(3, serieCollection.Count());
serieCollection = this.timeSerieCollection.GetSeriesByLocationId("LOC-36");
Assert.AreEqual(0, serieCollection.Count());
}
[Test]
public void TestSeriesOfParameter()
{
IEnumerable serieCollection = this.timeSerieCollection.GetSeriesByParameterId("Cat Speed");
Assert.AreEqual(3, serieCollection.Count());
serieCollection = this.timeSerieCollection.GetSeriesByParameterId("Dog Speed");
Assert.AreEqual(2, serieCollection.Count());
serieCollection = this.timeSerieCollection.GetSeriesByParameterId("Bird Speed");
Assert.AreEqual(0, serieCollection.Count());
}
[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.AreEqual(2, collection.Series[0].Entries[0].Value);
Assert.AreEqual(3, collection.Series[0].Entries[1].Value);
}
[Test]
public void UpdateAllEntriesDoesntThrowExceptionWhenParameterDoesNotExist()
{
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);
var newCollection = collection.Map(locationId, parameterId,
timeSerie => timeSerie.Map(timeSerieEntry => timeSerieEntry.Map(entry => entry.Value * 2)));
Assert.AreEqual(1, newCollection.Count());
var newTimeSerie = newCollection.ElementAt(0);
Assert.AreEqual(2, newTimeSerie.Entries.Count);
Assert.AreEqual(2, newTimeSerie.Entries[0].Value);
Assert.AreEqual(4, newTimeSerie.Entries[1].Value);
}
[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);
var newCollection = collection.Map(locationId1, parameterId,
timeSerie => timeSerie.Map(timeSerieEntry => timeSerieEntry.Map(entry => entry.Value * 2)));
Assert.AreEqual(1, newCollection.Count());
var newTimeSerie = newCollection.ElementAt(0);
Assert.AreEqual(1, newTimeSerie.Entries.Count);
Assert.AreEqual(2, newTimeSerie.Entries[0].Value);
}
}
}