def test_to_json(): brian = _User("Brian") brian_str = Json.to_json(brian) brian2 = Json.deserialize(brian_str, _User) assert brian.name == brian2.name assert brian == brian2 #"{\"py/object\": \"tests.test_json._User\", \"name\": \"Brian\"}" # diana = _Programmer("Dianna") diana_str = Json.to_json(diana) diana2 = Json.deserialize(diana_str, _Programmer) assert diana.job == diana2.job and diana.name == diana2.name #"{\"py/object\": \"tests.test_json._Programmer\", \"name\": \"Dianna\"}" # diana.job = "Test" diana_str = Json.to_json(diana) diana2 = Json.deserialize(diana_str, _Programmer) assert diana.job == diana2.job and diana.name == diana2.name blue = _Color.BLUE bluestr = Json.to_json(blue) blue2 = Json.deserialize(bluestr, _Color) assert blue == blue2 #assert bluestr == "{\"py/reduce\": [{\"py/type\": \"tests.test_json._Color\"}, {\"py/tuple\": [3]}]}" red = _ColorStr.RED redstr = Json.to_json(red) red2 = Json.deserialize(redstr, _ColorStr) assert red == red2
def test_gyomu_app(self): app = GyomuAppsInfoCdtbl() app.application_id = TEST_APPLICATION_ID2 app.description = "Test Application" user: User = UserFactory.get_current_user() app.mail_from_name = user.userid app.mail_from_address = "*****@*****.**" json_data = Json.to_json(app, gyomuapps_schema) app_loaded = Json.deserialize(json_data, GyomuAppsInfoCdtbl, gyomuapps_schema) assert app.application_id == app_loaded.application_id app2 = GyomuAppsInfoCdtbl() app2.application_id = TEST_APPLICAIONT_ID3 app2.description = "Testw Application" user: User = UserFactory.get_current_user() app2.mail_from_name = user.userid app2.mail_from_address = "*****@*****.**" app_list = [app, app2] json_data = Json.to_json(app_list, gyomuapps_total_list_schema) #json_data = gyomuapps_total_list_schema.dumps(app_list) #dictionary = gyomuapps_total_list_schema.loads(json_data=json_data) #assert len(dictionary)==2 app_list_loaded = Json.deserialize(json_data, GyomuAppsInfoCdtbl, gyomuapps_total_list_schema) assert len(app_list_loaded) == 2
def test_newapp(client): response: Response = client.get('/apps') assert response.status_code == HTTPStatus.OK original_count = len(response.json) gyomu_app_list: list[GyomuAppsInfoCdtbl] if original_count > 0: gyomu_app_list = Json.deserialize( response.data.decode(), GyomuAppsInfoCdtbl, gyomuapps_total_list_schema ) #gyomuapps_total_list_schema.load(response.json) gyomu_app = any(app for app in gyomu_app_list if app.application_id == TEST_APPLICATION_ID2) if gyomu_app is not None: response = client.delete('/apps/' + str(TEST_APPLICATION_ID2)) assert response.status_code == HTTPStatus.OK gyomu_app = GyomuAppsInfoCdtbl() gyomu_app.application_id = TEST_APPLICATION_ID2 gyomu_app.description = 'Test Application' gyomu_app.mail_from_address = '*****@*****.**' gyomu_app.mail_from_name = 'Test person' response = client.post('/apps', json=Json.to_json(gyomu_app, gyomuapps_schema)) print(response.data.decode()) assert response.status_code == HTTPStatus.CREATED response = client.delete('/apps/' + str(TEST_APPLICATION_ID2)) assert response.status_code == HTTPStatus.OK
def test_complicated_dict_type(): value1: dict = { 234: 234, True: True, "Hello": "Key", "array": [234, True, 'Hello World'] } value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, dict) print(value1_str)
def get_json_serialized_value(cls, key: str, class_type: Type[T], user: User = None, target_date: date = date.min) -> T: str_value = cls.__get_string_value(key, user, target_date) if str_value is None or str_value == "": return None return Json.deserialize(str_value, class_type)
def test_gyomu_app_validation(self): app = GyomuAppsInfoCdtbl() app.application_id = TEST_APPLICATION_ID2 app.description = "Test Application" user: User = UserFactory.get_current_user() app.mail_from_name = user.userid app.mail_from_address = "*****@*****.**" json_data = Json.to_json(app, gyomuapps_schema) json_data = '{"mail_from_name": "yoshm", "description": "Test Application", "application_id": "Test", "mail_from_address": "*****@*****.**"}' with pytest.raises(ValidationError) as ve: app2 = Json.deserialize(json_data, GyomuAppsInfoCdtbl, gyomuapps_schema) assert 'application_id' in ve.value.messages json_data = '{"mail_from_name": "yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789", ' \ '"description": "Test Application", "application_id": 32651, "mail_from_address": "*****@*****.**"}' with pytest.raises(ValidationError) as ve: dictionary = gyomuapps_schema.loads(json_data=json_data) assert 'mail_from_name' in ve.value.messages json_data = '{"mail_from_name": "yoshm",' \ ' "description": "Test Application123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789Application123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789", ' \ '"application_id": 32651, "mail_from_address": "*****@*****.**"}' with pytest.raises(ValidationError) as ve: dictionary = gyomuapps_schema.loads(json_data=json_data) assert 'description' in ve.value.messages json_data = '{"mail_from_name": "yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789yoshma123456789123456789123456789123456789123456789123456789", ' \ ' "description": "Test Application123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789Application123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789", ' \ '"application_id": 32651, "mail_from_address": "*****@*****.**"}' with pytest.raises(ValidationError) as ve: dictionary = gyomuapps_schema.loads(json_data=json_data) assert 'description' in ve.value.messages assert 'mail_from_name' in ve.value.messages json_data = '{"mail_from_name": "yoshm", "description": "Test Application", "application_id": 35456, "mail_from_address": "Testt.com"}' with pytest.raises(ValidationError) as ve: dictionary = gyomuapps_schema.loads(json_data=json_data) pass assert 'mail_from_address' in ve.value.messages
def put(self, application_id): app = get_app_from_request() # json_data = request.get_json() if app is None: return { 'message': 'update fails due to incorrect data' }, HTTPStatus.BAD_REQUEST return_value = GyomuAppsInfoCdtblAccess.update( app, original_application_id=application_id) if not return_value.is_success: return { 'message': 'fail to delete application' }, HTTPStatus.INTERNAL_SERVER_ERROR return Json.to_json(app, gyomuapps_schema), HTTPStatus.OK
def test_tuple(): value1: SimpleTuple = SimpleTuple(arg1=234, arg2=234) value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, SimpleTuple) print(value1_str) assert value1 == value2 value1: SimpleTuple = SimpleTuple(arg1=True, arg2=True) value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, SimpleTuple) print(value1_str) assert value1 == value2 value1: SimpleTuple = SimpleTuple(arg1="Hello", arg2="World") value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, SimpleTuple) print(value1_str) assert value1 == value2 value1: SimpleTuple = SimpleTuple(arg1=[234, True, 'Hello World'], arg2={ "Key1": "Value1", "Key2": "Value2", "Abc": "Def" }) value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, SimpleTuple) print(value1_str) assert value1 == value2 # def test_complicated_tuple(): # brian = _User("Brian") # value1: SimpleTuple = SimpleTuple(arg1=234, arg2=brian) # value1_str: str = Json.to_json(value1) # value2 = Json.deserialize(value1_str, SimpleTuple) # print(value1_str) # assert value1 == value2
def post(self): gyomu_app = get_app_from_request() check_app, return_value = GyomuAppsInfoCdtblAccess.get( gyomu_app.application_id) if not return_value.is_success: return { 'message': 'application fails to be retrieved' }, HTTPStatus.INTERNAL_SERVER_ERROR if check_app is not None: return { 'message': 'application id already used' }, HTTPStatus.BAD_REQUEST try: gyomu_app, return_value = GyomuAppsInfoCdtblAccess.add(gyomu_app) if not return_value.is_success: return { 'message': 'application fails to be retrieved' }, HTTPStatus.INTERNAL_SERVER_ERROR return Json.to_json(gyomu_app, gyomuapps_schema), HTTPStatus.CREATED except: return { 'message': 'Application addition returns error' }, HTTPStatus.INTERNAL_SERVER_ERROR
def test_bool_type(): value1: bool = True value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, bool) print(value1_str) assert value1 == value2
def test_int_type(): value1: int = 234 value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, int) print(value1_str) assert value1 == value2
def test_dict_type(): value1: dict = {"Key1": "Value1", "Key2": "Value2", "Abc": "Def"} value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, dict) print(value1_str) assert value1 == value2
def test_array_type(): value1: list = [234, True, 'Hello World'] value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, list) print(value1_str) assert value1 == value2
def test_str_type(): value1: str = 'Hello World' value1_str: str = Json.to_json(value1) value2 = Json.deserialize(value1_str, str) print(value1_str) assert value1 == value2
def get_app_from_request() -> GyomuAppsInfoCdtbl: json_data = request.json return Json.deserialize(json_data, GyomuAppsInfoCdtbl, gyomuapps_schema)
def set_json_serialized_value(cls, key: str, target_object, user: User = None): json_value: str = "" if target_object is not None: json_value = Json.to_json(target_object) cls.__set_string_value(key, json_value, user)