|
22 | 22 | #include <boost/test/unit_test.hpp>
|
23 | 23 |
|
24 | 24 | #include "Compiler.hh"
|
| 25 | +#include "Node.hh" |
25 | 26 | #include "ValidSchema.hh"
|
26 | 27 |
|
27 | 28 | // Assert that empty defaults don't make json schema compilation violate bounds
|
@@ -82,12 +83,42 @@ void test2dArray() {
|
82 | 83 | BOOST_CHECK_EQUAL(expected, actual.str());
|
83 | 84 | }
|
84 | 85 |
|
| 86 | +void testRecordWithNamedReference() { |
| 87 | + std::string nestedSchema = "{\"name\":\"NestedRecord\",\"type\":\"record\",\"fields\":[{\"name\":\"stringField\",\"type\":\"string\"}]}"; |
| 88 | + // The root schema references the nested schema above by name only. |
| 89 | + // This mimics tools that allow schemas to have references to other schemas. |
| 90 | + std::string rootSchema = "{\"name\":\"RootRecord\",\"type\":\"record\",\"fields\":[{\"name\": \"nestedField\",\"type\":\"NestedRecord\"}]}"; |
| 91 | + |
| 92 | + // First compile the nested schema |
| 93 | + avro::ValidSchema nestedRecord = avro::compileJsonSchemaFromString(nestedSchema); |
| 94 | + |
| 95 | + // Create a map of named references |
| 96 | + std::map<avro::Name, avro::ValidSchema> namedReferences; |
| 97 | + namedReferences[avro::Name("NestedRecord")] = nestedRecord; |
| 98 | + |
| 99 | + // Parse the root schema with named references |
| 100 | + std::istringstream rootSchemaStream(rootSchema); |
| 101 | + avro::ValidSchema rootRecord = avro::compileJsonSchemaWithNamedReferences(rootSchemaStream, namedReferences); |
| 102 | + |
| 103 | + // Verify the schema was compiled correctly |
| 104 | + BOOST_CHECK_EQUAL("RootRecord", rootRecord.root()->name().simpleName()); |
| 105 | + |
| 106 | + // Get the nested field and verify its type |
| 107 | + const avro::NodePtr &rootNode = rootRecord.root(); |
| 108 | + BOOST_CHECK_EQUAL(avro::AVRO_RECORD, rootNode->type()); |
| 109 | + BOOST_CHECK_EQUAL(1, rootNode->leaves()); |
| 110 | + |
| 111 | + const avro::NodePtr &nestedFieldNode = rootNode->leafAt(0); |
| 112 | + BOOST_CHECK_EQUAL("NestedRecord", nestedFieldNode->name().simpleName()); |
| 113 | +} |
| 114 | + |
85 | 115 | boost::unit_test::test_suite *
|
86 | 116 | init_unit_test_suite(int /*argc*/, char * /*argv*/[]) {
|
87 | 117 | using namespace boost::unit_test;
|
88 | 118 |
|
89 | 119 | auto *ts = BOOST_TEST_SUITE("Avro C++ unit tests for Compiler.cc");
|
90 | 120 | ts->add(BOOST_TEST_CASE(&testEmptyBytesDefault));
|
91 | 121 | ts->add(BOOST_TEST_CASE(&test2dArray));
|
| 122 | + ts->add(BOOST_TEST_CASE(&testRecordWithNamedReference)); |
92 | 123 | return ts;
|
93 | 124 | }
|
0 commit comments