def test_term_aggregation(O): errors = [] setupGraph(O) response = O.aggregate(gripql.term("test-agg", "Person", "name", 2)) if 'test-agg' not in response: errors.append("Result had Incorrect aggregation name") return errors response = response['test-agg'] if len(response["buckets"]) != 2: errors.append( "Unexpected number of terms: %d != %d" % (len(response["buckets"]), 2) ) for res in response["buckets"]: if res["key"] not in ["marko", "alex"]: errors.append( "Incorrect term returned: %s" % (res["key"]) ) if res["value"] != 2: errors.append( "Incorrect term count: %d != %d" % (res["value"], 2)) response = O.aggregate(gripql.term("test-agg-no-limit", "Person", "name", size=None)) if 'test-agg-no-limit' not in response: errors.append("Result had Incorrect aggregation name") return errors response = response['test-agg-no-limit'] if len(response["buckets"]) != 8: errors.append( "Unexpected number of terms: %d != %d" % (len(response["buckets"]), 8) ) for res in response["buckets"]: if res["key"] in ["marko", "alex"]: if res["value"] != 2: errors.append( "Incorrect term count: %d != %d" % (res["value"], 2)) else: if res["value"] != 1: errors.append( "Incorrect term count: %d != %d" % (res["value"], 1)) return errors
def test_traversal_term_aggregation(O): errors = [] setupGraph(O) count = 0 for row in O.query().V("1").out().hasLabel("Person").aggregate( gripql.term("traversal-agg", "name")): if 'traversal-agg' not in row: errors.append("Result had Incorrect aggregation name") return errors row = row['traversal-agg'] count += 1 if len(row["buckets"]) != 3: errors.append("Unexpected number of terms: %d != %d" % (len(row["buckets"]), 3)) for res in row["buckets"]: if res["key"] == "alex": if res["value"] != 2: errors.append("Incorrect term count: %d != %d" % (res["value"], 2)) else: if res["value"] != 1: errors.append("Incorrect term count: %d != %d" % (res["value"], 1)) if count != 1: errors.append("Incorrect number of aggregations returned: %d != %d" % (count, 1)) return errors
def test_traversal_gid_aggregation(O): errors = [] setupGraph(O) count = 0 for row in O.query().V().hasLabel("Person").as_("a").out("knows").select( "a").aggregate(gripql.term("gid-agg", "_gid")): count += 1 if 'gid-agg' not in row: errors.append("Result had Incorrect aggregation name") return errors row = row['gid-agg'] print(row) if len(row["buckets"]) < 2: errors.append("Unexpected number of terms: %d != %d" % (len(row["buckets"]), 2)) for res in row["buckets"]: if res["key"] == "1": if res["value"] != 4: errors.append("Incorrect bucket count returned: %s" % res) elif res["key"] == "2": if res["value"] != 3: errors.append("Incorrect bucket count returned: %s" % res) else: errors.append("Incorrect bucket key returned: %s" % res) if count != 1: errors.append("Incorrect number of aggregations returned: %d != %d" % (count, 1)) return errors
def test_traversal_term_aggregation(man): errors = [] G = man.setGraph("swapi") count = 0 for row in G.query().V("Film:1").out().hasLabel("Character").aggregate( gripql.term("traversal-agg", "eye_color")): if 'traversal-agg' not in row: errors.append("Result had Incorrect aggregation name") return errors row = row['traversal-agg'] count += 1 if len(row["buckets"]) != len(eye_color_count_map): errors.append("Unexpected number of terms: %d != %d" % (len(row["buckets"]), len(eye_color_count_map))) for res in row["buckets"]: if eye_color_count_map[res['key']] != res['value']: errors.append("Wrong key count for %s %d != %d" % (res['key'], res["value"], eye_color_count_map[res['key']])) if count != 1: errors.append("Incorrect number of aggregations returned: %d != %d" % (count, 1)) return errors
def test_traversal_gid_aggregation(man): errors = [] G = man.setGraph("swapi") planet_agg_map = {"Planet:1": 7, "Planet:2": 2} count = 0 for row in G.query().V().hasLabel("Planet").as_("a").out( "residents").select("a").aggregate(gripql.term("gid-agg", "_gid")): count += 1 if 'gid-agg' not in row: errors.append("Result had Incorrect aggregation name") return errors row = row['gid-agg'] print(row) if len(row["buckets"]) < len(planet_agg_map): errors.append("Unexpected number of terms: %d != %d" % (len(row["buckets"]), 2)) for res in row["buckets"]: if planet_agg_map[res["key"]] != res["value"]: errors.append("Incorrect bucket count returned: %s" % res) if count != 1: errors.append("Incorrect number of aggregations returned: %d != %d" % (count, 1)) return errors
def update_facets(label, graph): if label is None or graph is None: return {}, [] c = connect() G = c.graph(graph) schema = G.getSchema() columns = [] fieldType = {} fields = None for v in schema.get("vertices", []): if v["gid"] == label: fields = list(v["data"].keys()) for f in fields: t = v["data"][f] if t in ["STRING", "BOOL"]: columns.append({"name":f, "id":f}) fieldType[f] = t elif t in ["NUMERIC"]: columns.append({"name":f, "id":f}) fieldType[f] = t else: print("type", f, t) if fields is not None: facetAgg = {} for row in G.query().V().hasLabel(label).aggregate( list(gripql.term(f, f) for f in fieldType.keys()) ): if row['name'] not in facetAgg: facetAgg[row['name']] = {} if fieldType[row['name']] == "NUMERIC": facetAgg[row['name']]['min'] = min(facetAgg[row['name']].get('min', row['key']), row['key']) facetAgg[row['name']]['max'] = max(facetAgg[row['name']].get('max', row['key']), row['key']) else: facetAgg[row['name']][row['key']] = row['value'] facets = {} index = 0 for name, valueSet in facetAgg.items(): if fieldType[name] == "NUMERIC": facets[name] = {"index":index, "values":facetAgg[name], "type":fieldType[name]} index += 1 elif len(valueSet) < 50: values = [] options = [] for i, value in enumerate(valueSet): values.append(value) options.append({"value":i,"label":str(value)}) facets[name] = {"index":index, "options":options, "values":values, "type":fieldType[name]} index += 1 print("facets", facets) return facets, columns
def test_simple(man): errors = [] G = man.setGraph("swapi") count = 0 for row in G.query().V().aggregate(gripql.term("simple-agg", "eye_color")): if 'simple-agg' not in row: errors.append("Result had Incorrect aggregation name") return errors row = row['simple-agg'] for res in row["buckets"]: count += 1 if eye_color_count_map[res['key']] != res['value']: errors.append("Wrong key count for %s %d != %d" % (res['key'], res["value"], eye_color_count_map[res['key']])) if count != 8: errors.append("Wrong number of results recieved %d" % (count)) return errors
def term(self, *args, **kwargs): return gripql.term(*args, **kwargs)