Skip to content

Commit

Permalink
Using types for function definition in all tests, refactored graph so…
Browse files Browse the repository at this point in the history
…urce tests
  • Loading branch information
vmagamedov committed Sep 7, 2016
1 parent 4c90437 commit 8a22018
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
8 changes: 7 additions & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ def _eq(self, other):
return self.__dict__ == other.__dict__


def _edge_eq(self, other):
if type(self) is not type(other):
return False
return self.fields_map == dict(other.fields_map)


_field_patch = patch.multiple(Field, __eq__=_eq, __ne__=_ne)
_link_patch = patch.multiple(Link, __eq__=_eq, __ne__=_ne)
_edge_patch = patch.multiple(Edge, __eq__=_eq, __ne__=_ne)
_edge_patch = patch.multiple(Edge, __eq__=_edge_eq, __ne__=_ne)


@contextmanager
Expand Down
9 changes: 5 additions & 4 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@
import pytest

from hiku.expr import define, S, if_, each, to_expr, if_some
from hiku.types import Optional, String
from hiku.types import Optional, String, Record
from hiku.graph import Graph, Field, Edge, Link, Root, Many, One
from hiku.compat import PY3, PY35
from hiku.checker import check, graph_types, fn_types
from hiku.compiler import ExpressionCompiler
from hiku.typedef.types import Unknown


@define('[nil]', _name='foo')
@define(Unknown, _name='foo')
def foo(value):
pass


@define('[nil]', _name='bar')
@define(Unknown, _name='bar')
def bar(value):
pass


@define('[nil [:c]]', _name='baz')
@define(Unknown, Record[{'c': Unknown}], _name='baz')
def baz(a, y):
pass

Expand Down
22 changes: 14 additions & 8 deletions tests/test_refs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from unittest import skip
from collections import OrderedDict

from hiku import graph as g
from hiku.expr import define, S, each, to_expr, if_some
Expand Down Expand Up @@ -154,11 +153,11 @@ def test_query_each_root_edge_link_field():

def test_query_tuple_with_edge():

@define(Record[OrderedDict([('clacks', Unknown), ('panicle', Unknown)])])
@define(Record[{'clacks': Unknown, 'panicle': Unknown}])
def foo():
pass

@define(Record[OrderedDict([('oloroso', Unknown), ('gashes', Unknown)])])
@define(Record[{'oloroso': Unknown, 'gashes': Unknown}])
def bar():
pass

Expand Down Expand Up @@ -199,7 +198,9 @@ def bar():

def test_query_tuple_with_nested_one_edge():

@define('[[:clacks {:apatite [:oloroso :gashes]}]]')
@define(Record[{'clacks': Unknown,
'apatite': Record[{'oloroso': Unknown,
'gashes': Unknown}]}])
def foo():
pass

Expand All @@ -220,7 +221,9 @@ def foo():
@skip('fn_types() lacks information about links (one or many)')
def test_query_tuple_with_nested_many_edge():

@define('[[:panicle {:jakies [:oloroso :gashes]}]]')
@define(Record[{'panicle': Unknown,
'jakies': Record[{'oloroso': Unknown,
'gashes': Unknown}]}])
def foo():
pass

Expand All @@ -240,7 +243,10 @@ def foo():

def test_query_tuple_with_simple_args():

@define('[[:clacks :panicle] nil [:oloroso :gashes] nil]')
@define(Record[{'clacks': Unknown, 'panicle': Unknown}],
Unknown,
Record[{'oloroso': Unknown, 'gashes': Unknown}],
Unknown)
def foo():
pass

Expand All @@ -257,7 +263,7 @@ def foo():

def test_query_list():

@define('[[:clacks :panicle]]')
@define(Record[{'clacks': Unknown, 'panicle': Unknown}])
def foo():
pass

Expand All @@ -270,7 +276,7 @@ def foo():

def test_query_dict():

@define('[[:clacks :panicle]]')
@define(Record[{'clacks': Unknown, 'panicle': Unknown}])
def foo():
pass

Expand Down
66 changes: 30 additions & 36 deletions tests/test_source_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

from hiku.expr import define, S, each
from hiku.graph import Graph, Edge, Link, Field, Option, Root, Many, One
from hiku.types import Record, Sequence
from hiku.engine import Engine
from hiku.sources.graph import SubGraph, Expr
from hiku.typedef.types import Unknown
from hiku.readers.simple import read
from hiku.executors.threads import ThreadsExecutor

Expand Down Expand Up @@ -46,11 +48,11 @@ def query_y(fields, ids):


def to_x():
return [1, 2, 3]
return [1, 3, 2]


def to_y():
return [6, 5, 4]
return [6, 4, 5]


def x_to_y(ids):
Expand Down Expand Up @@ -78,29 +80,27 @@ def y_to_x(ids):
Root([
Field('f1', query_f),
Field('f2', query_f),
Link('xs', Many, to_x, edge='x', requires=None),
Link('ys', Many, to_y, edge='y', requires=None),
]),
])


@define('[[:b] [:d]]')
@define(Record[{'b': Unknown}], Record[{'d': Unknown}])
def foo(x, y):
return '{x[y]} {y[d]}'.format(x=x, y=y).upper()


