Skip to content

Commit

Permalink
fix tests, add example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wey-gu committed Mar 16, 2024
1 parent 9c1f685 commit 6413985
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def result_to_df(result: ResultSet) -> pd.DataFrame:
col_name = columns[col_num]
col_list = result.column_values(col_name)
d[col_name] = [x.cast() for x in col_list]
return pd.DataFrame.from_dict(d, orient='columns')
return pd.DataFrame(d)

# define a config
config = Config()
Expand Down
4 changes: 2 additions & 2 deletions example/FormatResp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Method 0 (Recommended) #
# nebula3-python>=3.6.0 #
################################
def result_to_df(result: ResultSet) -> pd.DataFrame:
def result_to_df_buildin(result: ResultSet) -> pd.DataFrame:
"""
build list for each column, and transform to dataframe
"""
Expand All @@ -42,7 +42,7 @@ def result_to_df(result: ResultSet) -> pd.DataFrame:
col_name = columns[col_num]
col_list = result.column_values(col_name)
d[col_name] = [x.cast() for x in col_list]
return pd.DataFrame.from_dict(d, columns=columns)
return pd.DataFrame(d)


################################
Expand Down
27 changes: 25 additions & 2 deletions example/GraphClientSimpleExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import json
import time

from FormatResp import print_resp
from FormatResp import print_resp, result_to_df_buildin, result_to_df
import pandas as pd

from nebula3.Config import Config
from nebula3.gclient.net import ConnectionPool
Expand All @@ -35,7 +36,7 @@
client.execute(
"CREATE SPACE IF NOT EXISTS test(vid_type=FIXED_STRING(30)); USE test;"
"CREATE TAG IF NOT EXISTS person(name string, age int);"
"CREATE EDGE like (likeness double);"
"CREATE EDGE IF NOT EXISTS like (likeness double);"
)

# insert data need to sleep after create schema
Expand All @@ -59,6 +60,28 @@
assert resp.is_succeeded(), resp.error_msg()
print_resp(resp)

# query data
resp = client.execute(
'GET SUBGRAPH WITH PROP 2 STEPS FROM "Bob" YIELD VERTICES AS nodes, EDGES AS relationships;'
)
df = result_to_df_buildin(resp)
df_1 = result_to_df(resp)

print("Testing pandas dataframe operations")
print(df_1)

# Convert the dataframe 'df' into a CSV file
df.to_csv('subgraph_data.csv', index=False)
print("Dataframe 'df' has been exported to 'subgraph_data.csv'.")

# Read the CSV file back into a dataframe
df_csv = pd.read_csv('subgraph_data.csv')
print("CSV file 'subgraph_data.csv' has been read into dataframe 'df_csv'.")

# Display the first 5 rows of the dataframe
print("Displaying the first 5 rows of dataframe 'df_csv':")
print(df_csv.head())

# drop space
resp = client.execute("DROP SPACE test")
assert resp.is_succeeded(), resp.error_msg()
Expand Down
10 changes: 6 additions & 4 deletions tests/test_data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,9 @@ def test_cast_primitive(self):
def _cast_node(node: Vertex):
return {
"vid": node.get_id().cast(),
"tags": node.tags(),
"props": node.properties(),
"tags": {
tag_name: node.properties(tag_name) for tag_name in node.tags()
},
}

def _cast_relationship(edge: Edge):
Expand Down Expand Up @@ -507,8 +508,9 @@ def _cast_relationship(edge: Edge):
# Test time
time_val = ttypes.Value()
time_val.set_tVal(Time(10, 10, 10, 10000))
time = ValueWrapper(time_val).as_time()
assert time.cast_primitive() == time.__repr__()
time_raw = ValueWrapper(time_val)
time = time_raw.as_time()
assert time_raw.cast_primitive() == time.__repr__()

def test_as_time(self):
time = Time()
Expand Down

0 comments on commit 6413985

Please sign in to comment.