Skip to content

Commit

Permalink
Merged the four encoding tests into one parameterized test.
Browse files Browse the repository at this point in the history
  • Loading branch information
EpsilonPrime committed Jul 26, 2023
1 parent 88a90b1 commit 0bbf8cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 93 deletions.
13 changes: 13 additions & 0 deletions include/substrait/common/Io.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ enum PlanFileEncoding {
kText = 3,
};

constexpr const char* PlanFileEncodingToString(PlanFileEncoding e) noexcept {
switch (e) {
case PlanFileEncoding::kBinary:
return "kBinary";
case PlanFileEncoding::kJson:
return "kJson";
case PlanFileEncoding::kProtoText:
return "kProtoText";
case PlanFileEncoding::kText:
return "kText";
}
}

// Loads a Substrait plan consisting of any encoding type from the given file.
absl::StatusOr<::substrait::proto::Plan> loadPlanWithUnknownEncoding(
std::string_view input_filename);
Expand Down
110 changes: 17 additions & 93 deletions src/substrait/common/tests/IoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using ::protobuf_matchers::EqualsProto;
using ::protobuf_matchers::Partially;

namespace io::substrait {

class IoTest : public ::testing::Test {};

TEST_F(IoTest, LoadMissingFile) {
Expand All @@ -20,78 +22,21 @@ TEST_F(IoTest, LoadMissingFile) {
::testing::ContainsRegex("Failed to open file non-existent-file"));
}

TEST_F(IoTest, SaveAndLoadBinary) {
::substrait::proto::Plan plan;
auto root = plan.add_relations()->mutable_root();
auto read = root->mutable_input()->mutable_read();
read->mutable_named_table()->add_names("table_name");
auto status =
::io::substrait::savePlan(plan, "rwtest.plan", io::substrait::kBinary);
ASSERT_TRUE(status.ok()) << status;
class SaveAndLoadTestFixture
: public ::testing::TestWithParam<PlanFileEncoding> {};

auto result = ::io::substrait::loadPlanWithUnknownEncoding("rwtest.plan");
ASSERT_TRUE(result.ok()) << result.status();
ASSERT_THAT(
*result,
Partially(EqualsProto<::substrait::proto::Plan>(
R"(relations {
root {
input {
read {
common {
direct {
}
}
named_table {
names: "table_name"
}
}
}
}
})")));
}

TEST_F(IoTest, SaveAndLoadJson) {
::substrait::proto::Plan plan;
auto root = plan.add_relations()->mutable_root();
auto read = root->mutable_input()->mutable_read();
read->mutable_named_table()->add_names("table_name");
auto status =
::io::substrait::savePlan(plan, "rwtest.json", io::substrait::kJson);
ASSERT_TRUE(status.ok()) << status;

auto result = ::io::substrait::loadPlanWithUnknownEncoding("rwtest.json");
ASSERT_TRUE(result.ok()) << result.status();
ASSERT_THAT(
*result,
Partially(EqualsProto<::substrait::proto::Plan>(
R"(relations {
root {
input {
read {
common {
direct {
}
}
named_table {
names: "table_name"
}
}
}
}
})")));
}
TEST_P(SaveAndLoadTestFixture, SaveAndLoad) {
auto tempFilename = std::tmpnam(nullptr);
PlanFileEncoding encoding = GetParam();

TEST_F(IoTest, SaveAndLoadProtoText) {
::substrait::proto::Plan plan;
auto root = plan.add_relations()->mutable_root();
auto read = root->mutable_input()->mutable_read();
read->mutable_named_table()->add_names("table_name");
auto status = ::io::substrait::savePlan(
plan, "rwtest.protobuf", io::substrait::kProtoText);
auto status = ::io::substrait::savePlan(plan, tempFilename, encoding);
ASSERT_TRUE(status.ok()) << status;

auto result = ::io::substrait::loadPlanWithUnknownEncoding("rwtest.protobuf");
auto result = ::io::substrait::loadPlanWithUnknownEncoding(tempFilename);
ASSERT_TRUE(result.ok()) << result.status();
ASSERT_THAT(
*result,
Expand All @@ -113,33 +58,12 @@ TEST_F(IoTest, SaveAndLoadProtoText) {
})")));
}

TEST_F(IoTest, SaveAndLoadText) {
::substrait::proto::Plan plan;
auto root = plan.add_relations()->mutable_root();
auto read = root->mutable_input()->mutable_read();
read->mutable_named_table()->add_names("table_name");
auto status =
::io::substrait::savePlan(plan, "rwtest.splan", io::substrait::kText);
ASSERT_TRUE(status.ok()) << status;
INSTANTIATE_TEST_SUITE_P(
SaveAndLoadTests,
SaveAndLoadTestFixture,
testing::Values(kBinary, kJson, kProtoText, kText),
[](const testing::TestParamInfo<SaveAndLoadTestFixture::ParamType>& info) {
return PlanFileEncodingToString(info.param);
});

auto result = ::io::substrait::loadPlanWithUnknownEncoding("rwtest.splan");
ASSERT_TRUE(result.ok()) << result.status();
ASSERT_THAT(
*result,
Partially(EqualsProto<::substrait::proto::Plan>(
R"(relations {
root {
input {
read {
common {
direct {
}
}
named_table {
names: "table_name"
}
}
}
}
})")));
}
} // namespace io::substrait

0 comments on commit 0bbf8cc

Please sign in to comment.