def testGraph(self): assert len(AnimalsNode.registry) == 3 assert len(AnimalsRelationship.registry) == 3 g = self.g rat = g.animals.create(name='rat', specie='rodent') mouse = g.animals.create(name='mouse', specie='rodent') queried_rat = g.query(Animal).filter( Animal.name.endswith('at') | (Animal.name == 'tiger')).one() assert rat == queried_rat invalid_query_args = {'name': 'rat', 'name="rat" OR 1': 1} try: g.animals.query(**invalid_query_args).all() except: pass else: assert False and 'Invalid params did not raise an exception!' queried_mouse = g.query(mouse).one() assert mouse == queried_mouse assert mouse == g.get_vertex(mouse._id) assert mouse == g.get_element(mouse._id) try: rat2 = g.animals.create(name='rat', specie='rodent') except: pass else: assert False and 'Uniqueness not enforced correctly' pea = g.foods.create(name='pea', color='green') queried_pea = g.foods.query(color='green', name='pea').one() cheese = g.foods.create(name='cheese', color='yellow') assert queried_pea == pea rat_eats_pea = g.eats.create(queried_rat, queried_pea, modifier='lots') mouse_eats_pea = g.eats.create(mouse, pea) mouse_eats_cheese = Eats.objects.create(mouse, cheese) assert rat_eats_pea.modifier == 'lots' assert rat_eats_pea == g.get_edge(rat_eats_pea._id) assert rat_eats_pea == g.get_element(rat_eats_pea._id) water = g.beverages.create(name='water', color='clear') mouse_drinks_water = g.drinks.create(mouse, water) assert [water] == mouse.out(Drinks) assert [mouse_drinks_water] == mouse.outE(Drinks) assert [water] == mouse.both(Drinks) assert [mouse_drinks_water] == mouse.bothE(Drinks) nut = g.foods.create(name='nut', color='brown') rat_dislikes_nut = g.dislikes.create(rat, nut) mouse_eats_nut = g.eats.create(mouse, nut) assert [rat] == nut.in_(Dislikes) assert [rat_dislikes_nut] == nut.inE(Dislikes) eaters = g.in_(Food, Eats) assert rat in eaters # Who eats the peas? pea_eaters = g.foods.query(name='pea').what(expand(in_(Eats))) for animal in pea_eaters: print(animal.name, animal.specie) # Which animals eat each food # FIXME Currently calling all() here, as iteration over expand() # results is currently broken. animal_foods = \ g.animals.query().what(expand(distinct(out(Eats)))).all() for food in animal_foods: print(food.name, food.color, g.query( g.foods.query(name=food.name).what(expand(in_(Eats)))) \ .what(Animal.name).all()) for food_name, food_color in g.query(Food.name, Food.color): print(food_name, food_color) # 'pea green' # 'cheese yellow' # FIXME While it is nicer to use files, parser should be more # permissive with whitespace g.scripts.add(GroovyScripts.from_string( """ def get_eaters_of(food_type) { return g.V('@class', 'food').has('name', T.eq, food_type).inE().outV(); } def get_foods_eaten_by(animal) { return g.v(animal).outE('eats').inV() } def get_colored_eaten_foods(animal, color) { return g.v(animal).outE('eats').inV().has('color', T.eq, color) } """)) pea_eaters = g.gremlin('get_eaters_of', 'pea') for animal in pea_eaters: print(animal.name, animal.specie) # 'rat rodent' # 'mouse rodent' rat_cuisine = g.gremlin('get_foods_eaten_by', (rat,)) for food in rat_cuisine: print(food.name, food.color) # 'pea green' batch = g.batch() batch['zombie'] = batch.animals.create(name='zombie',specie='undead') batch['brains'] = batch.foods.create(name='brains', color='grey') # Retry up to twenty times batch[:] = batch.eats.create(batch[:'zombie'], batch[:'brains']).retry(20) batch['unicorn'] = batch.animals.create(name='unicorn', specie='mythical') batch['unknown'] = batch.foods.create(name='unknown', color='rainbow') batch['mystery_diet'] = batch[:'unicorn'](Eats) > batch[:'unknown'] # Commits and clears batch zombie = batch['$zombie'] assert zombie.specie == 'undead'
def testGraph(self): assert len(AnimalsNode.registry) == 2 assert len(AnimalsRelationship.registry) == 1 g = self.g rat = g.animals.create(name="rat", specie="rodent") mouse = g.animals.create(name="mouse", specie="rodent") queried_rat = g.query(Animal).filter(Animal.name.endswith("at") | (Animal.name == "tiger")).one() assert rat == queried_rat queried_mouse = g.query(mouse).one() assert mouse == queried_mouse try: rat2 = g.animals.create(name="rat", specie="rodent") except: pass else: assert False and "Uniqueness not enforced correctly" pea = g.foods.create(name="pea", color="green") queried_pea = g.foods.query(color="green", name="pea").one() cheese = g.foods.create(name="cheese", color="yellow") assert queried_pea == pea rat_eats_pea = g.eats.create(queried_rat, queried_pea) mouse_eats_pea = g.eats.create(mouse, pea) mouse_eats_cheese = Eats.objects.create(mouse, cheese) eaters = g.in_(Food, Eats) assert rat in eaters # Who eats the peas? pea_eaters = g.foods.query(name="pea").what(expand(in_(Eats))) for animal in pea_eaters: print(animal.name, animal.specie) # Which animals eat each food # FIXME Currently calling all() here, as iteration over expand() # results is currently broken. animal_foods = g.animals.query().what(expand(distinct(out(Eats)))).all() for food in animal_foods: print( food.name, food.color, g.query(g.foods.query(name=food.name).what(expand(in_(Eats)))).what(Animal.name).all(), ) for food_name, food_color in g.query(Food.name, Food.color): print(food_name, food_color) # 'pea green' # 'cheese yellow' # FIXME While it is nicer to use files, parser should be more # permissive with whitespace g.scripts.add( GroovyScripts.from_string( """ def get_eaters_of(food_type) { return g.V('@class', 'food').has('name', T.eq, food_type).inE().outV(); } def get_foods_eaten_by(animal) { return g.v(animal).outE('eats').inV() } """ ) ) pea_eaters = g.gremlin("get_eaters_of", "pea") for animal in pea_eaters: print(animal.name, animal.specie) # 'rat rodent' # 'mouse rodent' rat_cuisine = g.gremlin("get_foods_eaten_by", (rat,)) for food in rat_cuisine: print(food.name, food.color) # 'pea green'
def testGraph(self): assert len(AnimalsNode.registry) == 3 assert len(AnimalsRelationship.registry) == 3 g = self.g rat = g.animals.create(name='rat', species='rodent') mouse = g.animals.create(name='mouse', species='rodent') queried_rat = g.query(Animal).filter( Animal.name.endswith('at') | (Animal.name == 'tiger')).one() assert rat == queried_rat invalid_query_args = {'name': 'rat', 'name="rat" OR 1': 1} try: g.animals.query(**invalid_query_args).all() except: pass else: assert False and 'Invalid params did not raise an exception!' queried_mouse = g.query(mouse).one() assert mouse == queried_mouse assert mouse == g.get_vertex(mouse._id) assert mouse == g.get_element(mouse._id) try: rat2 = g.animals.create(name='rat', species='rodent') except: pass else: assert False and 'Uniqueness not enforced correctly' pea = g.foods.create(name='pea', color='green') queried_pea = g.foods.query(color='green', name='pea').one() cheese = g.foods.create(name='cheese', color='yellow') assert queried_pea == pea rat_eats_pea = g.eats.create(queried_rat, queried_pea, modifier='lots') mouse_eats_pea = g.eats.create(mouse, pea) mouse_eats_cheese = Eats.objects.create(mouse, cheese) assert rat_eats_pea.modifier == 'lots' assert rat_eats_pea == g.get_edge(rat_eats_pea._id) assert rat_eats_pea == g.get_element(rat_eats_pea._id) water = g.beverages.create(name='water', color='clear') mouse_drinks_water = g.drinks.create(mouse, water) assert [water] == mouse.out(Drinks) assert [mouse_drinks_water] == mouse.outE(Drinks) assert [water] == mouse.both(Drinks) assert [mouse_drinks_water] == mouse.bothE(Drinks) nut = g.foods.create(name='nut', color='brown') rat_dislikes_nut = g.dislikes.create(rat, nut) mouse_eats_nut = g.eats.create(mouse, nut) assert [rat] == nut.in_(Dislikes) assert [rat_dislikes_nut] == nut.inE(Dislikes) eaters = g.in_(Food, Eats) assert rat in eaters # Who eats the peas? pea_eaters = g.foods.query(name='pea').what(expand(in_(Eats))) for animal in pea_eaters: print(animal.name, animal.species) # Which animals eat each food # FIXME Currently calling all() here, as iteration over expand() # results is currently broken. animal_foods = \ g.animals.query().what(expand(distinct(out(Eats)))).all() for food in animal_foods: print(food.name, food.color, g.query( g.foods.query(name=food.name).what(expand(in_(Eats)))) \ .what(Animal.name).all()) for food_name, food_color in g.query(Food.name, Food.color): print(food_name, food_color) # 'pea green' # 'cheese yellow' # FIXME While it is nicer to use files, parser should be more # permissive with whitespace g.scripts.add(GroovyScripts.from_string( """ def get_eaters_of(food_type) { return g.V('@class', 'food').has('name', T.eq, food_type).inE().outV(); } def get_foods_eaten_by(animal) { return g.v(animal).outE('eats').inV() } def get_colored_eaten_foods(animal, color) { return g.v(animal).outE('eats').inV().has('color', T.eq, color) } """)) pea_eaters = g.gremlin('get_eaters_of', 'pea') for animal in pea_eaters: print(animal.name, animal.species) # 'rat rodent' # 'mouse rodent' rat_cuisine = g.gremlin('get_foods_eaten_by', (rat,)) for food in rat_cuisine: print(food.name, food.color) # 'pea green' batch = g.batch() batch['zombie'] = batch.animals.create(name='zombie',species='undead') batch['brains'] = batch.foods.create(name='brains', color='grey') # Retry up to twenty times batch[:] = batch.eats.create(batch[:'zombie'], batch[:'brains']).retry(20) batch['unicorn'] = batch.animals.create(name='unicorn', species='mythical') batch['unknown'] = batch.foods.create(name='unknown', color='rainbow') batch['mystery_diet'] = batch[:'unicorn'](Eats) > batch[:'unknown'] # Commits and clears batch zombie = batch['$zombie'] assert zombie.species == 'undead'
def testGraph(self): assert len(AnimalsNode.registry) == 2 assert len(AnimalsRelationship.registry) == 1 g = self.g rat = g.animals.create(name='rat', specie='rodent') mouse = g.animals.create(name='mouse', specie='rodent') queried_rat = g.query(Animal).filter( Animal.name.endswith('at') | (Animal.name == 'tiger')).one() assert rat == queried_rat queried_mouse = g.query(mouse).one() assert mouse == queried_mouse try: rat2 = g.animals.create(name='rat', specie='rodent') except: pass else: assert False and 'Uniqueness not enforced correctly' pea = g.foods.create(name='pea', color='green') queried_pea = g.foods.query(color='green', name='pea').one() cheese = g.foods.create(name='cheese', color='yellow') assert queried_pea == pea rat_eats_pea = g.eats.create(queried_rat, queried_pea) mouse_eats_pea = g.eats.create(mouse, pea) mouse_eats_cheese = Eats.objects.create(mouse, cheese) eaters = g.in_(Food, Eats) assert rat in eaters # Who eats the peas? pea_eaters = g.foods.query(name='pea').what(expand(in_(Eats))) for animal in pea_eaters: print(animal.name, animal.specie) # Which animals eat each food # FIXME Currently calling all() here, as iteration over expand() # results is currently broken. animal_foods = \ g.animals.query().what(expand(distinct(out(Eats)))).all() for food in animal_foods: print(food.name, food.color, g.query( g.foods.query(name=food.name).what(expand(in_(Eats)))) \ .what(Animal.name).all()) for food_name, food_color in g.query(Food.name, Food.color): print(food_name, food_color) # 'pea green' # 'cheese yellow' # FIXME While it is nicer to use files, parser should be more # permissive with whitespace g.scripts.add(GroovyScripts.from_string( """ def get_eaters_of(food_type) { return g.V('@class', 'food').has('name', T.eq, food_type).inE().outV(); } def get_foods_eaten_by(animal) { return g.v(animal).outE('eats').inV() } def get_colored_eaten_foods(animal, color) { return g.v(animal).outE('eats').inV().has('color', T.eq, color) } """)) pea_eaters = g.gremlin('get_eaters_of', 'pea') for animal in pea_eaters: print(animal.name, animal.specie) # 'rat rodent' # 'mouse rodent' rat_cuisine = g.gremlin('get_foods_eaten_by', (rat,)) for food in rat_cuisine: print(food.name, food.color) # 'pea green' batch = g.batch() batch['zombie'] = batch.animals.create(name='zombie',specie='undead') batch['brains'] = batch.foods.create(name='brains', color='grey') # Retry up to twenty times batch[:] = batch.eats.create(batch[:'zombie'], batch[:'brains']).retry(20) batch['unicorn'] = batch.animals.create(name='unicorn', specie='mythical') batch['unknown'] = batch.foods.create(name='unknown', color='rainbow') batch['mystery_diet'] = batch[:'unicorn'](Eats) > batch[:'unknown'] # Commits and clears batch zombie = batch['$zombie'] assert zombie.specie == 'undead' schema_registry = g.build_mapping(AnimalsNode, AnimalsRelationship, auto_plural=True) assert all(c in schema_registry for c in ['animal', 'food', 'eats']) assert type(schema_registry['animal'].specie) == String # Plurals not communicated to schema; postprocess registry before # include() if you have a better solution than auto_plural. assert schema_registry['food'].registry_plural != Food.registry_plural
def testGraph(self): assert len(AnimalsNode.registry) == 2 assert len(AnimalsRelationship.registry) == 1 g = self.g rat = g.animals.create(name='rat', specie='rodent') mouse = g.animals.create(name='mouse', specie='rodent') queried_rat = g.query(Animal).filter( Animal.name.endswith('at') | (Animal.name == 'tiger')).one() assert rat == queried_rat queried_mouse = g.query(mouse).one() assert mouse == queried_mouse try: rat2 = g.animals.create(name='rat', specie='rodent') except: pass else: assert False and 'Uniqueness not enforced correctly' pea = g.foods.create(name='pea', color='green') queried_pea = g.foods.query(color='green', name='pea').one() cheese = g.foods.create(name='cheese', color='yellow') assert queried_pea == pea rat_eats_pea = g.eats.create(queried_rat, queried_pea) mouse_eats_pea = g.eats.create(mouse, pea) mouse_eats_cheese = Eats.objects.create(mouse, cheese) eaters = g.in_(Food, Eats) assert rat in eaters # Who eats the peas? pea_eaters = g.foods.query(name='pea').what(expand(in_(Eats))) for animal in pea_eaters: print(animal.name, animal.specie) # Which animals eat each food # FIXME Currently calling all() here, as iteration over expand() # results is currently broken. animal_foods = \ g.animals.query().what(expand(distinct(out(Eats)))).all() for food in animal_foods: print(food.name, food.color, g.query( g.foods.query(name=food.name).what(expand(in_(Eats)))) \ .what(Animal.name).all()) for food_name, food_color in g.query(Food.name, Food.color): print(food_name, food_color) # 'pea green' # 'cheese yellow' # FIXME While it is nicer to use files, parser should be more # permissive with whitespace g.scripts.add( GroovyScripts.from_string(""" def get_eaters_of(food_type) { return g.V('@class', 'food').has('name', T.eq, food_type).inE().outV(); } def get_foods_eaten_by(animal) { return g.v(animal).outE('eats').inV() } def get_colored_eaten_foods(animal, color) { return g.v(animal).outE('eats').inV().has('color', T.eq, color) } """)) pea_eaters = g.gremlin('get_eaters_of', 'pea') for animal in pea_eaters: print(animal.name, animal.specie) # 'rat rodent' # 'mouse rodent' rat_cuisine = g.gremlin('get_foods_eaten_by', (rat, )) for food in rat_cuisine: print(food.name, food.color) # 'pea green' batch = g.batch() batch['zombie'] = batch.animals.create(name='zombie', specie='undead') batch['brains'] = batch.foods.create(name='brains', color='grey') # Retry up to twenty times batch[::20] = batch.eats.create(batch[:'zombie'], batch[:'brains']) batch['unicorn'] = batch.animals.create(name='unicorn', specie='mythical') batch['unknown'] = batch.foods.create(name='unknown', color='rainbow') batch['mystery_diet'] = batch[:'unicorn'](Eats) > batch[:'unknown'] # Commits and clears batch zombie = batch['$zombie'] assert zombie.specie == 'undead' schema_registry = g.build_mapping(AnimalsNode, AnimalsRelationship, auto_plural=True) assert all(c in schema_registry for c in ['animal', 'food', 'eats']) assert type(schema_registry['animal'].specie) == String # Plurals not communicated to schema; postprocess registry before # include() if you have a better solution than auto_plural. assert schema_registry['food'].registry_plural != Food.registry_plural
def testGraph(self): assert len(AnimalsNode.registry) == 2 assert len(AnimalsRelationship.registry) == 1 g = self.g rat = g.animals.create(name='rat', specie='rodent') mouse = g.animals.create(name='mouse', specie='rodent') queried_rat = g.query(Animal).filter( Animal.name.endswith('at') | (Animal.name == 'tiger')).one() assert rat == queried_rat queried_mouse = g.query(mouse).one() assert mouse == queried_mouse try: rat2 = g.animals.create(name='rat', specie='rodent') except: pass else: assert False and 'Uniqueness not enforced correctly' pea = g.foods.create(name='pea', color='green') queried_pea = g.foods.query(color='green', name='pea').one() cheese = g.foods.create(name='cheese', color='yellow') assert queried_pea == pea rat_eats_pea = g.eats.create(queried_rat, queried_pea) mouse_eats_pea = g.eats.create(mouse, pea) mouse_eats_cheese = Eats.objects.create(mouse, cheese) eaters = g.in_(Food, Eats) assert rat in eaters # Who eats the peas? pea_eaters = g.foods.query(name='pea').what(expand(in_(Eats))) for animal in pea_eaters: print(animal.name, animal.specie) # Which animals eat each food # FIXME Currently calling all() here, as iteration over expand() # results is currently broken. animal_foods = \ g.animals.query().what(expand(distinct(out(Eats)))).all() for food in animal_foods: print(food.name, food.color, g.query( g.foods.query(name=food.name).what(expand(in_(Eats)))) \ .what(Animal.name).all()) for food_name, food_color in g.query(Food.name, Food.color): print(food_name, food_color) # 'pea green' # 'cheese yellow' # FIXME While it is nicer to use files, parser should be more # permissive with whitespace g.scripts.add( GroovyScripts.from_string(""" def get_eaters_of(food_type) { return g.V('@class', 'food').has('name', T.eq, food_type).inE().outV(); } def get_foods_eaten_by(animal) { return g.v(animal).outE('eats').inV() } """)) pea_eaters = g.gremlin('get_eaters_of', 'pea') for animal in pea_eaters: print(animal.name, animal.specie) # 'rat rodent' # 'mouse rodent' rat_cuisine = g.gremlin('get_foods_eaten_by', (rat, )) for food in rat_cuisine: print(food.name, food.color) # 'pea green'