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 tck compare in order #4149

Merged
merged 20 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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
104 changes: 62 additions & 42 deletions tests/common/comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@


class DataSetComparator:
def __init__(self,
strict=True,
order=False,
contains=CmpType.EQUAL,
first_n_records=-1,
decode_type='utf-8',
vid_fn=None):
def __init__(
self,
strict=True,
order=False,
contains=CmpType.EQUAL,
first_n_records=-1,
decode_type='utf-8',
vid_fn=None,
):
self._strict = strict
self._order = order
self._contains = contains
self._first_n_records=first_n_records
self._first_n_records = first_n_records
self._decode_type = decode_type
self._vid_fn = vid_fn

Expand All @@ -48,9 +50,11 @@ def s(self, b: bytes) -> str:
return b.decode(self._decode_type)

def _whether_return(self, cmp: bool) -> bool:
return ((self._contains == CmpType.EQUAL and not cmp)
or (self._contains == CmpType.CONTAINS and not cmp)
or (self._contains == CmpType.NOT_CONTAINS and cmp))
return (
(self._contains == CmpType.EQUAL and not cmp)
or (self._contains == CmpType.CONTAINS and not cmp)
or (self._contains == CmpType.NOT_CONTAINS and cmp)
)

def compare(self, resp: DataSet, expect: DataSet):
if self._contains == CmpType.NOT_CONTAINS and len(resp.rows) == 0:
Expand All @@ -66,19 +70,18 @@ def compare(self, resp: DataSet, expect: DataSet):
for (ln, rn) in zip(resp.column_names, expect.column_names):
if ln != self.bstr(rn):
return False, -2
if self._order:
if self._contains == CmpType.CONTAINS and self._first_n_records < 0:
for i in range(0, len(expect.rows)):
cmp = self.compare_row(resp.rows[i], expect.rows[i])
if self._whether_return(cmp):
return False, i
return True, None
elif self._contains == CmpType.CONTAINS and self._first_n_records > 0:
return self._compare_list(resp.rows[0:self._first_n_records], expect.rows, self.compare_row)
if self._order and self._contains == CmpType.EQUAL:
if self._first_n_records > 0:
# just compare the first n records
resp_rows = resp.rows[0 : self._first_n_records]
else:
return len(resp.rows) == len(expect.rows), -1
return self._compare_list(resp.rows, expect.rows, self.compare_row,
self._contains)
resp_rows = resp.rows

return self._compate_order_list(resp_rows, expect.rows, self.compare_row)

return self._compare_list(
resp.rows, expect.rows, self.compare_row, self._contains
)

