Skip to content

Commit 9e4b83c

Browse files
authored
Better Testing (#174)
Fixes #174 Fixes #168
1 parent 18ee4cd commit 9e4b83c

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
run: |
5959
python -m pytest -m "not atlas_xaod_runner and not cms_aod_runner"
6060
- name: Report coverage with Codecov
61-
if: github.event_name == 'push' && matrix.python-version == 3.7 && matrix.platform == 'ubuntu-latest'
61+
if: github.event_name == 'push' && matrix.python-version == 3.9 && matrix.platform == 'ubuntu-latest'
6262
uses: codecov/codecov-action@v1
6363
with:
6464
token: ${{ secrets.CODECOV_TOKEN }}

func_adl_xAOD/common/ast_to_cpp_translator.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,25 @@ def visit_Num(self, node):
802802
def visit_Str(self, node):
803803
crep.set_rep(node, crep.cpp_value('"{0}"'.format(node.s), self._gc.current_scope(), ctyp.terminal("string")))
804804

805+
def visit_Constant(self, node: ast.Constant):
806+
'''Visit Constnat node.
807+
808+
Note that this has to take the place of `visit_Str` and `visit_Num` as we roll
809+
python versions forward. So those methods and this method must be kept in sync.
810+
811+
Args:
812+
node (ast.Constant): The constant to visit
813+
'''
814+
value = node.value
815+
if type(value) is str:
816+
crep.set_rep(node, crep.cpp_value(f'"{value}"', self._gc.current_scope(), ctyp.terminal("string")))
817+
elif type(value) is int:
818+
crep.set_rep(node, crep.cpp_value(value, self._gc.current_scope(), ctyp.terminal("int")))
819+
elif type(value) is float:
820+
crep.set_rep(node, crep.cpp_value(value, self._gc.current_scope(), ctyp.terminal("double")))
821+
else:
822+
raise Exception(f"Unsupported constant type: {type(value)}")
823+
805824
def code_fill_ttree(self, e_rep: crep.cpp_rep_base, e_name: crep.cpp_variable,
806825
scope_fill: Union[gc_scope, gc_scope_top_level]) -> Union[gc_scope, gc_scope_top_level]:
807826
'''
@@ -1016,11 +1035,10 @@ def call_Where(self, node: ast.AST, args: List[ast.AST]):
10161035
new_sequence_var = w_val.copy_with_new_scope(self._gc.current_scope())
10171036
crep.set_rep(node, crep.cpp_sequence(new_sequence_var, seq.iterator_value(), self._gc.current_scope()))
10181037

1019-
def call_Range(self, node: ast.AST, args: List[ast.AST]):
1038+
def call_Range(self, node: ast.Call, args: List[ast.AST]):
10201039
'Create a collection of numbers from lower_bound'
10211040

1022-
if len(args) != 2:
1023-
raise Exception('blah blah')
1041+
assert len(args) == 2, 'Range(lower bound, upper bound) is the only allowed form'
10241042
lower_bound = args[0]
10251043
upper_bound = args[1]
10261044

tests/atlas/xaod/test_cpp_script_control.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def execute_result_async(self, a: ast.AST) -> Any:
3232
return ExecutorInfo(f_spec.main_script, f_spec.result_rep.filename)
3333

3434

35-
@pytest.yield_fixture()
35+
@pytest.fixture()
3636
def cache_directory():
3737
'Return a directory that can be deleted when the test is done'
3838
with tempfile.TemporaryDirectory() as d_temp:

tests/atlas/xaod/test_integrated_query.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_select_first_of_array():
3333
assert training_df.iloc[-1]['col1'] == 336
3434

3535

36-
@pytest.yield_fixture()
36+
@pytest.fixture()
3737
def event_loop():
3838
'Get the loop done right on windows'
3939
if os.name == 'nt':
@@ -148,7 +148,7 @@ def test_1D_array():
148148
.Select('lambda e: e.Jets("AntiKt4EMTopoJets").Select(lambda j: j.pt()/1000.0)'))
149149
print(training_df)
150150
assert len(training_df[b'col1']) == 10
151-
assert len(training_df[b'col1'][0]) == 32
151+
assert len(training_df[b'col1'][0]) == 32 # type: ignore
152152

153153

154154
def test_2D_array():
@@ -157,8 +157,8 @@ def test_2D_array():
157157
.Select('lambda e: e.Jets("AntiKt4EMTopoJets").Select(lambda j: j.Jets("AntiKt4EMTopoJets").Select(lambda j1: j1.pt()/1000.0))'))
158158
print(training_df)
159159
assert len(training_df[b'col1']) == 10
160-
assert len(training_df[b'col1'][0]) == 32
161-
assert len(training_df[b'col1'][0][0]) == 32
160+
assert len(training_df[b'col1'][0]) == 32 # type: ignore
161+
assert len(training_df[b'col1'][0][0]) == 32 # type: ignore
162162

163163

164164
def test_2D_nested_where():
@@ -172,4 +172,4 @@ def test_2D_nested_where():
172172
print(training_df)
173173
a = training_df[b'col1']
174174
assert len(a) == 10
175-
assert len(a[0]) == 8
175+
assert len(a[0]) == 8 # type: ignore

tests/atlas/xaod/test_query_ast_visitor.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Some very direct white box testing
22
import ast
3+
import sys
34

45
import func_adl_xAOD.common.cpp_representation as crep
56
import func_adl_xAOD.common.cpp_types as ctyp
@@ -27,6 +28,19 @@ def test_binary_plus_return_type_2():
2728
assert r.cpp_type().type == 'double'
2829

2930

31+
def test_complex_number_not_understood():
32+
if sys.version_info >= (3, 8):
33+
import cmath # NOQA
34+
c = complex(1, 2)
35+
node = ast.Constant(value=c, kind=None)
36+
37+
q = atlas_xaod_query_ast_visitor()
38+
with pytest.raises(Exception) as e:
39+
q.get_rep(node)
40+
41+
assert 'complex' in str(e)
42+
43+
3044
def test_binary_plus_return_type_3():
3145
q = atlas_xaod_query_ast_visitor()
3246
r = q.get_rep(ast.parse('1+1').body[0].value) # type: ignore

tests/atlas/xaod/test_xaod_executor.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from tests.utils.locators import find_line_numbers_with, find_line_with, find_open_blocks
2-
from tests.utils.general import get_lines_of_code, print_lines
31
import pytest
2+
from func_adl import Range
43
from tests.atlas.xaod.utils import atlas_xaod_dataset, exe_from_qastle
4+
from tests.utils.general import get_lines_of_code, print_lines
5+
from tests.utils.locators import (find_line_numbers_with, find_line_with,
6+
find_open_blocks)
57

68
# Tests that make sure the xaod executor is working correctly
79

@@ -596,3 +598,20 @@ async def test_electron_and_muon_from_qastle():
596598
q = "(call ResultTTree (call Select (call Select (call EventDataset (list 'localds:bogus')) (lambda (list e) (list (call (attr e 'Electrons') 'Electrons') (call (attr e 'Muons') 'Muons')))) (lambda (list e) (list (call (attr (subscript e 0) 'Select') (lambda (list ele) (call (attr ele 'E')))) (call (attr (subscript e 0) 'Select') (lambda (list ele) (call (attr ele 'pt')))) (call (attr (subscript e 0) 'Select') (lambda (list ele) (call (attr ele 'phi')))) (call (attr (subscript e 0) 'Select') (lambda (list ele) (call (attr ele 'eta')))) (call (attr (subscript e 1) 'Select') (lambda (list mu) (call (attr mu 'E')))) (call (attr (subscript e 1) 'Select') (lambda (list mu) (call (attr mu 'pt')))) (call (attr (subscript e 1) 'Select') (lambda (list mu) (call (attr mu 'phi')))) (call (attr (subscript e 1) 'Select') (lambda (list mu) (call (attr mu 'eta'))))))) (list 'e_E' 'e_pt' 'e_phi' 'e_eta' 'mu_E' 'mu_pt' 'mu_phi' 'mu_eta') 'forkme' 'dude.root')"
597599
r = await exe_from_qastle(q)
598600
print(r)
601+
602+
603+
def test_Range_good_call():
604+
# The following statement should be a straight sequence, not an array.
605+
r = (atlas_xaod_dataset()
606+
.SelectMany(lambda e: e.Jets("AntiKt4EMTopoJets"))
607+
.Select(lambda j: Range(0, 10)
608+
.Select(lambda index: j.pt() * index))
609+
.value()
610+
)
611+
# Check to see if there mention of push_back anywhere.
612+
lines = get_lines_of_code(r)
613+
print_lines(lines)
614+
for_loops = find_line_numbers_with('for (', lines)
615+
assert len(for_loops) == 2
616+
find_line_with('(0)', lines)
617+
find_line_with('(10)', lines)

0 commit comments

Comments
 (0)