summaryrefslogtreecommitdiff
path: root/python-believe.spec
blob: 8a21f5a1786c401115fe1569fe3fca1b484ffd68 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
%global _empty_manifest_terminate_build 0
Name:		python-believe
Version:	1.0.13
Release:	1
Summary:	A easy to use validator for json content
License:	None
URL:		https://pypi.org/project/believe/
Source0:	https://mirrors.nju.edu.cn/pypi/web/packages/91/17/d62d3f8f8b16c6266ee0c070ce51a90c1d732042ac560a41c239b6c08356/believe-1.0.13.tar.gz
BuildArch:	noarch

Requires:	python3-flit
Requires:	python3-pytest
Requires:	python3-pytest-cov
Requires:	python3-coverage-lcov

%description
# Believe
## Motivation
* We often need to compare expected results in our test. It's lousy to check expected result under the following way.

```
import uuid

class TestCases:
    def test_user(self):
        user = {
                    "name": "John",
                    "id": "30e7b1e2-4c80-44c2-8fe5-23bad73ed8f2"
                }
        assert set("name", "id") == set(user.keys())
        assert isinstance(user["name"], str)
        assert 0 < len(user["name"]) < 64
        assert isinstance(user["id"], str)
        uuid.UUID(user["id"])
```

* By using this package, we could compare the value using the following way. It's easier to read and maintain.

```
import believe as B

class TestCases:
    def test_user(self):
        user = {
                    "name": "John",
                    "id": "30e7b1e2-4c80-44c2-8fe5-23bad73ed8f2"
                }
        assert user == {"name": B.AnyStr(min_len=1, max_len=63),
                        "id": B.AnyUUID()}

```

