Skip to content

Commit

Permalink
Add more tests for HK display
Browse files Browse the repository at this point in the history
  • Loading branch information
mfacchinelli committed Feb 2, 2024
1 parent 0dee10a commit 97c0a61
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 56 deletions.
6 changes: 3 additions & 3 deletions tests/unit/data/tData.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ function copyMethod(testCase)
data = testCase.createTestData();

% Exercise.
dataCopy = data.copy();
copiedData = data.copy();

% Verify.
testCase.verifyNotSameHandle(data, dataCopy, "Copied data should be different instance.");
testCase.verifyNotSameHandle(data.MetaData, dataCopy.MetaData, "Copied data should be different instance.");
testCase.verifyNotSameHandle(data, copiedData, "Copied data should be different instance.");
testCase.verifyNotSameHandle(data.MetaData, copiedData.MetaData, "Copied data should be different instance.");
end
end

Expand Down
129 changes: 76 additions & 53 deletions tests/unit/data/tHK.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,113 +11,113 @@
function cropMethod_timerange(testCase)

% Set up.
data = testCase.createTestData();
hk = testCase.createTestData();

expectedTimes = {data(1).Time(3:end), data(2).Time(2:end)};
expectedData = {data(1).DependentVariables(3:end, :), data(2).DependentVariables(2:end, :)};
expectedTimes = {hk(1).Time(3:end), hk(2).Time(2:end)};
expectedData = {hk(1).DependentVariables(3:end, :), hk(2).DependentVariables(2:end, :)};

timeFilter = timerange(data(1).Time(2), data(1).Time(end), "openleft");
timeFilter = timerange(hk(1).Time(2), hk(1).Time(end), "openleft");

