Skip to content

Commit

Permalink
AVRO-3826: Common tests for C++ module (#2431)
Browse files Browse the repository at this point in the history
* AVRO-3826: Common tests for C++ module
  • Loading branch information
clesaec authored Aug 16, 2023
1 parent 5996cfe commit 466f605
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions lang/c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ unittest (JsonTests)
unittest (AvrogencppTests)
unittest (CompilerTests)
unittest (AvrogencppTestReservedWords)
unittest (CommonsSchemasTests)

add_dependencies (AvrogencppTestReservedWords cpp_reserved_words_hh)

Expand Down
3 changes: 2 additions & 1 deletion lang/c++/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ case "$target" in
&& ./build/SpecificTests \
&& ./build/AvrogencppTests \
&& ./build/DataFileTests \
&& ./build/SchemaTests)
&& ./build/SchemaTests \
&& ./build/CommonsSchemasTests)
;;

xcode-test)
Expand Down
100 changes: 100 additions & 0 deletions lang/c++/test/CommonsSchemasTests.cc
Original file line number Diff line number Diff line change
@@ -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 <boost/test/included/unit_test.hpp>
#include <boost/test/unit_test.hpp>
#include <filesystem>
#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<GenericDatum> reader(dataFile.c_str());
DataFileWriter<GenericDatum> writer(outputDataFile.c_str(), schema);

while (reader.read(datum)) {
avro::GenericRecord& rec = datum.value<avro::GenericRecord>();
BOOST_CHECK(rec.fieldCount() >= 0);
writer.write(datum);
}
writer.close();
reader.close();

GenericDatum datumOrig(schema);
GenericDatum datumNew(schema);

DataFileReader<GenericDatum> readerOrig(dataFile.c_str());
DataFileReader<GenericDatum> readerNew(outputDataFile.c_str());
while (readerOrig.read(datumOrig)) {
BOOST_CHECK(readerNew.read(datumNew));
avro::GenericRecord& rec1 = datumOrig.value<avro::GenericRecord>();
avro::GenericRecord& rec2 = datumNew.value<avro::GenericRecord>();
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;
}

0 comments on commit 466f605

Please sign in to comment.