Skip to content

Commit

Permalink
add more geo value test
Browse files Browse the repository at this point in the history
  • Loading branch information
jievince committed Sep 28, 2021
1 parent d72d471 commit 06f0998
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/common/datatypes/Geography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

namespace nebula {

StatusOr<std::unique_ptr<Geography>> Geography::fromWKT(const std::string& wkt) {
StatusOr<Geography> Geography::fromWKT(const std::string& wkt) {
auto geomRet = WKTReader().read(wkt);
NG_RETURN_IF_ERROR(geomRet);
auto geom = std::move(geomRet).value();
auto wkb = WKBWriter().write(*geom);
return std::make_unique<Geography>(wkb);
auto geom = geomRet.value();
auto wkb = WKBWriter().write(geom);
return Geography(wkb);
}

GeoShape Geography::shape() const {
Expand Down
2 changes: 1 addition & 1 deletion src/common/datatypes/Geography.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct Geography {
wkb = bytes;
}

static StatusOr<std::unique_ptr<Geography>> fromWKT(const std::string& wkt);
static StatusOr<Geography> fromWKT(const std::string& wkt);

GeoShape shape() const;

Expand Down
24 changes: 12 additions & 12 deletions src/common/datatypes/test/GeographyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ TEST(Geography, shape) {
std::string wkt = "POINT(3 7)";
auto gRet = Geography::fromWKT(wkt);
ASSERT_TRUE(gRet.ok());
auto g = std::move(gRet.value());
EXPECT_EQ(GeoShape::POINT, g->shape());
auto g = gRet.value();
EXPECT_EQ(GeoShape::POINT, g.shape());
}
{
std::string wkt = "LINESTRING(28.4 79.20,134.25 -28.34)";
auto gRet = Geography::fromWKT(wkt);
ASSERT_TRUE(gRet.ok());
auto g = std::move(gRet.value());
EXPECT_EQ(GeoShape::LINESTRING, g->shape());
auto g = gRet.value();
EXPECT_EQ(GeoShape::LINESTRING, g.shape());
}
{
std::string wkt = "POLYGON((1 2,3 4,5 6,1 2))";
auto gRet = Geography::fromWKT(wkt);
ASSERT_TRUE(gRet.ok());
auto g = std::move(gRet.value());
EXPECT_EQ(GeoShape::POLYGON, g->shape());
auto g = gRet.value();
EXPECT_EQ(GeoShape::POLYGON, g.shape());
}
}

Expand All @@ -41,26 +41,26 @@ TEST(Geography, asWKT) {
std::string wkt = "POINT(3 7)";
auto gRet = Geography::fromWKT(wkt);
ASSERT_TRUE(gRet.ok());
auto g = std::move(gRet.value());
auto got = g->asWKT();
auto g = gRet.value();
auto got = g.asWKT();
ASSERT_TRUE(!!got);
EXPECT_EQ(wkt, *got);
}
{
std::string wkt = "LINESTRING(28.4 79.2,134.25 -28.34)";
auto gRet = Geography::fromWKT(wkt);
ASSERT_TRUE(gRet.ok());
auto g = std::move(gRet.value());
auto got = g->asWKT();
auto g = gRet.value();
auto got = g.asWKT();
ASSERT_TRUE(!!got);
EXPECT_EQ(wkt, *got);
}
{
std::string wkt = "POLYGON((1 2,3 4,5 6,1 2))";
auto gRet = Geography::fromWKT(wkt);
ASSERT_TRUE(gRet.ok());
auto g = std::move(gRet.value());
auto got = g->asWKT();
auto g = gRet.value();
auto got = g.asWKT();
ASSERT_TRUE(!!got);
EXPECT_EQ(wkt, *got);
}
Expand Down
30 changes: 29 additions & 1 deletion src/common/datatypes/test/ValueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ TEST(Value, Comparison) {
Value vDateTime1(DateTime(1998, 9, 8, 12, 30, 04, 56));
Value vDateTime2(DateTime(1998, 9, 8, 13, 30, 04, 56));
Value vDateTime3(DateTime(1998, 9, 8, 13, 30, 04, 56)); // for further tests
Value vGeo1 = Geography::fromWKT("POINT(4 7)").value();
Value vGeo2 = Geography::fromWKT("POINT(5 7)").value();
Value vGeo3 = Geography::fromWKT("POINT(5 7)").value();

// null/empty
{
Expand Down Expand Up @@ -551,6 +554,25 @@ TEST(Value, Comparison) {
EXPECT_EQ(Value::Type::BOOL, v.type());
EXPECT_EQ(true, v.getBool());
}
// geography
{
Value v = vGeo1 == vGeo2;
EXPECT_EQ(Value::Type::BOOL, v.type());
EXPECT_EQ(false, v.getBool());

v = vGeo1 != vGeo2;
EXPECT_EQ(Value::Type::BOOL, v.type());
EXPECT_EQ(true, v.getBool());

v = vGeo2 == vGeo3;
EXPECT_EQ(Value::Type::BOOL, v.type());
EXPECT_EQ(true, v.getBool());

v = vGeo2 != vGeo3;
EXPECT_EQ(Value::Type::BOOL, v.type());
EXPECT_EQ(false, v.getBool());
// TODO(jie) Add more geo test
}
}

TEST(Value, Logical) {
Expand Down Expand Up @@ -1001,6 +1023,7 @@ TEST(Value, typeName) {
EXPECT_EQ("map", Value(Map()).typeName());
EXPECT_EQ("set", Value(Set()).typeName());
EXPECT_EQ("dataset", Value(DataSet()).typeName());
EXPECT_EQ("geography", Value(Geography()).typeName());
EXPECT_EQ("__NULL__", Value::kNullValue.typeName());
EXPECT_EQ("NaN", Value::kNullNaN.typeName());
EXPECT_EQ("BAD_DATA", Value::kNullBadData.typeName());
Expand Down Expand Up @@ -1091,6 +1114,9 @@ TEST(Value, DecodeEncode) {

// DataSet
Value(DataSet({"col1", "col2"})),

// Geography
Value(Geography::fromWKT("Point(3 8)").value()),
};
for (const auto& val : values) {
std::string buf;
Expand Down Expand Up @@ -1129,7 +1155,9 @@ TEST(Value, Ctor) {
EXPECT_TRUE(vSet.isSet());
Value vMap(Map({{"a", 9}, {"b", 10}}));
EXPECT_TRUE(vMap.isMap());

// TODO(jie) Add more geography value test
Value vGeo(Geography::fromWKT("LINESTRING(0 1, 2 7)").value());
EXPECT_TRUE(vGeo.isGeography());
// Disabled
// Lead to compile error
// Value v(nullptr);
Expand Down
4 changes: 2 additions & 2 deletions src/common/geo/io/wkt/WKTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class WKTReader {
if (geom_ != nullptr) delete geom_;
}

StatusOr<std::unique_ptr<Geometry>> read(std::string wkt) {
StatusOr<Geometry> read(std::string wkt) {
// Since WKTScanner needs a writable buffer, we have to copy the query string
buffer_ = std::move(wkt);
pos_ = &buffer_[0];
Expand All @@ -64,7 +64,7 @@ class WKTReader {
auto *geom = geom_;
geom_ = nullptr;
scanner_.setWKT(nullptr);
return std::unique_ptr<Geometry>(geom);
return *geom;
}

private:
Expand Down
8 changes: 4 additions & 4 deletions src/common/geo/io/wkt/test/WKTParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ class WKTParserTest : public ::testing::Test {
void TearDown() override {}

protected:
StatusOr<std::unique_ptr<Geometry>> parse(const std::string& wkt) {
StatusOr<Geometry> parse(const std::string& wkt) {
auto geomRet = WKTReader().read(wkt);
NG_RETURN_IF_ERROR(geomRet);
NG_RETURN_IF_ERROR(check(*geomRet.value().get()));
NG_RETURN_IF_ERROR(check(geomRet.value()));
return geomRet;
}

Status check(const Geometry& geom) {
auto wkt = WKTWriter().write(geom);
auto geomCopyRet = WKTReader().read(wkt);
auto geomCopy = std::move(geomCopyRet).value();
auto wktCopy = WKTWriter().write(*geomCopy.get());
auto geomCopy = geomCopyRet.value();
auto wktCopy = WKTWriter().write(geomCopy);

if (wkt != wktCopy) {
return Status::Error("The reparsed geometry `%s' is different from origin `%s'.",
Expand Down
5 changes: 4 additions & 1 deletion src/common/utils/IndexKeyUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ class IndexKeyUtils final {

static std::string encodeUint64(uint64_t v) {
auto val = folly::Endian::big(v);
return {reinterpret_cast<const char*>(&val), sizeof(uint64_t)};
std::string raw;
raw.reserve(sizeof(uint64_t));
raw.append(reinterpret_cast<const char*>(&val), sizeof(uint64_t));
return raw;
}

static std::string encodeRank(EdgeRanking rank) { return IndexKeyUtils::encodeInt64(rank); }
Expand Down

0 comments on commit 06f0998

Please sign in to comment.