Skip to content

Commit

Permalink
Merge 4b9ab85 into ef77bf9
Browse files Browse the repository at this point in the history
  • Loading branch information
amartyashankha committed Jul 25, 2018
2 parents ef77bf9 + 4b9ab85 commit efbd47f
Show file tree
Hide file tree
Showing 11 changed files with 1,956 additions and 883 deletions.

Large diffs are not rendered by default.

Expand Up @@ -46,6 +46,8 @@ def execute_graphql(schema, test_data, client, sample_parameters):
# - create a Counter (multi-set) of the row frozensets
# - convert the multi-set to a frozenset of its items
row_dicts = [row.oRecordData for row in client.command(result.query)]
if len(row_dicts) == 0:
raise AssertionError(u'Zero records returned. Trivial snapshot not allowed.')
row_dicts_using_tuples = [
{
column_name: convert_decimals_to_strings(
Expand Down
8 changes: 4 additions & 4 deletions graphql_compiler/tests/test_compiler.py
Expand Up @@ -1470,19 +1470,19 @@ def test_contains_op_filter_with_tag(self):
MATCH {{
class: Animal,
as: Animal___1
}}.out('Animal_ParentOf') {{
}}.in('Animal_ParentOf') {{
where: ((alias CONTAINS $matched.Animal___1.name)),
as: Animal__out_Animal_ParentOf___1
as: Animal__in_Animal_ParentOf___1
}}
RETURN $matches
)
'''
expected_gremlin = '''
g.V('@class', 'Animal')
.as('Animal___1')
.out('Animal_ParentOf')
.in('Animal_ParentOf')
.filter{it, m -> it.alias.contains(m.Animal___1.name)}
.as('Animal__out_Animal_ParentOf___1')
.as('Animal__in_Animal_ParentOf___1')
.back('Animal___1')
.transform{it, m -> new com.orientechnologies.orient.core.record.impl.ODocument([
animal_name: m.Animal___1.name
Expand Down
2,545 changes: 1,740 additions & 805 deletions graphql_compiler/tests/test_data_tools/data/commands.sql

Large diffs are not rendered by default.

27 changes: 16 additions & 11 deletions graphql_compiler/tests/test_data_tools/schema.sql
Expand Up @@ -19,6 +19,11 @@ ALTER PROPERTY Entity.alias DEFAULT {}
CREATE INDEX Entity.alias NOTUNIQUE

CREATE PROPERTY Entity.description String

CREATE CLASS Entity_Related EXTENDS E
CREATE PROPERTY Entity_Related.in LINK Entity
CREATE PROPERTY Entity_Related.out LINK Entity
CREATE INDEX Entity_Related ON Entity_Related (in, out) UNIQUE_HASH_INDEX
###############


Expand Down Expand Up @@ -69,8 +74,13 @@ CREATE INDEX Animal_BornAt ON Animal_BornAt (in, out) UNIQUE_HASH_INDEX
###############


### FoodOrSpecies ###
CREATE CLASS FoodOrSpecies EXTENDS Entity
###############


### Species ###
CREATE CLASS Species EXTENDS Entity
CREATE CLASS Species EXTENDS FoodOrSpecies

CREATE PROPERTY Species.limbs Integer
CREATE INDEX Species.limbs NOTUNIQUE
Expand All @@ -79,19 +89,14 @@ CREATE CLASS Animal_OfSpecies EXTENDS E
CREATE PROPERTY Animal_OfSpecies.in LINK Species
CREATE PROPERTY Animal_OfSpecies.out LINK Animal
CREATE INDEX Animal_OfSpecies ON Animal_OfSpecies (in, out) UNIQUE_HASH_INDEX
###############


### Food ###
CREATE CLASS Food EXTENDS Entity
###############


### FoodOrSpecies ###
CREATE CLASS FoodOrSpecies EXTENDS Food, Species

CREATE CLASS Species_Eats EXTENDS E
CREATE PROPERTY Species_Eats.in LINK FoodOrSpecies
CREATE PROPERTY Species_Eats.out LINK Species
CREATE INDEX Species_Eats ON Species_Eats (in, out) UNIQUE_HASH_INDEX
###############


### Food ###
CREATE CLASS Food EXTENDS FoodOrSpecies
###############
2 changes: 1 addition & 1 deletion graphql_compiler/tests/test_input_data.py
Expand Up @@ -910,7 +910,7 @@ def contains_op_filter_with_tag():
graphql_input = '''{
Animal {
name @output(out_name: "animal_name") @tag(tag_name: "name")
out_Animal_ParentOf {
in_Animal_ParentOf {
alias @filter(op_name: "contains", value: ["%name"])
}
}
Expand Down
8 changes: 4 additions & 4 deletions graphql_compiler/tests/test_ir_generation.py
Expand Up @@ -1458,20 +1458,20 @@ def test_contains_op_filter_with_tag(self):
test_data = test_input_data.contains_op_filter_with_tag()

base_location = helpers.Location(('Animal',))
child_location = base_location.navigate_to_subpath('out_Animal_ParentOf')
parent_location = base_location.navigate_to_subpath('in_Animal_ParentOf')

expected_blocks = [
blocks.QueryRoot({'Animal'}),
blocks.MarkLocation(base_location),
blocks.Traverse('out', 'Animal_ParentOf'),
blocks.Traverse('in', 'Animal_ParentOf'),
blocks.Filter(
expressions.BinaryComposition(
u'contains',
expressions.LocalField('alias'),
expressions.ContextField(base_location.navigate_to_field('name')),
)
),
blocks.MarkLocation(child_location),
blocks.MarkLocation(parent_location),
blocks.Backtrack(base_location),
blocks.ConstructResult({
'animal_name': expressions.OutputContextField(
Expand All @@ -1480,7 +1480,7 @@ def test_contains_op_filter_with_tag(self):
]
expected_location_types = {
base_location: 'Animal',
child_location: 'Animal',
parent_location: 'Animal',
}

check_test_data(self, test_data, expected_blocks, expected_location_types)
Expand Down
2 changes: 2 additions & 0 deletions scripts/generate_test_sql/__init__.py
Expand Up @@ -7,6 +7,7 @@
import sys

from .animals import get_animal_generation_commands
from .events import get_event_generation_commands
from .species import get_species_generation_commands


Expand Down Expand Up @@ -48,6 +49,7 @@ def main():
log_message.format(path=module_path, datetime=current_datetime, version=find_version()))

sql_command_generators = [
get_event_generation_commands,
get_species_generation_commands,
get_animal_generation_commands,
]
Expand Down
83 changes: 77 additions & 6 deletions scripts/generate_test_sql/animals.py
@@ -1,7 +1,8 @@
# Copyright 2017-present Kensho Technologies, LLC.
import random

from .species import SPECIES_LIST
from .events import EVENT_NAMES_LIST
from .species import FOOD_LIST, SPECIES_LIST
from .utils import (create_edge_statement, create_name, create_vertex_statement,
extract_base_name_and_label, get_random_date, get_random_net_worth, get_uuid)

Expand All @@ -19,16 +20,38 @@
'orange',
'indigo',
)
NUM_ENTITY_RELATED_COMMANDS_MULTIPLIER = 0.2
NUM_ALIASES = 3


def _create_animal_statement(animal_name):
"""Return a SQL statement to create an animal vertex."""
def _create_animal_fed_at_random_event_statement(from_name):
"""Return a SQL statement to create an Animal_FedAt edge connected to a random Event."""
event_name = random.choice(EVENT_NAMES_LIST)
return create_edge_statement('Animal_FedAt', 'Animal', from_name, 'Event', event_name)


def _get_animal_aliases(animal_name, parent_names):
"""Return list of animal aliases."""
base_name, _ = extract_base_name_and_label(animal_name)
random_aliases = [base_name + '_' + str(random.randint(0, 9)) for _ in range(NUM_ALIASES)]

# Some tests check for equality between names and aliases of different animals.
# If the animal has several parents, we add a parent name to the alias list.
if len(parent_names) > 2:
return random_aliases + random.sample(parent_names, 2)
else:
return random_aliases


def _create_animal_statement(animal_name, parent_names):
"""Return a SQL statement to create an Animal vertex."""
field_name_to_value = {
'name': animal_name,
'uuid': get_uuid(),
'color': random.choice(ANIMAL_COLOR_LIST), # nosec
'birthday': get_random_date(),
'net_worth': get_random_net_worth(),
'alias': _get_animal_aliases(animal_name, parent_names),
}
return create_vertex_statement('Animal', field_name_to_value)

Expand All @@ -38,6 +61,26 @@ def _create_animal_parent_of_statement(from_name, to_name):
return create_edge_statement('Animal_ParentOf', 'Animal', from_name, 'Animal', to_name)


def _detect_subtype(entity_name):
"""Detect and return the type of Entity from its name."""
if entity_name in SPECIES_LIST:
return 'Species'
elif entity_name in FOOD_LIST:
return 'Food'
elif extract_base_name_and_label(entity_name)[0] in SPECIES_LIST:
return 'Animal'
else:
raise AssertionError(u'Found invalid entity name. Must be either a species, food, '
u'or animal name: {}'.format(entity_name))


def _create_entity_related_statement(from_name, to_name):
"""Return a SQL statement to create an Entity_Related edge."""
from_class = _detect_subtype(from_name)
to_class = _detect_subtype(to_name)
return create_edge_statement('Entity_Related', from_class, from_name, to_class, to_name)


def _create_animal_of_species_statement(from_name, to_name):
"""Return a SQL statement to create an Animal_OfSpecies edge."""
return create_edge_statement('Animal_OfSpecies', 'Animal', from_name, 'Species', to_name)
Expand All @@ -49,7 +92,7 @@ def _get_initial_animal_generators(species_name, current_animal_names):
for index in range(NUM_INITIAL_ANIMALS):
animal_name = create_name(species_name, str(index))
current_animal_names.append(animal_name)
command_list.append(_create_animal_statement(animal_name))
command_list.append(_create_animal_statement(animal_name, []))
return command_list


Expand All @@ -63,11 +106,31 @@ def _get_new_parents(current_animal_names, previous_parent_sets, num_parents):
return new_parent_names


def _get_animal_entity_related_commands(all_animal_names):
"""Return a list of commands to create EntityRelated edges between random Animals and Foods."""
command_list = []
num_samples = int(NUM_ENTITY_RELATED_COMMANDS_MULTIPLIER * len(all_animal_names))
in_neighbor_samples = random.sample(all_animal_names, num_samples)
out_neighbor_samples = random.sample(all_animal_names, num_samples)

for from_name, to_name in zip(in_neighbor_samples, out_neighbor_samples):
# Related Animal Entities
command_list.append(_create_entity_related_statement(from_name, to_name))

# Related_Entity between Animals and Food
food_name = random.choice(FOOD_LIST)
command_list.append(_create_entity_related_statement(from_name, food_name))
command_list.append(_create_entity_related_statement(food_name, to_name))

return command_list


def get_animal_generation_commands():
"""Return a list of SQL statements to create animal vertices and their corresponding edges."""
command_list = []
species_to_names = {}
previous_parent_sets = set()
all_animal_names = []

for species_name in SPECIES_LIST:
current_animal_names = []
Expand All @@ -89,12 +152,20 @@ def get_animal_generation_commands():
new_label = '(' + '_'.join(parent_indices) + ')'
new_animal_name = create_name(species_name, new_label)
current_animal_names.append(new_animal_name)
command_list.append(_create_animal_statement(new_animal_name))
command_list.append(_create_animal_statement(new_animal_name, sorted(new_parent_names)))
for parent_name in sorted(new_parent_names):
new_edge = _create_animal_parent_of_statement(new_animal_name, parent_name)
command_list.append(new_edge)

for animal_name in current_animal_names:
command_list.append(_create_animal_of_species_statement(animal_name, species_name))
command_list.append(_create_animal_fed_at_random_event_statement(animal_name))

return command_list
# Entity_Related edges connecting each animal to their parents, children, and themselves
command_list.append(_create_entity_related_statement(animal_name, animal_name))
command_list.append(_create_entity_related_statement(animal_name, species_name))
command_list.append(_create_entity_related_statement(species_name, animal_name))

all_animal_names.extend(current_animal_names)

return command_list + _get_animal_entity_related_commands(all_animal_names)
26 changes: 26 additions & 0 deletions scripts/generate_test_sql/events.py
@@ -0,0 +1,26 @@
# Copyright 2017-present Kensho Technologies, LLC.
from .utils import create_vertex_statement, get_random_date, get_uuid


EVENT_NAMES_LIST = (
"Birthday",
"Bar Mitzvah",
"Coronation",
"Re-awakening",
)


def _create_event_statement(event_name):
"""Return a SQL statement to create a Event vertex."""
field_name_to_value = {'name': event_name, 'event_date': get_random_date(), 'uuid': get_uuid()}
return create_vertex_statement('Event', field_name_to_value)


def get_event_generation_commands():
"""Return a list of SQL statements to create all event vertices."""
command_list = []

for event_name in EVENT_NAMES_LIST:
command_list.append(_create_event_statement(event_name))

return command_list
36 changes: 34 additions & 2 deletions scripts/generate_test_sql/species.py
@@ -1,5 +1,7 @@
# Copyright 2017-present Kensho Technologies, LLC.
from .utils import create_vertex_statement, get_random_limbs, get_uuid
import random

from .utils import create_edge_statement, create_vertex_statement, get_random_limbs, get_uuid


SPECIES_LIST = (
Expand All @@ -8,18 +10,48 @@
'Dragon',
'Hippogriff',
)
FOOD_LIST = (
"Bacon",
"Lembas",
"Bloood pie",
)
NUM_FOODS = 2


def _create_food_statement(food_name):
"""Return a SQL statement to create a Food vertex."""
field_name_to_value = {'name': food_name, 'uuid': get_uuid()}
return create_vertex_statement('Food', field_name_to_value)


def _create_species_statement(species_name):
"""Return a SQL statement to create a species vertex."""
"""Return a SQL statement to create a Species vertex."""
field_name_to_value = {'name': species_name, 'limbs': get_random_limbs(), 'uuid': get_uuid()}
return create_vertex_statement('Species', field_name_to_value)


def _create_species_eats_statement(from_name, to_name):
"""Return a SQL statement to create a Species_Eats edge."""
if to_name in SPECIES_LIST:
to_class = 'Species'
elif to_name in FOOD_LIST:
to_class = 'Food'
else:
raise AssertionError(u'Invalid name for SpeciesEats endpoint: {}'.format(to_name))
return create_edge_statement('Species_Eats', 'Species', from_name, to_class, to_name)


def get_species_generation_commands():
"""Return a list of SQL statements to create all species vertices."""
command_list = []

for food_name in FOOD_LIST:
command_list.append(_create_food_statement(food_name))
for species_name in SPECIES_LIST:
command_list.append(_create_species_statement(species_name))

for species_name in SPECIES_LIST:
for food_or_species_name in random.sample(SPECIES_LIST + FOOD_LIST, NUM_FOODS):
command_list.append(_create_species_eats_statement(species_name, food_or_species_name))

return command_list

0 comments on commit efbd47f

Please sign in to comment.