from datetime import datetime

import pytest
from pytz import UTC

from ichnaea.exceptions import GZIPDecodeError
from ichnaea import util


class TestUtil(object):

    gzip_foo = (
        b"\x1f\x8b\x08\x00\xed\x7f\x9aU\x02\xffK"
        b"\xcb\xcf\x07\x00!es\x8c\x03\x00\x00\x00"
    )

    def test_utcnow(self):
        now = util.utcnow()
        assert isinstance(now, datetime)
        assert now.tzinfo == UTC

    def test_encode_gzip(self):
        data = util.encode_gzip(b"foo")
        # Test around the 4-byte timestamp
        assert data[:4] == self.gzip_foo[:4]
        assert data[8:] == self.gzip_foo[8:]

    def test_decode_gzip(self):
        data = util.decode_gzip(self.gzip_foo)
        assert data == b"foo"

    def test_roundtrip_gzip(self):
        data = util.decode_gzip(util.encode_gzip(b"foo"))
        assert data == b"foo"

    def test_decode_gzip_error(self):
        with pytest.raises(GZIPDecodeError):
            util.decode_gzip(self.gzip_foo[:1])
        with pytest.raises(GZIPDecodeError):
            util.decode_gzip(self.gzip_foo[:5])

from datetime import datetime

import pytest
from pytz import UTC

from ichnaea.exceptions import GZIPDecodeError
from ichnaea import util


class TestUtil(object):

    gzip_foo = (
        b"\x1f\x8b\x08\x00\xed\x7f\x9aU\x00\xffK"
        b"\xcb\xcf\x07\x00!es\x8c\x03\x00\x00\x00"
    )

    def test_utcnow(self):
        now = util.utcnow()
        assert isinstance(now, datetime)
        assert now.tzinfo == UTC

    def test_encode_gzip(self):
        data = util.encode_gzip(b"foo")
        # Test around the 4-byte timestamp
        assert data[:4] == self.gzip_foo[:4]
        assert data[8:] == self.gzip_foo[8:]

    def test_decode_gzip(self):
        data = util.decode_gzip(self.gzip_foo)
        assert data == b"foo"

    def test_roundtrip_gzip(self):
        data = util.decode_gzip(util.encode_gzip(b"foo"))
        assert data == b"foo"

    def test_decode_gzip_error(self):
        with pytest.raises(GZIPDecodeError):
            util.decode_gzip(self.gzip_foo[:1])
        with pytest.raises(GZIPDecodeError):
            util.decode_gzip(self.gzip_foo[:5])

Update gzip output for Python 3.8.2
Update gzip output for Python 3.8.2

Python 3.8.2 includes a fix for https://bugs.python.org/issue39389.
Previously, the extra flags byte was always set to 0x02, claiming
maximum compression. It now reflects the actual compression setting.
Since our default is neither fastest or maximum, the expected test
output is 0x00 for the extra flags byte.
