Skip to content

Commit

Permalink
Merge pull request #12 from davidclemens/add-table-methods
Browse files Browse the repository at this point in the history
Add `readTableFile` & `writeTableFile`
  • Loading branch information
davidclemens authored Oct 8, 2022
2 parents 02b7fc0 + c56bf11 commit a5ffff3
Show file tree
Hide file tree
Showing 18 changed files with 1,107 additions and 1 deletion.
234 changes: 234 additions & 0 deletions +Tests/+table/readTableFile_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
classdef (SharedTestFixtures = { ...
matlab.unittest.fixtures.PathFixture(subsref(strsplit(mfilename('fullpath'),'/+'),substruct('{}',{':'})))
}) readTableFile_test < matlab.unittest.TestCase
% readTableFile_test Unittests for table.readTableFile
% This class holds the unittests for the table.readTableFile function.
%
% It can be run with runtests('Tests.table.readTableFile_test').
%
%
% Copyright (c) 2022-2022 David Clemens ([email protected])
%

properties
RessourcePath = [fileparts(mfilename('fullpath')),'/ressources/']
end

methods (Test)
function testLogical(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileLogical.xlsx'];

% Read it
T = table.readTableFile(filename);

% Only compare the actual data. Metadata is tested elsewhere
actual = T{:,:};
expected = false(19,3);
expected([1,4:8,14:16],1) = true;
expected(3:19,3) = true;

testCase.verifyEqual(actual,expected)
end
function testCategorical(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileCategorical.xlsx'];

% Read it
T = table.readTableFile(filename);

% Only compare the actual data. Metadata is tested elsewhere
actual = T{:,:};
expected = categorical({
'Cat1', '', 'Cat1';...
'Cat2', '', 'Cat2';...
'', '', 'Cat1';...
'Cat3', '', 'Cat2';...
'4', '', 'Cat1';...
'-10', '', 'Cat2';...
'4', '', 'Cat1';...
'-10', '', 'Cat2';...
'44562', '', 'Cat1';...
'01.01.22', '', 'Cat2';...
'', '', 'Cat1';...
'', '', 'Cat2';...
'undefined','', 'Cat1';...
'', '', 'Cat2'});

testCase.verifyEqual(actual,expected)
end
function testDatetime(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileDatetime.xlsx'];

% Read it
T = table.readTableFile(filename);

% Only compare the actual data. Metadata is tested elsewhere
actual = T{:,:};
expected = reshape(datetime([
2022 1 1 0 0 0
2022 1 1 10 34 1
NaN NaN NaN NaN NaN NaN
2020 7 18 0 0 0
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
Inf Inf Inf Inf Inf Inf
-Inf -Inf -Inf -Inf -Inf -Inf
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
2022 1 1 0 0 0
2022 1 1 0 0 0
2022 1 1 0 0 0
1890 6 1 0 0 0
1900 1 1 0 0 0
1904 1 1 0 0 0
2022 1 1 0 0 0
2022 1 1 0 0 0]),[],3);

testCase.verifyEqual(actual,expected)
end
function testCellstr(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileCellstr.xlsx'];

% Read it
T = table.readTableFile(filename);

% Only compare the actual data. Metadata is tested elsewhere
actual = T{:,:};
expected = {
'1', '', 'abc';
'', '', 'abc';
'test', '', 'def';
'string', '', 'abc';
'1', '', 'def';
'-2', '', 'def';
'', '', 'def';
'', '', 'abc'};

testCase.verifyEqual(actual,expected)
end
function testNumeric(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileNumeric.xlsx'];

% Read it
T = table.readTableFile(filename);

% Only compare the actual data. Metadata is tested elsewhere
expectedClasses = {'double','single','uint16','int8'};
expectedValues = { ...
double([
1
2
NaN
38010000
31341
NaN
Inf
-Inf]),...
single([
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN]),...
uint16([
1
2
3
4
65535
6
7
8]),...
int8([
1
2
3
4
127
6
0
-8])
};
actualClasses = cell(1,size(T,2));
actualValues = cell(1,size(T,2));
for col = 1:size(T,2)
actualClasses{col} = class(T{1,col});
actualValues{col} = T{:,col};
testCase.verifyEqual(actualValues{col},expectedValues{col})
end

testCase.verifyEqual(actualClasses,expectedClasses)
end
function testSkipColumn(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileSkipColumn.xlsx'];

% Read it
T = table.readTableFile(filename);

% Only compare the actual data. Metadata is tested elsewhere
actual = T{:,:};
expected = {
'1', 'abc';
'', 'abc';
'test', 'def';
'string', 'abc';
'1', 'def';
'-2', 'def';
'', 'def';
'', 'abc'};

testCase.verifyEqual(actual,expected)
end
function testEmptyTable(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileEmptyTable.xlsx'];

% Read it
T = table.readTableFile(filename);

% Only compare the actual data. Metadata is tested elsewhere
actual = T{:,:};
expected = cell(0,3);

testCase.verifyEqual(actual,expected)
end
function testErrorEmptyFile(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileEmptyFile.xlsx'];

% Test error
errorId = 'Utilities:table:readTableFile:MissingHeader';
testCase.verifyError(@() table.readTableFile(filename),errorId)
end
function testErrorInvalidFile(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileInvalidFile.xlsx'];

% Test error
errorId = 'Utilities:table:readTableFile:InvalidFile';
testCase.verifyError(@() table.readTableFile(filename),errorId)
end
function testErrorNonExcelDateWithoutFormatSpec(testCase)
% Define the file with the test data
filename = [testCase.RessourcePath,'tableFileErrorNonExcelDateWithoutFormatSpec.xlsx'];

% Test error
errorId = 'Utilities:table:readTableFile:NonExcelDateWithoutFormatSpec';
testCase.verifyError(@() table.readTableFile(filename),errorId)
end
end
end
3 changes: 3 additions & 0 deletions +Tests/+table/ressources/tableFileCategorical.xlsx
Git LFS file not shown
3 changes: 3 additions & 0 deletions +Tests/+table/ressources/tableFileCellstr.xlsx
Git LFS file not shown
3 changes: 3 additions & 0 deletions +Tests/+table/ressources/tableFileDatetime.xlsx
Git LFS file not shown
3 changes: 3 additions & 0 deletions +Tests/+table/ressources/tableFileEmptyFile.xlsx
Git LFS file not shown
3 changes: 3 additions & 0 deletions +Tests/+table/ressources/tableFileEmptyTable.xlsx
Git LFS file not shown
Git LFS file not shown
3 changes: 3 additions & 0 deletions +Tests/+table/ressources/tableFileLogical.xlsx
Git LFS file not shown
3 changes: 3 additions & 0 deletions +Tests/+table/ressources/tableFileNumeric.xlsx
Git LFS file not shown
3 changes: 3 additions & 0 deletions +Tests/+table/ressources/tableFileSkipColumn.xlsx
Git LFS file not shown
86 changes: 86 additions & 0 deletions +Tests/+table/writeTableFile_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
classdef (SharedTestFixtures = { ...
matlab.unittest.fixtures.PathFixture(subsref(strsplit(mfilename('fullpath'),'/+'),substruct('{}',{':'})))
}) writeTableFile_test < matlab.unittest.TestCase
% writeTableFile_test Unittests for table.writeTableFile
% This class holds the unittests for the table.writeTableFile function.
%
% It can be run with runtests('Tests.table.writeTableFile_test').
%
%
% Copyright (c) 2022-2022 David Clemens ([email protected])
%

properties
RessourcePath = [fileparts(mfilename('fullpath')),'/ressources/']
WriteFolder char
end

methods(TestMethodSetup)
function setupWriteFolder(testCase)
import matlab.unittest.fixtures.TemporaryFolderFixture

writeFolderFixture = testCase.applyFixture(TemporaryFolderFixture);
testCase.WriteFolder = writeFolderFixture.Folder;
end
end

methods (Test)
function testReadWriteIntegrationCategorical(testCase)
% Define the file with the test data
readFilename = 'tableFileCategorical.xlsx';

% Get actual and expected tables
[actual,expected] = testReadWriteIntegration(testCase,readFilename);

testCase.verifyEqual(actual,expected)
end
function testReadWriteIntegrationCellstr(testCase)
% Define the file with the test data
readFilename = 'tableFileCellstr.xlsx';

% Get actual and expected tables
[actual,expected] = testReadWriteIntegration(testCase,readFilename);

testCase.verifyEqual(actual,expected)
end
function testReadWriteIntegrationDatetime(testCase)
% Define the file with the test data
readFilename = 'tableFileDatetime.xlsx';

% Get actual and expected tables
[actual,expected] = testReadWriteIntegration(testCase,readFilename);

testCase.verifyEqual(actual,expected)
end
function testReadWriteIntegrationLogical(testCase)
% Define the file with the test data
readFilename = 'tableFileLogical.xlsx';

% Get actual and expected tables
[actual,expected] = testReadWriteIntegration(testCase,readFilename);

testCase.verifyEqual(actual,expected)
end
function testReadWriteIntegrationNumeric(testCase)
% Define the file with the test data
readFilename = 'tableFileNumeric.xlsx';

% Get actual and expected tables
[actual,expected] = testReadWriteIntegration(testCase,readFilename);

testCase.verifyEqual(actual,expected)
end
end
end

function [actual,expected] = testReadWriteIntegration(testCase,filename)
% Define the file with the test data
readFilename = [testCase.RessourcePath,filename];

% Define the temporary write file
writeFilename = [testCase.WriteFolder,'/',filename];

expected = table.readTableFile(readFilename);
table.writeTableFile(expected,writeFilename)
actual = table.readTableFile(writeFilename);
end
Loading

0 comments on commit a5ffff3

Please sign in to comment.