-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_tools_utils.py
565 lines (360 loc) · 14 KB
/
test_tools_utils.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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import os
import pytest
DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
TOOLS_UTILS_DATA_DIR = os.path.join(DATA_DIR, "tools", "utils")
IMGREG_DATA_DIR = os.path.join(DATA_DIR, "imageregistration")
def test_uid_generator_len4():
import rsgislib.tools.utils
uid_str = rsgislib.tools.utils.uid_generator(4)
assert len(uid_str) == 4
def test_uid_generator_len6():
import rsgislib.tools.utils
uid_str = rsgislib.tools.utils.uid_generator(6)
assert len(uid_str) == 6
def test_uid_generator_len8():
import rsgislib.tools.utils
uid_str = rsgislib.tools.utils.uid_generator(8)
assert len(uid_str) == 8
def test_is_number_int_1():
import rsgislib.tools.utils
assert rsgislib.tools.utils.is_number(1)
def test_is_number_int_100():
import rsgislib.tools.utils
assert rsgislib.tools.utils.is_number(100)
def test_is_number_int_1str():
import rsgislib.tools.utils
assert rsgislib.tools.utils.is_number("1")
def test_is_number_int_100str():
import rsgislib.tools.utils
assert rsgislib.tools.utils.is_number("100")
def test_is_number_float_125():
import rsgislib.tools.utils
assert rsgislib.tools.utils.is_number(1.25)
def test_is_number_float_10025():
import rsgislib.tools.utils
assert rsgislib.tools.utils.is_number(100.25)
def test_is_number_float_str125():
import rsgislib.tools.utils
assert rsgislib.tools.utils.is_number("1.25")
def test_is_number_float_str10025():
import rsgislib.tools.utils
assert rsgislib.tools.utils.is_number("100.25")
def test_is_number_str_Hello():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.is_number("Hello")
def test_is_number_str_World():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.is_number("World")
def test_is_number_str_1a():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.is_number("1a")
def test_is_number_str_100q():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.is_number("100?")
def test_is_number_str_1e():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.is_number("1!")
def test_is_number_str_10at():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.is_number("10@")
def test_zero_pad_num_str_1():
import rsgislib.tools.utils
num_str = rsgislib.tools.utils.zero_pad_num_str(
1, str_len=3, round_num=False, round_n_digts=0, integerise=False
)
assert num_str == "001"
def test_zero_pad_num_str_11_round_int():
import rsgislib.tools.utils
num_str = rsgislib.tools.utils.zero_pad_num_str(
1.1, str_len=3, round_num=True, round_n_digts=0, integerise=True
)
assert num_str == "001"
def test_zero_pad_num_str_float_round():
import rsgislib.tools.utils
num_str = rsgislib.tools.utils.zero_pad_num_str(
112.1354, str_len=8, round_num=True, round_n_digts=2, integerise=False
)
assert num_str == "00112.14"
def test_powerset_lst():
import rsgislib.tools.utils
set_vals = [1, 2, 3]
pwr_set = rsgislib.tools.utils.powerset_lst(set_vals)
vld_pwr_set = [[1, 2, 3], [2, 3], [1, 3], [3], [1, 2], [2], [1], []]
correct_answer = True
if len(pwr_set) != len(vld_pwr_set):
correct_answer = False
else:
for set_val in vld_pwr_set:
if set_val not in pwr_set:
correct_answer = False
break
assert correct_answer
def test_powerset_lst_min2():
import rsgislib.tools.utils
set_vals = [1, 2, 3]
pwr_set = rsgislib.tools.utils.powerset_lst(set_vals, min_items=2)
vld_pwr_set = [[1, 2, 3], [2, 3], [1, 3], [1, 2]]
correct_answer = True
if len(pwr_set) != len(vld_pwr_set):
correct_answer = False
else:
for set_val in vld_pwr_set:
if set_val not in pwr_set:
correct_answer = False
break
assert correct_answer
def test_read_text_file_no_new_lines_str():
import rsgislib.tools.utils
input_file = os.path.join(TOOLS_UTILS_DATA_DIR, "basic_str.txt")
in_data = rsgislib.tools.utils.read_text_file_no_new_lines(input_file)
assert in_data == "Hello"
def test_read_text_file_no_new_lines_str_mline():
import rsgislib.tools.utils
input_file = os.path.join(TOOLS_UTILS_DATA_DIR, "basic_str_mline.txt")
in_data = rsgislib.tools.utils.read_text_file_no_new_lines(input_file)
assert in_data == "HelloWorld"
def test_read_text_file_no_new_lines_int():
import rsgislib.tools.utils
input_file = os.path.join(TOOLS_UTILS_DATA_DIR, "basic_int.txt")
in_data = rsgislib.tools.utils.read_text_file_no_new_lines(input_file)
assert int(in_data) == 100
def test_read_text_file_to_list_strs():
import rsgislib.tools.utils
input_file = os.path.join(TOOLS_UTILS_DATA_DIR, "basic_str_mline.txt")
in_data = rsgislib.tools.utils.read_text_file_to_list(input_file)
ref_data = ["Hello", "World"]
if len(in_data) != len(ref_data):
assert False
for data_val, ref_val in zip(in_data, ref_data):
if data_val != ref_val:
assert False
assert True
def test_write_data_to_file_fileExists(tmp_path):
import rsgislib.tools.utils
out_data = "CONTENT"
out_file = os.path.join(tmp_path, "out_file.txt")
rsgislib.tools.utils.write_data_to_file(out_data, out_file)
assert os.path.exists(out_file)
def test_write_data_to_file_str_chkcontent(tmp_path):
import rsgislib.tools.utils
out_data = "CONTENT"
out_file = os.path.join(tmp_path, "out_file.txt")
rsgislib.tools.utils.write_data_to_file(out_data, out_file)
in_data = rsgislib.tools.utils.read_text_file_no_new_lines(out_file)
assert in_data == out_data
def test_write_data_to_file_int_chkcontent(tmp_path):
import rsgislib.tools.utils
out_data = 100
out_file = os.path.join(tmp_path, "out_file.txt")
rsgislib.tools.utils.write_data_to_file(out_data, out_file)
in_data = rsgislib.tools.utils.read_text_file_no_new_lines(out_file)
assert int(in_data) == out_data
def test_read_json_to_dict_basic():
import rsgislib.tools.utils
input_file = os.path.join(TOOLS_UTILS_DATA_DIR, "basic_dict.json")
out_data = dict()
out_data["Hello"] = "World"
out_data["ten"] = 10
in_data = rsgislib.tools.utils.read_json_to_dict(input_file)
assert in_data == out_data
def test_write_dict_to_json_basic(tmp_path):
import rsgislib.tools.utils
out_data = dict()
out_data["Hello"] = "World"
out_data["ten"] = 10
out_file = os.path.join(tmp_path, "out_file.json")
rsgislib.tools.utils.write_dict_to_json(out_data, out_file)
in_data = rsgislib.tools.utils.read_json_to_dict(out_file)
assert in_data == out_data
def test_write_dict_to_json_numpy(tmp_path):
import rsgislib.tools.utils
import numpy
out_data = dict()
out_data["data"] = numpy.arange(10)
out_file = os.path.join(tmp_path, "out_file.json")
rsgislib.tools.utils.write_dict_to_json(out_data, out_file)
in_data = rsgislib.tools.utils.read_json_to_dict(out_file)
assert numpy.sum(in_data["data"] - out_data["data"]) == 0
def test_in_bounds_sgl_num():
import rsgislib.tools.utils
assert rsgislib.tools.utils.in_bounds(5, 1, 10, upper_strict=False)
def test_in_bounds_sgl_num_fail():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.in_bounds(15, 1, 10, upper_strict=False)
def test_in_bounds_sgl_num_strict_false():
import rsgislib.tools.utils
assert rsgislib.tools.utils.in_bounds(10, 1, 10, upper_strict=False)
def test_in_bounds_sgl_num_strict_true():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.in_bounds(10, 1, 10, upper_strict=True)
def test_in_bounds_arr():
import rsgislib.tools.utils
import numpy
arr = numpy.array([2, 3, 4, 7, 8])
assert rsgislib.tools.utils.in_bounds(arr, 1, 10, upper_strict=False)
def test_in_bounds_arr_fail():
import rsgislib.tools.utils
import numpy
arr = numpy.array([12, 23, 34, 47, 68])
assert not rsgislib.tools.utils.in_bounds(arr, 1, 10, upper_strict=False)
def test_in_bounds_arr_strict_false():
import rsgislib.tools.utils
import numpy
arr = numpy.array([2, 3, 4, 7, 8, 10])
assert rsgislib.tools.utils.in_bounds(arr, 1, 10, upper_strict=False)
def test_in_bounds_arr_strict_true():
import rsgislib.tools.utils
import numpy
arr = numpy.array([2, 3, 4, 7, 8, 10])
assert not rsgislib.tools.utils.in_bounds(arr, 1, 10, upper_strict=True)
def test_mixed_signs_sgl_num_pos():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.mixed_signs(5)
def test_mixed_signs_sgl_num_neg():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.mixed_signs(-5)
def test_mixed_signs_arr():
import rsgislib.tools.utils
import numpy
arr = numpy.array([-1, 1, -2, 2])
assert rsgislib.tools.utils.mixed_signs(arr)
def test_mixed_signs_arr_fail():
import rsgislib.tools.utils
import numpy
arr = numpy.array([1, 1, 2, 2])
assert not rsgislib.tools.utils.mixed_signs(arr)
def test_negative_sgl_num_pos():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.negative(5)
def test_negative_sgl_num_neg():
import rsgislib.tools.utils
assert rsgislib.tools.utils.negative(-5)
def test_negative_arr_mix():
import rsgislib.tools.utils
import numpy
arr = numpy.array([-1, 1, -2, 2])
assert not rsgislib.tools.utils.negative(arr)
def test_negative_arr_pos():
import rsgislib.tools.utils
import numpy
arr = numpy.array([1, 1, 2, 2])
assert not rsgislib.tools.utils.negative(arr)
def test_negative_arr_neg():
import rsgislib.tools.utils
import numpy
arr = numpy.array([-1, -1, -2, -2])
assert rsgislib.tools.utils.negative(arr)
def test_is_odd_num_even():
import rsgislib.tools.utils
assert not rsgislib.tools.utils.is_odd(2)
def test_is_odd_num_odd():
import rsgislib.tools.utils
assert rsgislib.tools.utils.is_odd(5)
def test_is_odd_str():
import rsgislib.tools.utils
with pytest.raises(TypeError):
assert rsgislib.tools.utils.is_odd("5")
def test_remove_repeated_chars_two():
import rsgislib.tools.utils
in_str = "Hello__World"
ref_str = "Hello_World"
out_str = rsgislib.tools.utils.remove_repeated_chars(in_str, repeat_char="_")
assert ref_str == out_str
def test_remove_repeated_chars_three():
import rsgislib.tools.utils
in_str = "Hello___World"
ref_str = "Hello_World"
out_str = rsgislib.tools.utils.remove_repeated_chars(in_str, repeat_char="_")
assert ref_str == out_str
def test_remove_repeated_chars_four():
import rsgislib.tools.utils
in_str = "Hello____World"
ref_str = "Hello_World"
out_str = rsgislib.tools.utils.remove_repeated_chars(in_str, repeat_char="_")
assert ref_str == out_str
def test_check_str_nochng():
import rsgislib.tools.utils
in_str = "Hello_World"
ref_str = "Hello_World"
out_str = rsgislib.tools.utils.check_str(
in_str, rm_non_ascii=False, rm_dashs=False, rm_spaces=False, rm_punc=False
)
assert ref_str == out_str
def test_check_str_rmDashs():
import rsgislib.tools.utils
in_str = "Hello-World"
ref_str = "Hello_World"
out_str = rsgislib.tools.utils.check_str(
in_str, rm_non_ascii=False, rm_dashs=True, rm_spaces=False, rm_punc=False
)
assert ref_str == out_str
def test_check_str_rmSpaces():
import rsgislib.tools.utils
in_str = "Hello World"
ref_str = "Hello_World"
out_str = rsgislib.tools.utils.check_str(
in_str, rm_non_ascii=False, rm_dashs=False, rm_spaces=True, rm_punc=False
)
assert ref_str == out_str
def test_check_str_rmPunc():
import rsgislib.tools.utils
in_str = "Hello_World!"
ref_str = "Hello_World"
out_str = rsgislib.tools.utils.check_str(
in_str, rm_non_ascii=False, rm_dashs=False, rm_spaces=False, rm_punc=True
)
assert ref_str == out_str
def test_check_str_rmNonASCII():
import rsgislib.tools.utils
in_str = "Hello_World£"
ref_str = "Hello_World"
out_str = rsgislib.tools.utils.check_str(
in_str, rm_non_ascii=True, rm_dashs=False, rm_spaces=False, rm_punc=False
)
assert ref_str == out_str
def test_get_days_since():
import datetime
import rsgislib.tools.utils
base_date = datetime.date(2000, 1, 1)
year = 2000
day_of_year = 195
n_days = rsgislib.tools.utils.get_days_since(year, day_of_year, base_date)
assert n_days == 194
def test_get_days_since_date():
import datetime
import rsgislib.tools.utils
base_date = datetime.date(2000, 1, 1)
year = 2000
month = 4
day = 15
n_days = rsgislib.tools.utils.get_days_since_date(year, month, day, base_date)
assert n_days == 105
def test_encode_base64_text():
import rsgislib.tools.utils
input_str = "Hello World"
encoded_str = rsgislib.tools.utils.encode_base64_text(input_str)
ref_encoded_str = "SGVsbG8gV29ybGQ="
assert encoded_str == ref_encoded_str
def test_decode_base64_text():
import rsgislib.tools.utils
input_str = "SGVsbG8gV29ybGQ="
decoded_str = rsgislib.tools.utils.decode_base64_text(input_str)
ref_decoded_str = "Hello World"
assert decoded_str == ref_decoded_str
def test_get_username_password():
import rsgislib.tools.utils
input_file = os.path.join(TOOLS_UTILS_DATA_DIR, "user_pass_file.txt")
rtn_user, rtn_pass = rsgislib.tools.utils.get_username_password(input_file)
assert (rtn_user == "Hello1") and (rtn_pass == "World!")
def test_create_username_password_file(tmp_path):
import rsgislib.tools.utils
out_file = os.path.join(tmp_path, "userpass.txt")
username = "Hello"
password = "World"
rsgislib.tools.utils.create_username_password_file(username, password, out_file)
file_exists = os.path.exists(out_file)
if not file_exists:
assert False
else:
rtn_user, rtn_pass = rsgislib.tools.utils.get_username_password(out_file)
assert (rtn_user == username) and (rtn_pass == password)