kevingurney commented on code in PR #37013: URL: https://github.com/apache/arrow/pull/37013#discussion_r1284689985
########## matlab/test/arrow/tabular/tSchema.m: ########## @@ -0,0 +1,474 @@ +% Licensed to the Apache Software Foundation (ASF) under one or more +% contributor license agreements. See the NOTICE file distributed with +% this work for additional information regarding copyright ownership. +% The ASF licenses this file to you under the Apache License, Version +% 2.0 (the "License"); you may not use this file except in compliance +% with the License. You may obtain a copy of the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +% implied. See the License for the specific language governing +% permissions and limitations under the License. + +classdef tSchema < matlab.unittest.TestCase +% Tests for the arrow.tabular.Schema class and the associated arrow.schema +% construction function. + + methods(Test) + + function ErrorIfUnsupportedInputType(testCase) + % Verify that an error is thrown by arrow.schema if an + % unsupported input argument is supplied. + testCase.verifyError(@() arrow.schema("test"), "MATLAB:validation:UnableToConvert"); + end + + function ErrorIfUnsupportedConstructorInputs(testCase) + % Verify that an error is thrown by the constructor of + % arrow.tabular.Schema if unsupported arguments are passed to + % the constructor. + testCase.verifyError(@() arrow.tabular.Schema("test"), "MATLAB:validation:UnableToConvert"); + end + + function ErrorIfTooFewInputs(testCase) + % Verify that an error is thrown by arrow.schema if too few + % input arguments are supplied. + testCase.verifyError(@() arrow.schema(), "MATLAB:minrhs"); + end + + function ErrorIfTooManyInputs(testCase) + % Verify that an error is thrown by arrow.schema if too many + % input arguments are supplied. + testCase.verifyError(@() arrow.schema("a", "b", "c"), "MATLAB:TooManyInputs"); + end + + function ClassType(testCase) + % Verify that the class type of the object returned by a call + % to arrow.schema is "arrow.tabular.Schema". + schema = arrow.schema(arrow.field("A", arrow.uint8)); + testCase.verifyInstanceOf(schema, "arrow.tabular.Schema"); + end + + function ConstructSchemaFromProxy(testCase) + % Verify that an arrow.tabular.Schema instance can be + % constructred directly from an existing + % arrow.tabular.proxy.Schema Proxy instance. + schema1 = arrow.schema(arrow.field("a", arrow.uint8)); + % Construct an instance of arrow.tabular.Schema directly from a + % Proxy of type "arrow.tabular.proxy.Schema". + schema2 = arrow.tabular.Schema(schema1.Proxy); + testCase.verifyEqual(schema1.FieldNames, schema2.FieldNames); + testCase.verifyEqual(schema1.NumFields, schema2.NumFields); + end + + function Fields(testCase) + % Verify that the Fields property returns an expected array of + % Field objects. + f1 = arrow.field("A", arrow.uint8); + f2 = arrow.field("B", arrow.uint16); + f3 = arrow.field("C", arrow.uint32); + expectedFields = [f1, f2, f3]; + schema = arrow.schema(expectedFields); + + actualFields = schema.Fields; + + testCase.verifyEqual(actualFields(1).Name, expectedFields(1).Name); + testCase.verifyEqual(actualFields(1).Type.ID, expectedFields(1).Type.ID); + testCase.verifyEqual(actualFields(2).Name, expectedFields(2).Name); + testCase.verifyEqual(actualFields(2).Type.ID, expectedFields(2).Type.ID); + testCase.verifyEqual(actualFields(3).Name, expectedFields(3).Name); + testCase.verifyEqual(actualFields(3).Type.ID, expectedFields(3).Type.ID); + end + + function FieldNames(testCase) + % Verify that the FieldNames property returns an expected + % string array of field names. + expectedFieldNames = ["A" , "B" , "C"]; + schema = arrow.schema([... + arrow.field(expectedFieldNames(1), arrow.uint8), ... + arrow.field(expectedFieldNames(2), arrow.uint16), ... + arrow.field(expectedFieldNames(3), arrow.uint32) ... + ]); + actualFieldNames = schema.FieldNames; + testCase.verifyEqual(actualFieldNames, expectedFieldNames); + end + + function FieldNamesNoSetter(testCase) + % Verify that an error is thrown when trying to set the value + % of the FieldNames property. + schema = arrow.schema(arrow.field("A", arrow.uint8)); + testCase.verifyError(@() setfield(schema, "FieldNames", "B"), "MATLAB:class:SetProhibited"); + end + + function NumFieldsNoSetter(testCase) + % Verify than an error is thrown when trying to set the value + % of the NumFields property. + schema = arrow.schema(arrow.field("A", arrow.uint8)); + testCase.verifyError(@() setfield(schema, "NumFields", 123), "MATLAB:class:SetProhibited"); + end + + function FieldsNoSetter(testCase) + % Verify that an error is thrown when trying to set the value + % of the Fields property. + schema = arrow.schema(arrow.field("A", arrow.uint8)); + testCase.verifyError(@() setfield(schema, "Fields", arrow.field("B", arrow.uint8)), "MATLAB:class:SetProhibited"); + end + + function NumFields(testCase) + % Verify that the NumFields property returns an execpted number + % of fields. + schema = arrow.schema([... + arrow.field("A", arrow.uint8), ... + arrow.field("B", arrow.uint16), ... + arrow.field("C", arrow.uint32) ... + ]); + expectedNumFields = int32(3); + actualNumFields = schema.NumFields; + testCase.verifyEqual(actualNumFields, expectedNumFields); + end + + function ErrorIfUnsupportedFieldIndex(testCase) + % Verify that an error is thrown if an invalid field index is + % supplied to the field method (e.g. -1.1, NaN, {1}, etc.). + schema = arrow.schema([... + arrow.field("A", arrow.uint8), ... + arrow.field("B", arrow.uint16), ... + arrow.field("C", arrow.uint32) ... + ]); + + index = []; + testCase.verifyError(@() schema.field(index), "arrow:tabular:schema:UnsupportedFieldIndexType"); + + index = 0; + testCase.verifyError(@() schema.field(index), "arrow:tabular:schema:UnsupportedFieldIndexType"); + + index = -1; + testCase.verifyError(@() schema.field(index), "arrow:tabular:schema:UnsupportedFieldIndexType"); + + index = -1.23; + testCase.verifyError(@() schema.field(index), "arrow:tabular:schema:UnsupportedFieldIndexType"); + + index = NaN; + testCase.verifyError(@() schema.field(index), "arrow:tabular:schema:UnsupportedFieldIndexType"); + + index = {1}; + testCase.verifyError(@() schema.field(index), "arrow:tabular:schema:UnsupportedFieldIndexType"); + + index = [1; 1]; + testCase.verifyError(@() schema.field(index), "arrow:tabular:schema:UnsupportedFieldIndexType"); + end + + function GetFieldByIndex(testCase) + % Verify that Fields can be accessed using a numeric index. + schema = arrow.schema([... + arrow.field("A", arrow.uint8), ... + arrow.field("B", arrow.uint16), ... + arrow.field("C", arrow.uint32) ... + ]); + + field = schema.field(1); + testCase.verifyEqual(field.Name, "A"); + testCase.verifyEqual(field.Type.ID, arrow.type.ID.UInt8); + + field = schema.field(2); + testCase.verifyEqual(field.Name, "B"); + testCase.verifyEqual(field.Type.ID, arrow.type.ID.UInt16); + + field = schema.field(3); + testCase.verifyEqual(field.Name, "C"); + testCase.verifyEqual(field.Type.ID, arrow.type.ID.UInt32); + end + + function GetFieldByName(testCase) + % Verify that Fields can be accessed using a field name. + % Verify that Fields can be accessed using a numeric index. Review Comment: Thanks for catching this mistake! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
