Skip to content

Commit

Permalink
improve the ut
Browse files Browse the repository at this point in the history
  • Loading branch information
jasinliu committed Sep 23, 2023
1 parent b69b6b5 commit c614f47
Show file tree
Hide file tree
Showing 176 changed files with 53 additions and 24 deletions.
41 changes: 24 additions & 17 deletions src/import/graphar_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef std::map<std::string, std::string> Dict;

/**
* @brief Parse the gar DataType to FieldType in config.
*
*
* @param data_type The GraphAr DataType of the vetex or property.
* @param type_name The FieldType string which used to make json object.
*/
Expand Down Expand Up @@ -57,15 +57,15 @@ inline void ParseType(const GraphArchive::DataType& data_type, std::string& type

/**
* @brief Check which AdjListType the Gar Edge info contains.
*
*
* @param edge_info The GraphAr edge info.
* @param adj_list_type The gar adj_list_type which used to get edge collection.
* The adj_list_type value maybe unordered_by_source, unordered_by_dest, ordered_by_source
* or ordered_by_dest.
* Reference: https://alibaba.github.io/GraphAr/reference/api-reference-cpp.html#adj-list-type
*/
inline void CheckAdjListType(const GraphArchive::EdgeInfo& edge_info,
GraphArchive::AdjListType& adj_list_type) {
GraphArchive::AdjListType& adj_list_type) {
for (std::uint8_t i = 0;
i <= static_cast<std::uint8_t>(GraphArchive::AdjListType::ordered_by_dest); ++i) {
GraphArchive::AdjListType type = static_cast<GraphArchive::AdjListType>(i);
Expand All @@ -76,13 +76,14 @@ inline void CheckAdjListType(const GraphArchive::EdgeInfo& edge_info,
/**
* Traverse all properties of the vertex, get the primary key, the properties and the property
* names. Keep the original order in yml config.
*
* @param ver_info The gar vertex information.
* @param primary The primary key of the vertex.
* @param props All the properties of the vertex. One of it maybe {"name":"id","type":"INT64"}.
* @param prop_names All the property names of the vertex. One of it maybe "id".
*/
inline void WalkVertex(const GraphArchive::VertexInfo& ver_info, std::string& primary,
std::vector<Dict>& props, std::vector<std::string>& prop_names) {
std::vector<Dict>& props, std::vector<std::string>& prop_names) {
auto ver_groups = ver_info.GetPropertyGroups();
for (auto ver_props : ver_groups) {
for (auto prop : ver_props.GetProperties()) {
Expand All @@ -99,12 +100,13 @@ inline void WalkVertex(const GraphArchive::VertexInfo& ver_info, std::string& pr
/**
* Traverse all properties of the edge, get the properties and the property names.
* Keep the original order in yml config. Similar to WalkVertex, but don't get primary.
*
* @param edge_info The gar edge information.
* @param props All the properties of the vertex. One of it maybe {"name":"id","type":"INT64"}.
* @param prop_names All the property names of the vertex. One of it maybe "id".
*/
inline void WalkEdge(const GraphArchive::EdgeInfo& edge_info, std::vector<Dict>& props,
std::vector<std::string>& prop_names) {
std::vector<std::string>& prop_names) {
GraphArchive::AdjListType adj_list_type = GraphArchive::AdjListType::ordered_by_dest;
CheckAdjListType(edge_info, adj_list_type);
auto edge_groups = edge_info.GetPropertyGroups(adj_list_type).value();
Expand All @@ -121,14 +123,15 @@ inline void WalkEdge(const GraphArchive::EdgeInfo& edge_info, std::vector<Dict>&

/**
* @brief Read the gar yml file to construct the import config in json form.
*
* @param gar_conf The json object of the import config used in import_v3.
*
* @param gar_conf The json object of the import config used in import_v3.
* @param path The location of gar yml file.
*/
inline void ParserGraphArConf(nlohmann::json& gar_conf, const std::string& path) {
auto graph_info = GraphArchive::GraphInfo::Load(path).value();
gar_conf["schema"] = {};
gar_conf["files"] = {};
std::unordered_set<std::string> labels;
auto vertex_infos = graph_info.GetVertexInfos();
for (const auto& [key, value] : vertex_infos) {
nlohmann::json schema_node;
Expand All @@ -151,22 +154,26 @@ inline void ParserGraphArConf(nlohmann::json& gar_conf, const std::string& path)
}

auto edge_infos = graph_info.GetEdgeInfos();
for (const auto& [key, value] : edge_infos) {
nlohmann::json schema_node;
schema_node["label"] = value.GetEdgeLabel();
schema_node["type"] = "EDGE";
for (const auto& [key, edge_info] : edge_infos) {
std::string label = edge_info.GetEdgeLabel();
auto result = labels.insert(label);
std::vector<Dict> properties;
std::vector<std::string> prop_names = {"SRC_ID", "DST_ID"};
WalkEdge(value, properties, prop_names);
schema_node["properties"] = properties;
gar_conf["schema"].push_back(schema_node);
WalkEdge(edge_info, properties, prop_names);
if (result.second) {
nlohmann::json schema_node;
schema_node["label"] = label;
schema_node["type"] = "EDGE";
schema_node["properties"] = properties;
gar_conf["schema"].push_back(schema_node);
}

nlohmann::json file_node;
file_node["path"] = path;
file_node["format"] = "GraphAr";
file_node["label"] = value.GetEdgeLabel();
file_node["SRC_ID"] = value.GetSrcLabel();
file_node["DST_ID"] = value.GetDstLabel();
file_node["label"] = edge_info.GetEdgeLabel();
file_node["SRC_ID"] = edge_info.GetSrcLabel();
file_node["DST_ID"] = edge_info.GetDstLabel();
file_node["columns"] = prop_names;
gar_conf["files"].push_back(file_node);
}
Expand Down
14 changes: 10 additions & 4 deletions test/integration/test_import_gar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
log = logging.getLogger(__name__)

class TestImportGar:
config_path = Path.cwd().parent.parent / "test/resource/data/ldbc_parquet/ldbc_sample.graph.yml"
config_path = Path.cwd().parent.parent / "test/resource/data/gar_test/ldbc_parquet/ldbc_sample.graph.yml"

IMPORTOPT = {"cmd":f"./lgraph_import -c {config_path} --gar true --overwrite true --d gar_db",
"cleanup_dir":["./gar_db"]}
Expand All @@ -19,9 +19,15 @@ class TestImportGar:
@pytest.mark.parametrize("server", [SERVEROPT], indirect=True)
@pytest.mark.parametrize("rest_client", [CLIENTOPT], indirect=True)
def test_import_gar(self, importor, server, rest_client):
label_res = rest_client.call_cypher("default", "CALL db.vertexLabels()")
log.info(label_res)
assert len(label_res) == 1
vertex_label_res = rest_client.call_cypher("default", "CALL db.vertexLabels()")
log.info(vertex_label_res)
assert len(vertex_label_res) == 1
assert vertex_label_res[0]['label'] == 'person'

edge_label_res = rest_client.call_cypher("default", "CALL db.edgeLabels()")
log.info(edge_label_res)
assert len(edge_label_res) == 1
assert edge_label_res[0]['label'] == 'knows'

vertex_res = rest_client.call_cypher("default", "MATCH (p:person) RETURN count(p)")
log.info(vertex_res)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 19 additions & 3 deletions test/test_import_gar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,32 @@ TEST_F(TestImportGar, TestGarConfig) {
UT_LOG() << "Parsing gar yaml config to lgraph_import json config";
Importer::Config config;
std::string tugraph_path = std::filesystem::path(__FILE__).parent_path().parent_path();
config.config_file = tugraph_path + "/test/resource/data/ldbc/ldbc.graph.yml";
config.config_file = tugraph_path + "/test/resource/data/gar_test/ldbc/ldbc.graph.yml";
config.is_graphar = true;
config.delete_if_exists = true;

nlohmann::json conf;
UT_EXPECT_NO_THROW(ParserGraphArConf(conf, config.config_file));

// test the conf
UT_EXPECT_NO_THROW(import_v2::ImportConfParser::ParseSchema(conf));
UT_EXPECT_NO_THROW(import_v2::ImportConfParser::ParseFiles(conf));

// test the schema
import_v2::SchemaDesc schemaDesc = import_v2::ImportConfParser::ParseSchema(conf);
UT_EXPECT_EQ(schemaDesc.Size(), 23);

// test the data_files
std::vector<import_v2::CsvDesc> data_files = import_v2::ImportConfParser::ParseFiles(conf);
UT_EXPECT_EQ(data_files.size(), 31);
}

TEST_F(TestImportGar, TestGarData) {
UT_LOG() << "Read gar datas";
UT_LOG() << "Read gar data";
Importer::Config config;
std::string tugraph_path = std::filesystem::path(__FILE__).parent_path().parent_path();
config.config_file = tugraph_path + "/test/resource/data/ldbc_parquet/ldbc_sample.graph.yml";
config.config_file =
tugraph_path + "/test/resource/data/gar_test/ldbc_parquet/ldbc_sample.graph.yml";
config.is_graphar = true;
config.delete_if_exists = true;

Expand All @@ -57,4 +70,7 @@ TEST_F(TestImportGar, TestGarData) {
import_v2::GraphArParser parser = import_v2::GraphArParser(data_files.front());
std::vector<std::vector<FieldData>> block;
UT_EXPECT_NO_THROW(parser.ReadBlock(block));
UT_EXPECT_EQ(block.size(), 903);
UT_EXPECT_EQ(block[0][0].ToString(), "933");
UT_EXPECT_EQ(block[0][1].ToString(), "Mahinda");
}

0 comments on commit c614f47

Please sign in to comment.