| 1 | from datetime import datetime, timedelta, timezone |
| 2 | |
| 3 | import arrow |
| 4 | import pytest |
| 5 | |
| 6 | from freqtrade.persistence import PairLocks |
| 7 | from freqtrade.persistence.models import PairLock |
| 8 | |
| 9 | |
| 10 | @pytest.mark.parametrize('use_db', (False, True)) |
| 11 | @pytest.mark.usefixtures("init_persistence") |
| 12 | def test_PairLocks(use_db): |
| 13 | PairLocks.timeframe = '5m' |
| 14 | PairLocks.use_db = use_db |
| 15 | # No lock should be present |
| 16 | if use_db: |
| 17 | assert len(PairLock.query.all()) == 0 |
| 18 | |
| 19 | assert PairLocks.use_db == use_db |
| 20 | |
| 21 | pair = 'ETH/BTC' |
| 22 | assert not PairLocks.is_pair_locked(pair) |
| 23 | PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) |
| 24 | # ETH/BTC locked for 4 minutes |
| 25 | assert PairLocks.is_pair_locked(pair) |
| 26 | |
| 27 | # XRP/BTC should not be locked now |
| 28 | pair = 'XRP/BTC' |
| 29 | assert not PairLocks.is_pair_locked(pair) |
| 30 | # Unlocking a pair that's not locked should not raise an error |
| 31 | PairLocks.unlock_pair(pair) |
| 32 | |
| 33 | PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) |
| 34 | assert PairLocks.is_pair_locked(pair) |
| 35 | |
| 36 | # Get both locks from above |
| 37 | locks = PairLocks.get_pair_locks(None) |
| 38 | assert len(locks) == 2 |
| 39 | |
| 40 | # Unlock original pair |
| 41 | pair = 'ETH/BTC' |
| 42 | PairLocks.unlock_pair(pair) |
| 43 | assert not PairLocks.is_pair_locked(pair) |
| 44 | assert not PairLocks.is_global_lock() |
| 45 | |
| 46 | pair = 'BTC/USDT' |
| 47 | # Lock until 14:30 |
| 48 | lock_time = datetime(2020, 5, 1, 14, 30, 0, tzinfo=timezone.utc) |
| 49 | PairLocks.lock_pair(pair, lock_time) |
| 50 | |
| 51 | assert not PairLocks.is_pair_locked(pair) |
| 52 | assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-10)) |
| 53 | assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-10)) |
| 54 | assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-50)) |
| 55 | assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-50)) |
| 56 | |
| 57 | # Should not be locked after time expired |
| 58 | assert not PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=10)) |
| 59 | |
| 60 | locks = PairLocks.get_pair_locks(pair, lock_time + timedelta(minutes=-2)) |
| 61 | assert len(locks) == 1 |
| 62 | assert 'PairLock' in str(locks[0]) |
| 63 | |
| 64 | # Unlock all |
| 65 | PairLocks.unlock_pair(pair, lock_time + timedelta(minutes=-2)) |
| 66 | assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-50)) |
| 67 | |
| 68 | # Global lock |
| 69 | PairLocks.lock_pair('*', lock_time) |
| 70 | assert PairLocks.is_global_lock(lock_time + timedelta(minutes=-50)) |
| 71 | # Global lock also locks every pair seperately |
| 72 | assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-50)) |
| 73 | assert PairLocks.is_pair_locked('XRP/USDT', lock_time + timedelta(minutes=-50)) |
| 74 | |
| 75 | if use_db: |
| 76 | locks = PairLocks.get_all_locks() |
| 77 | locks_db = PairLock.query.all() |
| 78 | assert len(locks) == len(locks_db) |
| 79 | assert len(locks_db) > 0 |
| 80 | else: |
| 81 | # Nothing was pushed to the database |
| 82 | assert len(PairLocks.get_all_locks()) > 0 |
| 83 | assert len(PairLock.query.all()) == 0 |
| 84 | # Reset use-db variable |
| 85 | PairLocks.reset_locks() |
| 86 | PairLocks.use_db = True |
| 87 | |
| 88 | |
| 89 | @pytest.mark.parametrize('use_db', (False, True)) |
| 90 | @pytest.mark.usefixtures("init_persistence") |
| 91 | def test_PairLocks_getlongestlock(use_db): |
| 92 | PairLocks.timeframe = '5m' |
| 93 | # No lock should be present |
| 94 | PairLocks.use_db = use_db |
| 95 | if use_db: |
| 96 | assert len(PairLock.query.all()) == 0 |
| 97 | |
| 98 | assert PairLocks.use_db == use_db |
| 99 | |
| 100 | pair = 'ETH/BTC' |
| 101 | assert not PairLocks.is_pair_locked(pair) |
| 102 | PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) |
| 103 | # ETH/BTC locked for 4 minutes |
| 104 | assert PairLocks.is_pair_locked(pair) |
| 105 | lock = PairLocks.get_pair_longest_lock(pair) |
| 106 | |
| 107 | assert lock.lock_end_time.replace(tzinfo=timezone.utc) > arrow.utcnow().shift(minutes=3) |
| 108 | assert lock.lock_end_time.replace(tzinfo=timezone.utc) < arrow.utcnow().shift(minutes=14) |
| 109 | |
| 110 | PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=15).datetime) |
| 111 | assert PairLocks.is_pair_locked(pair) |
| 112 | |
| 113 | lock = PairLocks.get_pair_longest_lock(pair) |
| 114 | # Must be longer than above |
| 115 | assert lock.lock_end_time.replace(tzinfo=timezone.utc) > arrow.utcnow().shift(minutes=14) |
| 116 | |
| 117 | PairLocks.reset_locks() |
| 118 | PairLocks.use_db = True |