Skip to content

Commit 9bd4cc8

Browse files
authored
apacheGH-37577: [MATLAB] Create a superclass for DateType-related MATLAB tests (apache#46923)
### Rationale for this change Many of the tests for `Date32Type` and `Date64Type` are similar. To reduce code duplication, we could consider adding a shared superclass for `DateType`-related tests. ### What changes are included in this PR? 1. Added abstract MATLAB test class `hDataType`. This class contains shared unit tests for `arrow.type.Date32Type` and `arrow.type.Date64Type`. 2. Updated both `tDate32Type` and `tDate64Type` to inherit from `hDataType`. ### Are these changes tested? Yes. ### Are there any user-facing changes? No. * GitHub Issue: apache#37577 Authored-by: Sarah Gilmore <[email protected]> Signed-off-by: Sarah Gilmore <[email protected]>
1 parent c5fdc8e commit 9bd4cc8

File tree

3 files changed

+98
-136
lines changed

3 files changed

+98
-136
lines changed

matlab/test/arrow/type/hDateType.m

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
% Test class that contains shared unit tests for classes that subclass
2+
% arrow.type.DateType.
3+
4+
% Licensed to the Apache Software Foundation (ASF) under one or more
5+
% contributor license agreements. See the NOTICE file distributed with
6+
% this work for additional information regarding copyright ownership.
7+
% The ASF licenses this file to you under the Apache License, Version
8+
% 2.0 (the "License"); you may not use this file except in compliance
9+
% with the License. You may obtain a copy of the License at
10+
%
11+
% http://www.apache.org/licenses/LICENSE-2.0
12+
%
13+
% Unless required by applicable law or agreed to in writing, software
14+
% distributed under the License is distributed on an "AS IS" BASIS,
15+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16+
% implied. See the License for the specific language governing
17+
% permissions and limitations under the License.
18+
19+
classdef hDateType < hFixedWidthType
20+
21+
properties(Abstract)
22+
DefaultDateUnit(1, 1) arrow.type.DateUnit
23+
ConstructionFcn
24+
ClassConstructorFcn
25+
end
26+
27+
methods (Test)
28+
function TestClass(testCase)
29+
% Verify ArrowType is an instance of the expected class type.
30+
name = string(class(testCase.ArrowType()));
31+
testCase.verifyEqual(name, testCase.ClassName);
32+
end
33+
34+
function DateUnitNoSetter(testCase)
35+
% Verify an exception is thrown when attempting to modify
36+
% the DateUnit property.
37+
type = testCase.ConstructionFcn();
38+
testCase.verifyError(@() setfield(type, "DateUnit", "Millisecond"), "MATLAB:class:SetProhibited");
39+
end
40+
41+
function InvalidProxy(testCase)
42+
% Verify that an exception is thrown when a Proxy of an
43+
% unexpected type is passed to the DateType's constructor.
44+
array = arrow.array([1, 2, 3]);
45+
proxy = array.Proxy;
46+
testCase.verifyError(@() testCase.ClassConstructorFcn(proxy), "arrow:proxy:ProxyNameMismatch");
47+
end
48+
49+
function IsEqualTrue(testCase)
50+
% Verifies the isequal method returns true if both conditions
51+
% listed below are satisfied:
52+
%
53+
% 1. All input arguments have the same class type (either
54+
% arrow.type.Date32Type or arrow.type.Date64Type).
55+
% 2. All inputs have the same size.
56+
57+
% Scalar arrays
58+
dateType1 = testCase.ConstructionFcn();
59+
dateType2 = testCase.ConstructionFcn();
60+
testCase.verifyTrue(isequal(dateType1, dateType2));
61+
62+
% Non-scalar arrays
63+
typeArray1 = [dateType1 dateType1];
64+
typeArray2 = [dateType2 dateType2];
65+
testCase.verifyTrue(isequal(typeArray1, typeArray2));
66+
end
67+
68+
function IsEqualFalse(testCase)
69+
% Verifies the isequal method returns false when at least of
70+
% these conditions is not satisfied:
71+
%
72+
% 1. All input arguments have the same class type (either
73+
% arrow.type.Date32Type or arrow.type.Date64Type).
74+
% 2. All inputs have the same size.
75+
76+
% Pass a different arrow.type.Type subclass to isequal.
77+
dateType = testCase.ConstructionFcn();
78+
int32Type = arrow.int32();
79+
testCase.verifyFalse(isequal(dateType, int32Type));
80+
testCase.verifyFalse(isequal([dateType dateType], [int32Type int32Type]));
81+
82+
% The array sizes are not equal.
83+
typeArray1 = [dateType dateType];
84+
typeArray2 = [dateType dateType]';
85+
testCase.verifyFalse(isequal(typeArray1, typeArray2));
86+
end
87+
88+
end
89+
90+
end

matlab/test/arrow/type/tDate32Type.m

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,80 +15,16 @@
1515
% implied. See the License for the specific language governing
1616
% permissions and limitations under the License.
1717

18-
classdef tDate32Type < hFixedWidthType
18+
classdef tDate32Type < hDateType
1919

2020
properties
21-
ConstructionFcn = @arrow.date32
2221
ArrowType = arrow.date32
2322
TypeID = arrow.type.ID.Date32
2423
BitWidth = int32(32)
2524
ClassName = "arrow.type.Date32Type"
26-
end
27-
28-
methods(Test)
29-
function TestClass(testCase)
30-
% Verify ArrowType is an object of the expected class type.
31-
name = string(class(testCase.ArrowType));
32-
testCase.verifyEqual(name, testCase.ClassName);
33-
end
34-
35-
function DefaultDateUnit(testCase)
36-
% Verify the default DateUnit is Day.
37-
type = testCase.ArrowType;
38-
actualUnit = type.DateUnit;
39-
expectedUnit = arrow.type.DateUnit.Day;
40-
testCase.verifyEqual(actualUnit, expectedUnit);
41-
end
42-
43-
function DateUnitNoSetter(testCase)
44-
% Verify that an error is thrown when trying to set the value
45-
% of the DateUnit property.
46-
type = arrow.date32();
47-
testCase.verifyError(@() setfield(type, "DateUnit", "Millisecond"), "MATLAB:class:SetProhibited");
48-
end
49-
50-
function InvalidProxy(testCase)
51-
% Verify that an error is thrown when a Proxy of an unexpected
52-
% type is passed to the arrow.type.Date32Type constructor.
53-
array = arrow.array([1, 2, 3]);
54-
proxy = array.Proxy;
55-
testCase.verifyError(@() arrow.type.Date32Type(proxy), "arrow:proxy:ProxyNameMismatch");
56-
end
57-
58-
function IsEqualTrue(testCase)
59-
% Verifies isequal method of arrow.type.Date32Type returns true if
60-
% these conditions are met:
61-
%
62-
% 1. All input arguments have a class type arrow.type.Date32Type
63-
% 2. All inputs have the same size
64-
65-
% Scalar Date32Type arrays
66-
date32Type1 = arrow.date32();
67-
date32Type2 = arrow.date32();
68-
testCase.verifyTrue(isequal(date32Type1, date32Type2));
69-
70-
% Non-scalar Date32Type arrays
71-
typeArray1 = [date32Type1 date32Type1];
72-
typeArray2 = [date32Type2 date32Type2];
73-
testCase.verifyTrue(isequal(typeArray1, typeArray2));
74-
end
75-
76-
function IsEqualFalse(testCase)
77-
% Verifies the isequal method of arrow.type.Date32Type returns
78-
% false when expected.
79-
80-
% Pass a different arrow.type.Type subclass to isequal
81-
date32Type = arrow.date32();
82-
int32Type = arrow.int32();
83-
testCase.verifyFalse(isequal(date32Type, int32Type));
84-
testCase.verifyFalse(isequal([date32Type date32Type], [int32Type int32Type]));
85-
86-
% Date32Type arrays have different sizes
87-
typeArray1 = [date32Type date32Type];
88-
typeArray2 = [date32Type date32Type]';
89-
testCase.verifyFalse(isequal(typeArray1, typeArray2));
90-
end
91-
25+
DefaultDateUnit = arrow.type.DateUnit.Day
26+
ConstructionFcn = @arrow.date32
27+
ClassConstructorFcn = @arrow.type.Date32Type
9228
end
9329

9430
end

matlab/test/arrow/type/tDate64Type.m

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,80 +15,16 @@
1515
% implied. See the License for the specific language governing
1616
% permissions and limitations under the License.
1717

18-
classdef tDate64Type < hFixedWidthType
18+
classdef tDate64Type < hDateType
1919

2020
properties
21-
ConstructionFcn = @arrow.date64
2221
ArrowType = arrow.date64
2322
TypeID = arrow.type.ID.Date64
2423
BitWidth = int32(64)
2524
ClassName = "arrow.type.Date64Type"
26-
end
27-
28-
methods(Test)
29-
function TestClass(testCase)
30-
% Verify ArrowType is an object of the expected class type.
31-
name = string(class(testCase.ArrowType));
32-
testCase.verifyEqual(name, testCase.ClassName);
33-
end
34-
35-
function DefaultDateUnit(testCase)
36-
% Verify the default DateUnit is Millisecond.
37-
type = testCase.ArrowType;
38-
actualUnit = type.DateUnit;
39-
expectedUnit = arrow.type.DateUnit.Millisecond;
40-
testCase.verifyEqual(actualUnit, expectedUnit);
41-
end
42-
43-
function DateUnitNoSetter(testCase)
44-
% Verify that an error is thrown when trying to set the value
45-
% of the DateUnit property.
46-
type = arrow.date64();
47-
testCase.verifyError(@() setfield(type, "DateUnit", "Day"), "MATLAB:class:SetProhibited");
48-
end
49-
50-
function InvalidProxy(testCase)
51-
% Verify that an error is thrown when a Proxy of an unexpected
52-
% type is passed to the arrow.type.Date64Type constructor.
53-
array = arrow.array([1, 2, 3]);
54-
proxy = array.Proxy;
55-
testCase.verifyError(@() arrow.type.Date64Type(proxy), "arrow:proxy:ProxyNameMismatch");
56-
end
57-
58-
function IsEqualTrue(testCase)
59-
% Verifies isequal method of arrow.type.Date64Type returns true if
60-
% these conditions are met:
61-
%
62-
% 1. All input arguments have a class type arrow.type.Date64Type
63-
% 2. All inputs have the same size
64-
65-
% Scalar Date64Type arrays
66-
date64Type1 = arrow.date64();
67-
date64Type2 = arrow.date64();
68-
testCase.verifyTrue(isequal(date64Type1, date64Type2));
69-
70-
% Non-scalar Date64Type arrays
71-
typeArray1 = [date64Type1 date64Type1];
72-
typeArray2 = [date64Type2 date64Type2];
73-
testCase.verifyTrue(isequal(typeArray1, typeArray2));
74-
end
75-
76-
function IsEqualFalse(testCase)
77-
% Verifies the isequal method of arrow.type.Date64Type returns
78-
% false when expected.
79-
80-
% Pass a different arrow.type.Type subclass to isequal
81-
date64Type = arrow.date64();
82-
int32Type = arrow.int32();
83-
testCase.verifyFalse(isequal(date64Type, int32Type));
84-
testCase.verifyFalse(isequal([date64Type date64Type], [int32Type int32Type]));
85-
86-
% Date64Type arrays have different sizes
87-
typeArray1 = [date64Type date64Type];
88-
typeArray2 = [date64Type date64Type]';
89-
testCase.verifyFalse(isequal(typeArray1, typeArray2));
90-
end
91-
25+
DefaultDateUnit = arrow.type.DateUnit.Millisecond
26+
ConstructionFcn = @arrow.date64
27+
ClassConstructorFcn = @arrow.type.Date64Type
9228
end
9329

9430
end

0 commit comments

Comments
 (0)