def testGetHistoricalPriceAtUTCTimeStampMidOfDayWrongExchange(self): crypto = 'BTC' unit = 'USD' exchange = 'Binance' localTimeZone = 'Europe/Zurich' #time stamp is always UTC ! timeStampLocalMidDay = DateTimeUtil.dateTimeStringToTimeStamp( "2017/09/30 12:59:59", localTimeZone, "YYYY/MM/DD HH:mm:ss") timeStampUtcNoHHMM = DateTimeUtil.dateTimeStringToTimeStamp( "2017/09/30 00:00:00", 'UTC', "YYYY/MM/DD HH:mm:ss") resultData = self.priceRequester.getHistoricalPriceAtUTCTimeStamp( crypto, unit, timeStampLocalMidDay, localTimeZone, timeStampUtcNoHHMM, exchange) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_PRICE_TYPE), resultData.PRICE_TYPE_HISTO_DAY) self.assertEqual( resultData.getValue(resultData.RESULT_KEY_ERROR_MSG), "PROVIDER ERROR - Binance market does not exist for this coin pair (BTC/USD)." ) self.assertEqual(crypto, resultData.getValue(resultData.RESULT_KEY_CRYPTO)) self.assertEqual(unit, resultData.getValue(resultData.RESULT_KEY_UNIT)) self.assertEqual(exchange, resultData.getValue(resultData.RESULT_KEY_EXCHANGE))
def doAssertAcceptingOneMinuteDateTimeDifference( unitTest, nowDayStr, nowHourStr, nowMinuteStr, nowMonthStr, nowYearStr, requestResultNoEndPrice, expectedPrintResultNoDateTimeNoEndPrice): """ This method verifies that the passed real time request result requestResultNoEndPrice date/time value correspond to now +/- 60 seconds. The purpose is to avoid a test failure due to the fact that the crypto price provider was requested at, say, 11:54:59 (now value) and returns a result with time 11:55. :param unitTest: :param nowDayStr: :param nowHourStr: :param nowMinuteStr: :param nowMonthStr: :param nowYearStr: :param requestResultNoEndPrice: :param expectedPrintResultNoDateTimeNoEndPrice: :return: """ actualDateTimeStr = UtilityForTest.extractDateTimeStr( requestResultNoEndPrice) expectedDateTimeStr = '{}/{}/{} {}:{}'.format(nowDayStr, nowMonthStr, nowYearStr, nowHourStr, nowMinuteStr) actualDateTimeStamp = DateTimeUtil.dateTimeStringToTimeStamp( actualDateTimeStr, 'Europe/Zurich', 'DD/MM/YY HH:mm') expectedDateTimeStamp = DateTimeUtil.dateTimeStringToTimeStamp( expectedDateTimeStr, 'Europe/Zurich', 'DD/MM/YY HH:mm') unitTest.assertAlmostEqual(actualDateTimeStamp, expectedDateTimeStamp, delta=60) unitTest.assertEqual( expectedPrintResultNoDateTimeNoEndPrice, requestResultNoEndPrice.replace(actualDateTimeStr, ''))
def testGetHistoricalPriceAtUTCTimeStampMidOfDay(self): crypto = 'BTC' unit = 'USD' exchange = 'CCCAGG' localTimeZone = 'Europe/Zurich' timeStampLocal = DateTimeUtil.dateTimeStringToTimeStamp("2017/09/30 12:59:59", localTimeZone, "YYYY/MM/DD HH:mm:ss") timeStampUtcNoHHMM = DateTimeUtil.dateTimeStringToTimeStamp("2017/09/30 00:00:00", 'UTC', "YYYY/MM/DD HH:mm:ss") resultData = self.priceRequester.getHistoricalPriceAtUTCTimeStamp(crypto, unit, timeStampLocal, localTimeZone, timeStampUtcNoHHMM, exchange) self.assertEqual(1506729600, resultData.getValue(resultData.RESULT_KEY_PRICE_TIME_STAMP)) priceArrowUTCDateTime = DateTimeUtil.timeStampToArrowLocalDate(resultData.getValue(resultData.RESULT_KEY_PRICE_TIME_STAMP), 'UTC') self.assertEqual(resultData.getValue(resultData.RESULT_KEY_PRICE_TYPE), resultData.PRICE_TYPE_HISTO_DAY) self.assertEqual('30/09/17', priceArrowUTCDateTime.format('DD/MM/YY')) self.assertEqual(4360.62, resultData.getValue(resultData.RESULT_KEY_PRICE)) self.assertEqual(crypto, resultData.getValue(resultData.RESULT_KEY_CRYPTO)) self.assertEqual(unit, resultData.getValue(resultData.RESULT_KEY_UNIT)) self.assertEqual(exchange, resultData.getValue(resultData.RESULT_KEY_EXCHANGE))
def testGetFormattedDateTimeComponents(self): zhArrowDateTimeObjRef = DateTimeUtil.dateTimeStringToArrowLocalDate("2017/09/30 02:00:00", 'Europe/Zurich', "YYYY/MM/DD HH:mm:ss") dateTimeComponentSymbolList, separatorsList, dateTimeComponentValueList = DateTimeUtil.getFormattedDateTimeComponents(zhArrowDateTimeObjRef, 'DD/MM/YY HH:mm') self.assertEqual(['DD', 'MM', 'YY', 'HH', 'mm'], dateTimeComponentSymbolList) self.assertEqual(['/', ':'], separatorsList) self.assertEqual(['30', '09', '17', '02', '00'], dateTimeComponentValueList) dateTimeComponentSymbolList, separatorsList, dateTimeComponentValueList = DateTimeUtil.getFormattedDateTimeComponents(zhArrowDateTimeObjRef, 'YYYY.MM.DD HH.mm') self.assertEqual(['YYYY', 'MM', 'DD', 'HH', 'mm'], dateTimeComponentSymbolList) self.assertEqual(['\.', '\.'], separatorsList) self.assertEqual(['2017', '09', '30', '02', '00'], dateTimeComponentValueList) dateTimeComponentSymbolList, separatorsList, dateTimeComponentValueList = DateTimeUtil.getFormattedDateTimeComponents(zhArrowDateTimeObjRef, 'YYYY.MM.DD HH-mm') self.assertEqual(['YYYY', 'MM', 'DD', 'HH', 'mm'], dateTimeComponentSymbolList) self.assertEqual(['\.', '-'], separatorsList) self.assertEqual(['2017', '09', '30', '02', '00'], dateTimeComponentValueList) dateTimeComponentSymbolList, separatorsList, dateTimeComponentValueList = DateTimeUtil.getFormattedDateTimeComponents(zhArrowDateTimeObjRef, 'MM-DD-YYYY HH.mm') self.assertEqual(['MM', 'DD', 'YYYY', 'HH', 'mm'], dateTimeComponentSymbolList) self.assertEqual(['-', '\.'], separatorsList) self.assertEqual(['09', '30', '2017', '02', '00'], dateTimeComponentValueList)
def computeCryptoFiatRate(self, crypto, fiat, dateStr=None): ''' :raise UnsupportedCryptoFiatPairError in case the crypto fiat exchange CSV file does not have the necessary information to compute the crypto/fiat pair rate. :raise AfterNowPriceRequestDateError in case the passed dateStr is after now. @param crypto: @param fiat: @param dateStr: if not None, means that an historical rate must be obtained. Otherwise, a current rate is returned. :return crypto/fiat pair current rate ''' if dateStr is not None: nowDateArrow = DateTimeUtil.localNow(LOCAL_TIME_ZONE) requestDateArrow = DateTimeUtil.dateTimeStringToArrowLocalDate(dateStr, LOCAL_TIME_ZONE, DATE_FORMAT) if DateTimeUtil.isAfter(requestDateArrow, nowDateArrow): raise AfterNowPriceRequestDateError(dateStr) intermediateExchangeRateRequestLst = self._getIntermediateExchangeRateRequests(crypto, fiat) rateRequestNumber = len(intermediateExchangeRateRequestLst) if rateRequestNumber == 1: exchange = intermediateExchangeRateRequestLst[0][2] if exchange == '1': # the case if the crypto/fiat pair is a stablecoin/ coin fiat pair, # like USDC/USD ! return 1 else: resultData = self._getCurrentOrHistoRate(crypto, dateStr, exchange, fiat) if not self._checkIfProblem(resultData): return resultData.getValue(resultData.RESULT_KEY_PRICE) elif rateRequestNumber == 2: crypto = intermediateExchangeRateRequestLst[0][0] unit = intermediateExchangeRateRequestLst[0][1] exchange = intermediateExchangeRateRequestLst[0][2] if exchange == '1': resultData = ResultData() resultData.setValue(resultData.RESULT_KEY_PRICE, 1) else: resultData = self._getCurrentOrHistoRate(crypto, dateStr, exchange, unit) if not self._checkIfProblem(resultData): firstRate = resultData.getValue(resultData.RESULT_KEY_PRICE) crypto = intermediateExchangeRateRequestLst[1][0] fiat = intermediateExchangeRateRequestLst[1][1] exchange = intermediateExchangeRateRequestLst[1][2] resultData = self._getCurrentOrHistoRate(crypto, dateStr, exchange, fiat) if not self._checkIfProblem(resultData): secondRate = resultData.getValue(resultData.RESULT_KEY_PRICE) return firstRate * secondRate raise UnsupportedCryptoFiatPairError(crypto, fiat, self.cryptoFiatCsvFilePathName)
def testGetHistoricalPriceAtUTCTimeStampLessThanSevenDay_USD_CHF(self): crypto = 'USD' unit = 'CHF' exchange = 'CCCAGG' now = DateTimeUtil.localNow('Europe/Zurich') oneDaysBeforeArrowDate = now.shift(days=-1).date() utcArrowDateTimeObj = DateTimeUtil.localNow('UTC') utcArrowDateTimeObj = utcArrowDateTimeObj.shift(days=-1) utcArrowDateTimeStamp = DateTimeUtil.shiftTimeStampToEndOfDay( utcArrowDateTimeObj.timestamp()) # for histominute price, resultData = self.priceRequester.getHistoricalPriceAtUTCTimeStamp( crypto, unit, timeStampLocalForHistoMinute=utcArrowDateTimeStamp, localTz=None, timeStampUTCNoHHMMForHistoDay=utcArrowDateTimeStamp, exchange=exchange) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_PRICE_TYPE), resultData.PRICE_TYPE_HISTO_MINUTE) self.assertEqual(crypto, resultData.getValue(resultData.RESULT_KEY_CRYPTO)) self.assertEqual(unit, resultData.getValue(resultData.RESULT_KEY_UNIT)) self.assertEqual(exchange, resultData.getValue(resultData.RESULT_KEY_EXCHANGE))
def test_extractDateTimeFormatComponentFromDateTimeFormat(self): dateTimeComponentSymbolList, separatorsList = DateTimeUtil._extractDateTimeFormatComponentFromDateTimeFormat( 'DD/MM/YY HH:mm') self.assertEqual(['DD', 'MM', 'YY', 'HH', 'mm'], dateTimeComponentSymbolList) self.assertEqual(['/', ':'], separatorsList) dateTimeComponentSymbolList, separatorsList = DateTimeUtil._extractDateTimeFormatComponentFromDateTimeFormat( 'YYYY.MM.DD HH.mm') self.assertEqual(['YYYY', 'MM', 'DD', 'HH', 'mm'], dateTimeComponentSymbolList) self.assertEqual(['\.', '\.'], separatorsList) dateTimeComponentSymbolList, separatorsList = DateTimeUtil._extractDateTimeFormatComponentFromDateTimeFormat( 'YYYY.MM.DD HH-mm') self.assertEqual(['YYYY', 'MM', 'DD', 'HH', 'mm'], dateTimeComponentSymbolList) self.assertEqual(['\.', '-'], separatorsList) dateTimeComponentSymbolList, separatorsList = DateTimeUtil._extractDateTimeFormatComponentFromDateTimeFormat( 'MM-DD-YYYY HH.mm') self.assertEqual(['MM', 'DD', 'YYYY', 'HH', 'mm'], dateTimeComponentSymbolList) self.assertEqual(['-', '\.'], separatorsList)
def testTimestampToSummerLocalDateFR(self): timeStamp = 1506787315 arrowObjIN = DateTimeUtil.timeStampToArrowLocalDate(timeStamp, 'Asia/Calcutta') self.assertEqual("30/09/17 21:31:55 +05:30", arrowObjIN.format(FR_YY_DATE_TIME_FORMAT_TZ_ARROW)) arrowObjZH = DateTimeUtil.timeStampToArrowLocalDate(timeStamp, 'Europe/Zurich') self.assertEqual("30/09/2017 18:01:55 +02:00", arrowObjZH.format(FR_DATE_TIME_FORMAT_TZ_ARROW))
def testTimestampToWinterLocalDate(self): timeStamp = 1512057715 arrowObjIN = DateTimeUtil.timeStampToArrowLocalDate(timeStamp, 'Asia/Calcutta') self.assertEqual("17/11/30 21:31:55 +05:30", arrowObjIN.format(US_YY_DATE_TIME_FORMAT_TZ_ARROW)) arrowObjZH = DateTimeUtil.timeStampToArrowLocalDate(timeStamp, 'Europe/Zurich') self.assertEqual("2017/11/30 17:01:55 +01:00", arrowObjZH.format(US_DATE_TIME_FORMAT_TZ_ARROW))
def testIsAfterDateBefore(self): zhArrowDateTimeObjRef = DateTimeUtil.dateTimeStringToArrowLocalDate( "2017/09/30 02:00:00", 'Europe/Zurich', "YYYY/MM/DD HH:mm:ss") zhArrowDateTimeObjOneSecBefore = DateTimeUtil.dateTimeStringToArrowLocalDate( "2017/09/30 01:59:59", 'Europe/Zurich', "YYYY/MM/DD HH:mm:ss") self.assertFalse( DateTimeUtil.isAfter(zhArrowDateTimeObjOneSecBefore, zhArrowDateTimeObjRef))
def testIsAfterOneSecond(self): zhArrowDateTimeObjRef = DateTimeUtil.dateTimeStringToArrowLocalDate( "2017/09/30 02:00:00", 'Europe/Zurich', "YYYY/MM/DD HH:mm:ss") zhArrowDateTimeObjOneSecAfter = DateTimeUtil.dateTimeStringToArrowLocalDate( "2017/09/30 02:00:01", 'Europe/Zurich', "YYYY/MM/DD HH:mm:ss") self.assertTrue( DateTimeUtil.isAfter(zhArrowDateTimeObjOneSecAfter, zhArrowDateTimeObjRef))
def testShiftTimeStampToEndOfDay(self): timeStamp = 1506787315 #30/09/2017 16:01:55 +00:00 or 30/09/2017 18:01:55 +02:00 timeStampEndOfDay = DateTimeUtil.shiftTimeStampToEndOfDay(timeStamp) arrowObjUTCEndOfDay = DateTimeUtil.timeStampToArrowLocalDate(timeStampEndOfDay, 'UTC') self.assertEqual("2017/09/30 23:59:59 +00:00", arrowObjUTCEndOfDay.format(US_DATE_TIME_FORMAT_TZ_ARROW)) arrowObjZHEndOfDay = DateTimeUtil.timeStampToArrowLocalDate(timeStampEndOfDay, 'Europe/Zurich') self.assertEqual("2017/10/01 01:59:59 +02:00", arrowObjZHEndOfDay.format(US_DATE_TIME_FORMAT_TZ_ARROW))
def testExecuteHistoricalPriceNoYear(self): testDayStr = '1' testMonthStr = '1' testHourStr = '01' testMinuteStr = '15' testTimeZoneStr = 'Europe/Zurich' self.commandPrice.parsedParmData[self.commandPrice.CRYPTO] = 'btc' self.commandPrice.parsedParmData[self.commandPrice.FIAT] = 'usd' self.commandPrice.parsedParmData[ self.commandPrice.EXCHANGE] = 'bittrex' self.commandPrice.parsedParmData[self.commandPrice.DAY] = testDayStr self.commandPrice.parsedParmData[ self.commandPrice.MONTH] = testMonthStr #self.commandPrice.parsedParmData[self.commandPrice.YEAR] = '2017' self.commandPrice.parsedParmData[self.commandPrice.HOUR] = testHourStr self.commandPrice.parsedParmData[ self.commandPrice.MINUTE] = testMinuteStr resultData = self.commandPrice.execute() now = DateTimeUtil.localNow(testTimeZoneStr) fourDigitYear = now.year nowYear = fourDigitYear - 2000 nowYearStr = str(nowYear) testDateTime = DateTimeUtil.dateTimeComponentsToArrowLocalDate( int(testDayStr), int(testMonthStr), fourDigitYear, int(testHourStr), int(testMinuteStr), 0, testTimeZoneStr) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_ERROR_MSG), None) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_CRYPTO), 'BTC') self.assertEqual(resultData.getValue(resultData.RESULT_KEY_FIAT), 'USD') self.assertEqual(resultData.getValue(resultData.RESULT_KEY_EXCHANGE), 'BitTrex') if DateTimeUtil.isDateOlderThan(testDateTime, 7): self.assertEqual( resultData.getValue(resultData.RESULT_KEY_PRICE_TYPE), resultData.PRICE_TYPE_HISTO_DAY) self.assertEqual( resultData.getValue( resultData.RESULT_KEY_PRICE_DATE_TIME_STRING), '01/01/{} 00:00'.format(nowYearStr)) else: self.assertEqual( resultData.getValue(resultData.RESULT_KEY_PRICE_TYPE), resultData.PRICE_TYPE_HISTO_MINUTE) self.assertEqual( resultData.getValue( resultData.RESULT_KEY_PRICE_DATE_TIME_STRING), '01/01/{} {}:{}'.format(nowYearStr, testHourStr, testMinuteStr))
def testConvertToTimeZoneWinter(self): locDateStr = '2017/11/30 09:00:00' datetimeObjLA = DateTimeUtil.dateTimeStringToArrowLocalDate(locDateStr, 'US/Pacific', US_DATE_TIME_FORMAT_ARROW) self.assertEqual('2017/11/30 09:00:00 -08:00', datetimeObjLA.format(US_DATE_TIME_FORMAT_TZ_ARROW)) datetimeObjZH = DateTimeUtil.convertToTimeZone(datetimeObjLA, 'Europe/Zurich') self.assertEqual('2017/11/30 18:00:00 +01:00', datetimeObjZH.format(US_DATE_TIME_FORMAT_TZ_ARROW)) datetimeObjIN = DateTimeUtil.convertToTimeZone(datetimeObjLA, 'Asia/Calcutta') self.assertEqual('2017/11/30 22:30:00 +05:30', datetimeObjIN.format(US_DATE_TIME_FORMAT_TZ_ARROW))
def testShiftTimeStampToEndOfDay_alt(self): utcArrowDateTimeObj_begOfDay = DateTimeUtil.dateTimeStringToArrowLocalDate("2017/09/30 00:00:00", 'UTC', "YYYY/MM/DD HH:mm:ss") timeStampBegDay = utcArrowDateTimeObj_begOfDay.timestamp utcArrowDateTimeObj_endOfDay = DateTimeUtil.dateTimeStringToArrowLocalDate("2017/09/30 23:59:59", 'UTC', "YYYY/MM/DD HH:mm:ss") timeStampEndDay = utcArrowDateTimeObj_endOfDay.timestamp timeStampShifted = DateTimeUtil.shiftTimeStampToEndOfDay(timeStampBegDay) self.assertEqual(timeStampShifted, timeStampEndDay)
def testSummerDateTimeStringToTimeStampUS(self): expTimeStamp = 1506787315 dateStr = '30/09/17 21:31:55' timeStamp = DateTimeUtil.dateTimeStringToTimeStamp(dateStr, 'Asia/Calcutta', FR_YY_DATE_TIME_FORMAT_ARROW) self.assertEqual(expTimeStamp, timeStamp) dateStr = '30/09/2017 18:01:55' timeStamp = DateTimeUtil.dateTimeStringToTimeStamp(dateStr, 'Europe/Zurich', FR_DATE_TIME_FORMAT_ARROW) self.assertEqual(expTimeStamp, timeStamp)
def testWinterDateTimeStringToTimeStamp(self): expTimeStamp = 1512061315 dateStr = '2017/11/30 22:31:55' timeStamp = DateTimeUtil.dateTimeStringToTimeStamp(dateStr, 'Asia/Calcutta', US_DATE_TIME_FORMAT_ARROW) self.assertEqual(expTimeStamp, timeStamp) dateStr = '2017/11/30 18:01:55' timeStamp = DateTimeUtil.dateTimeStringToTimeStamp(dateStr, 'Europe/Zurich', US_DATE_TIME_FORMAT_ARROW) self.assertEqual(expTimeStamp, timeStamp)
def testSummerDateTimeStringToArrowLocalDate(self): expTimeStamp = 1506787315 dateStr = '2017/09/30 21:31:55' arrowDateObj = DateTimeUtil.dateTimeStringToArrowLocalDate(dateStr, 'Asia/Calcutta', US_DATE_TIME_FORMAT_ARROW) self.assertEqual(expTimeStamp, arrowDateObj.timestamp) dateStr = '2017/09/30 18:01:55' arrowDateObj = DateTimeUtil.dateTimeStringToArrowLocalDate(dateStr, 'Europe/Zurich', US_DATE_TIME_FORMAT_ARROW) self.assertEqual(expTimeStamp, arrowDateObj.timestamp)
def _buildFullDateAndTimeStrings(self, commandDic, timezoneStr): ''' This method ensures that the full command string is unified whatever the completness of the dated/time components specified in the request by the user. Ex: btc usd 1/1 bitfinex or btc usd 1/01/18 bitfinex or btc usd 1/1 12:23 bitfinex all return a full commaand of btc usd 01/01/18 00:00 bitfinex, btc usd 01/01/18 12:23 bitfinex respectively. This is important since the ful command string is what is stored in the command history list, with no duplicates. Otherwxise, btc usd 1/1 00:00 bitfinex and btc usd 01/01/18 00:00 bitfinex would be stored as 2 entries ! :param commandDic: :param timezoneStr: :seqdiag_return requestDateDMY, requestDateHM :return: ''' dayInt = int(commandDic[CommandPrice.DAY]) monthInt = int(commandDic[CommandPrice.MONTH]) year = commandDic[CommandPrice.YEAR] if year == None: now = DateTimeUtil.localNow(timezoneStr) yearInt = now.year else: yearInt = int(year) hour = commandDic[CommandPrice.HOUR] minute = commandDic[CommandPrice.MINUTE] if hour != None and minute != None: # hour can not exist without minute and vice versa ! hourInt = int(hour) minuteInt = int(minute) else: hourInt = 0 minuteInt = 0 requestArrowDate = DateTimeUtil.dateTimeComponentsToArrowLocalDate( dayInt, monthInt, yearInt, hourInt, minuteInt, 0, timezoneStr) dateTimeComponentSymbolList, separatorsList, dateTimeComponentValueList = DateTimeUtil.getFormattedDateTimeComponents( requestArrowDate, self.configurationMgr.dateTimeFormat) dateSeparator = separatorsList[0] timeSeparator = separatorsList[1] requestDateDMY = dateTimeComponentValueList[0] + dateSeparator + dateTimeComponentValueList[1] + dateSeparator + \ dateTimeComponentValueList[2] requestDateHM = dateTimeComponentValueList[ 3] + timeSeparator + dateTimeComponentValueList[4] from seqdiagbuilder import SeqDiagBuilder SeqDiagBuilder.recordFlow() return requestDateDMY, requestDateHM
def testWinterDateTimeStringToArrowLocalDate(self): expTimeStamp = 1512061315 dateStr = '2017/11/30 22:31:55' arrowDateObj = DateTimeUtil.dateTimeStringToArrowLocalDate( dateStr, 'Asia/Calcutta', US_DATE_TIME_FORMAT_ARROW) self.assertEqual(expTimeStamp, arrowDateObj.timestamp()) dateStr = '2017/11/30 18:01:55' arrowDateObj = DateTimeUtil.dateTimeStringToArrowLocalDate( dateStr, 'Europe/Zurich', US_DATE_TIME_FORMAT_ARROW) self.assertEqual(expTimeStamp, arrowDateObj.timestamp())
def testGetCryptoPriceRealTimeExchangeNotSupportPair(self): now = DateTimeUtil.localNow('Europe/Zurich') crypto = 'BTC' fiat = 'USD' exchange = 'BTC38' day = 0 month = 0 year = 0 hour = 0 minute = 0 resultData = self.processor.getCryptoPrice(crypto, fiat, exchange, day, month, year, hour, minute) self.assertEqual( resultData.getValue(resultData.RESULT_KEY_ERROR_MSG), "PROVIDER ERROR - BTC38 market does not exist for this coin pair (BTC-USD)" ) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_CRYPTO), crypto) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_FIAT), fiat) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_EXCHANGE), exchange) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_PRICE_TYPE), resultData.PRICE_TYPE_RT) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_PRICE), None) self.assertEqual( resultData.getValue(resultData.RESULT_KEY_PRICE_DATE_TIME_STRING), None) self.assertEqual( resultData.getValue(resultData.RESULT_KEY_PRICE_TIME_STAMP), None)
def testGetCryptoPriceRealTimeWrongExchange(self): now = DateTimeUtil.localNow('Europe/Zurich') crypto = 'BTC' fiat = 'USD' exchange = 'unknown' day = 0 month = 0 year = 0 hour = 1 minute = 1 resultData = self.processor.getCryptoPrice(crypto, fiat, exchange, day, month, year, hour, minute) self.assertEqual( resultData.getValue(resultData.RESULT_KEY_ERROR_MSG), "ERROR - unknown market does not exist for this coin pair (BTC-USD)" ) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_CRYPTO), None) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_FIAT), None) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_EXCHANGE), None) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_PRICE_TYPE), None) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_PRICE), None) self.assertEqual( resultData.getValue(resultData.RESULT_KEY_PRICE_DATE_TIME_STRING), None) self.assertEqual( resultData.getValue(resultData.RESULT_KEY_PRICE_TIME_STAMP), None)
def testDateTimeStringToArrowLocalDate(self): zhArrowDateTimeObj_begOfDay = DateTimeUtil.dateTimeStringToArrowLocalDate( "2017/09/30 02:00:00", 'Europe/Zurich', "YYYY/MM/DD HH:mm:ss") self.assertEqual(1506729600, zhArrowDateTimeObj_begOfDay.timestamp()) self.assertEqual( "2017/09/30 02:00:00 +02:00", zhArrowDateTimeObj_begOfDay.format(US_DATE_TIME_FORMAT_TZ_ARROW))
def testDateTimeComponentsToArrowLocalDate(self): zhArrowDateTimeObj_begOfDay = DateTimeUtil.dateTimeComponentsToArrowLocalDate( 30, 9, 2017, 2, 0, 0, 'Europe/Zurich') self.assertEqual(1506729600, zhArrowDateTimeObj_begOfDay.timestamp()) self.assertEqual( "2017/09/30 02:00:00 +02:00", zhArrowDateTimeObj_begOfDay.format(US_DATE_TIME_FORMAT_TZ_ARROW))
def testGetHistoricalPriceAtUTCTimeStampMoreThanSevenDayForCryptoUnitPairNotSupportedByExchange( self): crypto = 'BTC' unit = 'USD' exchange = 'Binance' utcArrowDateTimeObj = DateTimeUtil.localNow('UTC') utcArrowDateTimeObj = utcArrowDateTimeObj.shift(days=-12) #here, since histominute price is fetched, time stamp UTC no HHMM for histoDay wil not be used ! resultData = self.priceRequester.getHistoricalPriceAtUTCTimeStamp( crypto, unit, timeStampLocalForHistoMinute=utcArrowDateTimeObj.timestamp(), localTz=None, timeStampUTCNoHHMMForHistoDay=utcArrowDateTimeObj.timestamp(), exchange=exchange) self.assertEqual(resultData.getValue(resultData.RESULT_KEY_PRICE_TYPE), resultData.PRICE_TYPE_HISTO_DAY) self.assertEqual( resultData.getValue(resultData.RESULT_KEY_ERROR_MSG), "PROVIDER ERROR - Binance market does not exist for this coin pair (BTC/USD)." ) self.assertEqual(crypto, resultData.getValue(resultData.RESULT_KEY_CRYPTO)) self.assertEqual(unit, resultData.getValue(resultData.RESULT_KEY_UNIT)) self.assertEqual(exchange, resultData.getValue(resultData.RESULT_KEY_EXCHANGE))
def testExecuteHistoricalPriceNoMonth(self): self.commandPrice.parsedParmData[self.commandPrice.CRYPTO] = 'btc' self.commandPrice.parsedParmData[self.commandPrice.FIAT] = 'usd' self.commandPrice.parsedParmData[ self.commandPrice.EXCHANGE] = 'bittrex' self.commandPrice.parsedParmData[self.commandPrice.DAY] = '1' self.commandPrice.parsedParmData[self.commandPrice.MONTH] = None self.commandPrice.parsedParmData[self.commandPrice.YEAR] = None self.commandPrice.parsedParmData[self.commandPrice.HOUR] = '10' self.commandPrice.parsedParmData[self.commandPrice.MINUTE] = '5' resultData = self.commandPrice.execute() now = DateTimeUtil.localNow('Europe/Zurich') nowMonth = now.month if nowMonth < 10: nowMonthStr = '0' + str(nowMonth) else: nowMonthStr = str(nowMonth) nowYear = now.year - 2000 nowYearStr = str(nowYear) self.assertIsNone(resultData.getValue(resultData.RESULT_KEY_ERROR_MSG))
def getFormattedNowDateTimeComponents(self): now = DateTimeUtil.localNow('Europe/Zurich') nowMinute = now.minute if nowMinute < 10: if nowMinute > 0: nowMinuteStr = '0' + str(nowMinute) else: nowMinuteStr = '00' else: nowMinuteStr = str(nowMinute) nowHour = now.hour if nowHour < 10: if nowHour > 0: nowHourStr = '0' + str(nowHour) else: nowHourStr = '00' else: nowHourStr = str(nowHour) nowDay = now.day if nowDay < 10: nowDayStr = '0' + str(nowDay) else: nowDayStr = str(nowDay) nowMonth = now.month if nowMonth < 10: nowMonthStr = '0' + str(nowMonth) else: nowMonthStr = str(nowMonth) return now, nowDayStr, nowMonthStr, nowHourStr, nowMinuteStr
def _buildFullDateAndTimeStrings(self, commandDic, timezoneStr): ''' This method ensures that the full command string is unified whatever the completness of the dated/time components specified in the request by the user. Ex: btc usd 1/1 bitfinex or btc usd 1/01/18 bitfinex or btc usd 1/1 12:23 bitfinex all return a full commaand of btc usd 01/01/18 00:00 bitfinex, btc usd 01/01/18 12:23 bitfinex respectively. This is important since the ful command string is what is stored in the command history list, with no duplicates. Otherwxise, btc usd 1/1 00:00 bitfinex and btc usd 01/01/18 00:00 bitfinex would be stored as 2 entries ! :param commandDic: :param timezoneStr: :seqdiag_return requestDateDMY, requestDateHM :return: ''' day = commandDic[CommandPrice.DAY] month = commandDic[CommandPrice.MONTH] year = commandDic[CommandPrice.YEAR] hour = commandDic[CommandPrice.HOUR] minute = commandDic[CommandPrice.MINUTE] requestDateDMY, requestDateHM = DateTimeUtil.formatPrintDateTimeFromStringComponents( day, month, year, hour, minute, timezoneStr, self.configurationMgr.dateTimeFormat) from seqdiagbuilder import SeqDiagBuilder SeqDiagBuilder.recordFlow() return requestDateDMY, requestDateHM
def getCurrentPrice(self, crypto, unit, exchange): url = "https://min-api.cryptocompare.com/data/price?fsym={}&tsyms={}&e={}".format(crypto, unit, exchange) resultData = ResultData() resultData.setValue(ResultData.RESULT_KEY_CRYPTO, crypto) resultData.setValue(ResultData.RESULT_KEY_UNIT, unit) resultData.setValue(ResultData.RESULT_KEY_EXCHANGE, exchange) resultData.setValue(ResultData.RESULT_KEY_PRICE_TYPE, resultData.PRICE_TYPE_RT) try: if self.ctx == None: #here, run in QPython under Python 3.2 webURL = urllib.request.urlopen(url) else: webURL = urllib.request.urlopen(url, context=self.ctx) except HTTPError as e: resultData.setError('ERROR - could not complete request ' + url + '. Reason: ' + str(e.reason) + '.') except URLError as e: resultData.setError('ERROR - No internet. Please connect and retry !') except: the_type, the_value, the_traceback = sys.exc_info() resultData.setError('ERROR - could not complete request ' + url + '. Reason: ' + str(the_type) + '.') else: page = webURL.read() soup = BeautifulSoup(page, 'html.parser') dic = json.loads(soup.prettify()) if unit in dic: resultData.setValue(ResultData.RESULT_KEY_PRICE_TIME_STAMP, DateTimeUtil.utcNowTimeStamp()) resultData.setValue(ResultData.RESULT_KEY_PRICE, dic[unit]) #current price is indexed by unit symbol in returned dic else: resultData = self._handleProviderError(dic, resultData, url, crypto, unit, exchange, isRealTime=True) return resultData
def testGetCryptoPriceRealTimeExchangeNotSupportPair(self): now = DateTimeUtil.localNow('Europe/Zurich') crypto = 'BTC' fiat = 'USD' exchange = 'BTC38' day = 0 month = 0 year = 0 hour = 1 minute = 1 resultData = ResultData() resultData.setValue( resultData.RESULT_KEY_ERROR_MSG, "ERROR - BTC38 market does not exist for this coin pair (BTC-USD)") resultData.setValue(resultData.RESULT_KEY_CRYPTO, crypto) resultData.setValue(resultData.RESULT_KEY_FIAT, fiat) resultData.setValue(resultData.RESULT_KEY_EXCHANGE, exchange) resultData.setValue(resultData.RESULT_KEY_PRICE_TYPE, resultData.PRICE_TYPE_RT) resultData.setValue(resultData.RESULT_KEY_PRICE, None) resultData.setValue(resultData.RESULT_KEY_PRICE_DATE_TIME_STRING, None) resultData.setValue(resultData.RESULT_KEY_PRICE_TIME_STAMP, None) stdout = sys.stdout capturedStdout = StringIO() sys.stdout = capturedStdout self.printer.printDataToConsole(resultData) sys.stdout = stdout self.assertEqual( "ERROR - BTC38 market does not exist for this coin pair (BTC-USD)\n", capturedStdout.getvalue())