Commit 246939d0 authored by Ismail Moumni's avatar Ismail Moumni
Browse files

adding json tests to server

parent 9341f38a
Pipeline #717 passed with stage
in 36 seconds
......@@ -31,13 +31,13 @@ class RdosServer:
# Receiving Data from Client
data = conn.recv(4096)
query = (json.loads(data.decode('utf-8')))
print(query)
print("Requete reçu : ", query)
match = verif_param(query)
if match == query:
print("ok")
if match != query:
print('Erreur query : ', match)
conn.send(bytes(json.dumps(match), "utf-8"))
else:
print('Erreur query : ')
print("ok")
conn.send(bytes(json.dumps(match), "utf-8"))
......@@ -53,17 +53,6 @@ def query_valid(data: dict):
return False
# Function send_param_client send a Dict containing parameters
# needed to add a query to db
# Function exmaple s = {"ARGUMENTS","Tools":"List of Tools" }
# Function input takes dictionary and a list of tools
# Function output returns a new dictionary containing arguments
# # for db query and list of tools
def send_param_client(__RDOS_Dict__: dict, __RDOS_Tools__: list):
req = db.getParameters()
return req
# Function json_loads changes a json file into a Dict
# Function json_loads returns a dictionary
def json_to_dict(js):
......@@ -79,6 +68,20 @@ def db_req(tool: dict):
return tool_dict
# Function match_dict returns non existing fields in a dict
# Function input biblio and dict
# Function returns a list containing missing values
def missing_keys(biblio: dict, gen: dict):
missing = []
if(biblio is not None and gen is not None):
for key in biblio:
if key not in gen:
missing.append(key)
return missing
else:
raise Exception("Dictionnaire Vide!!")
# Function match_query_dict matches two dictionaries
# Function input takes 2 dictionaries to match
# Function output return trur if dictionaries matches else raise a value error exception
......@@ -89,17 +92,6 @@ def match_query_dict(biblio: dict, data: dict):
raise ValueError("Error Dict Values ")
# Function match_generator_dict Matches dictionary with a list
# Function input 2 arguments dictionary and query from Server
# Function output true if dictionary keys match list elements else false
def match_generator_dict(biblio: dict, Genereator: list):
for a in Genereator:
for b in biblio.keys():
if a != b:
return False
return True
# Function verif_param verifies json send from client to server by matching it with database query
# Function input takes a dictionary
# Function output returns query if it matches with database query keys if not it raises an error
......@@ -123,19 +115,3 @@ def verif_param(data: dict):
raise Exception("Query Doesn't match")
else:
raise Exception("Error Database")
'''
if __name__ == '__main__':
tool = 'Regal'
# sa = ({'Regal': {'size': '', 'asize': '', 'outputf': '', 'number': '', 'comp': '', 'min': '', 'con': ''}},
# {'size': '10', 'asize': '2', 'outputf': '', 'number': '1', 'comp': 'yes', 'min': '', 'con': ''})
# print(verif_param(d, data))
s = {'Regal': {'size': 'qsd', 'asize': '', 'outputf': 'qsd', 'number': 'ss',
'comp': 'qsd', 'min': 'sqsd', 'con': 'qsd'}}
# print(s.get('Regal'))
print(s.keys())
data_test1 = '{"parameters":"request"}'
data_load = json.loads(data_test1)
print(query_valid(data_load) is True)
'''
#!/usr/bin/env python3
# Authors : Julien DAVID & Ismail MOUMNI
import sys
import os
import pytest
sys.path.append(os.path.realpath('../softwares/'))
import server.server as srv # noqa E402
data1 = {"id": "", "name": "", "state": "", "launch_date": "", "mdr_number": "", "nameC": {"first": "", "last": ""}, "title": "", "id": ""}
data2 = {"id": "", "name": "", "state": "", "launch_date": "", "mdr_number": "", "id": ""}
def test_match_query():
var = srv.match_query_dict(data1, data1)
assert var is not False
def test_diff_match_query():
result = srv.match_query_dict(data1, data2)
assert result is not None
def test_bad_match():
with pytest.raises(Exception):
assert srv.match_query_dict({}, data2)
#!/usr/bin/env python3
# Authors : Julien DAVID & Ismail MOUMNI
import sys
import os
import json
import pytest
sys.path.append(os.path.realpath('../softwares/'))
import server.server as srv # noqa E402
data1 = '{"id": "", "name": "", "state": "", "launch_date": "", "mdr_number": "", "nameC": {"first": "", "last": ""}, "title": "", "id": ""}'
data2 = '{"id": "", "name": "", "state": "", "launch_date": "", "mdr_number": "", "id": ""}'
obj = (json.loads(data1))
obj2 = (json.loads(data2))
def test_missing_json():
var = srv.missing_keys(obj, obj)
assert var == []
def test_diff_missing_json():
result = srv.missing_keys(obj, obj2)
assert result is not None
def test_bad_match_json():
with pytest.raises(Exception):
assert srv.missing_keys({}, obj)
#!/usr/bin/ python3
# Authors : Julien DAVID & Ismail MOUMNI
import sys
import os
import pytest
sys.path.append(os.path.realpath('../softwares/'))
import server.server as srv # noqa E402
data1 = {
"id": "",
"name": "",
"state": "",
"launch_date": "",
"mdr_number": "",
"nces_id": "",
"error": "",
"sis_type": "",
"pause_start": "",
"pause_end": "",
"district_contact": {
"district": "",
"email": "",
"name": {
"first": "",
"last": "",
},
"title": "",
"id": ""
}
}
data2 = {
"id": "",
"name": "",
"state": "",
"launch_date": "",
"mdr_number": "",
"nces_id": "",
"error": "",
"sis_type": "",
"pause_start": "",
"pause_end": "",
"district_contact": {
"district": "",
"email": "",
"name": {
"first": "",
"last": "",
},
"title": "",
"id": ""
}
}
def test_missing_keys():
var = srv.missing_keys(data1, data2)
assert not var
def test_bad_missing():
with pytest.raises(Exception):
assert srv.missing_keys({}, data1)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment