def test_exemplars(key): if isinstance(EXEMPLARS[key], Sequence): for e in EXEMPLARS[key]: v = validate(key, e, SCHEMAS) assert v[0] else: v = validate(key, EXEMPLARS[key], SCHEMAS) if not v[0]: for vv, reason in v[1].items(): print(vv, reason) print(type(EXEMPLARS[key][vv])) pprint(EXEMPLARS[key][vv]) assert v[0]
def validate(rc): """Validate the combined database against the schemas""" from regolith.schemas import validate print("=" * 10 + "\nVALIDATING\n") any_errors = False if getattr(rc, "collection"): db = {rc.collection: rc.client.chained_db[rc.collection]} else: db = rc.client.chained_db for name, collection in db.items(): errored_print = False for doc_id, doc in collection.items(): v = validate(name, doc, rc.schemas) if v[0] is False: if errored_print is False: errored_print = True any_errors = True print("Errors found in {}".format(name)) print("=" * len("Errors found in {}".format(name))) print("ERROR in {}:".format(doc_id)) pprint(v[1]) for vv in v[1]: pprint(doc.get(vv)) print("-" * 15) print("\n") if not any_errors: print("\nNO ERRORS IN DBS\n" + "=" * 15) else: sys.exit("Validation failed on some records")
def collection_page(dbname, collname): rc = app.rc try: coll = rc.client[dbname][collname] except (KeyError, AttributeError): abort(404) status = status_id = None if request.method == "POST": form = request.form if "shutdown" in form: return shutdown() elif "cancel" in form: body = json.loads(form["body"].strip()) status = "canceled" status_id = str(body["_id"]) elif "save" in form: try: body = json.loads(form["body"].strip()) except Exception: td = tempfile.TemporaryDirectory() n = os.path.join(td.name, "regolith.txt") print("Error in json parsing writing text file to {}. " "Please try again.".format(n)) with open(n, "w", encoding='utf-8') as f: f.write(form["body"]) traceback.print_exc() raise tv, errors = validate(dbname, body, rc.schemas) if not tv: td = tempfile.TemporaryDirectory() n = os.path.join(td.name, "regolith.txt") with open(n, "w", encoding='utf-8') as f: f.write(form["body"]) raise ValueError("Error while validating the record," " writing text file to {}. " "Please try again.\n\n" "Your errors were\n" "------------------" "{}".format(n, errors)) rc.client.update_one(dbname, collname, {"_id": body["_id"]}, body) status = "saved ✓" status_id = str(body["_id"]) elif "add" in form: try: body = json.loads(form["body"].strip()) print(body) except Exception: td = tempfile.TemporaryDirectory() n = os.path.join(td.name, "regolith.txt") print("Error in json parsing writing text file to {}. " "Please try again.".format(n)) with open(n, encoding='utf-8') as f: f.write(form["body"]) traceback.print_exc() raise tv, errors = validate(dbname, body, rc.schemas) if not tv: td = tempfile.TemporaryDirectory() n = os.path.join(td.name, "regolith.txt") with open(n, encoding='utf-8') as f: f.write(form["body"]) raise ValueError("Error while validating the record," " writing text file to {}. " "Please try again.\n\n" "Your errors were\n" "------------------" "{}".format(n, errors)) try: added = rc.client.insert_one(dbname, collname, body) except Exception: traceback.print_exc() raise status = "added ✓" status_id = str(body["_id"]) elif "delete" in form: body = json.loads(form["body"].strip()) deled = rc.client.delete_one(dbname, collname, body) return render_template( "collection.html", rc=rc, dbname=dbname, len=len, str=str, status=status, status_id=status_id, collname=collname, coll=coll, json=json, min=min, conv=_convert_to_dict, )
def test_validation(key): if isinstance(EXEMPLARS[key], Sequence): for e in EXEMPLARS[key]: validate(key, e, SCHEMAS) else: validate(key, EXEMPLARS[key], SCHEMAS)
def collection_page(dbname, collname): rc = app.rc try: coll = rc.client[dbname][collname] except (KeyError, AttributeError): abort(404) status = status_id = None if request.method == 'POST': form = request.form if 'shutdown' in form: return shutdown() elif 'cancel' in form: body = json.loads(form['body'].strip()) status = 'canceled' status_id = str(body['_id']) elif 'save' in form: try: body = json.loads(form['body'].strip()) except Exception: td = tempfile.TemporaryDirectory() n = os.path.join(td.name, 'regolith.txt') print('Error in json parsing writing text file to {}. ' 'Please try again.'.format(n)) with open(n, 'w') as f: f.write(form['body']) traceback.print_exc() raise tv, errors = validate(dbname, body) if not tv: td = tempfile.TemporaryDirectory() n = os.path.join(td.name, 'regolith.txt') with open(n, 'w') as f: f.write(form['body']) raise ValueError('Error while validating the record,' ' writing text file to {}. ' 'Please try again.\n\n' 'Your errors were\n' '------------------' '{}'.format(n, errors)) rc.client.update_one(dbname, collname, {'_id': body['_id']}, body) status = 'saved ✓' status_id = str(body['_id']) elif 'add' in form: try: body = json.loads(form['body'].strip()) print(body) except Exception: td = tempfile.TemporaryDirectory() n = os.path.join(td.name, 'regolith.txt') print('Error in json parsing writing text file to {}. ' 'Please try again.'.format(n)) with open(n) as f: f.write(form['body']) traceback.print_exc() raise tv, errors = validate(dbname, body) if not tv: td = tempfile.TemporaryDirectory() n = os.path.join(td.name, 'regolith.txt') with open(n) as f: f.write(form['body']) raise ValueError('Error while validating the record,' ' writing text file to {}. ' 'Please try again.\n\n' 'Your errors were\n' '------------------' '{}'.format(n, errors)) try: added = rc.client.insert_one(dbname, collname, body) except Exception: traceback.print_exc() raise status = 'added ✓' status_id = str(body['_id']) elif 'delete' in form: body = json.loads(form['body'].strip()) deled = rc.client.delete_one(dbname, collname, body) return render_template('collection.html', rc=rc, dbname=dbname, len=len, str=str, status=status, status_id=status_id, collname=collname, coll=coll, json=json, min=min)
def test_validation(key): validate(key, EXEMPLARS[key])