* If you are looking for web framework input validation, I suggest use [FastAPI](https://fastapi.tiangolo.com/). It properly integrate with openapi as well.

## Installation
```
pip install believe
```

## Basic usage
```
import believe as B
import time

# Match any string
assert B.AnyStr() == "any_str"

# Match string length >= 1 and <= 10
assert B.AnyStr(min_len=1, max_len=10) == "a"

# Match any string that can be converted to int
assert B.AnyIntStr() == "123"

# Match any UUID format string
assert B.AnyUUID() == "732c0743-2638-47d5-902d-0daa3080348b"

# Match any sha1 string
assert B.AnySHA1() == "b130c4b97d0640eaf3f45f7360a5b4dbbf561f58"

# Match any IPv4 string
assert B.AnyIPV4() == "1.2.3.4"

# Match integer that is >=1 and <= 10
assert B.AnyInt(min_value=1, max_value=10) == 5

# Match any float that is >= 1.0 and <= 10.0
assert B.AnyFloat(min_value=1.0, max_value=10.0) == 5.0 # 1.0 <= X <= 10.0

# Match if values is "one" or "two"
assert B.OneOf("one", "two") == "one"

# Sometimes we don't care about the order, we can use AnyOrder
assert B.AnyOrder([1, 2, 3]) == [2, 1, 3]

# Sometimes we assign value as timestamp but test cases takes more than 1 sec
# We can use almost to accept a range of values
assert B.Almost(time.time(), ts_range=3) == time.time() # Allow 3 sec gap

# If we allow None or any string
assert B.Nullable(B.AnyStr()) == None
assert B.Nullable(B.AnyStr()) == "123"

# Only check type
assert B.Any(bytes) == b'123'

# Reverse check result, anything but "A" or "B"
assert B.Not(B.OneOf("A", "B")) == "C"

# Match list
assert B.ListOf(B.AnyStr()) == ["A", "B", "C"]
assert B.ListOf(B.AnyStr(), n_item=3) == ["A", "B", "C"]  # exact 3 items
assert B.ListOf(B.AnyStr(), min_item=1) == ["A", "B", "C"]  # >= 1 items
assert B.ListOf(B.AnyStr(), max_item=5) == ["A", "B", "C"]  # <= 5 items
```

## Advance Usage
```
# If we don't want to use json.load('{"foo": "bar"}') == {"foo": "bar"}, we can use the following way
assert B.AnyJsonStr({"foo": "bar"}) == '{"foo": "bar"}'

# We can use AnyUrl to compare the normalized url
# 1. We can compare one with default port and one without, they are identical
assert B.AnyUrl("https://foo.com/") == "https://foo.com:443/"
assert B.AnyUrl("http://foo.com/") == "http://foo.com:80/"
# 2. We can ignore the order in query string
assert B.AnyUrl("https://foo.com/bar?p1=1&p2=2") == "https://foo.com/bar?p2=2&p1=1"

# We can use Dict to compare a dict with Optional field
assert B.Dict({"name": B.AnyStr(), "value": B.Optional(B.AnyStr())}) == {"name": "abc"}
assert B.Dict({"name": B.AnyStr(), "value": B.Optional(B.AnyStr())}) == {"name": "abc", "value": "def"}

# If key is a dynamic value, we can use DictOf(<key_matcher>, <value_matcher>)
# i.e. We want to match a dict with random uuid as key
assert B.DictOf(B.AnyUUID(), B.OneOf("ok", "fail")) == {"732c0743-2638-47d5-902d-0daa3080348b": "ok",
                                                        "5cfd50ba-c3d3-4fb7-b2fe-e9a6e039ad29": "fail"}
```

## Use Validate Function
```
# validate with error exception
import believe as B
validator = B.Dict({"name": B.AnyInt()})

B.validate(validator, {"name": "ken"})  # believe.error.ValidateError: [e_path=$.name] 'ken' != AnyInt()
```

## A Complex Example
```
import believe as B
import time

result_json = {"name": "john",
               "age": 32,
               "download_link": "https://download.server.com/?name=john&id=abc",
               "role": "admin",
               "address": "10.1.2.3",
               "updated_at": int(time.time()),
               "tags": ["admin", "john"]}

exp_result = B.Dict({"name": B.AnyStr(min_len=1, max_len=64),
                     "age": B.AnyInt(min_value=0, max_value=200),
                     "download_link": B.AnyUrl("https://download.server.com/?id=abc&name=john"),
                     "role": B.OneOf("admin", "user"),
                     "address": B.AnyIPV4(),
                     "updated_at": B.Almost(int(time.time())),
                     "tags": B.ListOf(B.AnyStr()),
                     "extra": B.Optional(B.Nullable(B.AnyStr()))})
B.validate(exp_result, result_json)
```


%package -n python3-believe
Summary:	A easy to use validator for json content
Provides:	python-believe
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-believe
# Believe
## Motivation
* We often need to compare expected results in our test. It's lousy to check expected result under the following way.

```
import uuid

class TestCases:
    def test_user(self):
        user = {
                    "name": "John",
                    "id": "30e7b1e2-4c80-44c2-8fe5-23bad73ed8f2"
                }
        assert set("name", "id") == set(user.keys())
        assert isinstance(user["name"], str)
        assert 0 < len(user["name"]) < 64
        assert isinstance(user["id"], str)
        uuid.UUID(user["id"])
```

* By using this package, we could compare the value using the following way. It's easier to read and maintain.

```
import believe as B

class TestCases:
    def test_user(self):
        user = {
                    "name": "John",
                    "id": "30e7b1e2-4c80-44c2-8fe5-23bad73ed8f2"
                }
        assert user == {"name": B.AnyStr(min_len=1, max_len=63),
                        "id": B.AnyUUID()}

```

* If you are looking for web framework input validation, I suggest use [FastAPI](https://fastapi.tiangolo.com/). It properly integrate with openapi as well.

## Installation
```
pip install believe
```

## Basic usage
```
import believe as B
import time

# Match any string
assert B.AnyStr() == "any_str"

# Match string length >= 1 and <= 10
assert B.AnyStr(min_len=1, max_len=10) == "a"

# Match any string that can be converted to int
assert B.AnyIntStr() == "123"

# Match any UUID format string
assert B.AnyUUID() == "732c0743-2638-47d5-902d-0daa3080348b"

# Match any sha1 string
assert B.AnySHA1() == "b130c4b97d0640eaf3f45f7360a5b4dbbf561f58"

# Match any IPv4 string
assert B.AnyIPV4() == "1.2.3.4"

# Match integer that is >=1 and <= 10
assert B.AnyInt(min_value=1, max_value=10) == 5

# Match any float that is >= 1.0 and <= 10.0
assert B.AnyFloat(min_value=1.0, max_value=10.0) == 5.0 # 1.0 <= X <= 10.0

# Match if values is "one" or "two"
assert B.OneOf("one", "two") == "one"

# Sometimes we don't care about the order, we can use AnyOrder
assert B.AnyOrder([1, 2, 3]) == [2, 1, 3]

# Sometimes we assign value as timestamp but test cases takes more than 1 sec
# We can use almost to accept a range of values
assert B.Almost(time.time(), ts_range=3) == time.time() # Allow 3 sec gap

# If we allow None or any string
assert B.Nullable(B.AnyStr()) == None
assert B.Nullable(B.AnyStr()) == "123"

# Only check type
assert B.Any(bytes) == b'123'

# Reverse check result, anything but "A" or "B"
assert B.Not(B.OneOf("A", "B")) == "C"

# Match list
assert B.ListOf(B.AnyStr()) == ["A", "B", "C"]
assert B.ListOf(B.AnyStr(), n_item=3) == ["A", "B", "C"]  # exact 3 items
assert B.ListOf(B.AnyStr(), min_item=1) == ["A", "B", "C"]  # >= 1 items
assert B.ListOf(B.AnyStr(), max_item=5) == ["A", "B", "C"]  # <= 5 items
```

## Advance Usage
```
# If we don't want to use json.load('{"foo": "bar"}') == {"foo": "bar"}, we can use the following way
assert B.AnyJsonStr({"foo": "bar"}) == '{"foo": "bar"}'

# We can use AnyUrl to compare the normalized url
# 1. We can compare one with default port and one without, they are identical
assert B.AnyUrl("https://foo.com/") == "https://foo.com:443/"
assert B.AnyUrl("http://foo.com/") == "http://foo.com:80/"
# 2. We can ignore the order in query string
assert B.AnyUrl("https://foo.com/bar?p1=1&p2=2") == "https://foo.com/bar?p2=2&p1=1"

# We can use Dict to compare a dict with Optional field
assert B.Dict({"name": B.AnyStr(), "value": B.Optional(B.AnyStr())}) == {"name": "abc"}
assert B.Dict({"name": B.AnyStr(), "value": B.Optional(B.AnyStr())}) == {"name": "abc", "value": "def"}

# If key is a dynamic value, we can use DictOf(<key_matcher>, <value_matcher>)
# i.e. We want to match a dict with random uuid as key
assert B.DictOf(B.AnyUUID(), B.OneOf("ok", "fail")) == {"732c0743-2638-47d5-902d-0daa3080348b": "ok",
                                                        "5cfd50ba-c3d3-4fb7-b2fe-e9a6e039ad29": "fail"}
```

## Use Validate Function
```
# validate with error exception
import believe as B
validator = B.Dict({"name": B.AnyInt()})

B.validate(validator, {"name": "ken"})  # believe.error.ValidateError: [e_path=$.name] 'ken' != AnyInt()
```

## A Complex Example
```
import believe as B
import time

result_json = {"name": "john",
               "age": 32,
               "download_link": "https://download.server.com/?name=john&id=abc",
               "role": "admin",
               "address": "10.1.2.3",
               "updated_at": int(time.time()),
               "tags": ["admin", "john"]}

exp_result = B.Dict({"name": B.AnyStr(min_len=1, max_len=64),
                     "age": B.AnyInt(min_value=0, max_value=200),
                     "download_link": B.AnyUrl("https://download.server.com/?id=abc&name=john"),
                     "role": B.OneOf("admin", "user"),
                     "address": B.AnyIPV4(),
                     "updated_at": B.Almost(int(time.time())),
                     "tags": B.ListOf(B.AnyStr()),
                     "extra": B.Optional(B.Nullable(B.AnyStr()))})
B.validate(exp_result, result_json)
```


%package help
Summary:	Development documents and examples for believe
Provides:	python3-believe-doc
%description help
# Believe
## Motivation
* We often need to compare expected results in our test. It's lousy to check expected result under the following way.

```
import uuid

class TestCases:
    def test_user(self):
        user = {
                    "name": "John",
                    "id": "30e7b1e2-4c80-44c2-8fe5-23bad73ed8f2"
                }
        assert set("name", "id") == set(user.keys())
        assert isinstance(user["name"], str)
        assert 0 < len(user["name"]) < 64
        assert isinstance(user["id"], str)
        uuid.UUID(user["id"])
```

* By using this package, we could compare the value using the following way. It's easier to read and maintain.

```
import believe as B

class TestCases:
    def test_user(self):
        user = {
                    "name": "John",
                    "id": "30e7b1e2-4c80-44c2-8fe5-23bad73ed8f2"
                }
        assert user == {"name": B.AnyStr(min_len=1, max_len=63),
                        "id": B.AnyUUID()}

```

* If you are looking for web framework input validation, I suggest use [FastAPI](https://fastapi.tiangolo.com/). It properly integrate with openapi as well.

## Installation
```
pip install believe
```

## Basic usage
```
import believe as B
import time

# Match any string
assert B.AnyStr() == "any_str"

# Match string length >= 1 and <= 10
assert B.AnyStr(min_len=1, max_len=10) == "a"

# Match any string that can be converted to int
assert B.AnyIntStr() == "123"

# Match any UUID format string
assert B.AnyUUID() == "732c0743-2638-47d5-902d-0daa3080348b"

# Match any sha1 string
assert B.AnySHA1() == "b130c4b97d0640eaf3f45f7360a5b4dbbf561f58"

# Match any IPv4 string
assert B.AnyIPV4() == "1.2.3.4"

# Match integer that is >=1 and <= 10
assert B.AnyInt(min_value=1, max_value=10) == 5

# Match any float that is >= 1.0 and <= 10.0
assert B.AnyFloat(min_value=1.0, max_value=10.0) == 5.0 # 1.0 <= X <= 10.0

# Match if values is "one" or "two"
assert B.OneOf("one", "two") == "one"

# Sometimes we don't care about the order, we can use AnyOrder
assert B.AnyOrder([1, 2, 3]) == [2, 1, 3]

# Sometimes we assign value as timestamp but test cases takes more than 1 sec
# We can use almost to accept a range of values
assert B.Almost(time.time(), ts_range=3) == time.time() # Allow 3 sec gap

# If we allow None or any string
assert B.Nullable(B.AnyStr()) == None
assert B.Nullable(B.AnyStr()) == "123"

# Only check type
assert B.Any(bytes) == b'123'

# Reverse check result, anything but "A" or "B"
assert B.Not(B.OneOf("A", "B")) == "C"

# Match list
assert B.ListOf(B.AnyStr()) == ["A", "B", "C"]
assert B.ListOf(B.AnyStr(), n_item=3) == ["A", "B", "C"]  # exact 3 items
assert B.ListOf(B.AnyStr(), min_item=1) == ["A", "B", "C"]  # >= 1 items
assert B.ListOf(B.AnyStr(), max_item=5) == ["A", "B", "C"]  # <= 5 items
```

## Advance Usage
```
# If we don't want to use json.load('{"foo": "bar"}') == {"foo": "bar"}, we can use the following way
assert B.AnyJsonStr({"foo": "bar"}) == '{"foo": "bar"}'

# We can use AnyUrl to compare the normalized url
# 1. We can compare one with default port and one without, they are identical
assert B.AnyUrl("https://foo.com/") == "https://foo.com:443/"
assert B.AnyUrl("http://foo.com/") == "http://foo.com:80/"
# 2. We can ignore the order in query string
assert B.AnyUrl("https://foo.com/bar?p1=1&p2=2") == "https://foo.com/bar?p2=2&p1=1"

# We can use Dict to compare a dict with Optional field
assert B.Dict({"name": B.AnyStr(), "value": B.Optional(B.AnyStr())}) == {"name": "abc"}
assert B.Dict({"name": B.AnyStr(), "value": B.Optional(B.AnyStr())}) == {"name": "abc", "value": "def"}

# If key is a dynamic value, we can use DictOf(<key_matcher>, <value_matcher>)
# i.e. We want to match a dict with random uuid as key
assert B.DictOf(B.AnyUUID(), B.OneOf("ok", "fail")) == {"732c0743-2638-47d5-902d-0daa3080348b": "ok",
                                                        "5cfd50ba-c3d3-4fb7-b2fe-e9a6e039ad29": "fail"}
```

## Use Validate Function
```
# validate with error exception
import believe as B
validator = B.Dict({"name": B.AnyInt()})

B.validate(validator, {"name": "ken"})  # believe.error.ValidateError: [e_path=$.name] 'ken' != AnyInt()
```

## A Complex Example
```
import believe as B
import time

result_json = {"name": "john",
               "age": 32,
               "download_link": "https://download.server.com/?name=john&id=abc",
               "role": "admin",
               "address": "10.1.2.3",
               "updated_at": int(time.time()),
               "tags": ["admin", "john"]}

exp_result = B.Dict({"name": B.AnyStr(min_len=1, max_len=64),
                     "age": B.AnyInt(min_value=0, max_value=200),
                     "download_link": B.AnyUrl("https://download.server.com/?id=abc&name=john"),
                     "role": B.OneOf("admin", "user"),
                     "address": B.AnyIPV4(),
                     "updated_at": B.Almost(int(time.time())),
                     "tags": B.ListOf(B.AnyStr()),
                     "extra": B.Optional(B.Nullable(B.AnyStr()))})
B.validate(exp_result, result_json)
```


%prep
%autosetup -n believe-1.0.13

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-believe -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Wed May 31 2023 Python_Bot <Python_Bot@openeuler.org> - 1.0.13-1
- Package Spec generated