/
test_main.py
2349 lines (2091 loc) · 88.1 KB
/
test_main.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import re
import shutil
import subprocess
import sys
import unittest
import warnings
from logging import StreamHandler
from flask_testing import TestCase
from wtforms.fields import core as wtfields
from wtforms import widgets as wtwidgets
from wtforms.validators import InputRequired, Optional
from t_utils import (
get_algorithms,
get_chains,
get_updated_chain,
get_chain_algs,
get_test_run_chain,
test_algorithm_form_data,
get_chain_builder_block_list,
get_chain_builder_additional_block,
get_chain_builder_block_scripts,
get_chain_builder_additional_script,
get_chain_builder_chain_blocks
)
os.environ['ATK_CONFIG'] = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 't_config.py')
updated_chain = get_updated_chain()
algs = get_algorithms()
this_path = os.path.dirname(os.path.abspath(__file__))
test_alg_path = os.path.join(this_path, 'test_project')
block_list = get_chain_builder_block_list()
block_scripts = get_chain_builder_block_scripts()
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
class ATKTestCase(TestCase):
def create_app(self):
from algorithm_toolkit import app
return app
def setUp(self):
sys.path.append(test_alg_path)
subprocess.call(['alg', 'cp', 'test_project', '-e', '-q'])
def tearDown(self):
# pass
shutil.rmtree(test_alg_path)
def test_config_vars(self):
print('Ensure environment is set up properly')
config = self.app.config
self.assertEqual(type(config['LOG_HANDLERS'][0]), StreamHandler)
self.assertEqual(
config['CORS_ORIGIN_WHITELIST'],
[
'http://localhost',
'https://mytiledriver.com',
'https://tdprocess.com'
]
)
self.assertEqual(config['ATK_PATH'], test_alg_path)
self.assertEqual(
self.app.jinja_loader.searchpath[0], test_alg_path + '/templates')
def test_atk_home(self):
print('Home page should display correctly')
chain_def = get_chains()
response = self.client.get('/')
self.assert200(response)
self.assertTemplateUsed('index.html')
self.assertContext('nav', 'index')
self.assertContext('docs', False)
self.assertContext('chains', chain_def)
def test_algorithm_home(self):
print('Algorithm list page should display correctly')
chain_def = get_chains()
response = self.client.get('/algorithms/')
self.assert200(response)
self.assertTemplateUsed('algorithms.html')
self.assertContext('nav', 'algorithms')
self.assertContext('docs', False)
self.assertContext('chains', chain_def)
self.assertContext('algs', sorted(algs, key=lambda a: a['name']))
def test_list_chains_no_key(self):
print('List chains should fail if the API key is missing')
response = self.client.get('/list_chains/')
self.assert401(response)
self.assertEqual(response.data, 'API key wrong or missing')
def test_list_chains(self):
print('List chains should ... list the chains')
chain_def = get_chains()
response = self.client.get('/list_chains/?api_key=testkey')
self.assert200(response)
self.assertEqual(response.data, json.dumps(
chain_def, separators=(',', ':'), sort_keys=True) + '\n')
def test_chain_info_no_key(self):
print('Chain info should fail if the API key is missing')
response = self.client.get('/chain_info/map_tiles/')
self.assert401(response)
self.assertEqual(response.data, 'API key wrong or missing')
def test_chain_info(self):
print('Chain info should ... provide chain info')
chain_def = get_chains()
response = self.client.get('/chain_info/map_tiles/?api_key=testkey')
self.assert200(response)
self.assertEqual(response.data, json.dumps(
chain_def['map_tiles'],
separators=(',', ':'),
sort_keys=True
) + '\n')
def test_chain_algs_no_key(self):
print('Chain algorithms should fail if the API key is missing')
response = self.client.get('/chain_algorithms/map_tiles/')
self.assert401(response)
self.assertEqual(response.data, 'API key wrong or missing')
def test_chain_algs(self):
print('Chain algorithms should list the algorithms in a chain')
chain_algs = get_chain_algs()
response = self.client.get(
'/chain_algorithms/map_tiles/?api_key=testkey')
self.assert200(response)
self.assertEqual(response.data, json.dumps(
chain_algs,
separators=(',', ':'),
sort_keys=True
) + '\n')
# add a required parameter and ensure it displays in chain_algorithms
r_param = {
"default_value": "",
"field_type": "text",
"display_name": "Useless Yet Required Parameter",
"name": "useless_required",
"data_type": "string",
"max_value": None,
"min_value": None,
"sort_order": 2,
"parameter_choices": [],
"help_text": "Don't bother entering anything, and yet you must",
"custom_validation": None,
"description": "A completely useless yet required parameter.",
"source": "user"
}
chain_algs[1]['required_parameters'].append(r_param)
json_file = os.path.join(
test_alg_path, 'algorithms', 'stitch_tiles', 'algorithm.json')
chains_file = os.path.join(test_alg_path, 'chains.json')
with open(json_file, 'r') as alg_file:
stitch_tiles = json.load(alg_file)
with open(json_file, 'w') as alg_file:
stitch_tiles['required_parameters'].append(r_param)
alg_file.write(json.dumps(stitch_tiles))
# new parameter is missing fron chains.json
# atk should assume this is a user parameter
response = self.client.get(
'/chain_algorithms/map_tiles/?api_key=testkey')
self.assert200(response)
# add param to chains.json
chain_def = get_chains()
chain_def['map_tiles'][1]['parameters']['useless_required'] = {
'source': 'user'
}
# print(chain_def)
with open(chains_file, 'w') as c_file:
c_file.write(json.dumps(chain_def))
# should now be included
response = self.client.get(
'/chain_algorithms/map_tiles/?api_key=testkey')
self.assert200(response)
self.assertEqual(response.data, json.dumps(
chain_algs,
separators=(',', ':'),
sort_keys=True
) + '\n')
# add an optional parameter and ensure it displays in chain_algorithms
o_param = {
"default_value": "",
"field_type": "text",
"display_name": "Useless Parameter",
"name": "useless",
"data_type": "string",
"max_value": None,
"min_value": None,
"sort_order": 2,
"parameter_choices": [],
"help_text": "Don't bother entering anything",
"custom_validation": None,
"description": "A completely useless parameter.",
"source": "user"
}
chain_algs[1]['optional_parameters'].append(o_param)
json_file = os.path.join(
test_alg_path, 'algorithms', 'stitch_tiles', 'algorithm.json')
chains_file = os.path.join(test_alg_path, 'chains.json')
with open(json_file, 'r') as alg_file:
stitch_tiles = json.load(alg_file)
with open(json_file, 'w') as alg_file:
stitch_tiles['optional_parameters'].append(o_param)
alg_file.write(json.dumps(stitch_tiles))
# new parameter is missing fron chains.json
# but should be fine
response = self.client.get(
'/chain_algorithms/map_tiles/?api_key=testkey')
self.assert200(response)
# add param to chains.json
chain_def['map_tiles'][1]['parameters']['useless'] = {
'source': 'user'
}
# print(chain_def)
with open(chains_file, 'w') as c_file:
c_file.write(json.dumps(chain_def))
# should now be included
response = self.client.get(
'/chain_algorithms/map_tiles/?api_key=testkey')
self.assert200(response)
self.assertEqual(response.data, json.dumps(
chain_algs,
separators=(',', ':'),
sort_keys=True
) + '\n')
def test_alg_info_no_key(self):
print('Algorithm info should fail if the API key is missing')
response = self.client.get('/algorithm_info/getmaptiles_roi/')
self.assert401(response)
self.assertEqual(response.data, 'API key wrong or missing')
def test_alg_info(self):
print('Algorithm info should ... provide algorithm info')
alg = [x for x in algs if x['name'] == 'getmaptiles_roi'][0]
response = self.client.get(
'/algorithm_info/getmaptiles_roi/?api_key=testkey')
self.assert200(response)
self.assertEqual(response.data, json.dumps(
alg,
separators=(',', ':'),
sort_keys=True
) + '\n')
def test_create_algorithms_get(self):
from algorithm_toolkit.forms import (
AlgorithmCreateForm,
AlgorithmOutputForm,
AlgorithmParameterForm
)
print('Create algorithm page should display correctly')
chain_def = get_chains()
response = self.client.get('/algorithms/create/')
form = self.get_context_variable('form')
o_form = self.get_context_variable('o_form')
p_form = self.get_context_variable('p_form')
self.assert200(response)
self.assertTemplateUsed('create_algorithm.html')
self.assertContext('nav', 'algorithms')
self.assertContext('docs', False)
self.assertContext('chains', chain_def)
self.assertContext('algs', sorted([x['name'] for x in algs]))
self.assertEqual(type(form), AlgorithmCreateForm)
self.assertEqual(type(o_form), AlgorithmOutputForm)
self.assertEqual(type(p_form), AlgorithmParameterForm)
# test without the private flag
json_file = os.path.join(
test_alg_path, 'algorithms', 'stitch_tiles', 'algorithm.json')
with open(json_file, 'r') as alg_file:
stitch_tiles = json.load(alg_file)
with open(json_file, 'w') as alg_file:
del stitch_tiles['private']
alg_file.write(json.dumps(stitch_tiles))
response = self.client.get('/algorithms/create/stitch_tiles/')
self.assert200(response)
self.assertContext(
'algs',
sorted([x['name'] for x in algs if x['name'] != 'stitch_tiles'])
)
def test_create_algorithms_post(self):
print('Create algorithm page should create an algorithm correctly')
data = test_algorithm_form_data()
data['parameters'] = json.dumps(
data['parameters'], separators=(', ', ': '), sort_keys=True)
data['outputs'] = json.dumps(
data['outputs'], separators=(', ', ': '), sort_keys=True)
data['api_key'] = 'testkey'
new_alg_path = os.path.join(
test_alg_path, 'algorithms', 'add_numbers')
response = self.client.post(
'/algorithms/create/', data=data, follow_redirects=True)
self.assert200(response)
self.assertTrue(os.path.exists(new_alg_path))
response = self.client.get('/algorithms/create/add_numbers/')
form = self.get_context_variable('form')
self.assertEqual(form.name.data, 'add_numbers')
self.assertEqual(
form.display_name.data,
'Add two numbers'
)
self.assertEqual(form.license.data, 'Proprietary')
self.assertEqual(form.private.data, True)
self.assertEqual(form.version.data, '0.0.1')
self.assertEqual(
form.homepage.data, 'google.com')
self.assertEqual(
form.description.data,
'Add two numbers together to get a result.'
)
self.assertEqual(
json.loads(form.parameters.data), json.loads(data['parameters']))
self.assertEqual(
json.loads(form.outputs.data), json.loads(data['outputs']))
def test_edit_algorithms_get(self):
from algorithm_toolkit.forms import (
AlgorithmCreateForm,
AlgorithmOutputForm,
AlgorithmParameterForm
)
print('Edit algorithm page should display correctly')
def add_required(p, r):
p['required'] = r
return p
chain_def = get_chains()
this_alg = [x for x in algs if x['name'] == 'getmaptiles_roi'][0]
r_params = [add_required(
x, True) for x in this_alg['required_parameters']]
o_params = [add_required(
x, False) for x in this_alg['optional_parameters']]
params = r_params + o_params
response = self.client.get('/algorithms/create/getmaptiles_roi/')
form = self.get_context_variable('form')
o_form = self.get_context_variable('o_form')
p_form = self.get_context_variable('p_form')
self.assert200(response)
self.assertTemplateUsed('create_algorithm.html')
self.assertContext('nav', 'algorithms')
self.assertContext('docs', False)
self.assertContext('chains', chain_def)
self.assertContext(
'algs',
sorted([x['name'] for x in algs if x['name'] != 'getmaptiles_roi'])
)
self.assertEqual(type(form), AlgorithmCreateForm)
self.assertEqual(type(o_form), AlgorithmOutputForm)
self.assertEqual(type(p_form), AlgorithmParameterForm)
self.assertEqual(form.name.data, 'getmaptiles_roi')
self.assertEqual(form.display_name.data, 'Get Map Tiles In ROI')
self.assertEqual(form.license.data, 'MIT')
self.assertEqual(form.private.data, False)
self.assertEqual(form.version.data, '0.0.1')
self.assertEqual(
form.homepage.data, 'https://tiledriver.com/developer')
self.assertEqual(
form.description.data,
'This algorithm will gather up map tiles at a given zoom level '
'that intersect with the provided polygon. The source is the '
'national map provided by USGS. All tiles will be written out '
'to disk at a specified location. This location is also saved '
'onto the chain ledger.'
)
temp_params = json.loads(form.parameters.data)
for p in temp_params:
p.pop('original_name', None)
temp_outs = json.loads(form.outputs.data)
for out in temp_outs:
out.pop('original_name', None)
self.assertEqual(temp_params, params)
self.assertEqual(temp_outs, this_alg['outputs'])
def test_edit_algorithms_post(self):
print('Edit algorithm page should modify an algorithm correctly')
# get initial algorithm data
response = self.client.get('/algorithms/create/getmaptiles_roi/')
form = self.get_context_variable('form')
this_alg = form.data
# modify it
this_alg['display_name'] = 'Do something crazy with map tiles'
this_alg['license'] = 'GNU LGPLv3'
this_alg['private'] = True
this_alg['version'] = '0.0.2'
this_alg['homepage'] = 'google.com'
this_alg['description'] = 'Do some stuff, I dunno.'
this_alg['api_key'] = 'testkey'
new_license_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi', 'LICENSE')
# should see new data reflected in the algorithm
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
with open(new_license_path, 'r') as lic_file:
lic = lic_file.read()
self.assertTrue('GNU LESSER GENERAL PUBLIC LICENSE' in lic)
self.assertEqual(new_form.name.data, 'getmaptiles_roi')
self.assertEqual(
new_form.display_name.data, 'Do something crazy with map tiles')
self.assertEqual(new_form.license.data, 'GNU LGPLv3')
self.assertEqual(new_form.private.data, True)
self.assertEqual(new_form.version.data, '0.0.2')
self.assertEqual(new_form.homepage.data, 'google.com')
self.assertEqual(
new_form.description.data, 'Do some stuff, I dunno.')
def test_change_algorithm_name(self):
print('A user should be able to change the name of an algorithm')
# get initial algorithm data
response = self.client.get('/algorithms/create/getmaptiles_roi/')
form = self.get_context_variable('form')
this_alg = form.data
# modify it
this_alg['name'] = 'thingie_splunge'
new_alg_path = os.path.join(
test_alg_path, 'algorithms', 'thingie_splunge')
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/thingie_splunge/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.name.data, 'thingie_splunge')
self.assertTrue(os.path.exists(new_alg_path))
def test_edit_algorithm_not_found(self):
print(
'A user trying to edit a non-existing algorithm should get a 404')
# get initial algorithm data
response = self.client.get('/algorithms/create/thingie_splunge/')
self.assert404(response)
self.assertEqual(response.data, 'Algorithm not found')
def test_edit_algorithm_no_private_field(self):
print(
'An algorithm should still be saved if the private flag is missing'
)
# get initial algorithm data
response = self.client.get('/algorithms/create/getmaptiles_roi/')
form = self.get_context_variable('form')
this_alg = form.data
# modify it
del this_alg['private']
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.private.data, False)
def test_edit_algorithm_change_license(self):
print('A user should be able to change the license of an algorithm')
# get initial algorithm data
response = self.client.get('/algorithms/create/getmaptiles_roi/')
form = self.get_context_variable('form')
this_alg = form.data
# modify it to AGPL
this_alg['license'] = 'GNU AGPLv3'
new_license_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi', 'LICENSE')
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.license.data, 'GNU AGPLv3')
with open(new_license_path, 'r') as lic_file:
lic = lic_file.read()
self.assertTrue('GNU AFFERO GENERAL PUBLIC LICENSE' in lic)
# modify it to GPL
this_alg['license'] = 'GNU GPLv3'
new_license_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi', 'LICENSE')
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.license.data, 'GNU GPLv3')
with open(new_license_path, 'r') as lic_file:
lic = lic_file.read()
self.assertTrue('GNU GENERAL PUBLIC LICENSE' in lic)
# modify it to Unlicense
this_alg['license'] = 'The Unlicense'
new_license_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi', 'LICENSE')
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.license.data, 'The Unlicense')
with open(new_license_path, 'r') as lic_file:
lic = lic_file.read()
self.assertTrue('http://unlicense.org' in lic)
# modify it to Apache
this_alg['license'] = 'Apache'
new_license_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi', 'LICENSE')
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.license.data, 'Apache')
with open(new_license_path, 'r') as lic_file:
lic = lic_file.read()
self.assertTrue('http://www.apache.org/licenses/' in lic)
# modify it to Mozilla
this_alg['license'] = 'Mozilla'
new_license_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi', 'LICENSE')
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.license.data, 'Mozilla')
with open(new_license_path, 'r') as lic_file:
lic = lic_file.read()
self.assertTrue('Mozilla Public License Version 2.0' in lic)
# modify it to MIT
this_alg['license'] = 'MIT'
new_license_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi', 'LICENSE')
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.license.data, 'MIT')
with open(new_license_path, 'r') as lic_file:
lic = lic_file.read()
self.assertTrue('MIT License' in lic)
# modify it to Propietary
this_alg['license'] = 'Proprietary'
new_license_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi', 'LICENSE')
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.license.data, 'Proprietary')
with open(new_license_path, 'r') as lic_file:
lic = lic_file.read()
self.assertTrue(lic == '')
# modify it to Other
this_alg['license'] = 'Other'
new_license_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi', 'LICENSE')
response = self.client.post(
'/algorithms/create/getmaptiles_roi/',
data=this_alg,
follow_redirects=True
)
self.assert200(response)
response = self.client.get('/algorithms/create/getmaptiles_roi/')
self.assert200(response)
new_form = self.get_context_variable('form')
self.assertEqual(new_form.license.data, 'Other')
with open(new_license_path, 'r') as lic_file:
lic = lic_file.read()
self.assertTrue(lic == '')
def test_copy_algorithms(self):
print('Users should be able to copy an algorithm')
def add_required(p, r):
p['required'] = r
return p
# make a copy
response = self.client.get(
'/algorithm/copy/getmaptiles_roi/', follow_redirects=True)
self.assert200(response)
# ensure we're redirected to algorithms page
self.assertTemplateUsed('algorithms.html')
new_alg_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi_copy')
self.assertTrue(os.path.exists(new_alg_path))
# ensure copy has the right data
this_alg = [x for x in algs if x['name'] == 'getmaptiles_roi'][0]
r_params = [add_required(
x, True) for x in this_alg['required_parameters']]
o_params = [add_required(
x, False) for x in this_alg['optional_parameters']]
params = r_params + o_params
response = self.client.get('/algorithms/create/getmaptiles_roi_copy/')
form = self.get_context_variable('form')
self.assert200(response)
self.assertEqual(form.name.data, 'getmaptiles_roi_copy')
self.assertEqual(form.display_name.data, 'Get Map Tiles In ROI')
self.assertEqual(form.license.data, 'MIT')
self.assertEqual(form.private.data, False)
self.assertEqual(form.version.data, '0.0.1')
self.assertEqual(
form.homepage.data, 'https://tiledriver.com/developer')
self.assertEqual(
form.description.data,
'This algorithm will gather up map tiles at a given zoom level '
'that intersect with the provided polygon. The source is the '
'national map provided by USGS. All tiles will be written out '
'to disk at a specified location. This location is also saved '
'onto the chain ledger.'
)
temp_params = json.loads(form.parameters.data)
for p in temp_params:
p.pop('original_name', None)
temp_outs = json.loads(form.outputs.data)
for out in temp_outs:
out.pop('original_name', None)
self.assertEqual(temp_params, params)
self.assertEqual(temp_outs, this_alg['outputs'])
# make a second copy
response = self.client.get(
'/algorithm/copy/getmaptiles_roi/', follow_redirects=True)
self.assert200(response)
new_alg_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi_copy1')
self.assertTrue(os.path.exists(new_alg_path))
def test_delete_algorithms(self):
print('Users should be able to delete an algorithm')
# set working directory
os.chdir(test_alg_path)
# delete the algorithm
response = self.client.get(
'/algorithm/delete/getmaptiles_roi/', follow_redirects=True)
self.assert200(response)
# ensure we're redirected to algorithms page
self.assertTemplateUsed('algorithms.html')
new_alg_path = os.path.join(
test_alg_path, 'algorithms', 'getmaptiles_roi')
self.assertFalse(os.path.exists(new_alg_path))
# ensure references are removed from chains.json
chain_path = os.path.join(test_alg_path, 'chains.json')
with open(chain_path, 'r') as chain_file:
chains = chain_file.read()
self.assertFalse('getmaptiles_roi' in chains)
# reset working directory
os.chdir(this_path)
def test_chain_builder(self):
print('Chain builder page should display correctly')
chain_def = get_chains()
additional_block = get_chain_builder_additional_block()
additional_script = get_chain_builder_additional_script()
response = self.client.get('/chain_builder/')
blocks = self.get_context_variable('block_list')
scripts = self.get_context_variable('block_scripts')
self.assert200(response)
self.assertTemplateUsed('chain_builder.html')
self.assertContext('nav', 'chain_builder')
self.assertContext('docs', False)
self.assertContext('chains', chain_def)
for idx, block in enumerate(blocks):
temp_block = re.sub(r'colour="(\d+)"', 'colour="222"', block)
self.assertEqual(temp_block, block_list[idx])
for idx, script in enumerate(scripts):
temp_script = re.sub(
r'setColour\((\d+)\)', 'setColour(222)', script)
self.assertEqual(temp_script, block_scripts[idx])
self.assertContext('chain_obj', json.dumps(chain_def))
# add additional algorithm
data = test_algorithm_form_data()
data['name'] = 'beamio/add_numbers'
data['parameters'] = json.dumps(
data['parameters'], separators=(', ', ': '), sort_keys=True)
data['outputs'] = json.dumps(
data['outputs'], separators=(', ', ': '), sort_keys=True)
data['api_key'] = 'testkey'
response = self.client.post(
'/algorithms/create/', data=data, follow_redirects=True)
response = self.client.get('/chain_builder/')
blocks = self.get_context_variable('block_list')
scripts = self.get_context_variable('block_scripts')
for idx, block in enumerate(blocks):
temp_block = re.sub(r'colour="(\d+)"', 'colour="222"', block)
self.assertEqual(temp_block, additional_block[idx])
for idx, script in enumerate(scripts):
temp_script = re.sub(
r'setColour\((\d+)\)', 'setColour(222)', script)
self.assertEqual(temp_script, additional_script[idx])
# test removing parameters
json_file = os.path.join(
test_alg_path,
'algorithms',
'beamio',
'add_numbers',
'algorithm.json'
)
with open(json_file, 'r') as alg_file:
add_numbers = json.load(alg_file)
with open(json_file, 'w') as alg_file:
add_numbers['required_parameters'] = []
alg_file.write(json.dumps(add_numbers))
response = self.client.get('/chain_builder/')
temp_script = self.get_context_variable('block_scripts')[3]
self.assert200(response)
self.assertFalse('Required Input Fields:' in temp_script)
self.assertTrue('Optional Input Fields:' in temp_script)
with open(json_file, 'w') as alg_file:
del add_numbers['required_parameters']
del add_numbers['optional_parameters']
alg_file.write(json.dumps(add_numbers))
response = self.client.get('/chain_builder/')
temp_script = self.get_context_variable('block_scripts')[3]
self.assert200(response)
self.assertFalse('Required Input Fields:' in temp_script)
self.assertFalse('Optional Input Fields:' in temp_script)
def test_chain_builder_get_blocks(self):
print(
'When a user requests a chain in chain builder, '
'the chain blocks should display correctly.'
)
chain_blocks = get_chain_builder_chain_blocks()
response = self.client.get('/chain_builder/get_blocks/map_tiles/')
temp_blocks = re.sub(
r'<block type="(\w+)" id="\w+"',
r'<block type="\1" id="testid"',
response.data
)
self.assert200(response)
self.assertEqual(temp_blocks, chain_blocks)
# add an optional parameter
o_param = {
"default_value": "",
"field_type": "text",
"display_name": "Useless Parameter",
"name": "useless",
"data_type": "string",
"max_value": None,
"min_value": None,
"sort_order": 2,
"parameter_choices": [],
"help_text": "Don't bother entering anything",
"custom_validation": None,
"description": "A completely useless parameter.",
}
json_file = os.path.join(
test_alg_path, 'algorithms', 'stitch_tiles', 'algorithm.json')
chains_file = os.path.join(test_alg_path, 'chains.json')
with open(json_file, 'r') as alg_file:
stitch_tiles = json.load(alg_file)
with open(json_file, 'w') as alg_file:
stitch_tiles['optional_parameters'].append(o_param)
alg_file.write(json.dumps(stitch_tiles))
# add param to chains.json
chain_def = get_chains()
chain_def['map_tiles'][1]['parameters']['useless'] = {
'source': 'user'
}
with open(chains_file, 'w') as c_file:
c_file.write(json.dumps(chain_def))
new_block = (
'<value name="useless">\n'
'<block type="user_input" id="testid">\n'
'<data>input</data>\n'
)
response = self.client.get('/chain_builder/get_blocks/map_tiles/')
temp_blocks = re.sub(
r'<block type="(\w+)" id="\w+"',
r'<block type="\1" id="testid"',
response.data
)
self.assert200(response)
self.assertTrue(new_block in temp_blocks)
def test_update_chains_error(self):
print(
'If update_chains is called without a chains '
'value, atk should throw an error'
)
response = self.client.post('/chain_builder/update_chains/', data={})
self.assert400(response)
self.assertEqual(response.data, 'Missing chain definitions')
def test_update_chains(self):
print('Update chains should ... update the chains')
chain_list = '<a class="dropdown-item" href="'
chain_list += '/test_run/map_tiles/">map_tiles</a>'
chain_list += '<a class="dropdown-item" href="'
chain_list += '/test_run/do_some_math/">do_some_math</a>'
new_chain_path = os.path.join(test_alg_path, 'chains.json')
response = self.client.post(
'/chain_builder/update_chains/',
data={'chains': json.dumps(updated_chain)}
)
with open(new_chain_path) as new_chain_file:
new_chains = new_chain_file.read()
self.assert200(response)
self.assertEqual(json.loads(new_chains), updated_chain)
self.assertEqual(response.data, chain_list)
def test_main_errors(self):
print(
'If main is called without proper request '
'values, atk should throw an error'
)
# no api key
response = self.client.post('/main/', data={})
self.assert401(response)
self.assertEqual(response.data, 'API key wrong or missing')
# no chain parameter
response = self.client.post('/main/', data={'api_key': 'testkey'})
self.assert400(response)
self.assertEqual(response.data, 'Missing chain parameter in request')
# chain parameter is not json serializable
response = self.client.post(
'/main/', data={'chain': 'splunge', 'api_key': 'testkey'})
self.assert400(response)
self.assertEqual(
response.data, 'Chain parameter not properly formatted')
# no key for chain name in chain parameter
response = self.client.post(
'/main/', data={
'chain': '{"thingie": "splunge"}', 'api_key': 'testkey'})
self.assert400(response)
self.assertEqual(response.data, 'Chain name not defined')
# no key for algorithms
response = self.client.post(
'/main/', data={
'chain': '{"chain_name": "splunge"}', 'api_key': 'testkey'})
self.assert400(response)
self.assertEqual(response.data, 'Algorithms not defined')
# chain name does not exist
response = self.client.post(
'/main/', data={
'chain': '{"chain_name": "splunge", "algorithms": []}',
'api_key': 'testkey'
})
self.assert404(response)
self.assertEqual(response.data, 'Chain name not found')
# non-existent algorithm name
test_run_chain = get_test_run_chain()
test_run_chain['algorithms'][0]['parameters']['zoom'] = 10
extra_alg = {
"name": "thingie",
"parameters": {}
}
test_run_chain['algorithms'].append(extra_alg)
data = {
'api_key': 'testkey',
'chain': json.dumps(test_run_chain)
}
response = self.client.post('/main/', data=data)
self.assert400(response)
self.assertEqual(
json.loads(response.data),
{
"error_list": {
"error": "Algorithm not found",
"parameter": ""
},
"message": "Error in parameters",
"output_type": "error"
}
)
def test_main_run(self):
print(
'Calling the main endpoint should execute a chain'
)
test_run_chain = get_test_run_chain()
test_run_chain['algorithms'][0]['parameters']['zoom'] = 10
retval = {
"output_type": "geo_raster",
"output_value": {
"extent":
"[[39.095962936305476, -77.34374999999999], "
"[38.54816542304656, -76.640625]]"
}
}
data = {
'api_key': 'testkey',
'chain': json.dumps(test_run_chain)
}
response = self.client.post('/main/', data=data)
self.assert200(response)
resp_json = json.loads(response.data)
self.assertEqual(resp_json['output_type'], retval['output_type'])
self.assertEqual(
resp_json['output_value']['extent'],
retval['output_value']['extent'])
self.assertTrue('raster' in resp_json['output_value'])
def test_main_run_no_chain_output(self):
print(