Beispiel #1
0
 def test_jsonToSqlParms_complex(self):
     json = '{"operation": "operation-here", "animals": "animal-here", "field": { "$and": [{ "age": { "eq": 5 } }, { "weight": { "lt": 20 } }, { "$or": [{ "height": { "eq": 20 } }, { "length": { "eq": 20 } }] }, { "$or": [{ "height": { "eq": 20 } }, { "length": { "eq": 20 } }] }] }}'
     sql = jsonToSqlParms.JsonToSqlParms(json)
     self.assertEqual(
         sql,
         '(age = 5 and weight < 20 and (height = 20 or length = 20) and (height = 20 or length = 20))'
     )
Beispiel #2
0
 def test_jsonToSqlParms_complex_2(self):
     json = '{"operation": "lookup", "animals": "cat", "field": { "$or": [{ "age": { "eq": 5 } }, { "weight": { "lt": 20 } }, { "$or": [{ "height": { "eq": 20 } }, { "length": { "eq": 20 } }] }, { "$or": [{ "height": { "eq": 20 } }, { "length": { "eq": 20 } }] }] }}'
     sql = jsonToSqlParms.JsonToSqlParms(json)
     self.assertEqual(
         sql,
         '(age = 5 or weight < 20 or (height = 20 or length = 20) or (height = 20 or length = 20))'
     )
Beispiel #3
0
 def test_jsonToSqlParms_complex_4(self):
     json = '{"operation":"lookup","animals":"cat","field":{"$or":[{"age":{"eq":"5"}},{"$and":[{"height":{"lt":"50"}},{"weight":{"gt":"500"}},{"age":{"eq":"100"}},{"$or":[{"butts":{"eq":"1"}},{"diabetes":{"ne":"true"}},{"$and":[{"dob":{"eq":"1998"}},{"dod":{"eq":"1999"}},{"$or":{"tail":{"ne":"false"}}},{"color":{"eq":"orange"}}]}]}]}]}}'
     sql = jsonToSqlParms.JsonToSqlParms(json)
     self.assertEqual(
         sql,
         '(age = 5 or (height < 50 and weight > 500 and age = 100 and (butts = 1 or diabetes != true or (dob = 1998 and dod = 1999 and tail != false and color = orange))))'
     )
def ProcessRequest(dataInput):

    sqlParms = jsonToSqlParms.JsonToSqlParms(dataInput)
    if (type(sqlParms) == type(dict())):  # if an error json is returned
        return json.dumps(sqlParms)

    sqlQuery = sqlParmsToQuery.sqlParmsToQuery(sqlParms, dataInput)

    sql_data = sql_utils.get_dict(sqlQuery[0])
    cats = Cat.sql_data_to_cats(sql_data)

    # if statistical operation
    if len(sqlQuery) == 3:
        col = []
        for cat in cats:
            col.append(getattr(cat.base_info, sqlQuery[2]))
        return "{\"" + sqlQuery[2] + "\": " + str(sqlQuery[1](col)) + "}"

    else:
        completedRequest = Cat.cats_to_json(cats)

    return completedRequest
Beispiel #5
0
 def test_jsonToSqlParms_simple_2(self):
     json = '{"operation": "operation-here", "animals": "animal-here", "field": { "$and": [{"length" : {"lt" : 20} }, {"$or": [{"height": {"lt": 20}}, [{"age":{"eq":10}}]]}, {"$or": [{"height": {"lt": 20}}]}]}}'
     sql = jsonToSqlParms.JsonToSqlParms(json)
     self.assertEqual(
         sql,
         '(length < 20 and (height < 20 or (age = 10)) and (height < 20))')
Beispiel #6
0
 def test_jsonToSqlParms_simple(self):
     json = '{"operation": "operation-here", "animals": "animal-here", "field": {"height":{"gt":5}}}'
     sql = jsonToSqlParms.JsonToSqlParms(json)
     self.assertEqual(sql, 'height > 5')
Beispiel #7
0
 def test_invalid_operation(self):
     json_string = '{"operation": "operation-here", "animals": "animal-here", "field": {"height":{"ltee":5}}}'
     data = jsonToSqlParms.JsonToSqlParms(json_string)
     self.assertEqual(data['code'], 'ERROR_02')