예제 #1
0
 def __init__(
     self,
     ttl: int,
     exchange_name: str,
     currency: str,
     market: str,
     gauge: Gauge,
 ) -> None:
     self.cache = Cache(default_ttl=ttl)
     self.gauge = gauge
     self.exchange = exchanges.integrations[exchange_name]()
     self.currency = currency
     self.market = market
     log.debug(f"new pair {self}")
예제 #2
0
def test_set_with_datetime_ttl():
    cache = Cache()
    planned = datetime.now()
    cache.set("one", "test", ttl=planned)
    assert cache._items["one"].value == "test"
    assert cache._items["one"].expiration.second < planned.second + 1
    assert cache._items["one"].expiration.second > planned.second - 1
    cache = Cache(ttl=60)
    planned = datetime.now()
    cache.set("one", "test", ttl=planned)
    assert cache._items["one"].value == "test"
    assert cache._items["one"].expiration.second < planned.second + 1
    assert cache._items["one"].expiration.second > planned.second - 1
예제 #3
0
def test_set_with_timedelta_ttl():
    cache = Cache()
    delta = timedelta(seconds=10)
    planned = datetime.now() + delta
    cache.set("one", "test", ttl=delta)
    assert cache._items["one"].value == "test"
    assert cache._items["one"].expiration.second < planned.second + 1
    assert cache._items["one"].expiration.second > planned.second - 1
    cache = Cache(ttl=60)
    delta = timedelta(seconds=10)
    planned = datetime.now() + delta
    cache.set("one", "test", ttl=delta)
    assert cache._items["one"].value == "test"
    assert cache._items["one"].expiration.second < planned.second + 1
    assert cache._items["one"].expiration.second > planned.second - 1
예제 #4
0
class Pair:
    def __init__(
        self,
        ttl: int,
        exchange_name: str,
        currency: str,
        market: str,
        gauge: Gauge,
    ) -> None:
        self.cache = Cache(default_ttl=ttl)
        self.gauge = gauge
        self.exchange = exchanges.integrations[exchange_name]()
        self.currency = currency
        self.market = market
        log.debug(f"new pair {self}")

    def __repr__(self) -> str:
        return (
            f"Pair({self.cache.default_ttl}, {self.exchange.name}, "
            f"{self.currency}, {self.market})"
        )

    def get(self) -> None:
        try:
            candle = self.cache.get("candle")
        except KeyError:
            candle = self.exchange.get(
                currency=self.currency, market=self.market
            )
            self.cache.set("candle", candle)
        for olhcv in helpers.EMPTY_CANDLE.keys():
            self.gauge.labels(
                exchange=self.exchange.name,
                currency=self.currency,
                market=self.market,
                olhcv=olhcv,
            ).set(candle.__dict__[olhcv])
        log.info(f"fetched {candle} for {self}")
예제 #5
0
def test_get_expiration_with_no_ttl():
    cache = Cache()
    assert not cache._get_expiration_from_ttl(ttl=None)
    cache = Cache(ttl=60)
    planned = datetime.now()
    expiration = cache._get_expiration_from_ttl(ttl=None)
    assert isinstance(expiration, datetime)
    assert expiration.second < planned.second + 1
    assert expiration.second > planned.second - 1
예제 #6
0
def test_flush():
    cache = Cache()
    item_one = Item(value="test", expiration=None)
    item_two = Item(value="test", expiration=None)
    cache._items["one"] = item_one
    cache._items["two"] = item_two
    assert len(cache._items.keys()) == 2
    cache.flush()
    assert cache._items == {}
예제 #7
0
def test_get_expiration_with_datetime_ttl():
    cache = Cache()
    planned = datetime.now() + timedelta(seconds=10)
    expiration = cache._get_expiration_from_ttl(ttl=planned)
    assert isinstance(expiration, datetime)
    assert expiration.second < planned.second + 1
    assert expiration.second > planned.second - 1
    cache = Cache(ttl=60)
    planned = datetime.now() + timedelta(seconds=10)
    expiration = cache._get_expiration_from_ttl(ttl=planned)
    assert isinstance(expiration, datetime)
    assert expiration.second < planned.second + 1
    assert expiration.second > planned.second - 1
예제 #8
0
def test_magic_getitem_with_non_existent_item():
    cache = Cache()
    with pytest.raises(KeyError):
        cache["one"]
예제 #9
0
def test_magic_getitem_with_existent_item():
    cache = Cache()
    item = Item(value="test", expiration=None)
    cache._items["one"] = item
    assert cache["one"] == "test"
예제 #10
0
def test_magic_delitem():
    cache = Cache()
    item = Item(value="test", expiration=None)
    cache._items["one"] = item
    del cache["one"]
    assert not cache._items.get("one")
예제 #11
0
def test_repr():
    cache = Cache()
    assert f"{cache}" == "Cache(None)"
    cache = Cache(ttl=60)
    assert f"{cache}" == f"Cache({timedelta(seconds=60)})"
예제 #12
0
def test_new_with_ttl_typeerror():
    with pytest.raises(TypeError):
        Cache(ttl="str")
    with pytest.raises(TypeError):
        Cache(ttl=True)
예제 #13
0
def test_getset():
    cache = Cache()
    item = Item(value="test", expiration=None)
    cache._items["one"] = item
    assert cache.getset("one", "test2") == "test"
    assert cache._items["one"].value == "test2"
예제 #14
0
def test_get_expiration_with_ttl_typeerror():
    cache = Cache()
    with pytest.raises(TypeError):
        assert cache._get_expiration_from_ttl(ttl="test")
    with pytest.raises(TypeError):
        assert cache._get_expiration_from_ttl(ttl=True)
예제 #15
0
def test_set_with_no_ttl():
    cache = Cache()
    cache.set("one", "test")
    assert cache._items["one"].value == "test"
예제 #16
0
def test_get_with_expiration_expired():
    cache = Cache()
    item = Item(value="test", expiration=datetime.now())
    cache._items["one"] = item
    assert not cache.get("one")
예제 #17
0
def test_get_with_expiration_not_expired():
    cache = Cache()
    item = Item(value="test", expiration=datetime.now() + timedelta(seconds=60))
    cache._items["one"] = item
    assert cache.get("one") == "test"
예제 #18
0
def test_get_with_no_expiration():
    cache = Cache()
    item = Item(value="test", expiration=None)
    cache._items["one"] = item
    assert cache.get("one") == "test"
예제 #19
0
def test_new_with_no_ttl():
    cache = Cache()
    assert cache._items == {}
    assert not cache.ttl
예제 #20
0
def test_new_with_timedelta_ttl():
    cache = Cache(ttl=timedelta(seconds=60))
    assert cache._items == {}
    assert isinstance(cache.ttl, timedelta)
    assert cache.ttl == timedelta(seconds=60)