-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AVRO-1849 Fix C++ schema to JSON converter #97
Changes from 1 commit
a2718fe
58ecc80
700af69
39c5195
3f0ec2c
d3d0aa8
e80fd1a
f7826c6
3ad5aac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ | |
#include <boost/test/unit_test.hpp> | ||
#include <boost/test/parameterized_test.hpp> | ||
|
||
|
||
namespace avro { | ||
namespace schema { | ||
|
||
|
@@ -48,6 +47,7 @@ const char* basicSchemas[] = { | |
"{ \"type\": \"string\" }", | ||
|
||
// Record | ||
"{\"type\": \"record\",\"name\": \"Test\",\"fields\": []}", | ||
"{\"type\": \"record\",\"name\": \"Test\",\"fields\": " | ||
"[{\"name\": \"f\",\"type\": \"long\"}]}", | ||
"{\"type\": \"record\",\"name\": \"Test\",\"fields\": " | ||
|
@@ -136,6 +136,57 @@ const char* basicSchemaErrors[] = { | |
"{\"type\": \"fixed\", \"size\": 314}", | ||
}; | ||
|
||
const char* roundTripSchemas[] = { | ||
"\"null\"", | ||
"\"boolean\"", | ||
"\"int\"", | ||
"\"long\"", | ||
"\"float\"", | ||
"\"double\"", | ||
"\"bytes\"", | ||
"\"string\"", | ||
// Record | ||
"{\"type\":\"record\",\"name\":\"Test\",\"fields\":[]}", | ||
"{\"type\":\"record\",\"name\":\"Test\",\"fields\":" | ||
"[{\"name\":\"f\",\"type\":\"long\"}]}", | ||
"{\"type\":\"record\",\"name\":\"Test\",\"fields\":" | ||
"[{\"name\":\"f1\",\"type\":\"long\"}," | ||
"{\"name\":\"f2\",\"type\":\"int\"}]}", | ||
/* Avro-C++ cannot do a round-trip on error schemas. | ||
* "{\"type\":\"error\",\"name\":\"Test\",\"fields\":" | ||
"[{\"name\":\"f1\",\"type\":\"long\"}," | ||
"{\"name\":\"f2\",\"type\":\"int\"}]}" | ||
*/ | ||
// Recursive. | ||
"{\"type\":\"record\",\"name\":\"LongList\"," | ||
"\"fields\":[{\"name\":\"value\",\"type\":\"long\"}," | ||
"{\"name\":\"next\",\"type\":[\"LongList\",\"null\"]}]}", | ||
// Enum | ||
"{\"type\":\"enum\",\"name\":\"Test\",\"symbols\":[\"A\",\"B\"]}", | ||
|
||
// Array | ||
"{\"type\":\"array\",\"items\":\"long\"}", | ||
"{\"type\":\"array\",\"items\":{\"type\":\"enum\"," | ||
"\"name\":\"Test\",\"symbols\":[\"A\",\"B\"]}}", | ||
|
||
// Map | ||
"{\"type\":\"map\",\"values\":\"long\"}", | ||
"{\"type\":\"map\",\"values\":{\"type\":\"enum\"," | ||
"\"name\":\"Test\",\"symbols\":[\"A\",\"B\"]}}", | ||
|
||
// Union | ||
"[\"string\",\"null\",\"long\"]", | ||
|
||
// Fixed | ||
"{\"type\":\"fixed\",\"name\":\"Test\",\"size\":1}", | ||
"{\"type\":\"fixed\",\"namespace\":\"org.apache.hadoop.avro\"," | ||
"\"name\":\"MyFixed\",\"size\":1}", | ||
"{\"type\":\"fixed\",\"name\":\"Test\",\"size\":1}", | ||
"{\"type\":\"fixed\",\"name\":\"Test\",\"size\":1}" | ||
}; | ||
|
||
|
||
|
||
static void testBasic(const char* schema) | ||
{ | ||
BOOST_CHECKPOINT(schema); | ||
|
@@ -154,6 +205,19 @@ static void testCompile(const char* schema) | |
compileJsonSchemaFromString(std::string(schema)); | ||
} | ||
|
||
// Test that the JSON output from a valid schema matches the JSON that was | ||
// used to construct it, apart from whitespace changes. | ||
static void testRoundTrip(const char* schema) | ||
{ | ||
BOOST_CHECKPOINT(schema); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other tests have been updated to use BOOST_TEST_CHECKPOINT, please update this as well. |
||
avro::ValidSchema compiledSchema = compileJsonSchemaFromString(std::string(schema)); | ||
std::ostringstream os; | ||
compiledSchema.toJson(os); | ||
std::string result = os.str(); | ||
result.erase( std::remove_if( result.begin(), result.end(), ::isspace ), result.end() ); // Remove whitespace | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of removing all whitespaces, consecutive whitespaces should be collapsed into a single space. It is true that it doesn't matter how many whitespaces there are, but it does matter whether there is or isn't whitespace between specific characters. A slight modification will give the desired result as described in http://stackoverflow.com/a/8362145/5613485 Additionally this line uses a different style than the other lines, there should be no spaces after '('-s or before ')'-s. |
||
BOOST_CHECK(result == std::string(schema)); | ||
} | ||
|
||
} | ||
} | ||
|
||
|
@@ -173,6 +237,6 @@ init_unit_test_suite(int argc, char* argv[]) | |
ADD_PARAM_TEST(ts, avro::schema::testBasic_fail, | ||
avro::schema::basicSchemaErrors); | ||
ADD_PARAM_TEST(ts, avro::schema::testCompile, avro::schema::basicSchemas); | ||
|
||
ADD_PARAM_TEST(ts, avro::schema::testRoundTrip, avro::schema::roundTripSchemas); | ||
return ts; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Comment style is inconsistent, some lines start with a *, while others don't.