-
Notifications
You must be signed in to change notification settings - Fork 33
/
run_tests.py
542 lines (478 loc) · 22.3 KB
/
run_tests.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
import os
import sys
import kintree.config.settings as settings
from kintree.common.tools import cprint, create_library, download_with_retry
from kintree.config import config_interface
from kintree.database import inventree_api, inventree_interface
from kintree.kicad import kicad_interface
from kintree.search import (
digikey_api,
mouser_api,
element14_api,
lcsc_api,
tme_api,
snapeda_api,
automationdirect_api,
jameco_api,
)
from kintree.setup_inventree import setup_inventree
# SETTINGS
# Enable API tests
try:
ENABLE_API = int(sys.argv[1])
except IndexError:
ENABLE_API = 0
# Enable InvenTree tests
ENABLE_INVENTREE = True
# Enable KiCad tests
ENABLE_KICAD = True
# Set categories to test
PART_CATEGORIES = [
'Capacitors',
'Circuit Protections',
'Connectors',
'Crystals and Oscillators',
'Diodes',
'Inductors',
'Integrated Circuits',
'Mechanicals',
'Power Management',
'Resistors',
'RF',
'Transistors',
]
# Enable tests on extra methods
ENABLE_TEST_METHODS = True
###
# Pretty test printing
def pretty_test_print(message: str):
cprint(message.ljust(65), end='')
# Check result
def check_result(status: str, new_part: bool) -> bool:
# Build result
success = False
if (status == 'original') or (status == 'fake_alternate'):
if new_part:
success = True
elif status == 'alternate_mpn':
if not new_part:
success = True
else:
pass
return success
# --- SETUP ---
# Enable test mode
settings.enable_test_mode()
# Enable InvenTree and KiCad
settings.set_enable_flag('inventree', True)
settings.set_enable_flag('alternate', False)
settings.set_enable_flag('kicad', True)
# Load user configuration files
settings.load_user_config()
# Set path to test libraries
test_library_path = os.path.join(settings.PROJECT_DIR, 'tests', 'TEST.kicad_sym')
symbol_libraries_test_path = os.path.join(settings.PROJECT_DIR, 'tests', 'files', 'SYMBOLS')
footprint_libraries_test_path = os.path.join(settings.PROJECT_DIR, 'tests', 'files', 'FOOTPRINTS', '')
if ENABLE_API:
# Disable Digi-Key API logging
digikey_api.disable_api_logger()
# Test Digi-Key API
if 'Digi-Key' in settings.SUPPORTED_SUPPLIERS_API:
pretty_test_print('[MAIN]\tDigi-Key API Test')
if not digikey_api.test_api(check_content=True):
cprint('[ FAIL ]')
cprint('[INFO]\tFailed to get Digi-Key API token, aborting.')
sys.exit(-1)
else:
cprint('[ PASS ]')
# Test Mouser API
if 'Mouser' in settings.SUPPORTED_SUPPLIERS_API:
pretty_test_print('[MAIN]\tMouser API Test')
if not mouser_api.test_api():
cprint('[ FAIL ]')
sys.exit(-1)
else:
cprint('[ PASS ]')
# Test Element14 API (with retry, to avoid the few false positives)
if 'Element14' in settings.SUPPORTED_SUPPLIERS_API:
for i in range(2):
pretty_test_print('[MAIN]\tElement14 API Test')
if not element14_api.test_api() or not element14_api.test_api(store_url='www.newark.com'):
cprint('[ FAIL ]')
else:
cprint('[ PASS ]')
break
sys.exit(-1)
# Test LCSC API
if 'LCSC' in settings.SUPPORTED_SUPPLIERS_API:
pretty_test_print('[MAIN]\tLCSC API Test')
if not lcsc_api.test_api():
cprint('[ FAIL ]')
sys.exit(-1)
else:
cprint('[ PASS ]')
# Test TME API
if 'TME' in settings.SUPPORTED_SUPPLIERS_API:
pretty_test_print('[MAIN]\tTME API Test')
if not tme_api.test_api():
cprint('[ FAIL ]')
sys.exit(-1)
else:
cprint('[ PASS ]')
# Test AutomationDirect API
if 'AutomationDirect' in settings.SUPPORTED_SUPPLIERS_API:
pretty_test_print('[MAIN]\tAutomationDirect API Test')
if not automationdirect_api.test_api():
cprint('[ FAIL ]')
sys.exit(-1)
else:
cprint('[ PASS ]')
# Test Jameco API
if 'Jameco' in settings.SUPPORTED_SUPPLIERS_API:
pretty_test_print('[MAIN]\tJameco API Test')
if not jameco_api.test_api():
cprint('[ FAIL ]')
sys.exit(-1)
else:
cprint('[ PASS ]')
# Test SnapEDA API methods
pretty_test_print('[MAIN]\tSnapEDA API Test')
if not snapeda_api.test_snapeda_api():
cprint('[ FAIL ]')
sys.exit(-1)
else:
cprint('[ PASS ]')
cprint('\n-----')
# Setup InvenTree
setup_inventree()
cprint('\n-----')
# Load test samples
samples = config_interface.load_file(os.path.abspath(
os.path.join('tests', 'test_samples.yaml')))
PART_TEST_SAMPLES = {}
for category in PART_CATEGORIES:
PART_TEST_SAMPLES.update({category: samples[category]})
# Store results
exit_code = 0
kicad_results = {}
inventree_results = {}
# --- TESTS ---
if __name__ == '__main__':
if settings.ENABLE_TEST:
if ENABLE_INVENTREE:
pretty_test_print('\n[MAIN]\tConnecting to Inventree')
inventree_connect = inventree_interface.connect_to_server()
if inventree_connect:
cprint('[ PASS ]')
else:
cprint('[ FAIL ]')
sys.exit(-1)
if ENABLE_KICAD or ENABLE_INVENTREE:
for category in PART_TEST_SAMPLES.keys():
cprint(f'\n[MAIN]\tCategory: {category.upper()}')
# For last category, combine creation of KiCad and InvenTree parts
last_category = False
if ENABLE_KICAD and ENABLE_INVENTREE and category == list(PART_TEST_SAMPLES.keys())[-1]:
last_category = True
for number, status in PART_TEST_SAMPLES[category].items():
kicad_result = False
inventree_result = False
# Fetch supplier data
supplier_info = inventree_interface.supplier_search(
supplier='Digi-Key',
part_number=number,
test_mode=True,
)
# Translate to form
part_info = inventree_interface.translate_supplier_to_form(
supplier='Digi-Key',
part_info=supplier_info,
)
# Stitch categories and parameters
part_info.update({
'category_tree': [supplier_info['category'], supplier_info['subcategory']],
'parameters': supplier_info['parameters'],
'Symbol': f'{category}:{number}',
'IPN': supplier_info['manufacturer_part_number'],
})
# Update categories
part_info['category_tree'] = inventree_interface.get_categories_from_supplier_data(part_info)
# Needed for tests
part_info['Template'] = part_info['category_tree']
# Display part to be tested
pretty_test_print(f'[INFO]\tChecking "{number}" ({status})')
if ENABLE_INVENTREE:
# Adding part information to InvenTree
categories = [None, None]
new_part = False
part_pk = 0
part_data = {}
# Create part in InvenTree
new_part, part_pk, part_data = inventree_interface.inventree_create(
part_info=part_info,
kicad=last_category,
symbol=part_info['Symbol'],
show_progress=False,
enable_upload=True if number == 'BSS84-7-F' else False,
)
inventree_result = check_result(status, new_part)
pk_list = [data[0] for data in inventree_results.values()]
if part_pk != 0 and part_pk not in pk_list:
delete = True
else:
delete = False
# Log results
inventree_results.update({number: [part_pk, inventree_result, delete]})
if ENABLE_KICAD:
if settings.AUTO_GENERATE_LIB:
create_library(
os.path.dirname(test_library_path),
'TEST',
settings.symbol_template_lib,
)
kicad_result, kicad_new_part, kicad_part_name = kicad_interface.inventree_to_kicad(
part_data=part_info,
library_path=test_library_path,
show_progress=False,
)
# Log result
if number not in kicad_results.keys():
kicad_results.update({number: kicad_result})
# Combine KiCad and InvenTree for less verbose
result = False
if ENABLE_KICAD and ENABLE_INVENTREE:
result = kicad_result and inventree_result
else:
result = kicad_result or inventree_result
# Print live results
if result:
cprint('[ PASS ]')
else:
cprint('[ FAIL ]')
exit_code = -1
if ENABLE_KICAD:
cprint(f'[DBUG]\tkicad_result = {kicad_result}')
cprint(f'[DBUG]\tkicad_new_part = {kicad_new_part}')
cprint(f'[DBUG]\tkicad_part_name = {kicad_part_name}')
if ENABLE_INVENTREE:
cprint(f'[DBUG]\tinventree_result = {inventree_result}')
cprint(f'[DBUG]\tnew_part = {new_part}')
cprint(f'[DBUG]\tpart_pk = {part_pk}')
# Disable datasheet download/upload after first part (to speed up testing)
# settings.DATASHEET_UPLOAD = False
if ENABLE_TEST_METHODS:
methods = [
'Fuzzy category matching',
'Custom parts form',
'Digi-Key search missing part number',
'Load KiCad library paths',
'Add symbol library to user file',
'Add footprint library to user file',
'Add supplier category',
'Sync InvenTree and supplier categories',
'Download image/PDF method',
'Get category parameters',
'Add valid alternate supplier part using part ID',
'Add invalid alternate supplier part using part IPN',
'Save InvenTree settings',
'Load configuration files',
'Build InvenTree category tree (file, db and branch)',
]
method_success = True
# Line return
cprint('')
cprint('[MAIN]\tChecking untested methods'.ljust(65))
for method_idx, method_name in enumerate(methods):
pretty_test_print(method_name)
if method_idx == 0:
# Fuzzy category matching
part_info = {
'category_tree': ['Capacitors', 'Super',],
}
categories = tuple(inventree_interface.get_categories_from_supplier_data(part_info))
if not (categories[0] and categories[1]):
method_success = False
elif method_idx == 1:
# Custom part form
try:
inventree_interface.translate_form_to_inventree(part_info, categories)
# If the above function does not fail, it's a problem
method_success = False
except KeyError:
pass
part_info = {
'name': 'part_name',
'description': 'part_desc',
'revision': 'part_rev',
'keywords': 'part_key',
'supplier_name': 'part_supplier',
'supplier_part_number': 'part_sku',
'supplier_link': 'part_link',
'manufacturer_name': 'part_man',
'manufacturer_part_number': 'part_mpn',
'datasheet': 'part_data',
'image': 'part_image',
'IPN': 'part_mpn',
}
if not inventree_interface.translate_form_to_inventree(part_info, categories, is_custom=True):
method_success = False
elif method_idx == 2:
# Digi-Key search missing part number
search = inventree_interface.supplier_search(supplier='Digi-Key', part_number='')
if search:
method_success = False
elif method_idx == 3:
# Load KiCad library paths
config_interface.load_library_path(settings.KICAD_CONFIG_PATHS, silent=True)
symbol_libraries_paths = config_interface.load_libraries_paths(settings.KICAD_CONFIG_CATEGORY_MAP, symbol_libraries_test_path)
footprint_libraries_paths = config_interface.load_footprint_paths(settings.KICAD_CONFIG_CATEGORY_MAP, footprint_libraries_test_path)
if not (symbol_libraries_paths and footprint_libraries_paths):
method_success = False
elif method_idx == 4:
# Add symbol library to user file
add_symbol_lib = config_interface.add_library_path(user_config_path=settings.KICAD_CONFIG_CATEGORY_MAP,
category='category_test',
symbol_library='symbol_library_test')
if not add_symbol_lib:
method_success = False
elif method_idx == 5:
# Add footprint library to user file
add_footprint_lib = config_interface.add_footprint_library(user_config_path=settings.KICAD_CONFIG_CATEGORY_MAP,
category='category_test',
library_folder='footprint_folder_test')
if not add_footprint_lib:
method_success = False
elif method_idx == 6:
# Add supplier category
categories = {
'Capacitors':
{'Super': 'Super'}
}
add_category = config_interface.add_supplier_category(categories, settings.CONFIG_DIGIKEY_CATEGORIES)
if not add_category:
method_success = False
elif method_idx == 7:
# Sync InvenTree and Supplier categories
sync_categories = config_interface.sync_inventree_supplier_categories(inventree_config_path=settings.CONFIG_CATEGORIES,
supplier_config_path=settings.CONFIG_DIGIKEY_CATEGORIES)
if not sync_categories:
method_success = False
elif method_idx == 8:
test_image_urllib = 'https://media.digikey.com/Renders/Diodes%20Renders/31;%20SOD-123;%20;%202.jpg'
test_image_requestslib = 'https://www.newark.com/productimages/standard/en_GB/GE2SOD12307-40.jpg'
test_pdf_urllib = 'https://www.seielect.com/Catalog/SEI-CF_CFM.pdf'
# Test different download methods for images
if not download_with_retry(test_image_urllib, './image1.jpg', silent=True, filetype='Image'):
print(' [1] ')
method_success = False
if not download_with_retry(test_image_requestslib, './image2.jpg', silent=True, filetype='Image'):
print(' [2] ')
method_success = False
# Test PDF
if not download_with_retry(test_pdf_urllib, './datasheet.pdf', silent=True, filetype='PDF'):
print(' [3] ')
method_success = False
# Wrong folder
if download_with_retry(test_pdf_urllib, './myfolder/datasheet.pdf', silent=True, filetype='PDF'):
print(' [4] ')
method_success = False
# Test erroneous URL
if download_with_retry('http', '', silent=True):
print(' [5] ')
method_success = False
# Test empty URL
if download_with_retry('', '', silent=True):
print(' [6] ')
method_success = False
elif method_idx == 9:
# Test InvenTree category parameters
if inventree_api.get_category_parameters(1):
method_success = False
elif method_idx == 10:
# Test manufacturer and supplier alternates using Part ID
part_info = {
"datasheet": "https://search.murata.co.jp/Ceramy/image/img/A01X/G101/ENG/GRM155R71C104KA88-01.pdf",
"manufacturer_name": "Murata Electronics",
"manufacturer_part_number": "GRM155R71C104KA88D",
"supplier_link": "https://www.digikey.com/en/products/detail/murata-electronics/GRM155R71C104KA88D/675947",
"supplier_name": "Digi-Key",
"supplier_part_number": "490-3261-1-ND",
"name": "",
"description": "",
"revision": "",
"keywords": "",
"IPN": "",
"image": "",
"parameters": {},
"pricing": {},
}
if not inventree_interface.inventree_create_alternate(part_info=part_info,
part_id='1',
show_progress=False, ):
method_success = False
elif method_idx == 11:
# Test manufacturer and supplier alternates using Part IPN
if inventree_interface.inventree_create_alternate(part_info=part_info,
part_ipn='CAP-000001-00',
show_progress=False, ):
method_success = False
elif method_idx == 12:
# Save InvenTree settings
if not config_interface.save_inventree_user_settings(
enable=True,
server='http://127.0.0.1:8000',
username='admin',
password='admin',
enable_proxy=False,
proxies={},
datasheet_upload=True,
user_config_path=settings.INVENTREE_CONFIG,
pricing_upload=True,
):
method_success = False
elif method_idx == 13:
# Select one configuration file
element14_config = os.path.join(
settings.USER_SETTINGS['USER_FILES'],
'element14_config.yaml',
)
# Delete the user configuration file
os.remove(element14_config)
# Try to load this file
if config_interface.load_file(element14_config):
method_success = False
if method_success:
# Load user configuration files
if not settings.load_user_config():
method_success = False
if method_success:
# Load configuration files with incorrect paths
if config_interface.load_user_config_files('', ''):
method_success = False
elif method_idx == 14:
# Reload categories from file
cat_from_file = inventree_interface.build_category_tree(reload=False)
if isinstance(cat_from_file, type(list)):
print(f'{type(cat_from_file)} != list')
method_success = False
if method_success:
# Reload categories from InvenTree database
cat_from_db = inventree_interface.build_category_tree(reload=True)
if len(cat_from_db) != len(cat_from_file):
print(f'{len(cat_from_db)} != {len(cat_from_file)}')
method_success = False
if method_success:
# Reload category branch
cat_branch = inventree_interface.build_category_tree(category='Crystals and Oscillators')
if len(cat_branch) != 3:
print(f'{len(cat_branch)} != 3')
method_success = False
if method_success:
cprint('[ PASS ]')
else:
cprint('[ FAIL ]')
exit_code = -1
break
# Line return
cprint('')
sys.exit(exit_code)