def compare_value(self, lhs: Value, rhs: Union[Value, Pattern]) -> bool:
"""
Expand All @@ -104,7 +107,7 @@ def compare_value(self, lhs: Value, rhs: Union[Value, Pattern]) -> bool:
if lhs.getType() == Value.FVAL:
if not rhs.getType() == Value.FVAL:
return False
return math.fabs(lhs.get_fVal() - rhs.get_fVal()) < 1.0E-8
return math.fabs(lhs.get_fVal() - rhs.get_fVal()) < 1.0e-8
if lhs.getType() == Value.SVAL:
if not rhs.getType() == Value.SVAL:
return False
Expand All @@ -123,8 +126,7 @@ def compare_value(self, lhs: Value, rhs: Union[Value, Pattern]) -> bool:
return lhs.get_tVal() == rhs.get_tVal()
if rhs.getType() == Value.SVAL:
lt = lhs.get_tVal()
lts = "%02d:%02d:%02d.%06d" % (lt.hour, lt.minute, lt.sec,
lt.microsec)
lts = "%02d:%02d:%02d.%06d" % (lt.hour, lt.minute, lt.sec, lt.microsec)
rv = rhs.get_sVal()
return lts == rv if type(rv) == str else self.b(lts) == rv
return False
Expand All @@ -134,8 +136,14 @@ def compare_value(self, lhs: Value, rhs: Union[Value, Pattern]) -> bool:
if rhs.getType() == Value.SVAL:
ldt = lhs.get_dtVal()
ldts = "%d-%02d-%02dT%02d:%02d:%02d.%06d" % (
ldt.year, ldt.month, ldt.day, ldt.hour, ldt.minute,
ldt.sec, ldt.microsec)
ldt.year,
ldt.month,
ldt.day,
ldt.hour,
ldt.minute,
ldt.sec,
ldt.microsec,
)
rv = rhs.get_sVal()
return ldts == rv if type(rv) == str else self.b(ldts) == rv
return False
Expand Down Expand Up @@ -227,16 +235,18 @@ def compare_edge(self, lhs: Edge, rhs: Edge):
if not lhs.ranking == rhs.ranking:
return False
rsrc, rdst = self.eid(rhs, lhs.type)
if not (self.compare_vid(lhs.src, rsrc)
and self.compare_vid(lhs.dst, rdst)):
if not (
self.compare_vid(lhs.src, rsrc) and self.compare_vid(lhs.dst, rdst)
):
return False
if rhs.props is None or len(lhs.props) != len(rhs.props):
return False
else:
if rhs.src is not None and rhs.dst is not None:
rsrc, rdst = self.eid(rhs, lhs.type)
if not (self.compare_vid(lhs.src, rsrc)
and self.compare_vid(lhs.dst, rdst)):
if not (
self.compare_vid(lhs.src, rsrc) and self.compare_vid(lhs.dst, rdst)
):
return False
if rhs.ranking is not None:
if lhs.ranking != rhs.ranking:
Expand All @@ -252,9 +262,9 @@ def bstr(self, vid) -> bytes:
return self.b(vid) if type(vid) == str else vid

def _compare_vid(
self,
lid: Union[int, bytes],
rid: Union[int, bytes, str],
self,
lid: Union[int, bytes],
rid: Union[int, bytes, str],
) -> bool:
if type(lid) is bytes:
return type(rid) in [str, bytes] and lid == self.bstr(rid)
Expand Down Expand Up @@ -296,8 +306,9 @@ def compare_node(self, lhs: Vertex, rhs: Vertex):
return False
rtags = [] if rhs.tags is None else rhs.tags
for tag in rtags:
ltag = [[lt.name, lt.props] for lt in lhs.tags
if self.bstr(tag.name) == lt.name]
ltag = [
[lt.name, lt.props] for lt in lhs.tags if self.bstr(tag.name) == lt.name
]
if len(ltag) != 1:
return False
if self._strict:
Expand All @@ -311,9 +322,7 @@ def compare_node(self, lhs: Vertex, rhs: Vertex):
return False
return True

def _get_map_value_by_key(self,
key: bytes,
kv: Dict[Union[str, bytes], Value]):
def _get_map_value_by_key(self, key: bytes, kv: Dict[Union[str, bytes], Value]):
for k, v in kv.items():
if key == self.bstr(k):
return True, v
Expand All @@ -339,8 +348,19 @@ def compare_list(self, lhs: List[Value], rhs: List[Value]):
def compare_row(self, lhs: Row, rhs: Row):
if not len(lhs.values) == len(rhs.values):
return False
return all(
self.compare_value(l, r) for (l, r) in zip(lhs.values, rhs.values))
return all(self.compare_value(l, r) for (l, r) in zip(lhs.values, rhs.values))

def _compate_order_list(self, lhs, rhs, cmp_fn):
"""
compare the order list,
different with _compare_list, the order should be strict
"""
if len(lhs) != len(rhs):
return False, -1
for i, lr in enumerate(lhs):
if not cmp_fn(lr, rhs[i]):
return False, i
return True, -1

def _compare_list(self, lhs, rhs, cmp_fn, contains=False):
visited = []
Expand Down
51 changes: 30 additions & 21 deletions tests/tck/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ def exec_ctx(session):


@given(parse('parameters: {parameters}'))
def preload_parameters(
parameters
):
def preload_parameters(parameters):
try:
paramMap = json.loads(parameters)
for (k, v) in paramMap.items():
Expand All @@ -137,26 +135,26 @@ def preload_parameters(
def clear_parameters():
params = {}


# construct python-type to nebula.Value


def value(any):
v = Value()
if (isinstance(any, bool)):
if isinstance(any, bool):
v.set_bVal(any)
elif (isinstance(any, int)):
elif isinstance(any, int):
v.set_iVal(any)
elif (isinstance(any, str)):
elif isinstance(any, str):
v.set_sVal(any)
elif (isinstance(any, float)):
elif isinstance(any, float):
v.set_fVal(any)
elif (isinstance(any, list)):
elif isinstance(any, list):
v.set_lVal(list2Nlist(any))
elif (isinstance(any, dict)):
elif isinstance(any, dict):
v.set_mVal(map2NMap(any))
else:
raise TypeError("Do not support convert " +
str(type(any))+" to nebula.Value")
raise TypeError("Do not support convert " + str(type(any)) + " to nebula.Value")
return v


Expand Down Expand Up @@ -256,8 +254,7 @@ def new_space(request, exec_ctx):

@given(parse('load "{data}" csv data to a new space'))
def import_csv_data(request, data, exec_ctx, pytestconfig):
data_dir = os.path.join(
DATA_DIR, normalize_outline_scenario(request, data))
data_dir = os.path.join(DATA_DIR, normalize_outline_scenario(request, data))
space_desc = load_csv_data(
exec_ctx.get('current_session'),
data_dir,
Expand Down Expand Up @@ -380,6 +377,7 @@ def when_login_graphd(graph, user, password, class_fixture_variables, pytestconf
class_fixture_variables["sessions"].append(sess)
class_fixture_variables["pool"] = pool


# This is a workaround to test login retry because nebula-python treats
# authentication failure as exception instead of error.

Expand Down Expand Up @@ -409,7 +407,9 @@ def executing_query(query, exec_ctx, request):


@when(parse("executing query with user {username} with password {password}:\n{query}"))
def executing_query(username, password, conn_pool_to_first_graph_service, query, exec_ctx, request):
def executing_query(
username, password, conn_pool_to_first_graph_service, query, exec_ctx, request
):
sess = conn_pool_to_first_graph_service.get_session(username, password)
ngql = combine_query(query)
exec_query(request, ngql, exec_ctx, sess)
Expand Down Expand Up @@ -678,8 +678,12 @@ def result_should_contain(request, result, exec_ctx):
)


@then(parse("the result should contain, replace the holders with cluster info:\n{result}"))
def then_result_should_contain_replace(request, result, exec_ctx, class_fixture_variables):
@then(
parse("the result should contain, replace the holders with cluster info:\n{result}")
)
def then_result_should_contain_replace(
request, result, exec_ctx, class_fixture_variables
):
result = replace_result_with_cluster_info(result, class_fixture_variables)
cmp_dataset(
request,
Expand Down Expand Up @@ -732,7 +736,11 @@ def execution_should_be_succ(exec_ctx):
check_resp(rs, stmt)


@then(rparse(r"(?P<unit>a|an) (?P<err_type>\w+) should be raised at (?P<time>runtime|compile time)(?P<sym>:|.)(?P<msg>.*)"))
@then(
rparse(
r"(?P<unit>a|an) (?P<err_type>\w+) should be raised at (?P<time>runtime|compile time)(?P<sym>:|.)(?P<msg>.*)"
)
)
def raised_type_error(unit, err_type, time, sym, msg, exec_ctx):
res = exec_ctx["result_set"]
ngql = exec_ctx['ngql']
Expand All @@ -746,7 +754,9 @@ def raised_type_error(unit, err_type, time, sym, msg, exec_ctx):
else:
expect_msg = "{}: {}".format(err_type, msg)
m = res_msg.startswith(expect_msg)
assert m, f'Could not find "{expect_msg}" in "{res_msg}" when execute query: "{ngql}"'
assert (
m
), f'Could not find "{expect_msg}" in "{res_msg}" when execute query: "{ngql}"'


@then("drop the used space")
Expand All @@ -769,8 +779,7 @@ def check_plan(plan, exec_ctx):
idx = column_names.index('dependencies')
rows = expect.get("rows", [])
for i, row in enumerate(rows):
row[idx] = [int(cell.strip())
for cell in row[idx].split(",") if len(cell) > 0]
row[idx] = [int(cell.strip()) for cell in row[idx].split(",") if len(cell) > 0]
rows[i] = row
differ = PlanDiffer(resp.plan_desc(), expect)
assert differ.diff(), differ.err_msg()
Expand Down Expand Up @@ -808,7 +817,7 @@ def result_should_be_in_order_and_register_key(
result,
order=True,
strict=True,
contains=CmpType.CONTAINS,
contains=CmpType.EQUAL,
first_n_records=n,
)
register_result_key(request.node.name, result_ds, column_name, key)
Expand Down
Loading