@define('[[:b {:y [:d]}]]')
@define(Record[{'b': Unknown, 'y': Record[{'d': Unknown}]}])
def bar(x):
return '{x[b]} {x[y][d]}'.format(x=x).upper()


@define('[[:d {:xs [:b]}]]')
@define(Record[{'d': Unknown, 'xs': Sequence[Record[{'b': Unknown}]]}])
def baz(y):
xs = ', '.join('{x[b]}'.format(x=x) for x in y['xs'])
return '{y[d]} [{xs}]'.format(y=y, xs=xs).upper()


@define('[[:a] nil]')
@define(Record[{'a': Unknown}], Unknown)
def buz(x, size):
return '{x[a]} - {size}'.format(x=x, size=size)

Expand All @@ -118,7 +118,7 @@ def buz(x, size):
Expr('f', sg_x, S.f1),
Expr('foo', sg_x, foo(S.this, S.this.y)),
Expr('bar', sg_x, bar(S.this)),
# Expr('baz', baz(S.this.y)),
Expr('baz', sg_x, baz(S.this.y)),
Expr('buz', sg_x, buz(S.this, S.size), options=[Option('size')]),
Expr('buz2', sg_x, buz(S.this, S.size),
options=[Option('size', default=100)]),
Expand All @@ -129,31 +129,11 @@ def buz(x, size):
Expr('f', sg_y, S.f2),
Expr('foo', sg_y, each(S.x, S.this.xs, foo(S.x, S.this))),
Expr('bar', sg_y, each(S.x, S.this.xs, bar(S.x))),
# Expr('baz', baz(S.this)),
Expr('baz', sg_y, baz(S.this)),
]),
Root([
Edge('x1', [
Expr('id', sg_x, S.this.id),
Expr('a', sg_x, S.this.a),
Expr('f', sg_x, S.f1),
Expr('foo', sg_x, foo(S.this, S.this.y)),
Expr('bar', sg_x, bar(S.this)),
# Expr('baz', baz(S.this.y)),
Expr('buz', sg_x, buz(S.this, S.size), options=[Option('size')]),
Expr('buz2', sg_x, buz(S.this, S.size),
options=[Option('size', default=100)]),
]),
Edge('y1', [
Expr('id', sg_y, S.this.id),
Expr('c', sg_y, S.this.c),
Expr('f', sg_y, S.f2),
Expr('foo', sg_y, each(S.x, S.this.xs, foo(S.x, S.this))),
Expr('bar', sg_y, each(S.x, S.this.xs, bar(S.x))),
# Expr('baz', baz(S.this)),
]),
# TODO: links reuse
Link('x1s', Many, to_x, edge='x1', requires=None),
Link('y1s', Many, to_y, edge='y2', requires=None),
Link('y1s', Many, to_y, edge='y1', requires=None),
]),
])

Expand All @@ -167,40 +147,54 @@ def testField(self):
result = self.engine.execute(GRAPH, read('[{:x1s [:a :f]}]'))
self.assertResult(result, {'x1s': [
{'a': 'a1', 'f': 7},
{'a': 'a2', 'f': 7},
{'a': 'a3', 'f': 7},
{'a': 'a2', 'f': 7},
]})

def testFieldOptions(self):
result = self.engine.execute(GRAPH,
read('[{:x1s [(:buz {:size "100"})]}]'))
self.assertResult(result, {'x1s': [
{'buz': 'a1 - 100'},
{'buz': 'a2 - 100'},
{'buz': 'a3 - 100'},
{'buz': 'a2 - 100'},
]})

def testFieldWithoutOptions(self):
result = self.engine.execute(GRAPH,
read('[{:x1s [:buz]}]'))
self.assertResult(result, {'x1s': [
{'buz': 'a1 - None'},
{'buz': 'a2 - None'},
{'buz': 'a3 - None'},
{'buz': 'a2 - None'},
]})

def testFieldOptionDefaults(self):
result = self.engine.execute(GRAPH,
read('[{:x1s [:buz2]}]'))
self.assertResult(result, {'x1s': [
{'buz2': 'a1 - 100'},
{'buz2': 'a2 - 100'},
{'buz2': 'a3 - 100'},
{'buz2': 'a2 - 100'},
]})
result = self.engine.execute(GRAPH,
read('[{:x1s [(:buz2 {:size 200})]}]'))
self.assertResult(result, {'x1s': [
{'buz2': 'a1 - 200'},
{'buz2': 'a2 - 200'},
{'buz2': 'a3 - 200'},
{'buz2': 'a2 - 200'},
]})

def testSequenceInArgType(self):
result = self.engine.execute(GRAPH, read('[{:x1s [:baz]}]'))
self.assertResult(result, {'x1s': [
{'baz': 'D3 [B1]'},
{'baz': 'D1 [B3]'},
{'baz': 'D2 [B2]'},
]})
result = self.engine.execute(GRAPH, read('[{:y1s [:baz]}]'))
self.assertResult(result, {'y1s': [
{'baz': 'D3 [B1]'},
{'baz': 'D1 [B3]'},
{'baz': 'D2 [B2]'},
]})

0 comments on commit 8a22018

Please sign in to comment.