Skip to content

Commit

Permalink
Improve tests with pytest fixture (vesoft-inc#321)
Browse files Browse the repository at this point in the history
* Improve test with pytest fixture

Format

Simplify

* refactor class fixture

* Replace CommonTtypes with ttypes

Extract fill_ve function

Fix plan node name typo

Try to fix checkout action

* Fix path test failure

* Try to close socket
  • Loading branch information
yixinglu authored Oct 15, 2020
1 parent abd1b8b commit 4687cad
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 298 deletions.
16 changes: 8 additions & 8 deletions src/planner/PlanNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,21 @@ const char* PlanNode::toString(PlanNode::Kind kind) {
case Kind::kAlterEdge:
return "AlterEdge";
case Kind::kCreateTagIndex:
return "kCreateTagIndex";
return "CreateTagIndex";
case Kind::kCreateEdgeIndex:
return "kCreateEdgeIndex";
return "CreateEdgeIndex";
case Kind::kDropTagIndex:
return "kDropTagIndex";
return "DropTagIndex";
case Kind::kDropEdgeIndex:
return "kDropEdgeIndex";
return "DropEdgeIndex";
case Kind::kDescTagIndex:
return "kDescTagIndex";
return "DescTagIndex";
case Kind::kDescEdgeIndex:
return "kDescEdgeIndex";
return "DescEdgeIndex";
case Kind::kRebuildTagIndex:
return "kRebuildTagIndex";
return "RebuildTagIndex";
case Kind::kRebuildEdgeIndex:
return "kRebuildEdgeIndex";
return "RebuildEdgeIndex";
case Kind::kInsertVertices:
return "InsertVertices";
case Kind::kInsertEdges:
Expand Down
8 changes: 4 additions & 4 deletions tests/common/nebula_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def _find_free_port(self):
return ports

def _telnet_port(self, port):
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(1)
result = sk.connect_ex(('127.0.0.1', port))
return result == 0
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sk:
sk.settimeout(1)
result = sk.connect_ex(('127.0.0.1', port))
return result == 0

def install(self):
os.mkdir(self.work_dir)
Expand Down
228 changes: 1 addition & 227 deletions tests/common/nebula_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,6 @@ def setup_class(self):
self.set_delay()
self.prepare()

@classmethod
def load_vertex_edge(self):
self.VERTEXS = dict()
self.EDGES = dict()
nba_file = self.data_dir + '/data/nba.ngql'
print("load will open ", nba_file)
with open(nba_file, 'r') as data_file:
lines = data_file.readlines()
ddl = False
dataType = ['none']
for line in lines:
strip_line = line.strip()
if len(strip_line) == 0:
continue
elif strip_line.startswith('--'):
comment = strip_line[2:]
if comment == 'DDL':
ddl = True
elif comment == 'END':
if ddl:
ddl = False
else:
if not ddl:
self.parse_line(line.strip(), dataType)
if line.endswith(';'):
dataType[0] = 'none'

@classmethod
def load_data(self):
self.data_loaded = True
Expand Down Expand Up @@ -433,7 +406,7 @@ def check_result(self, resp, expect, ignore_col: Set[int] = set(), is_regex=Fals
if resp.data is None:
assert False, 'resp.data is None'
rows = resp.data.rows

msg = 'len(rows)[%d] != len(expect)[%d]' % (len(rows), len(expect))
assert len(rows) == len(expect), msg

Expand Down Expand Up @@ -642,204 +615,6 @@ def check_error_msg(self, resp, expect):
else:
assert err_msg == expect, msg

@classmethod
def parse_line(self, line, dataType):
if line.startswith('INSERT') or line.startswith('VALUES'):
return ''

if line.startswith('VERTEX player'):
dataType[0] = 'player'
elif line.startswith('VERTEX team'):
dataType[0] = 'team'
elif line.startswith('VERTEX bachelor'):
dataType[0] = 'bachelor'
elif line.startswith('EDGE serve'):
dataType[0] = 'serve'
elif line.startswith('EDGE like'):
dataType[0] = 'like'
elif line.startswith('EDGE teammate'):
dataType[0] = 'teammate'
else:
line = re.split(':|,|->', line.strip(',; \t'))
line = list(map(lambda i: i.strip(' ()"'), line))
value = CommonTtypes.Value()
if dataType[0] == 'none':
assert False
elif dataType[0] == 'player':
vertex = self.create_vertex_player(line)
key = str(vertex.vid, encoding='utf-8')
if key in self.VERTEXS:
temp = self.VERTEXS[key].get_vVal()
temp.tags.append(vertex.tags[0])
temp.tags.sort(key=lambda x : x.name)
value.set_vVal(temp)
self.VERTEXS[key] = value
else:
value.set_vVal(vertex)
self.VERTEXS[key] = value
elif dataType[0] == 'team':
vertex = self.create_vertex_team(line)
value.set_vVal(vertex)
key = str(vertex.vid, encoding = 'utf-8')
self.VERTEXS[key] = value
elif dataType[0] == 'bachelor':
vertex = self.create_vertex_bachelor(line)
key = str(vertex.vid, encoding = 'utf-8')
if key in self.VERTEXS:
temp = self.VERTEXS[key].get_vVal()
temp.tags.append(vertex.tags[0])
temp.tags.sort(key=lambda x : x.name)
value.set_vVal(temp)
self.VERTEXS[key] = value
else:
value.set_vVal(vertex)
self.VERTEXS[key] = value
elif dataType[0] == 'serve':
edge = self.create_edge_serve(line)
value.set_eVal(edge)
key = str(edge.src, encoding = 'utf-8') + str(edge.dst, encoding = 'utf-8') + str(edge.name, encoding = 'utf-8') + str(edge.ranking)
self.EDGES[key] = value
elif dataType[0] == 'like':
edge = self.create_edge_like(line)
value.set_eVal(edge)
key = str(edge.src, encoding = 'utf-8') + str(edge.dst, encoding = 'utf-8') + str(edge.name, encoding = 'utf-8') + str(edge.ranking)
self.EDGES[key] = value
elif dataType[0] == 'teammate':
edge = self.create_edge_teammate(line)
value.set_eVal(edge)
key = str(edge.src, encoding = 'utf-8') + str(edge.dst, encoding = 'utf-8') + str(edge.name, encoding = 'utf-8') + str(edge.ranking)
self.EDGES[key] = value
else:
assert False

@classmethod
def create_vertex_player(self, line):
if len(line) != 3:
assert False

vertex = CommonTtypes.Vertex()
vertex.vid = bytes(line[0], encoding = 'utf-8')
tags = []
tag = CommonTtypes.Tag()
tag.name = bytes('player', encoding = 'utf-8')

props = dict()
name = CommonTtypes.Value()
name.set_sVal(bytes(line[1], encoding = 'utf-8'))
props[bytes('name', encoding = 'utf-8')] = name
age = CommonTtypes.Value()
age.set_iVal(int(line[2]))
props[bytes('age', encoding = 'utf-8')] = age
tag.props = props
tags.append(tag)
vertex.tags = tags
return vertex

@classmethod
def create_vertex_team(self, line):
if len(line) != 2:
assert False
vertex = CommonTtypes.Vertex()
vertex.vid = bytes(line[0], encoding = 'utf-8')
tags = []
tag = CommonTtypes.Tag()
tag.name = bytes('team', encoding = 'utf-8')

props = dict()
name = CommonTtypes.Value()
name.set_sVal(bytes(line[1], encoding = 'utf-8'))
props[bytes('name', encoding = 'utf-8')] = name
tag.props = props
tags.append(tag)
vertex.tags = tags
return vertex

@classmethod
def create_vertex_bachelor(self, line):
if len(line) != 3:
assert False

vertex = CommonTtypes.Vertex()
vertex.vid = bytes(line[0], encoding = 'utf-8')
tags = []
tag = CommonTtypes.Tag()
tag.name = bytes('bachelor', encoding = 'utf-8')

props = dict()
name = CommonTtypes.Value()
name.set_sVal(bytes(line[1], encoding = 'utf-8'))
props[bytes('name', encoding = 'utf-8')] = name
speciality = CommonTtypes.Value()
speciality.set_sVal(bytes(line[2], encoding = 'utf-8'))
props[bytes('speciality', encoding = 'utf-8')] = speciality
tag.props = props
tags.append(tag)
vertex.tags = tags
return vertex

@classmethod
def create_edge_serve(self, line):
if len(line) != 4:
assert False
edge = CommonTtypes.Edge()
edge.src = bytes(line[0], encoding = 'utf-8')
if '@' in line[1]:
temp = list(map(lambda i: i.strip('"'), re.split('@', line[1])))
edge.dst = bytes(temp[0], encoding = 'utf-8')
edge.ranking = int(temp[1])
else:
edge.dst = bytes(line[1], encoding = 'utf-8')
edge.ranking = 0
edge.type = 1
edge.name = bytes('serve', encoding = 'utf-8')
props = dict()
start_year = CommonTtypes.Value()
start_year.set_iVal(int(line[2]))
end_year = CommonTtypes.Value()
end_year.set_iVal(int(line[3]))
props[bytes('start_year', encoding = 'utf-8')] = start_year
props[bytes('end_year', encoding = 'utf-8')] = end_year
edge.props = props
return edge

@classmethod
def create_edge_like(self, line):
if len(line) != 3:
assert False
edge = CommonTtypes.Edge()

edge.src = bytes(line[0], encoding = 'utf-8')
edge.dst = bytes(line[1], encoding = 'utf-8')
edge.type = 1
edge.ranking = 0
edge.name = bytes('like', encoding = 'utf-8')
props = dict()
likeness = CommonTtypes.Value()
likeness.set_iVal(int(line[2]))
props[bytes('likeness', encoding = 'utf-8')] = likeness
edge.props = props
return edge

@classmethod
def create_edge_teammate(self, line):
if len(line) != 4:
assert False
edge = CommonTtypes.Edge()
edge.src = bytes(line[0], encoding = 'utf-8')
edge.dst = bytes(line[1], encoding = 'utf-8')
edge.type = 1
edge.ranking = 0
edge.name = bytes('teammate', encoding = 'utf-8')
props = dict()
start_year = CommonTtypes.Value()
start_year.set_iVal(int(line[2]))
end_year = CommonTtypes.Value()
end_year.set_iVal(int(line[3]))
props[bytes('start_year', encoding = 'utf-8')] = start_year
props[bytes('end_year', encoding = 'utf-8')] = end_year
edge.props = props
return edge

@classmethod
def check_exec_plan(cls, resp, expect):
cls.check_resp_succeeded(resp)
Expand Down Expand Up @@ -873,4 +648,3 @@ def diff_plan_node(cls, plan_desc, line_num, expect, expect_idx):
for i in range(len(plan_node_desc.dependencies)):
line_num = plan_desc.node_index_map[plan_node_desc.dependencies[i]]
cls.diff_plan_node(plan_desc, line_num, expect, expect_node[1][i])

Loading

0 comments on commit 4687cad

Please sign in to comment.