import os
import sys

root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
sys.path.append(root)

# ----------------------------------------------------------------------------

# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

# ----------------------------------------------------------------------------
# -*- coding: utf-8 -*-

import ccxt  # noqa: F402
from ccxt.base.precise import Precise  # noqa E402

def precise_equal_str(exchange, result, key, expected):
    return Precise.string_eq(exchange.safe_string(result, key), expected)


def test_safe_ticker():
    exchange = ccxt.Exchange({
        'id': 'sampleexchange',
    })
    # CASE 1 - by open
    ticker1 = {
        'open': 5,
        'change': 1,
    }
    result1 = exchange.safe_ticker(ticker1)
    assert precise_equal_str(exchange, result1, 'percentage', '20.0')
    assert precise_equal_str(exchange, result1, 'average', '5.5')
    assert precise_equal_str(exchange, result1, 'close', '6.0')
    assert precise_equal_str(exchange, result1, 'last', '6.0')
    # CASE 2 - by open
    ticker2 = {
        'open': 5,
        'percentage': 20,
    }
    result2 = exchange.safe_ticker(ticker2)
    assert precise_equal_str(exchange, result2, 'change', '1.0')
    assert precise_equal_str(exchange, result2, 'average', '5.5')
    assert precise_equal_str(exchange, result2, 'close', '6.0')
    assert precise_equal_str(exchange, result2, 'last', '6.0')
    # CASE 3 - by close
    ticker3 = {
        'close': 6,
        'change': 1,
    }
    result3 = exchange.safe_ticker(ticker3)
    assert precise_equal_str(exchange, result3, 'open', '5.0')
    assert precise_equal_str(exchange, result3, 'percentage', '20.0')
    assert precise_equal_str(exchange, result3, 'average', '5.5')
    assert precise_equal_str(exchange, result3, 'last', '6.0')
    # CASE 4 - by close
    ticker4 = {
        'close': 6,
        'percentage': 20,
    }
    result4 = exchange.safe_ticker(ticker4)
    assert precise_equal_str(exchange, result4, 'open', '5.0')
    assert precise_equal_str(exchange, result4, 'change', '1.0')
    assert precise_equal_str(exchange, result4, 'average', '5.5')
    assert precise_equal_str(exchange, result4, 'last', '6.0')
    # CASE 5 - by average
    ticker5 = {
        'average': 5.5,
        'percentage': 20,
    }
    result5 = exchange.safe_ticker(ticker5)
    assert precise_equal_str(exchange, result5, 'open', '5.0')
    assert precise_equal_str(exchange, result5, 'change', '1.0')
    assert precise_equal_str(exchange, result5, 'close', '6.0')
    assert precise_equal_str(exchange, result5, 'last', '6.0')
    # CASE 6
    ticker6 = {
        'average': 5.5,
        'change': 1,
    }
    result6 = exchange.safe_ticker(ticker6)
    assert precise_equal_str(exchange, result6, 'open', '5.0')
    assert precise_equal_str(exchange, result6, 'percentage', '20.0')
    assert precise_equal_str(exchange, result6, 'close', '6.0')
    assert precise_equal_str(exchange, result6, 'last', '6.0')
    # CASE 7 - by open and close
    ticker7 = {
        'open': 5,
        'close': 6,
    }
    result7 = exchange.safe_ticker(ticker7)
    assert precise_equal_str(exchange, result7, 'change', '1.0')
    assert precise_equal_str(exchange, result7, 'percentage', '20.0')
    assert precise_equal_str(exchange, result7, 'average', '5.5')
    assert precise_equal_str(exchange, result7, 'last', '6.0')
    # CASE 8 - full ticker
    ticker8 = {
        'open': 5,
        'close': 6,
        'last': 6,
        'high': 6.5,
        'low': 4.5,
        'average': 5.5,
        'bid': 5.9,
        'bidVolume': 100,
        'ask': 6.1,
        'askVolume': 200,
        'change': 1,
        'percentage': 20,
        'vwap': 5.75,
        'baseVolume': 1000,
        'quoteVolume': 5750,
        'previousClose': 4.9,
        'indexPrice': 5.8,
        'markPrice': 5.9,
        'info': {},
    }
    result8 = exchange.safe_ticker(ticker8)
    assert precise_equal_str(exchange, result8, 'open', '5.0')
    assert precise_equal_str(exchange, result8, 'high', '6.5')
    assert precise_equal_str(exchange, result8, 'low', '4.5')
    assert precise_equal_str(exchange, result8, 'close', '6.0')
    assert precise_equal_str(exchange, result8, 'last', '6.0')
    assert precise_equal_str(exchange, result8, 'change', '1.0')
    assert precise_equal_str(exchange, result8, 'percentage', '20.0')
    assert precise_equal_str(exchange, result8, 'average', '5.5')
    assert precise_equal_str(exchange, result8, 'bid', '5.9')
    assert precise_equal_str(exchange, result8, 'bidVolume', '100.0')
    assert precise_equal_str(exchange, result8, 'ask', '6.1')
    assert precise_equal_str(exchange, result8, 'askVolume', '200.0')
    assert precise_equal_str(exchange, result8, 'vwap', '5.75')
    assert precise_equal_str(exchange, result8, 'baseVolume', '1000.0')
    assert precise_equal_str(exchange, result8, 'quoteVolume', '5750.0')
    assert precise_equal_str(exchange, result8, 'previousClose', '4.9')
    assert precise_equal_str(exchange, result8, 'indexPrice', '5.8')
    assert precise_equal_str(exchange, result8, 'markPrice', '5.9')
    assert result8['info'] is not None
