def Function_withSuccess(): # Arrange TEST = RandomHelper.string(minimum=10) @Function def myFunction(something): return TEST, something @Function def myOtherFunction(something): raise Exception(TEST) SOMETHING = RandomHelper.string(minimum=10) exception = None # Act myRestult = myFunction(SOMETHING) myOtherResult = None try: myOtherResult = myOtherFunction(SOMETHING) except Exception as ext: exception = ext # Assert assert (TEST, SOMETHING) == myRestult assert ObjectHelper.isNone(myOtherResult) assert ObjectHelper.isNotNone(exception) assert TEST == str(exception)
def handleEnvironmentChangesProperly_withSuccess_whenActionsHaveNoArguments(): beforeTestEnvironmentSettings = {**EnvironmentHelper.getSet()} inBetweenTestEnvironmentSettings = None afterTestEnvironmentSettings = None MY_BEFORE_ACTION_RETURN = RandomHelper.string(minimum=10) MY_AFTER_ACTION_RETURN = RandomHelper.string(minimum=10) # Arrange def myBeforeAction(): return MY_BEFORE_ACTION_RETURN def myAfterAction(): return MY_AFTER_ACTION_RETURN def myFunction(a): return a returns = {} @Test(callBefore=myBeforeAction, callAfter=myAfterAction, returns=returns, environmentVariables={ SettingHelper.ACTIVE_ENVIRONMENT: None, **MUTED_LOG_HELPER_SETTINGS }) def myTest(): inBetweenTestEnvironmentSettings = EnvironmentHelper.getSet() assert ObjectHelper.isNotNone(inBetweenTestEnvironmentSettings) assert ObjectHelper.isDictionary(inBetweenTestEnvironmentSettings) assert 'a' == myFunction('a') return inBetweenTestEnvironmentSettings # Act inBetweenTestEnvironmentSettings = myTest() afterTestEnvironmentSettings = {**EnvironmentHelper.getSet()} # Assert assert MY_BEFORE_ACTION_RETURN == returns['returnOfCallBefore'] assert MY_AFTER_ACTION_RETURN == returns['returnOfCallAfter'] assert ObjectHelper.isNotNone(beforeTestEnvironmentSettings) assert ObjectHelper.isDictionary(beforeTestEnvironmentSettings) assert ObjectHelper.isNotNone(inBetweenTestEnvironmentSettings) assert ObjectHelper.isDictionary(inBetweenTestEnvironmentSettings) assert ObjectHelper.isNotNone(afterTestEnvironmentSettings) assert ObjectHelper.isDictionary(afterTestEnvironmentSettings) assert afterTestEnvironmentSettings == beforeTestEnvironmentSettings assert not beforeTestEnvironmentSettings == inBetweenTestEnvironmentSettings
def apiKeyManager_worksProperly(): # arrange SECRET = 'abcd' SESSION_DURATION = 10 + 360 ALGORITHM = 'HS256' HEADER_NAME = 'Context' HEADER_TYPE = 'ApiKey ' IDENTITY = RandomHelper.string(minimum=100, maximum=150) CONTEXT = 'ABCD' CONTEXT_LIST = [CONTEXT] DATA = {'personal': 'data'} deltaMinutes = DateTimeHelper.timeDelta(minutes=SESSION_DURATION) apiKeyManager = ApiKeyManager.JwtManager(SECRET, ALGORITHM, HEADER_NAME, HEADER_TYPE) timeNow = DateTimeHelper.dateTimeNow() payload = { JwtConstant.KW_IAT: timeNow, JwtConstant.KW_NFB: timeNow, JwtConstant.KW_JTI: f"{int(f'{time.time()}'.replace('.', ''))+int(f'{time.time()}'.replace('.', ''))}", JwtConstant.KW_EXPIRATION: timeNow + deltaMinutes, JwtConstant.KW_IDENTITY: IDENTITY, JwtConstant.KW_FRESH: False, JwtConstant.KW_TYPE: JwtConstant.ACCESS_VALUE_TYPE, JwtConstant.KW_CLAIMS: { JwtConstant.KW_CONTEXT: CONTEXT_LIST, JwtConstant.KW_DATA: DATA } } # act totalRuns = 10000 lines = 3 initTime = time.time() for i in range(totalRuns): encodedPayload = apiKeyManager.encode(payload) decodedPayload = apiKeyManager.decode(encodedPayload) accessException = TestHelper.getRaisedException( apiKeyManager.validateAccessApiKey, rawJwt=decodedPayload) refreshException = TestHelper.getRaisedException( apiKeyManager.validateRefreshApiKey, rawJwt=decodedPayload) endTime = time.time() - initTime # assert assert lines * .0001 > endTime / totalRuns, (lines * .0001, endTime / totalRuns) assert ObjectHelper.equals(payload, decodedPayload), (payload, decodedPayload) assert ObjectHelper.isNone(accessException), accessException assert ObjectHelper.isNotNone(refreshException), refreshException assert ObjectHelper.equals( GlobalException.__name__, type(refreshException).__name__), (GlobalException.__name__, type(refreshException).__name__, refreshException) assert ObjectHelper.equals(401, refreshException.status) assert ObjectHelper.equals('Invalid apiKey', refreshException.message) assert ObjectHelper.equals( 'Refresh apiKey should have type refresh, but it is access', refreshException.logMessage)
def Method_withSuccess(): # Arrange TEST = RandomHelper.string(minimum=10) class MyClass: @Method def myMethod(self, something): return TEST, something @Method def myOtherMethod(self, something): raise Exception(TEST) @Method def myNotMethod(self, something): raise Exception(TEST) SOMETHING = RandomHelper.string(minimum=10) methodException = None notMethodEception = None myClass = MyClass() # Act myRestult = myClass.myMethod(SOMETHING) myOtherResult = None try: myOtherResult = myClass.myOtherMethod(SOMETHING) except Exception as ext: methodException = ext myNotMethodResult = None try: myNotMethodResult = myNotMethod(None, SOMETHING) except Exception as ext: notMethodEception = ext # print(notMethodEception) # Assert assert (TEST, SOMETHING) == myRestult assert ObjectHelper.isNone(myOtherResult) assert ObjectHelper.isNotNone(methodException) assert TEST == str(methodException) assert ObjectHelper.isNone(myNotMethodResult) assert ObjectHelper.isNotNone(notMethodEception) assert TEST == str(notMethodEception)
def randomValues(): # Arrange MINIMUM_CUSTOM_RANGE = 30 MAXIMUM_CUSTOM_RANGE = 10 OTHER_MINIMUM_CUSTOM_RANGE = 100 OTHER_MAXIMUM_CUSTOM_RANGE = 200 MINIMUM_CUSTOM_FLOAT_RANGE = 400.33 MAXIMUM_CUSTOM_FLOAT_RANGE = 500.55 # Act stringValueInDefaultRange = RandomHelper.string() stringValueInCustomRange = RandomHelper.string( minimum=MINIMUM_CUSTOM_RANGE, maximum=MAXIMUM_CUSTOM_RANGE) stringValueInOtherCustomRange = RandomHelper.string( minimum=OTHER_MINIMUM_CUSTOM_RANGE, maximum=OTHER_MAXIMUM_CUSTOM_RANGE) integerValueInDefaultRange = RandomHelper.integer() integerValueInCustomRange = RandomHelper.integer( minimum=MINIMUM_CUSTOM_RANGE, maximum=MAXIMUM_CUSTOM_RANGE) integerValueInOtherCustomRange = RandomHelper.integer( minimum=OTHER_MINIMUM_CUSTOM_RANGE, maximum=OTHER_MAXIMUM_CUSTOM_RANGE) floatValueInDefaultRange = RandomHelper.float() floatValueInCustomRange = RandomHelper.float(minimum=MINIMUM_CUSTOM_RANGE, maximum=MAXIMUM_CUSTOM_RANGE) floatValueInOtherCustomRange = RandomHelper.float( minimum=OTHER_MINIMUM_CUSTOM_RANGE, maximum=OTHER_MAXIMUM_CUSTOM_RANGE) floatValueInOtherCustomFloatRange = RandomHelper.float( minimum=MINIMUM_CUSTOM_FLOAT_RANGE, maximum=MAXIMUM_CUSTOM_FLOAT_RANGE) # Assert assert RandomHelper.DEFAULT_MINIMUM_LENGHT <= len( stringValueInDefaultRange) <= RandomHelper.DEFAULT_MAXIMUM_LENGHT assert MINIMUM_CUSTOM_RANGE == len(stringValueInCustomRange) assert OTHER_MINIMUM_CUSTOM_RANGE <= len( stringValueInOtherCustomRange) <= OTHER_MAXIMUM_CUSTOM_RANGE assert RandomHelper.DEFAULT_MINIMUM_LENGHT <= integerValueInDefaultRange <= RandomHelper.DEFAULT_MAXIMUM_LENGHT assert MINIMUM_CUSTOM_RANGE == integerValueInCustomRange assert OTHER_MINIMUM_CUSTOM_RANGE <= integerValueInOtherCustomRange <= OTHER_MAXIMUM_CUSTOM_RANGE assert RandomHelper.DEFAULT_MINIMUM_LENGHT <= floatValueInDefaultRange <= RandomHelper.DEFAULT_MAXIMUM_LENGHT assert MINIMUM_CUSTOM_RANGE == floatValueInCustomRange assert OTHER_MINIMUM_CUSTOM_RANGE <= floatValueInOtherCustomRange < OTHER_MAXIMUM_CUSTOM_RANGE assert MINIMUM_CUSTOM_FLOAT_RANGE <= floatValueInOtherCustomFloatRange <= MAXIMUM_CUSTOM_FLOAT_RANGE
def enumName(): # arrange @Enum(associateReturnsTo='value', instanceLog=False) class MyOtherEnumTest: ONE = EnumItem(value=1, otherValue='1') TWO = EnumItem(value=2, otherValue='1') THREE = EnumItem(value=3, otherValue='1') @Enum(associateReturnsTo='value', instanceLog=False) class MyThirdEnumTest: ONE = EnumItem(value=4, otherValue='1') TWO = EnumItem(value=5, otherValue='1') THREE = EnumItem(value=6, otherValue='1') ITS_NAME = RandomHelper.string(minimum=5, maximum=10) A_NAME = RandomHelper.string(minimum=5, maximum=10) TEST_NAME = RandomHelper.string(minimum=5, maximum=10) ITS_LABEL = RandomHelper.string(minimum=5, maximum=10) A_LABEL = RandomHelper.string(minimum=5, maximum=10) TEST_LABEL = RandomHelper.string(minimum=5, maximum=10) ITS_NUMERIC = RandomHelper.integer(minimum=5, maximum=1000) A_NUMERIC = RandomHelper.integer(minimum=5, maximum=1000) TEST_NUMERIC = RandomHelper.integer(minimum=5, maximum=1000) ITS_VALUE = 'ITS' A_VALUE = 'A' TEST_VALUE = 'TEST' @Enum() class MyEnumTest: ITS = EnumItem(name=ITS_NAME, label=ITS_LABEL, numeric=ITS_NUMERIC) A = EnumItem(name=A_NAME, label=A_LABEL, numeric=A_NUMERIC) TEST = EnumItem(name=TEST_NAME, label=TEST_LABEL, numeric=TEST_NUMERIC) MY_ENUM_TEST = MyEnumTest() # act enumType = MY_ENUM_TEST # assert assert 'OK' == HttpStatus.OK.enumName assert 'ONE' == MyOtherEnumTest().ONE.enumName assert 'ONE' == MyOtherEnumTest.ONE.enumName assert 1 == MyOtherEnumTest.ONE assert MyEnumTest.map(MY_ENUM_TEST.ITS.enumValue) == MY_ENUM_TEST.ITS assert MyEnumTest.map(MY_ENUM_TEST.ITS) == MY_ENUM_TEST.ITS assert MyOtherEnumTest.map( MyOtherEnumTest.ONE.enumValue) == MyOtherEnumTest.ONE assert MyOtherEnumTest.map(MyOtherEnumTest.ONE) == MyOtherEnumTest.ONE assert MyOtherEnumTest.map('ONE') == MyOtherEnumTest.ONE.enumValue assert MyOtherEnumTest.map('ONE') == MyOtherEnumTest.ONE assert MY_ENUM_TEST.map('TEST') == MyEnumTest.TEST.enumValue assert MY_ENUM_TEST.map('TEST') == MyEnumTest.TEST assert MyEnumTest == type(enumType)
def enum_withSuccess(): TEST_ITERATION = 1 ASSERT_ITERATION = 1 discount = 0 instanciationTime = 0 equalTestTime = 0 start = time.time() for _ in range(TEST_ITERATION): # arrange discountStart = time.time() ITS_NAME = RandomHelper.string(minimum=5, maximum=10) A_NAME = RandomHelper.string(minimum=5, maximum=10) TEST_NAME = RandomHelper.string(minimum=5, maximum=10) ITS_LABEL = RandomHelper.string(minimum=5, maximum=10) A_LABEL = RandomHelper.string(minimum=5, maximum=10) TEST_LABEL = RandomHelper.string(minimum=5, maximum=10) ITS_NUMERIC = RandomHelper.integer(minimum=5, maximum=1000) A_NUMERIC = RandomHelper.integer(minimum=5, maximum=1000) TEST_NUMERIC = RandomHelper.integer(minimum=5, maximum=1000) ITS_VALUE = 'ITS' A_VALUE = 'A' TEST_VALUE = 'TEST' discount += time.time() - discountStart # act discountStart = time.time() @Enum() class MyEnumTest: ITS = EnumItem(name=ITS_NAME, label=ITS_LABEL, numeric=ITS_NUMERIC) A = EnumItem(name=A_NAME, label=A_LABEL, numeric=A_NUMERIC) TEST = EnumItem(name=TEST_NAME, label=TEST_LABEL, numeric=TEST_NUMERIC) discount += time.time() - discountStart instanciationTimeInit = time.time() MY_ENUM_TEST = MyEnumTest() instanciationTime += time.time() - instanciationTimeInit # assert timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert ITS_VALUE == MY_ENUM_TEST.ITS discountStart = time.time() log.log( enum_withSuccess, f'ITS_VALUE == MY_ENUM_TEST.ITS assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert A_VALUE == MY_ENUM_TEST.A discountStart = time.time() log.log( enum_withSuccess, f'A_VALUE == MY_ENUM_TEST.A assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert TEST_VALUE == MY_ENUM_TEST.TEST discountStart = time.time() log.log( enum_withSuccess, f'TEST_VALUE == MY_ENUM_TEST.TEST assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert ITS_NAME == MY_ENUM_TEST.ITS.name discountStart = time.time() log.log( enum_withSuccess, f'ITS_NAME == MY_ENUM_TEST.ITS.name assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert A_NAME == MY_ENUM_TEST.A.name discountStart = time.time() log.log( enum_withSuccess, f'A_NAME == MY_ENUM_TEST.A.name assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert TEST_NAME == MY_ENUM_TEST.TEST.name discountStart = time.time() log.log( enum_withSuccess, f'TEST_NAME == MY_ENUM_TEST.TEST.name assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert ITS_LABEL == MY_ENUM_TEST.ITS.label discountStart = time.time() log.log( enum_withSuccess, f'ITS_LABEL == MY_ENUM_TEST.ITS.label assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert A_LABEL == MY_ENUM_TEST.A.label discountStart = time.time() log.log( enum_withSuccess, f'A_LABEL == MY_ENUM_TEST.A.label assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert TEST_LABEL == MY_ENUM_TEST.TEST.label discountStart = time.time() log.log( enum_withSuccess, f'TEST_LABEL == MY_ENUM_TEST.TEST.label assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert ITS_NUMERIC == MY_ENUM_TEST.ITS.numeric discountStart = time.time() log.log( enum_withSuccess, f'ITS_NUMERIC == MY_ENUM_TEST.ITS.numeric assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert A_NUMERIC == MY_ENUM_TEST.A.numeric discountStart = time.time() log.log( enum_withSuccess, f'A_NUMERIC == MY_ENUM_TEST.A.numeric assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert TEST_NUMERIC == MY_ENUM_TEST.TEST.numeric discountStart = time.time() log.log( enum_withSuccess, f'TEST_NUMERIC == MY_ENUM_TEST.TEST.numeric assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert MyEnumTest.map( MY_ENUM_TEST.ITS.enumValue) == MY_ENUM_TEST.ITS discountStart = time.time() log.log( enum_withSuccess, f'MyEnumTest.map(MY_ENUM_TEST.ITS.enumValue) == MY_ENUM_TEST.ITS assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert MyEnumTest.map(MY_ENUM_TEST.A.enumValue) == MY_ENUM_TEST.A discountStart = time.time() log.log( enum_withSuccess, f'MyEnumTest.map(MY_ENUM_TEST.A.enumValue) == MY_ENUM_TEST.A assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert MyEnumTest.map( MY_ENUM_TEST.TEST.enumValue) == MY_ENUM_TEST.TEST discountStart = time.time() log.log( enum_withSuccess, f'MyEnumTest.map(MY_ENUM_TEST.TEST.enumValue) == MY_ENUM_TEST.TEST assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert MY_ENUM_TEST.ITS.enumValue.enum == MY_ENUM_TEST discountStart = time.time() log.log( enum_withSuccess, f'MY_ENUM_TEST.ITS.enumValue.enum == MY_ENUM_TEST assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert MY_ENUM_TEST.A.enumValue.enum == MY_ENUM_TEST discountStart = time.time() log.log( enum_withSuccess, f'MY_ENUM_TEST.A.enumValue.enum == MY_ENUM_TEST assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert MY_ENUM_TEST.TEST.enumValue.enum == MY_ENUM_TEST discountStart = time.time() log.log( enum_withSuccess, f'MY_ENUM_TEST.TEST.enumValue.enum == MY_ENUM_TEST assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert not MY_ENUM_TEST.ITS.enumValue.enum == MyEnumTest discountStart = time.time() log.log( enum_withSuccess, f'not MY_ENUM_TEST.ITS.enumValue.enum == MyEnumTest assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert not MY_ENUM_TEST.A.enumValue.enum == MyEnumTest discountStart = time.time() log.log( enum_withSuccess, f'not MY_ENUM_TEST.A.enumValue.enum == MyEnumTest assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert not MY_ENUM_TEST.TEST.enumValue.enum == MyEnumTest discountStart = time.time() log.log( enum_withSuccess, f'not MY_ENUM_TEST.TEST.enumValue.enum == MyEnumTest assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart equalTestTimeStart = time.time() timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert MY_ENUM_TEST.ITS.enumValue.enum == MyEnumTest() discountStart = time.time() log.log( enum_withSuccess, f'MY_ENUM_TEST.ITS.enumValue.enum == MyEnumTest() assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert MY_ENUM_TEST.A.enumValue.enum == MyEnumTest() discountStart = time.time() log.log( enum_withSuccess, f'MY_ENUM_TEST.A.enumValue.enum == MyEnumTest() assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart timeAssertInit = time.time() for _ in range(ASSERT_ITERATION): assert MY_ENUM_TEST.TEST.enumValue.enum == MyEnumTest() discountStart = time.time() log.log( enum_withSuccess, f'MY_ENUM_TEST.TEST.enumValue.enum == MyEnumTest() assert duration: {time.time() - timeAssertInit}' ) discount += time.time() - discountStart assert MyEnumTest() == MyEnumTest() equalTestTime += (time.time() - equalTestTimeStart)
def convertFromObjectToObject_whenTargetClassIsList() : # arrange a = RandomHelper.string() b = RandomHelper.string() c = RandomHelper.string() otherA = MyOtherDto(RandomHelper.string()) otherB = MyOtherDto(RandomHelper.string()) otherC = MyOtherDto(RandomHelper.string()) myFirst = MyDto(RandomHelper.string(), otherA, [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), None), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]) mySecond = MyDto(RandomHelper.string(), otherB, [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), None), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]) myThird = MyDto(RandomHelper.string(), otherC, [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), None), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]) thirdOne = RandomHelper.integer() thirdTwo = RandomHelper.integer() thirdThree = RandomHelper.integer() myThirdOne = [MyThirdDto(myFirst, thirdOne)] myThirdTwo = [MyThirdDto(mySecond, thirdTwo)] myThirdThree = [MyThirdDto(myThird, thirdThree)] expected = [MyDto(a, otherA, myThirdOne), MyDto(b, otherB, myThirdTwo), MyDto(c, otherC, myThirdThree)] null = 'null' inspectEquals = False # act toAssert = Serializer.convertFromObjectToObject(expected, [[MyDto]]) another = Serializer.convertFromObjectToObject([MyDto(a, otherA, [MyThirdDto(myFirst, thirdOne)]), MyDto(b, otherB, myThirdTwo), MyDto(c, otherC, myThirdThree)], [[MyDto]]) another[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my = MyDto( MyDto(RandomHelper.string(), None, None), expected[0].myThirdList[0].my.myOther, expected[0].myThirdList[0].my.myThirdList ) # assert assert expected == toAssert, f'{expected} == {toAssert}: {expected == toAssert}' assert ObjectHelper.equals(expected, toAssert) assert ObjectHelper.isNotNone(expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my) assert expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my == toAssert[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my assert ObjectHelper.equals(expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my, toAssert[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my) assert ObjectHelper.isNone(expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList) assert ObjectHelper.equals(expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList, toAssert[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList) assert ObjectHelper.equals(json.loads(Serializer.jsonifyIt(expected)), json.loads(Serializer.jsonifyIt(toAssert))), f'ObjectHelper.equals({json.loads(Serializer.jsonifyIt(expected))},\n\n{json.loads(Serializer.jsonifyIt(toAssert))})' assert False == (expected == another), f'False == ({expected} == {another}): False == {(expected == another)}' assert False == ObjectHelper.equals(expected, another, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(another, expected, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(another, toAssert, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(toAssert, another, muteLogs=not inspectEquals) assert str(expected) == str(toAssert), f'str({str(expected)}) == str({str(toAssert)}): {str(expected) == str(toAssert)}'
def sample_withSuccess(): # Arrange SAMPLE_SIZE = 2 listColletcion = [ RandomHelper.string(), RandomHelper.string(), RandomHelper.string(), RandomHelper.string(), RandomHelper.string() ] tupleColletcion = (RandomHelper.string(), RandomHelper.string(), RandomHelper.string(), RandomHelper.string(), RandomHelper.string()) setColletcion = { RandomHelper.string(minimum=1), RandomHelper.string(minimum=1), RandomHelper.string(minimum=1), RandomHelper.string(minimum=1), RandomHelper.string(minimum=1) } dictionaryColletcion = { RandomHelper.string(minimum=1): RandomHelper.string(), RandomHelper.string(minimum=1): RandomHelper.string(), RandomHelper.string(minimum=1): RandomHelper.string(), RandomHelper.string(minimum=1): RandomHelper.string(), RandomHelper.string(minimum=1): RandomHelper.string(), } # Act listElement = RandomHelper.sample(listColletcion) listSample = RandomHelper.sample(listColletcion, length=SAMPLE_SIZE) tupleElement = RandomHelper.sample(tupleColletcion) tupleSample = RandomHelper.sample(tupleColletcion, length=SAMPLE_SIZE) setElement = RandomHelper.sample(setColletcion) setSample = RandomHelper.sample(setColletcion, length=SAMPLE_SIZE) dictionaryElement = RandomHelper.sample(dictionaryColletcion) dictionarySample = RandomHelper.sample(dictionaryColletcion, length=SAMPLE_SIZE) # Assert assert listElement in listColletcion assert list == type(listSample) assert SAMPLE_SIZE == len(listSample) for element in listSample: assert element in listColletcion assert tupleElement in tupleColletcion assert tuple == type(tupleSample) assert SAMPLE_SIZE == len(tupleSample) for element in tupleSample: assert element in tupleColletcion assert setElement in setColletcion assert set == type(setSample) assert SAMPLE_SIZE == len(setSample) for element in setSample: assert element in setColletcion assert 1 == len(dictionaryElement.keys()) assert list(dictionaryElement.keys())[0] in dictionaryColletcion assert dict == type(dictionarySample) assert SAMPLE_SIZE == len(dictionarySample) for key in dictionarySample: assert key in dictionaryColletcion assert dictionarySample[key] == dictionaryColletcion[key]
def equal_whenObjects(): # arrange a = RandomHelper.string() b = RandomHelper.string() c = RandomHelper.string() otherA = MyOtherDto(RandomHelper.string()) otherB = MyOtherDto(RandomHelper.string()) otherC = MyOtherDto(RandomHelper.string()) myFirst = MyDto(None, None, None) mySecond = MyDto(None, None, None) myThird = MyDto(None, None, None) thirdOne = RandomHelper.integer() thirdTwo = RandomHelper.integer() thirdThree = RandomHelper.integer() myThirdOne = [MyThirdDto(myFirst, thirdOne)] myThirdTwo = [MyThirdDto(mySecond, thirdTwo)] myThirdThree = [MyThirdDto(myThird, thirdThree)] expected = [ MyDto(a, otherA, myThirdOne), MyDto(b, otherB, myThirdTwo), MyDto(c, otherC, myThirdThree) ] null = 'null' inspectEquals = False # act toAssert = [ MyDto(a, otherA, myThirdOne), MyDto(b, otherB, myThirdTwo), MyDto(c, otherC, myThirdThree) ] another = [ MyDto(a, otherA, [MyThirdDto(myFirst, thirdOne)]), MyDto(b, otherB, myThirdTwo), MyDto(c, otherC, myThirdThree) ] another[0].myThirdList[0].my = MyDto( MyDto(None, None, None), expected[0].myThirdList[0].my.myOther, expected[0].myThirdList[0].my.myThirdList) # assert assert False == ( expected == toAssert ), f'False == ({expected} == {toAssert}): {False == (expected == toAssert)}' assert ObjectHelper.equals(expected, toAssert) assert ObjectHelper.equals(toAssert, expected) assert ObjectHelper.isNotNone( expected[0].myThirdList[0].my), expected[0].myThirdList[0].my assert expected[0].myThirdList[0].my == toAssert[0].myThirdList[0].my assert ObjectHelper.equals(expected[0].myThirdList[0].my, toAssert[0].myThirdList[0].my) assert ObjectHelper.isNone(expected[0].myThirdList[0].my.myThirdList) assert ObjectHelper.equals(expected[0].myThirdList[0].my.myThirdList, toAssert[0].myThirdList[0].my.myThirdList) assert ObjectHelper.equals(expected[1].myThirdList[0], toAssert[1].myThirdList[0]) assert ObjectHelper.equals(toAssert[1].myThirdList[0], expected[1].myThirdList[0]) assert False == ( expected == another ), f'False == ({expected} == {another}): False == {(expected == another)}' assert False == ObjectHelper.equals(expected, another, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(another, expected, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(another, toAssert, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(toAssert, another, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(expected, [ MyDto(None, None, None), MyDto(None, None, None), MyDto(None, None, None) ])