Skip to content

Commit d034db5

Browse files
author
Steven Lott
committed
add conversions test cases
1 parent 16f3e11 commit d034db5

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

‎tests/test_main.py‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# SPDX-Copyright: Copyright (c) Capital One Services, LLC
2+
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright 2020 Capital One Services, LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and limitations under the License.
15+
16+
"""
17+
Pure Python implementation of CEL.
18+
19+
Test the main CLI
20+
"""
21+
22+
import argparse
23+
import io
24+
import sys
25+
from unittest.mock import Mock, sentinel, call
26+
27+
from pytest import *
28+
import celpy.__main__
29+
from celpy import celtypes
30+
31+
32+
@fixture
33+
def mock_os_environ(monkeypatch):
34+
monkeypatch.setitem(celpy.__main__.os.environ, 'OS_ENV_VAR', "3.14")
35+
36+
37+
def test_arg_type_value(mock_os_environ):
38+
assert celpy.__main__.arg_type_value("name:int=42") == ("name", 42)
39+
assert celpy.__main__.arg_type_value("OS_ENV_VAR") == ("OS_ENV_VAR", "3.14")
40+
assert celpy.__main__.arg_type_value("OS_ENV_VAR:double") == ("OS_ENV_VAR", 3.14)
41+
with raises(argparse.ArgumentTypeError):
42+
celpy.__main__.arg_type_value("name:type:value")
43+
44+
45+
def test_get_options():
46+
options = celpy.__main__.get_options(["--arg", "name:int=42", "-n", "355./113."])
47+
assert options.arg == [("name", 42)]
48+
assert options.null_input
49+
assert options.expr == "355./113."
50+
51+
52+
@fixture
53+
def mock_cel_environment(monkeypatch):
54+
mock_runner = Mock(
55+
evaluate = Mock(return_value=str(sentinel.OUTPUT))
56+
)
57+
mock_env = Mock(
58+
compile = Mock(return_value=sentinel.AST),
59+
program = Mock(return_value=mock_runner)
60+
)
61+
mock_env_class = Mock(
62+
return_value = mock_env
63+
)
64+
monkeypatch.setattr(celpy.__main__, 'Environment', mock_env_class)
65+
return mock_env_class
66+
67+
68+
def test_main_0(mock_cel_environment, caplog, capsys):
69+
argv = ['--null-input', '"Hello world! I\'m " + name + "."']
70+
celpy.__main__.main(argv)
71+
assert mock_cel_environment.mock_calls == [call(package=None)]
72+
env = mock_cel_environment.return_value
73+
assert env.compile.mock_calls == [call('"Hello world! I\'m " + name + "."')]
74+
assert env.program.mock_calls == [call(sentinel.AST)]
75+
prgm = env.program.return_value
76+
assert prgm.evaluate.mock_calls == [call({})]
77+
78+
79+
def test_main_1(mock_cel_environment, caplog, capsys):
80+
argv = ['--arg', 'name:string=CEL', '--null-input', '"Hello world! I\'m " + name + "."']
81+
celpy.__main__.main(argv)
82+
assert mock_cel_environment.mock_calls == [call(package=None)]
83+
env = mock_cel_environment.return_value
84+
assert env.compile.mock_calls == [call('"Hello world! I\'m " + name + "."')]
85+
assert env.program.mock_calls == [call(sentinel.AST)]
86+
prgm = env.program.return_value
87+
assert prgm.evaluate.mock_calls == [call({"name": "CEL"})]
88+
89+
90+
def test_main_pipe(mock_cel_environment, caplog, capsys):
91+
argv = ['"Hello world! I\'m " + name + "."']
92+
sys.stdin = io.StringIO('{"name": "CEL"}\n')
93+
celpy.__main__.main(argv)
94+
sys.stdin = sys.__stdin__
95+
assert mock_cel_environment.mock_calls == [call(package="jq")]
96+
env = mock_cel_environment.return_value
97+
assert env.compile.mock_calls == [call('"Hello world! I\'m " + name + "."')]
98+
assert env.program.mock_calls == [call(sentinel.AST)]
99+
prgm = env.program.return_value
100+
assert prgm.evaluate.mock_calls == [
101+
call({'jq': celtypes.MapType({celtypes.StringType('name'): celtypes.StringType('CEL')})})
102+
]

0 commit comments

Comments
 (0)