From abdffbb1b91914637ded1d938d615a343f4aadd6 Mon Sep 17 00:00:00 2001 From: mfacchinelli Date: Thu, 1 Feb 2024 11:01:22 +0000 Subject: [PATCH] Update tests for science and HK for cropping --- src/data/+mag/HK.m | 4 +++- src/data/+mag/Science.m | 4 +++- tests/unit/data/tHK.m | 19 +++++++++++++++++++ tests/unit/data/tScience.m | 18 +++++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/data/+mag/HK.m b/src/data/+mag/HK.m index 1303b9da..05354ae1 100644 --- a/src/data/+mag/HK.m +++ b/src/data/+mag/HK.m @@ -52,7 +52,9 @@ function crop(this, timeFilter) this(i).Data = this(i).Data(timeFilter, :); - if ~isempty(this(i).Time) + if isempty(this(i).Time) + this(i).MetaData.Timestamp = NaT(TimeZone = mag.process.DateTime.TimeZone); + else this(i).MetaData.Timestamp = this(i).Time(1); end end diff --git a/src/data/+mag/Science.m b/src/data/+mag/Science.m index 86257966..8efe3fa5 100644 --- a/src/data/+mag/Science.m +++ b/src/data/+mag/Science.m @@ -102,7 +102,9 @@ function crop(this, timeFilter) this.Data.Properties.Events = this.Data.Properties.Events(timePeriod, :); end - if ~isempty(this.Time) + if isempty(this.Time) + this.MetaData.Timestamp = NaT(TimeZone = mag.process.DateTime.TimeZone); + else this.MetaData.Timestamp = this.Time(1); end end diff --git a/tests/unit/data/tHK.m b/tests/unit/data/tHK.m index 60ec76a6..cff33164 100644 --- a/tests/unit/data/tHK.m +++ b/tests/unit/data/tHK.m @@ -18,6 +18,25 @@ function cropMethod_timerange(testCase) testCase.cropAndVerify(data, timeFilter, expectedTimes, expectedData); end + % Test that "crop" method does not fail when no data is selected. + function cropMethod_noSelection(testCase) + + % Set up. + data = testCase.createTestData(); + + % Exercise. + data.crop(timerange(datetime("Inf", TimeZone = "local"), datetime("-Inf", TimeZone = "local"))); + + % Verify. + for i = 1:numel(data) + + testCase.verifyEmpty(data(i).IndependentVariable, "All data should be cropped out."); + testCase.verifyEmpty(data(i).DependentVariables, "All data should be cropped out."); + + testCase.verifyTrue(ismissing(data(i).MetaData.Timestamp), "All data should be cropped out."); + end + end + % Test that "resample" method can resample to a higher frequency. function resampleMethod_higherFrequency(testCase) diff --git a/tests/unit/data/tScience.m b/tests/unit/data/tScience.m index 1fb0388f..26ec90ef 100644 --- a/tests/unit/data/tScience.m +++ b/tests/unit/data/tScience.m @@ -63,6 +63,22 @@ function cropMethod_events(testCase) testCase.verifyEqual(data.Events.Time, data.Time, "Data should be cropped as expected."); end + % Test that "crop" method does not fail when no data is selected. + function cropMethod_noSelection(testCase) + + % Set up. + data = testCase.createTestData(); + + % Exercise. + data.crop(timerange(datetime("Inf", TimeZone = "local"), datetime("-Inf", TimeZone = "local"))); + + % Verify. + testCase.verifyEmpty(data.IndependentVariable, "All data should be cropped out."); + testCase.verifyEmpty(data.DependentVariables, "All data should be cropped out."); + + testCase.verifyTrue(ismissing(data.MetaData.Timestamp), "All data should be cropped out."); + end + % Test that "resample" method can resample to a lower frequency. function resampleMethod_lowerFrequency(testCase) @@ -189,7 +205,7 @@ function cropAndVerify(testCase, data, timeFilter, expectedTimes, expectedData) function [data, rawData] = createTestData() rawData = timetable(datetime("now", TimeZone = "UTC") + minutes(1:10)', (1:10)', (11:20)', (21:30)', 3 * ones(10, 1), (1:10)', VariableNames = ["x", "y", "z", "range", "sequence"]); - data = mag.Science(rawData, mag.meta.Science()); + data = mag.Science(rawData, mag.meta.Science(Timestamp = datetime("now", TimeZone = "UTC"))); end end end