File: test_tai64n.py

package info (click to toggle)
python-eliot 1.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: python: 8,641; makefile: 151
file content (66 lines) | stat: -rw-r--r-- 1,819 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Tests for L{eliot.tai64n}.
"""

import errno
import time
import subprocess
from unittest import TestCase, SkipTest

from ..tai64n import encode, decode


class CodecTests(TestCase):
    """
    Tests for L{encode} and L{decode}.
    """

    def test_encode(self):
        """
        L{encode} encodes timestamps in TAI64N format.
        """
        t = 1387299889.153187625
        self.assertEqual(encode(t), "@4000000052b0843b092174b9")

    def test_decode(self):
        """
        L{decode} decodes timestamps from TAI64N format.
        """
        t = time.time()
        self.assertAlmostEqual(t, decode(encode(t)), 9)


class FunctionalTests(TestCase):
    """
    Functional tests for L{encode}.
    """

    def test_encode(self):
        """
        The daemontools tai64nlocal tool can correctly decode timestamps output
        by L{encode}.
        """
        try:
            process = subprocess.Popen(
                ["tai64nlocal"],
                bufsize=4096,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
            )
        except OSError as e:
            if e.errno == errno.ENOENT:
                raise SkipTest("This test requires the daemontools package")
            else:
                raise
        # Because of limitations of the time libraries tai64nlocal uses we
        # apparently can't verify beyond this level of accuracy.
        timestamp = int(time.time()) + 0.12345
        process.stdin.write((encode(timestamp) + "\n").encode("ascii"))
        process.stdin.close()
        decodedToLocalTime = process.stdout.read().strip()
        self.assertEqual(
            time.strftime("%Y-%m-%d %H:%M:%S.12345", time.localtime(timestamp)).encode(
                "ascii"
            ),
            decodedToLocalTime[:25],
        )