def test_select_operators(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") lr = [("nom", 10), ("jean", 40), ("jeanne", 2)] schema = [("nom", str), ("age", int)] tbl = IterRow(schema, lr) iter = tbl.select(tbl.nom, age0=tbl.age, agem=tbl.age * 0.5, agea=tbl.age + 0.5, ages=tbl.age - 0.5, agep=tbl.age ** 0.5, agedd=tbl.age // 3, agemod=tbl.age % 3 ) res = list(iter) exp = [{'age0': 10, 'ages': 9.5, 'agemod': 1, 'agem': 5.0, 'agep': 3.1622776601683795, 'agea': 10.5, 'agedd': 3, 'nom': 'nom'}, {'age0': 40, 'ages': 39.5, 'agemod': 1, 'agem': 20.0, 'agep': 6.324555320336759, 'agea': 40.5, 'agedd': 13, 'nom': 'jean'}, {'age0': 2, 'ages': 1.5, 'agemod': 2, 'agem': 1.0, 'agep': 1.4142135623730951, 'agea': 2.5, 'agedd': 0, 'nom': 'jeanne'}] if res != exp: raise ValueError(str(res))
def test_select_simple(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") lr = [("jake", 10), ("jean", 40)] schema = [("nom", str), ("age", int)] tbl = IterRow(schema, lr) lr = list(tbl.select(tbl.nom, tbl.age)) for _ in lr: fLOG("+", _) if lr != [{'age': 10, 'nom': 'jake'}, {'age': 40, 'nom': 'jean'}]: raise Exception(str(lr))
def test_iter_simple_dict2(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") l0 = [{"nom": "jean", "age": 10}, {"nom": "j", "age": 20}] tbl = IterRow(None, l0) tbl2 = tbl.select(tbl.nom) lr = list(tbl2) assert len(lr) == 2 if lr != [{"nom": "jean"}, {"nom": "j"}]: raise ValueError(str(lr))
def test_select_simple2(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") lr = [("nom", 10), ("jean", 40)] schema = [("nom", str), ("age", int)] tbl = IterRow(schema, lr) iter = tbl.select(tbl.nom, age2=tbl.age * 2, age3=tbl.age * 3) lr = list(iter) assert len(lr) == 2 if lr != [{'nom': 'nom', 'age2': 20, 'age3': 30}, {'nom': 'jean', 'age2': 80, 'age3': 120}]: raise Exception(str(lr)) iter = tbl.select(tbl.nom, age2=tbl.age * 2) sch = iter.Schema assert sch[0].Name == "nom" assert sch[1].Name == "age2"
def test_select_mismatch(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") lr = [("nom", 10), ("jean", 40), ("jeanne", 2)] schema = [("nom", str), ("age", int)] tbl = IterRow(schema, lr) iter = tbl.select(tbl.nom, age2=tbl.age, age3=tbl.age * 0.5) try: iter.select(iter.nom, tbl.age) raise TypeError( "we should not be able to reach this code due to confusion between iter and tbl") except IterException as e: fLOG(e) assert "mismatch" in str(e)
def test_select_bracket(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") lr = [("nom", 10), ("jean", 40), ("jeanne", 2)] schema = [("nom", str), ("age", int)] tbl = IterRow(schema, lr) iter = tbl.select(tbl.nom, formula=tbl.age + (tbl.age + 2) / 3) res = list(iter) exp = [{'formula': 14.0, 'nom': 'nom'}, {'formula': 54.0, 'nom': 'jean'}, {'formula': 3.333333333333333, 'nom': 'jeanne'}] if res != exp: raise ValueError(str(res))
def test_select_function(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") lr = [("nom", 10), ("jean", 40), ("jeanne", 2)] schema = [("nom", str), ("age", int)] tbl = IterRow(schema, lr) def myf(x, y): return x * 2.5 + y iter = tbl.select(tbl.nom, age0=CFT(myf, tbl.age, tbl.age)) res = list(iter) exp = [{'nom': 'nom', 'age0': 35.0}, {'nom': 'jean', 'age0': 140.0}, {'nom': 'jeanne', 'age0': 7.0}] if res != exp: raise ValueError(str(res))
def test_select_function2(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") lr = [{"nom": "j", "age": 10}, {"nom": "jean", "age": 40}, {"nom": "jeanne", "age": 2}] tbl = IterRow(None, lr) def myf(x, y): return x * 2.5 + y iter = tbl.select(tbl.nom, age0=CFT(myf, tbl.age, tbl.age)) res = list(iter) exp = [{'nom': 'j', 'age0': 35.0}, {'nom': 'jean', 'age0': 140.0}, {'nom': 'jeanne', 'age0': 7.0}] if res != exp: raise ValueError(str(res))
def test_select_simple_square(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") lr = [("nom", 10), ("jean", 40), ("jeanne", 2)] schema = [("nom", str), ("age", int)] tbl = IterRow(schema, lr) iter = tbl.select(tbl.nom, age2=tbl.age, age3=tbl.age * 0.5) iter2 = iter.select(iter.nom, age4=iter.age2 * iter.age3) lr = list(iter2) assert len(lr) == 3 fLOG(";".join([str(_) for _ in iter2.Schema])) fLOG(lr) if lr != [{'age4': 50.0, 'nom': 'nom'}, { 'age4': 800.0, 'nom': 'jean'}, {'age4': 2.0, 'nom': 'jeanne'}]: raise Exception(str(lr)) sch = iter2.Schema assert sch[0].Name == "nom" assert sch[1].Name == "age4"