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

def test_remove_repeated_elements_from_array():
    exchange = ccxt.Exchange({
        'id': 'sampleexchange',
    })
    # CASE 1: by id
    array1 = [{
    'id': 'a',
    'timestamp': 1,
    'uniq': 'x1',
}, {
    'id': 'b',
    'timestamp': 2,
    'uniq': 'x2',
}, {
    'id': 'a',
    'timestamp': 3,
    'uniq': 'x3',
}, {
    'id': 'c',
    'timestamp': 1,
    'uniq': 'x4',
}]
    res1 = exchange.remove_repeated_elements_from_array(array1, False)
    res1_length = len(res1)
    assert res1_length == 3
    assert res1[0]['uniq'] == 'x1'
    assert res1[1]['uniq'] == 'x2'
    assert res1[2]['uniq'] == 'x4'
    # CASE 2: by timestamp
    array2 = [{
    'id': None,
    'timestamp': 1,
    'uniq': 'x1',
}, {
    'id': None,
    'timestamp': 2,
    'uniq': 'x2',
}, {
    'id': None,
    'timestamp': 1,
    'uniq': 'x3',
}, {
    'id': None,
    'timestamp': 3,
    'uniq': 'x4',
}]
    res2 = exchange.remove_repeated_elements_from_array(array2, True)
    res2_length = len(res2)
    assert res2_length == 3
    assert res2[0]['uniq'] == 'x1'
    assert res2[1]['uniq'] == 'x2'
    assert res2[2]['uniq'] == 'x4'
    # CASE 3: by timestamp index (used in ohlcv)
    array3 = [[555, 1, 1, 'x1'], [666, 1, 1, 'x2'], [555, 1, 1, 'x3']]
    res3 = exchange.remove_repeated_elements_from_array(array3, True)
    assert len(res3) == 2
    assert res3[0][3] == 'x1'
    assert res3[1][3] == 'x2'
