• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chkware / cli / 5985476408

26 Aug 2023 03:02PM UTC coverage: 86.154% (+0.1%) from 86.01%
5985476408

push

github

web-flow
Merge pull request #238 from 0hsn/feat/mod-validate-assertion-func-date

Feature: Date assertion funcions for mod validate

87 of 87 new or added lines in 2 files covered. (100.0%)

1904 of 2210 relevant lines covered (86.15%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.58
/chk/modules/validate/assertion_function.py
1
"""
2
Assertion Functions mod
3
"""
4
import datetime
1✔
5
import types
1✔
6
from typing import TypeAlias, Union
1✔
7

8
# assertion result type; internal use
9
_AResult: TypeAlias = Union[ValueError | bool]
1✔
10

11

12
def equal(actual: object, expected: object, **_: object) -> _AResult:
1✔
13
    """Assert equals
14

15
    Args:
16
        actual: object
17
        expected: object
18
        **_: object ignores any other params
19
    Returns:
20
        _AResult result
21
    """
22

23
    return actual == expected
1✔
24

25

26
def not_equal(actual: object, expected: object, **_: object) -> _AResult:
1✔
27
    """Assert not equal
28

29
    Args:
30
        actual: object
31
        expected: object
32
        **_: object ignores any other params
33
    Returns:
34
        _AResult result
35
    """
36

37
    return actual != expected
1✔
38

39

40
def accepted(actual: object, **_: object) -> _AResult:
1✔
41
    """Assert has accepted values:
42
    1. yes, YES, Yes
43
    2. 1
44
    3. on, ON, On
45
    3. True, "True", "TRUE", "true"
46

47
    Args:
48
        actual: object
49
        **_: object ignores any other params
50
    Returns:
51
        _AResult result
52
    """
53

54
    # fmt: off
55
    accepted_values = [
1✔
56
        "yes", "YES", "Yes",
57
        "on", "ON", "On",
58
        1, True,
59
        "True", "TRUE", "true",
60
    ]
61
    declined_values = [
1✔
62
        "no", "NO", "No",
63
        "off", "OFF", "Off",
64
        0, False,
65
        "False", "FALSE", "false",
66
    ]
67
    # fmt: on
68

69
    if actual not in accepted_values + declined_values:
1✔
70
        return False
1✔
71

72
    return actual in accepted_values
1✔
73

74

75
def declined(actual: object, **_: object) -> _AResult:
1✔
76
    """Assert has declined values:
77
    1. no, NO, No
78
    2. 0, False,
79
    3. off, OFF, Off
80
    3. "False", "FALSE", "false"
81

82
    Args:
83
        actual: object
84
        **_: object ignores any other params
85
    Returns:
86
        _AResult result
87
    """
88

89
    # fmt: off
90
    accepted_values = [
1✔
91
        "yes", "YES", "Yes",
92
        "on", "ON", "On",
93
        1, True,
94
        "True", "TRUE", "true",
95
    ]
96
    declined_values = [
1✔
97
        "no", "NO", "No",
98
        "off", "OFF", "Off",
99
        0, False,
100
        "False", "FALSE", "false",
101
    ]
102
    # fmt: on
103

104
    if actual not in accepted_values + declined_values:
1✔
105
        return ValueError("actual_not_allowed")
1✔
106

107
    return actual in declined_values
1✔
108

109

110
def empty(actual: object, **_: object) -> _AResult:
1✔
111
    """Assert empty
112

113
    Args:
114
        actual: object
115
        **_: object ignores any other params
116
    Returns:
117
        _AResult result
118
    """
119

120
    return not bool(actual)
1✔
121

122

123
def not_empty(actual: object, **_: object) -> _AResult:
1✔
124
    """Assert not empty
125

126
    Args:
127
        actual: object
128
        **_: object ignores any other params
129
    Returns:
130
        _AResult result
131
    """
132

133
    return bool(actual)
1✔
134

135

136
def boolean(actual: object, expected: object, **_: object) -> _AResult:
1✔
137
    """Assert boolean"""
138

139
    if not isinstance(actual, bool):
1✔
140
        return ValueError("actual_not_bool")
1✔
141

142
    if not isinstance(expected, types.NotImplementedType) and not isinstance(
1✔
143
        expected, bool
144
    ):
145
        return ValueError("expected_not_bool")
1✔
146

147
    if isinstance(expected, bool):
1✔
148
        return True if actual == expected else ValueError("expected_mismatch")
1✔
149

150
    return True
1✔
151

152

153
def integer(actual: object, **_: object) -> _AResult:
1✔
154
    """Assert integer"""
155

156
    return isinstance(actual, int)
1✔
157

158

159
def integer_between(actual: object, extra_fields: dict, **_: object) -> _AResult:
1✔
160
    """Assert integer between"""
161

162
    if not isinstance(actual, int):
1✔
163
        return ValueError("actual_not_int")
1✔
164

165
    return int(extra_fields["min"]) < actual < int(extra_fields["max"])
1✔
166

167

168
def integer_greater(actual: object, extra_fields: dict, **_: object) -> _AResult:
1✔
169
    """Assert integer is greater than"""
170

171
    if not isinstance(actual, int):
1✔
172
        return ValueError("actual_not_int")
1✔
173

174
    return int(extra_fields["other"]) < actual
1✔
175

176

177
def integer_greater_or_equal(
1✔
178
    actual: object, extra_fields: dict, **_: object
179
) -> _AResult:
180
    """Assert integer is greater than or equal to other"""
181

182
    if not isinstance(actual, int):
1✔
183
        return ValueError("actual_not_int")
1✔
184

185
    return int(extra_fields["other"]) <= actual
1✔
186

187

188
def integer_less(actual: object, extra_fields: dict, **_: object) -> _AResult:
1✔
189
    """Assert integer is less than"""
190

191
    if not isinstance(actual, int):
1✔
192
        return ValueError("actual_not_int")
1✔
193

194
    return int(extra_fields["other"]) > actual
1✔
195

196

197
def integer_less_or_equal(actual: object, extra_fields: dict, **_: object) -> _AResult:
1✔
198
    """Assert integer is less than or equal to other"""
199

200
    if not isinstance(actual, int):
1✔
201
        return ValueError("actual_not_int")
1✔
202

203
    return int(extra_fields["other"]) >= actual
1✔
204

205

206
def float_(actual: object, **_: object) -> _AResult:
1✔
207
    """Assert float"""
208

209
    return isinstance(actual, float)
1✔
210

211

212
def float_between(actual: object, extra_fields: dict, **_: object) -> _AResult:
1✔
213
    """Assert float between"""
214

215
    if not isinstance(actual, float):
1✔
216
        return ValueError("actual_not_float")
1✔
217

218
    return extra_fields["min"] < actual < extra_fields["max"]
1✔
219

220

221
def float_greater(actual: object, extra_fields: dict, **_: object) -> _AResult:
1✔
222
    """Assert float is greater than"""
223

224
    if not isinstance(actual, float):
1✔
225
        return ValueError("actual_not_float")
1✔
226

227
    return extra_fields["other"] < actual
1✔
228

229

230
def float_greater_or_equal(actual: object, extra_fields: dict, **_: object) -> _AResult:
1✔
231
    """Assert float is greater than or equal to other"""
232

233
    if not isinstance(actual, float):
1✔
234
        return ValueError("actual_not_float")
1✔
235

236
    return extra_fields["other"] <= actual
1✔
237

238

239
def float_less(actual: object, extra_fields: dict, **_: object) -> _AResult:
1✔
240
    """Assert float is less than"""
241

242
    if not isinstance(actual, float):
1✔
243
        return ValueError("actual_not_float")
1✔
244

245
    return extra_fields["other"] > actual
1✔
246

247

248
def float_less_or_equal(actual: object, extra_fields: dict, **_: object) -> _AResult:
1✔
249
    """Assert float is greater than or equal to other"""
250

251
    if not isinstance(actual, float):
1✔
252
        return ValueError("actual_not_float")
1✔
253

254
    return extra_fields["other"] >= actual
1✔
255

256

257
def str_(actual: object, **_: object) -> _AResult:
1✔
258
    """Assert string"""
259

260
    return isinstance(actual, str)
1✔
261

262

263
def str_have(actual: str, extra_fields: dict, **_: object) -> _AResult:
1✔
264
    """Assert string have a sub-string"""
265

266
    if not isinstance(actual, str):
1✔
267
        return ValueError("actual_not_str")
1✔
268

269
    if not isinstance(extra_fields["other"], str):
1✔
270
        return ValueError("other_not_str")
1✔
271

272
    return extra_fields["other"] in actual
1✔
273

274

275
def str_do_not_have(actual: str, extra_fields: dict, **_: object) -> _AResult:
1✔
276
    """Assert string do not have a sub-string"""
277

278
    if not isinstance(actual, str):
1✔
279
        return ValueError("actual_not_str")
1✔
280

281
    if not isinstance(extra_fields["other"], str):
1✔
282
        return ValueError("other_not_str")
1✔
283

284
    return extra_fields["other"] not in actual
1✔
285

286

287
def str_starts_with(actual: str, extra_fields: dict, **_: object) -> _AResult:
1✔
288
    """Assert string starts with sub-string"""
289

290
    if not isinstance(actual, str):
1✔
291
        return ValueError("actual_not_str")
1✔
292

293
    if not isinstance(extra_fields["other"], str):
1✔
294
        return ValueError("other_not_str")
1✔
295

296
    return actual.startswith(extra_fields["other"])
1✔
297

298

299
def str_do_not_starts_with(actual: str, extra_fields: dict, **_: object) -> _AResult:
1✔
300
    """Assert string do not starts with sub-string"""
301

302
    if not isinstance(actual, str):
1✔
303
        return ValueError("actual_not_str")
1✔
304

305
    if not isinstance(extra_fields["other"], str):
1✔
306
        return ValueError("other_not_str")
1✔
307

308
    return not actual.startswith(extra_fields["other"])
1✔
309

310

311
def str_ends_with(actual: str, extra_fields: dict, **_: object) -> _AResult:
1✔
312
    """Assert string ends with sub-string"""
313

314
    if not isinstance(actual, str):
1✔
315
        return ValueError("actual_not_str")
1✔
316

317
    if not isinstance(extra_fields["other"], str):
1✔
318
        return ValueError("other_not_str")
1✔
319

320
    return actual.endswith(extra_fields["other"])
1✔
321

322

323
def str_do_not_ends_with(actual: str, extra_fields: dict, **_: object) -> _AResult:
1✔
324
    """Assert string do not ends with sub-string"""
325

326
    if not isinstance(actual, str):
1✔
327
        return ValueError("actual_not_str")
1✔
328

329
    if not isinstance(extra_fields["other"], str):
1✔
330
        return ValueError("other_not_str")
1✔
331

332
    return not actual.endswith(extra_fields["other"])
1✔
333

334

335
def date(actual: str, extra_fields: dict, **_: object) -> _AResult:
1✔
336
    """Assert actual date"""
337

338
    if not isinstance(actual, str):
1✔
339
        return ValueError("actual_not_str")
×
340

341
    try:
1✔
342
        datetime.datetime.strptime(actual, extra_fields["format"]).date()
1✔
343
        return True
1✔
344
    except ValueError:
1✔
345
        return False
1✔
346

347

348
def date_after(actual: str, expected: str, extra_fields: dict, **_: object) -> _AResult:
1✔
349
    """Assert actual date is after expected date"""
350

351
    if not isinstance(actual, str):
1✔
352
        return ValueError("actual_not_str")
×
353

354
    if not isinstance(expected, str):
1✔
355
        return ValueError("expected_not_str")
×
356

357
    try:
1✔
358
        d_actual = datetime.datetime.strptime(actual, extra_fields["format"]).date()
1✔
359
        d_expected = datetime.datetime.strptime(expected, extra_fields["format"]).date()
1✔
360

361
        return d_actual > d_expected
1✔
362
    except ValueError:
1✔
363
        return ValueError("date_conversion_issue")
1✔
364

365

366
def date_after_or_equal(
1✔
367
    actual: str, expected: str, extra_fields: dict, **_: object
368
) -> _AResult:
369
    """Assert actual date is after or equal to expected date"""
370

371
    if not isinstance(actual, str):
1✔
372
        return ValueError("actual_not_str")
×
373

374
    if not isinstance(expected, str):
1✔
375
        return ValueError("expected_not_str")
×
376

377
    try:
1✔
378
        d_actual = datetime.datetime.strptime(actual, extra_fields["format"]).date()
1✔
379
        d_expected = datetime.datetime.strptime(expected, extra_fields["format"]).date()
1✔
380

381
        return d_actual >= d_expected
1✔
382
    except ValueError:
1✔
383
        return ValueError("date_conversion_issue")
1✔
384

385

386
def date_before(
1✔
387
    actual: str, expected: str, extra_fields: dict, **_: object
388
) -> _AResult:
389
    """Assert actual date is after expected date"""
390

391
    if not isinstance(actual, str):
1✔
392
        return ValueError("actual_not_str")
×
393

394
    if not isinstance(expected, str):
1✔
395
        return ValueError("expected_not_str")
×
396

397
    try:
1✔
398
        d_actual = datetime.datetime.strptime(actual, extra_fields["format"]).date()
1✔
399
        d_expected = datetime.datetime.strptime(expected, extra_fields["format"]).date()
1✔
400

401
        return d_actual < d_expected
1✔
402
    except ValueError:
1✔
403
        return ValueError("date_conversion_issue")
1✔
404

405

406
def date_before_or_equal(
1✔
407
    actual: str, expected: str, extra_fields: dict, **_: object
408
) -> _AResult:
409
    """Assert actual date is after expected date"""
410

411
    if not isinstance(actual, str):
1✔
412
        return ValueError("actual_not_str")
×
413

414
    if not isinstance(expected, str):
1✔
415
        return ValueError("expected_not_str")
×
416

417
    try:
1✔
418
        d_actual = datetime.datetime.strptime(actual, extra_fields["format"]).date()
1✔
419
        d_expected = datetime.datetime.strptime(expected, extra_fields["format"]).date()
1✔
420

421
        return d_actual <= d_expected
1✔
422
    except ValueError:
1✔
423
        return ValueError("date_conversion_issue")
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc