From 466f6056f5995d0222aede0c02351d9b5b78cfbb Mon Sep 17 00:00:00 2001 From: Christophe Le Saec <51320496+clesaec@users.noreply.github.com> Date: Wed, 16 Aug 2023 12:13:36 +0200 Subject: [PATCH] AVRO-3826: Common tests for C++ module (#2431) * AVRO-3826: Common tests for C++ module --- lang/c++/CMakeLists.txt | 1 + lang/c++/build.sh | 3 +- lang/c++/test/CommonsSchemasTests.cc | 100 +++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 lang/c++/test/CommonsSchemasTests.cc diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt index 472684f4c3d..3ac0974946d 100644 --- a/lang/c++/CMakeLists.txt +++ b/lang/c++/CMakeLists.txt @@ -189,6 +189,7 @@ unittest (JsonTests) unittest (AvrogencppTests) unittest (CompilerTests) unittest (AvrogencppTestReservedWords) +unittest (CommonsSchemasTests) add_dependencies (AvrogencppTestReservedWords cpp_reserved_words_hh) diff --git a/lang/c++/build.sh b/lang/c++/build.sh index ac9964c75e5..f31c91feff3 100755 --- a/lang/c++/build.sh +++ b/lang/c++/build.sh @@ -92,7 +92,8 @@ case "$target" in && ./build/SpecificTests \ && ./build/AvrogencppTests \ && ./build/DataFileTests \ - && ./build/SchemaTests) + && ./build/SchemaTests \ + && ./build/CommonsSchemasTests) ;; xcode-test) diff --git a/lang/c++/test/CommonsSchemasTests.cc b/lang/c++/test/CommonsSchemasTests.cc new file mode 100644 index 00000000000..5dd560182cb --- /dev/null +++ b/lang/c++/test/CommonsSchemasTests.cc @@ -0,0 +1,100 @@ +/* + * 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 + * + * https://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. + */ + +#include +#include +#include +#include "DataFile.hh" +#include "Compiler.hh" +#include "ValidSchema.hh" +#include "Generic.hh" + + +using avro::validatingDecoder; +using avro::GenericReader; +using avro::DataFileReader; +using avro::DataFileWriter; +using avro::GenericDatum; + + +void testCommonSchema(const std::filesystem::path &dir_path) +{ + const std::filesystem::path& schemaFile = dir_path / "schema.json"; + std::ifstream in(schemaFile.c_str()); + + avro::ValidSchema schema; + avro::compileJsonSchema(in, schema); + + const std::filesystem::path& dataFile = dir_path / "data.avro"; + + + GenericDatum datum(schema); + const std::filesystem::path& outputDataFile = dir_path / "data_out.avro"; + + + DataFileReader reader(dataFile.c_str()); + DataFileWriter writer(outputDataFile.c_str(), schema); + + while (reader.read(datum)) { + avro::GenericRecord& rec = datum.value(); + BOOST_CHECK(rec.fieldCount() >= 0); + writer.write(datum); + } + writer.close(); + reader.close(); + + GenericDatum datumOrig(schema); + GenericDatum datumNew(schema); + + DataFileReader readerOrig(dataFile.c_str()); + DataFileReader readerNew(outputDataFile.c_str()); + while (readerOrig.read(datumOrig)) { + BOOST_CHECK(readerNew.read(datumNew)); + avro::GenericRecord& rec1 = datumOrig.value(); + avro::GenericRecord& rec2 = datumNew.value(); + BOOST_CHECK_EQUAL(rec1.fieldCount(), rec2.fieldCount()); + } + BOOST_CHECK(!readerNew.read(datumNew)); + + + std::filesystem::remove(outputDataFile); +} + + + +void testCommonsSchemas() +{ + const std::filesystem::path commons_schemas{"../../share/test/data/schemas"}; + if (!std::filesystem::exists(commons_schemas)) { + std::cout << "\nWarn: Can't access share test folder '../../share/test/data/schemas'\n" << std::endl; + return; + } + for (auto const& dir_entry : std::filesystem::directory_iterator{commons_schemas}) { + if (std::filesystem::is_directory(dir_entry)) { + testCommonSchema(dir_entry.path()); + } + } +} + +boost::unit_test::test_suite * +init_unit_test_suite(int /*argc*/, char * /*argv*/[]) { + using namespace boost::unit_test; + + auto *ts = BOOST_TEST_SUITE("Avro C++ unit tests for commons schemas"); + ts->add(BOOST_TEST_CASE(&testCommonsSchemas)); + return ts; +}