Skip to content

Commit

Permalink
[WAYANG-#8] add types Tests and small corrections
Browse files Browse the repository at this point in the history
Signed-off-by: bertty <[email protected]>
  • Loading branch information
Bertty Contreras-Rojas authored and berttty committed Apr 8, 2022
1 parent e0da05b commit 220590b
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 53 deletions.
8 changes: 4 additions & 4 deletions python/src/pywy/dataquanta.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Set, List, cast

from pywy.core import Translator
from pywy.types import (GenericTco, Predicate, Function, FlatmapFunction, IterableO, T, I, O)
from pywy.types import (GenericTco, Predicate, Function, FlatmapFunction, IterableOut, T, In, Out)
from pywy.operators import *
from pywy.core import PywyPlan
from pywy.core import Plugin
Expand Down Expand Up @@ -57,13 +57,13 @@ def __init__(self, context: WayangContext, operator: PywyOperator):
def filter(self: "DataQuanta[T]", p: Predicate) -> "DataQuanta[T]":
return DataQuanta(self.context, self._connect(FilterOperator(p)))

def map(self: "DataQuanta[I]", f: Function) -> "DataQuanta[O]":
def map(self: "DataQuanta[In]", f: Function) -> "DataQuanta[Out]":
return DataQuanta(self.context, self._connect(MapOperator(f)))

def flatmap(self: "DataQuanta[I]", f: FlatmapFunction) -> "DataQuanta[IterableO]":
def flatmap(self: "DataQuanta[In]", f: FlatmapFunction) -> "DataQuanta[IterableOut]":
return DataQuanta(self.context, self._connect(FlatmapOperator(f)))

def store_textfile(self: "DataQuanta[I]", path: str):
def store_textfile(self: "DataQuanta[In]", path: str):
last: List[SinkOperator] = [cast(SinkOperator, self._connect(TextFileSink(path)))]
plan = PywyPlan(self.context.plugins, last)

Expand Down
3 changes: 3 additions & 0 deletions python/src/pywy/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

class PywyException(Exception):
pass
22 changes: 11 additions & 11 deletions python/src/pywy/operators/unary.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from itertools import chain
from pywy.operators.base import PywyOperator
from pywy.types import (
GenericTco,
GenericUco,
Predicate,
getTypePredicate,
Function,
getTypeFunction,
FlatmapFunction,
getTypeFlatmapFunction
GenericTco,
GenericUco,
Predicate,
get_type_predicate,
Function,
get_type_function,
FlatmapFunction,
get_type_flatmap_function
)


Expand All @@ -34,7 +34,7 @@ class FilterOperator(UnaryToUnaryOperator):
predicate: Predicate

def __init__(self, predicate: Predicate):
type = getTypePredicate(predicate) if predicate else None
type = get_type_predicate(predicate) if predicate else None
super().__init__("Filter", type, type)
self.predicate = predicate

Expand All @@ -49,7 +49,7 @@ class MapOperator(UnaryToUnaryOperator):
function: Function

def __init__(self, function: Function):
types = getTypeFunction(function) if function else (None, None)
types = get_type_function(function) if function else (None, None)
super().__init__("Map", types[0], types[1])
self.function = function

Expand All @@ -71,7 +71,7 @@ class FlatmapOperator(UnaryToUnaryOperator):
fmfunction: FlatmapFunction

def __init__(self, fmfunction: FlatmapFunction):
types = getTypeFlatmapFunction(fmfunction) if fmfunction else (None, None)
types = get_type_flatmap_function(fmfunction) if fmfunction else (None, None)
super().__init__("Flatmap", types[0], types[1])
self.fmfunction = fmfunction

Expand Down
259 changes: 251 additions & 8 deletions python/src/pywy/tests/unit/types_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,259 @@
import inspect
import unittest
from unittest.mock import Mock

from pywy.exception import PywyException
from pywy.types import get_type_function, get_type_bifunction, get_type_flatmap_function, get_type_predicate

class TestUnitTypes(unittest.TestCase):
empty_type = inspect._empty


class TestUnitTypesPredicate(unittest.TestCase):
def setUp(self):
pass

def test_predicate_without_parameters(self):
def pred() -> bool:
return True

try:
get_type_predicate(pred)
self.fail("The predicates parameters are mandatory")
except PywyException as ex:
self.assertTrue("the parameters for the Predicate are distinct than one," in str(ex))

def test_predicate_with_one_parameter_no_type(self):
def pred(x) -> bool:
return True

try:
pred_type = get_type_predicate(pred)
self.assertEqual(pred_type, empty_type)
except PywyException as ex:
self.fail(str(ex))

def test_predicate_with_one_parameter_with_basic_type(self):
def pred(x: int) -> bool:
return True

try:
pred_type = get_type_predicate(pred)
self.assertEqual(pred_type, int)
except PywyException as ex:
self.fail(str(ex))

def test_predicate_with_one_parameter_with_obe_type(self):
def pred(x: Mock) -> bool:
return True

try:
pred_type = get_type_predicate(pred)
self.assertEqual(pred_type, Mock)
except PywyException as ex:
self.fail(str(ex))

def test_predicate_with_two_parameters(self):
def pred(x: Mock, y: Mock) -> bool:
return True

try:
get_type_predicate(pred)
self.fail("the predicate can have just one input")
except PywyException as ex:
self.assertTrue("the parameters for the Predicate are distinct than one" in str(ex))


class TestUnitTypesFunction(unittest.TestCase):
def setUp(self):
pass

def test_TO_REMOVE(self):
"""
TODO REMOVE THIS TEST, IT JUST TO VALIDATE THAT EVERYTHING IS CORRECT IN TERMS OF ENVIRONMENT
Returns
-------
def test_function_without_parameters_no_return(self):
def func():
return

try:
get_type_function(func)
self.fail("The function parameters are mandatory")
except PywyException as ex:
self.assertTrue("the parameters for the Function are distinct than one," in str(ex))

def test_function_with_one_parameter_no_type_no_return(self):
def func(x):
return

try:
input_type, output_type = get_type_function(func)
self.assertEqual(input_type, empty_type)
self.assertEqual(output_type, empty_type)
except PywyException as ex:
self.fail(str(ex))

def test_function_with_one_parameter_with_basic_type_no_return(self):
def func(x: int):
return

try:
input_type, output_type = get_type_function(func)
self.assertEqual(input_type, int)
self.assertEqual(output_type, empty_type)
except PywyException as ex:
self.fail(str(ex))

def test_function_with_one_parameter_with_obj_type_no_return(self):
def func(x: Mock):
return

try:
input_type, output_type = get_type_function(func)
self.assertEqual(input_type, Mock)
self.assertEqual(output_type, empty_type)
except PywyException as ex:
self.fail(str(ex))

def test_function_with_two_parameters_no_return(self):
def func(x: Mock, y: Mock):
return

try:
get_type_function(func)
self.fail("the function can have just one input")
except PywyException as ex:
self.assertTrue("the parameters for the Function are distinct than one" in str(ex))

def test_function_without_parameters_basic_return(self):
def func() -> int:
return 0

try:
get_type_function(func)
self.fail("The function parameters are mandatory")
except PywyException as ex:
self.assertTrue("the parameters for the Function are distinct than one," in str(ex))

def test_function_with_one_parameter_no_type_basic_return(self):
def func(x) -> int:
return 0

try:
input_type, output_type = get_type_function(func)
self.assertEqual(input_type, empty_type)
self.assertEqual(output_type, int)
except PywyException as ex:
self.fail(str(ex))

def test_function_with_one_parameter_with_basic_type_basic_return(self):
def func(x: int) -> int:
return 0

try:
input_type, output_type = get_type_function(func)
self.assertEqual(input_type, int)
self.assertEqual(output_type, int)
except PywyException as ex:
self.fail(str(ex))

def test_function_with_one_parameter_with_obj_type_basic_return(self):
def func(x: Mock) -> int:
return 0

try:
input_type, output_type = get_type_function(func)
self.assertEqual(input_type, Mock)
self.assertEqual(output_type, int)
except PywyException as ex:
self.fail(str(ex))

def test_function_with_two_parameters_basic_return(self):
def func(x: Mock, y: Mock) -> int:
return 0

try:
get_type_function(func)
self.fail("the function can have just one input")
except PywyException as ex:
self.assertTrue("the parameters for the Function are distinct than one" in str(ex))

def test_function_without_parameters_obj_return(self):
def func() -> Mock:
return Mock()

try:
get_type_function(func)
self.fail("The function parameters are mandatory")
except PywyException as ex:
self.assertTrue("the parameters for the Function are distinct than one," in str(ex))

def test_function_with_one_parameter_no_type_obj_return(self):
def func(x) -> Mock:
return Mock()

try:
input_type, output_type = get_type_function(func)
self.assertEqual(input_type, empty_type)
self.assertEqual(output_type, Mock)
except PywyException as ex:
self.fail(str(ex))

def test_function_with_one_parameter_with_basic_type_basic_return(self):
def func(x: int) -> Mock:
return Mock()

try:
input_type, output_type = get_type_function(func)
self.assertEqual(input_type, int)
self.assertEqual(output_type, Mock)
except PywyException as ex:
self.fail(str(ex))

def test_function_with_one_parameter_with_obe_type_basic_return(self):
def func(x: Mock) -> Mock:
return Mock()

try:
input_type, output_type = get_type_function(func)
self.assertEqual(input_type, Mock)
self.assertEqual(output_type, Mock)
except PywyException as ex:
self.fail(str(ex))

def test_function_with_two_parameters_basic_return(self):
def func(x: Mock, y: Mock) -> Mock:
return Mock()

try:
get_type_function(func)
self.fail("the function can have just one input")
except PywyException as ex:
self.assertTrue("the parameters for the Function are distinct than one" in str(ex))


class TestUnitTypesBiFunction(unittest.TestCase):
def setUp(self):
pass

# TODO add the missing test for get_type_bifunction
def test_bifunction_without_parameters_no_return(self):
def func():
return

try:
get_type_bifunction(func)
self.fail("The bifunction parameters are mandatory")
except PywyException as ex:
self.assertTrue("the parameters for the BiFunction are distinct than two," in str(ex))


class TestUnitTypesFlatmapFunction(unittest.TestCase):
def setUp(self):
pass

"""
self.assertEqual("a", "a")
# TODO add the missing test for get_type_flatmap_function
def test_flatmapfunction_without_parameters_no_return(self):
def func():
return

try:
get_type_flatmap_function(func)
self.fail("The bifunction parameters are mandatory")
except PywyException as ex:
self.assertTrue("the parameters for the FlatmapFunction are distinct than one," in str(ex))
Loading

0 comments on commit 220590b

Please sign in to comment.