Skip to content
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

Fix json serialization error in Python Driver #1228

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions drivers/python/age/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,12 @@ def _nodeToString(node, buf, extraFormatter=None):

if node.properties != None:
buf.write(", properties:{")
for k,v in node.properties.items():
buf.write(k)
buf.write(": ")
buf.write(str(v))
buf.write(", ")
prop_list = []
for k, v in node.properties.items():
prop_list.append(f"{k}: {str(v)}")

# Join properties with comma and write to buffer
buf.write(", ".join(prop_list))
buf.write("}")

if extraFormatter != None:
Expand Down Expand Up @@ -281,12 +282,13 @@ def _nodeToJson(node, buf, extraFormatter=None):

if node.properties != None:
buf.write(", \"properties\":{")
for k,v in node.properties.items():
buf.write("\"")
buf.write(k)
buf.write("\": \"")
buf.write(str(v))
buf.write("\", ")

prop_list = []
for k, v in node.properties.items():
prop_list.append(f"\"{k}\": \"{str(v)}\"")

# Join properties with comma and write to buffer
buf.write(", ".join(prop_list))
buf.write("}")
buf.write("}")

48 changes: 48 additions & 0 deletions drivers/python/test_age_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json

from age.models import Vertex
import unittest
Expand Down Expand Up @@ -328,6 +329,52 @@ def testCollect(self):
self.assertEqual(3,len(collected))
print("\nTest 6 Successful...")

def testSerialization(self):

print("\n---------------------------------------")
print("Test 6: Testing Vertex Serialization.....")
print("-----------------------------------------\n")

ag = self.ag

with ag.connection.cursor() as cursor:
try:
ag.cypher(cursor, "CREATE (n:Person {name: %s}) ", params=('Joe',))
ag.cypher(cursor, "CREATE (n:Person {name: %s}) ", params=('Jack',))
ag.cypher(cursor, "CREATE (n:Person {name: %s}) ", params=('Andy',))
ag.cypher(cursor, "CREATE (n:Person {name: %s}) ", params=('Smith',))
ag.cypher(cursor, "CREATE (n:Person {name: %s}) ", params=('Tom',))

# You must commit explicitly
ag.commit()
except Exception as ex:
print(ex)
ag.rollback()

print(" -------- TESTING Output #1 --------")
cursor = ag.execCypher("MATCH (n) RETURN n")

for row in cursor:
vertex = row[0]
try:
# json.loads will fail if the json str is not properly formatted
as_dict = json.loads(vertex.toJson())
print("Vertex.toJson() returns a correct json string.")
assert True
except:
assert False

print(" -------- TESTING Output #2 --------")
cursor = ag.execCypher("MATCH (n) RETURN n")

for row in cursor:
vertex = row[0]
as_str = vertex.toString()
# Checking if the trailing comma appears in .toString() output
self.assertFalse(as_str.endswith(", }}::VERTEX"))
print("Vertex.toString() 'properties' field is formatted properly.")


if __name__ == '__main__':
parser = argparse.ArgumentParser()

Expand Down Expand Up @@ -364,5 +411,6 @@ def testCollect(self):
suite.addTest(TestAgeBasic('testCypher'))
suite.addTest(TestAgeBasic('testMultipleEdges'))
suite.addTest(TestAgeBasic('testCollect'))
suite.addTest(TestAgeBasic('testSerialization'))
TestAgeBasic.args = args
unittest.TextTestRunner().run(suite)