-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_queries.py
209 lines (179 loc) · 6.67 KB
/
test_queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#
# Copyright (c) 2017-2019 University of Antwerp, Aloxy NV.
#
# This file is part of Sub-IoT Testsuite
# (see https://github.com/Sub-Iot/Sub-IoT-testsuite).
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
import struct
from time import sleep
from pytest_bdd import scenario, given, when, then, parsers
from d7a.alp.command import Command
from d7a.alp.operands.file import DataRequest
from d7a.alp.operands.length import Length
from d7a.alp.operands.offset import Offset
from d7a.alp.operands.query import QueryOperand, QueryType, ArithQueryParams, ArithComparisonType
from d7a.alp.operations.break_query import BreakQuery
from d7a.alp.operations.requests import ReadFileData
from d7a.alp.regular_action import RegularAction
@scenario('queries.feature',
'When predicate of Break Query action fails all subsequent actions are dropped')
def test_break_query_fails():
pass
@given("a command containing a Break Query action, which results in a fail, and a Read action")
def query_cmd_fail(context):
cmd = Command()
# we assume comparing the UID file to 0 results in a fail
cmd.add_action(
RegularAction(
operation=BreakQuery(
operand=QueryOperand(
type=QueryType.ARITH_COMP_WITH_VALUE,
mask_present=False,
params=ArithQueryParams(comp_type=ArithComparisonType.EQUALITY, signed_data_type=False),
compare_length=Length(8),
compare_value=[0, 0, 0, 0, 0, 0, 0, 0],
file_a_offset=Offset(id=0, offset=Length(0))
)
)
)
)
cmd.add_action(
RegularAction(
operation=ReadFileData(
operand=DataRequest(
offset=Offset(id=0, offset=Length(0)),
length=Length(8)
)
)
)
)
context.query_cmd = cmd
@when("the testdevice executes the command")
def send_command(test_device, context):
context.response = test_device.execute_command(context.query_cmd, timeout_seconds=10)
@then("the command executes successfully")
def executes_successfully(context):
assert len(context.response) == 1, "expected one response"
assert context.response[0].execution_completed, "execution should be completed"
assert not context.response[0].completed_with_error, "the command should execute without error"
@then("the Read action does not return a result")
def does_not_return_result(context):
assert len(context.response) == 1, "expected one response"
assert len(context.response[0].actions) == 0, "expected no return file action"
@scenario('queries.feature',
'When predicate of Break Query action succeeds all subsequent actions are executed')
def test_break_query_succeeds():
pass
@given("a command containing a Break Query action, which results in a success, and a Read action")
def query_cmd_success(test_device, context):
cmd = Command()
# comparing the UID file to the UID results in a success
cmd.add_action(
RegularAction(
operation=BreakQuery(
operand=QueryOperand(
type=QueryType.ARITH_COMP_WITH_VALUE,
mask_present=False,
params=ArithQueryParams(comp_type=ArithComparisonType.EQUALITY, signed_data_type=False),
compare_length=Length(8),
compare_value=struct.pack(">Q", int(test_device.uid, 16)),
file_a_offset=Offset(id=0, offset=Length(0))
)
)
)
)
cmd.add_action(
RegularAction(
operation=ReadFileData(
operand=DataRequest(
offset=Offset(id=0, offset=Length(0)),
length=Length(8)
)
)
)
)
context.query_cmd = cmd
@then("the Read action does return a result")
def does_return_result(context):
assert len(context.response) == 1, "expected one response"
assert len(context.response[0].actions) == 1, "expected a return file action"
def get_arithm_comp_to_uid_cmd(value, comp_type):
# we compare the UID file to value using the comp_type comparator
cmd = Command()
cmd.add_action(
RegularAction(
operation=BreakQuery(
operand=QueryOperand(
type=QueryType.ARITH_COMP_WITH_VALUE,
mask_present=False,
params=ArithQueryParams(comp_type=comp_type, signed_data_type=False),
compare_length=Length(8),
compare_value=struct.pack(">Q", value),
file_a_offset=Offset(id=0, offset=Length(0))
)
)
)
)
cmd.add_action(
RegularAction(
operation=ReadFileData(
operand=DataRequest(
offset=Offset(id=0, offset=Length(0)),
length=Length(8)
)
)
)
)
return cmd
@scenario('queries.feature',
'Validate correct execution of queries with arithmetic comparison',
dict(comp_type=str, value_comparison=str, result_count=str))
def test_comp_generic():
pass
@given(parsers.parse("a command containing a query with a {comp_type} comparison comparing a known value with a value which is {value_comparison}, and a Read action"))
def query_cmd_generic(context, test_device, comp_type, value_comparison):
# we compare the UID file to UID, or UID - 1 or UID + 1, depending on value_comparisor
if comp_type == ">":
c = ArithComparisonType.GREATER_THAN
elif comp_type == ">=":
c = ArithComparisonType.GREATER_THAN_OR_EQUAL_TO
elif comp_type == "<":
c = ArithComparisonType.LESS_THAN
elif comp_type == "<=":
c = ArithComparisonType.LESS_THAN_OR_EQUAL_TO
elif comp_type == "==":
c = ArithComparisonType.EQUALITY
elif comp_type == "!=":
c = ArithComparisonType.INEQUALITY
else:
assert False
if value_comparison == "bigger":
v = 1
elif value_comparison == "equal":
v = 0
elif value_comparison == "smaller":
v = -1
else:
assert False
context.query_cmd = get_arithm_comp_to_uid_cmd(int(test_device.uid, 16) + v, c)
@then(parsers.parse("the Read action does return {result_count} results"))
def return_result(context, result_count):
assert len(context.response) == 1, "expected one response"
assert len(context.response[0].actions) == int(result_count), "expected {} return file action".format(result_count)