% Exercise and verify.
testCase.cropAndVerify(data, timeFilter, expectedTimes, expectedData);
testCase.cropAndVerify(hk, 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();
hk = testCase.createTestData();

% Exercise.
data.crop(timerange(datetime("Inf", TimeZone = "local"), datetime("-Inf", TimeZone = "local")));
hk.crop(timerange(datetime("Inf", TimeZone = "local"), datetime("-Inf", TimeZone = "local")));

% Verify.
for i = 1:numel(data)
for i = 1:numel(hk)

testCase.verifyEmpty(data(i).IndependentVariable, "All data should be cropped out.");
testCase.verifyEmpty(data(i).DependentVariables, "All data should be cropped out.");
testCase.verifyEmpty(hk(i).IndependentVariable, "All data should be cropped out.");
testCase.verifyEmpty(hk(i).DependentVariables, "All data should be cropped out.");

testCase.verifyTrue(ismissing(data(i).MetaData.Timestamp), "All data should be cropped out.");
testCase.verifyTrue(ismissing(hk(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)

% Set up.
data = testCase.createTestData();
data = data(1);
hk = testCase.createTestData();
hk = hk(1);

initialFrequency = 1 / seconds(mode(data.dT));
initialFrequency = 1 / seconds(mode(hk.dT));

% Exercise.
resampledData = data.copy();
resampledData = hk.copy();
resampledData.resample(2 * initialFrequency);

% Verify.
testCase.assertEqual(height(resampledData.IndependentVariable), (2 * height(data.Time)) - 1, "Frequency should be halved.");
testCase.verifyEqual(resampledData.Time, (data.Time(1):seconds(30):data.Time(end))', "Frequency should be halved.");
testCase.assertEqual(height(resampledData.IndependentVariable), (2 * height(hk.Time)) - 1, "Frequency should be halved.");
testCase.verifyEqual(resampledData.Time, (hk.Time(1):seconds(30):hk.Time(end))', "Frequency should be halved.");

testCase.assertEqual(height(resampledData.DependentVariables), (2 * height(data.DependentVariables)) - 1, "Frequency should be halved.");
testCase.assertEqual(height(resampledData.DependentVariables), (2 * height(hk.DependentVariables)) - 1, "Frequency should be halved.");
end

% Test that "downsample" method can resample to a lower frequency.
function downsampleMethod_lowerFrequency(testCase)

% Set up.
data = testCase.createTestData();
data = data(1);
hk = testCase.createTestData();
hk = hk(1);

initialFrequency = 1 / seconds(mode(data.dT));
initialFrequency = 1 / seconds(mode(hk.dT));

% Exercise.
resampledData = data.copy();
resampledData = hk.copy();
resampledData.downsample(initialFrequency / 2);

% Verify.
testCase.assertEqual(height(resampledData.IndependentVariable), height(data.Time) / 2, "Frequency should be halved.");
testCase.verifyEqual(resampledData.Time, (data.Time(1):minutes(2):data.Time(end))', "Frequency should be halved.");
testCase.assertEqual(height(resampledData.IndependentVariable), height(hk.Time) / 2, "Frequency should be halved.");
testCase.verifyEqual(resampledData.Time, (hk.Time(1):minutes(2):hk.Time(end))', "Frequency should be halved.");

testCase.assertEqual(height(resampledData.DependentVariables), height(data.DependentVariables) / 2, "Frequency should be halved.");
testCase.assertEqual(height(resampledData.DependentVariables), height(hk.DependentVariables) / 2, "Frequency should be halved.");
end

% Test that "getHKType" returns empty on empty input.
function getHKType_empty(testCase)

% Set up.
data = mag.hk.Power.empty();
hk = mag.hk.Power.empty();

% Exercise.
hk = data.getHKType("PROCSTAT");
selectedHK = hk.getHKType("PROCSTAT");

% Verify.
testCase.verifyEmpty(hk, "Empty should be returned for empty input.");
testCase.verifyEmpty(selectedHK, "Empty should be returned for empty input.");
end

% Test that "getHKType" method returns Power HK by default.
function getHKType_default(testCase)

% Set up.
data = testCase.createTestData();
hk = testCase.createTestData();

% Exercise.
hk = data.getHKType();
selectedHK = hk.getHKType();

% Verify.
testCase.verifyEmpty(hk, "Empty should be returned when no such type exists.");
testCase.verifyEmpty(selectedHK, "Empty should be returned when no such type exists.");
end

% Test that "getHKType" method selects the correct type.
function getHKType(testCase)

% Set up.
data = testCase.createTestData();
hk = testCase.createTestData();

% Exercise.
hk = data.getHKType("PROCSTAT");
selectedHK = hk.getHKType("PROCSTAT");

% Verify.
testCase.verifyClass(hk, "mag.hk.Processor", "Correct type should be returned.");
testCase.verifyClass(selectedHK, "mag.hk.Processor", "Correct type should be returned.");
end

% Test that all dependent properties of the HK types supported can
Expand All @@ -127,29 +127,52 @@ function dependentProperties(~, HKTypes)
% Set up.
fileName = fullfile(fileparts(mfilename("fullpath")), "../../data", HKTypes + ".csv");

data = readtimetable(fileName);
data.Properties.DimensionNames{1} = 't';
hk = readtimetable(fileName);
hk.Properties.DimensionNames{1} = 't';

metaClass = meta.class.fromName("mag.hk." + HKTypes);
properties = metaClass.PropertyList;

% Exercise and verify.
data = mag.hk.(HKTypes)(data, mag.meta.HK());
hk = mag.hk.(HKTypes)(hk, mag.meta.HK());

for p = properties([properties.Dependent] & cellfun(@(x) isequal(x, "public"), {properties.GetAccess}))'
data.(p.Name);
hk.(p.Name);
end
end

% Test that displaying a deleted handle does not error.
function customDisplay_deleted(testCase)

% Set up.
hk = testCase.createTestData();
hk = hk(1);

delete(hk);

% Exercise and verify.
evalc("display(hk)");
end

% Test that displaying an empty array does not error.
function customDisplay_empty(~)

% Set up.
hk = mag.hk.Power.empty(); %#ok<NASGU>

% Exercise and verify.
evalc("display(hk)");
end

% Test that displaying a single object displays the correct
% information.
function customDisplay_singleObject(testCase)

% Set up.
data = testCase.createTestData(); %#ok<NASGU>
hk = testCase.createTestData(); %#ok<NASGU>

% Exercise.
output = evalc("display(data(1))");
output = evalc("display(hk(1))");

% Verify.
testCase.verifySubstring(eraseTags(output), "Status HK (STATUS)", "HK meta data should be included in display.");
Expand All @@ -159,45 +182,45 @@ function customDisplay_singleObject(testCase)
function customDisplay_heterogeneous(testCase)

% Set up.
data = testCase.createTestData();
hk = testCase.createTestData(); %#ok<NASGU>

% Exercise and verify.
display(data);
evalc("display(hk)");
end
end

methods (Access = private)

function cropAndVerify(testCase, data, timeFilter, expectedTimes, expectedData)
function cropAndVerify(testCase, hk, timeFilter, expectedTimes, expectedData)

% Exercise.
data.crop(timeFilter);
hk.crop(timeFilter);

% Verify.
for i = 1:numel(data)
for i = 1:numel(hk)

testCase.assertSize(data(i).IndependentVariable, size(expectedTimes{i}), "Data should be cropped as expected.");
testCase.verifyEqual(data(i).Time, expectedTimes{i}, "Data should be cropped as expected.");
testCase.assertSize(hk(i).IndependentVariable, size(expectedTimes{i}), "Data should be cropped as expected.");
testCase.verifyEqual(hk(i).Time, expectedTimes{i}, "Data should be cropped as expected.");

testCase.assertSize(data(i).DependentVariables, size(expectedData{i}), "Data should be cropped as expected.");
testCase.verifyEqual(data(i).DependentVariables, expectedData{i}, "Data should be cropped as expected.");
testCase.assertSize(hk(i).DependentVariables, size(expectedData{i}), "Data should be cropped as expected.");
testCase.verifyEqual(hk(i).DependentVariables, expectedData{i}, "Data should be cropped as expected.");

testCase.verifyEqual(data(i).MetaData.Timestamp, data(i).Time(1), "Meta data timestamp should be updated.");
testCase.verifyEqual(hk(i).MetaData.Timestamp, hk(i).Time(1), "Meta data timestamp should be updated.");
end
end
end

methods (Static, Access = private)

function data = createTestData()
function hk = createTestData()

timestamps = datetime("now", TimeZone = "UTC") + minutes(1:10)';

statusData = timetable(timestamps, ones(10, 1), zeros(10, 1), VariableNames = ["FOBSTAT", "FIBSTAT"]);
procstatData = timetable(timestamps(1:2:end), (1:5)', (11:15)', VariableNames = ["OBNQ_NUM_MSG", "IBNQ_NUM_MSG"]);

data(1) = mag.hk.Status(statusData, mag.meta.HK(Type = "STATUS"));
data(2) = mag.hk.Processor(procstatData, mag.meta.HK(Type = "PROCSTAT"));
hk(1) = mag.hk.Status(statusData, mag.meta.HK(Type = "STATUS"));
hk(2) = mag.hk.Processor(procstatData, mag.meta.HK(Type = "PROCSTAT"));
end
end
end

0 comments on commit 97c0a61

Please sign in to comment.