Beispiel #1
0
def compare_config():
    query_params = request.args.to_dict()
    db1 = query_params.get('db1')
    db2 = query_params.get('db2')
    if not (db1 and db2):
        return jsonify({'error': 'NEED_TWO_DB'}), 400

    db_binds = set(app.config['SQLALCHEMY_BINDS'].keys())
    if (db1 not in db_binds) or (db2 not in db_binds):
        return jsonify({'error': 'INVALID_DB_NAME'}), 400

    rs1 = db.get_session(db1).query(DConfig).all()
    cfg1 = [{
        'name': i.name,
        'value': i.value,
        'value_type': i.value_type,
        'description': i.description
    } for i in rs1]
    app.logger.debug('%s: length: %s' % (db1, len(cfg1)))
    md5_cfg1 = {create_md5(json.dumps(i)): i for i in cfg1}
    rs2 = db.get_session(db2).query(DConfig).all()
    cfg2 = [{
        'name': i.name,
        'value': i.value,
        'value_type': i.value_type,
        'description': i.description
    } for i in rs2]
    app.logger.debug('%s: length: %s' % (db2, len(cfg2)))
    md5_cfg2 = {create_md5(json.dumps(j)): j for j in cfg2}
    diff1 = set(md5_cfg1.keys()) - set(md5_cfg2.keys())
    diff2 = set(md5_cfg2.keys()) - set(md5_cfg1.keys())
    diff_keys = []
    for k in diff1:
        diff_keys.append(md5_cfg1[k]['name'])

    for l in diff2:
        diff_keys.append(md5_cfg2[l]['name'])

    name_cfg1 = {i['name']: i for i in cfg1}
    name_cfg2 = {j['name']: j for j in cfg2}
    final_diff = []
    for key in diff_keys:
        final_diff.append([name_cfg1.get(key, {}), name_cfg2.get(key, {})])

    app.logger.debug('final diff: %s' % final_diff)
    return jsonify(final_diff)
Beispiel #2
0
def get_all_suppliers():
    query = """MATCH (n:Supplier) RETURN n"""
    with get_session() as session:
        tx = session.begin_transaction()
        result = tx.run(query)
        suppliers = [dict(i['n']) for i in result]
        tx.close()
    return JSONResponse(content={'suppliers': suppliers})
Beispiel #3
0
def test_create_pokemon():
    assert get_session().query(Pokemon).filter_by(no="006").count() == 0

    response = client.post(
        "/api/pokemon/create",
        json={
            "no": "006",
            "name": "CHARIZARD",
            "types": ["FIRE", "FLYING"]
        },
    )
    data = response.json()
    data["types"].sort(key=lambda k: k["name"])
    assert response.status_code == 200
    assert data == {
        "id":
        "006",
        "no":
        "006",
        "name":
        "CHARIZARD",
        "types": [
            {
                "id": data["types"][0]["id"],
                "name": "FIRE"
            },
            {
                "id": data["types"][1]["id"],
                "name": "FLYING"
            },
        ],
        "evolutions": {
            "before": [],
            "after": []
        },
    }
    assert get_session().query(Pokemon).filter_by(no="006").count() == 1
Beispiel #4
0
def configuration():
    db_binds = set(app.config['SQLALCHEMY_BINDS'].keys())
    query_params = request.args.to_dict()
    db_name = query_params.get('db', 'local')
    if db_name and db_name not in db_binds:
        # TODO render error page
        return jsonify({'error': 'INVALID_DB_NAME'}), 400

    rs = db.get_session(db_name).query(DConfig).all()
    configs = [{
        'name': i.name,
        'value': i.value,
        'value_type': i.value_type,
        'description': i.description
    } for i in rs]

    return render_template('index.html', configs=configs, dbs=sorted(db_binds))
Beispiel #5
0
def test_delete_pokemon():
    assert get_session().query(Pokemon).filter_by(no="006").count() == 1

    response = client.delete("/api/pokemon/006")
    data = response.json()
    assert response.status_code == 200
    assert data == {
        "id": "006",
        "no": "006",
        "name": "CHARIZARD_2",
        "types": [
            {
                "id": data["types"][0]["id"],
                "name": "NONE"
            },
        ],
        "evolutions": {
            "before": [],
            "after": []
        },
    }
Beispiel #6
0
def create_new_supplier(supplier: Supplier):
    query = """
		CREATE (n:Supplier {
			contactName: $contactName,
			contactTitle: $contactTitle,
			country: $country,
			address: $address,
			supplierID: $supplierID,
			phone: $phone,
			city: $city,
			companyName: $companyName,
			postalCode: $postalCode,
			region: $region,
			fax: $fax,
			homePage: $homePage
		})
		RETURN n.supplierID
	"""
    with get_session() as session:
        tx = session.begin_transaction()
        tx.run(query, dict(supplier))
        tx.commit()
    return JSONResponse(content={'supplier': dict(supplier)})
def test_get_session():
    session = get_session()
    assert session != None
Beispiel #8
0
 def _session(self):
     return get_session()