File: test_utils.py

package info (click to toggle)
python-uuid-extension 0.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: python: 228; makefile: 2
file content (104 lines) | stat: -rw-r--r-- 3,867 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Tests for utility functions in the uuid-extension package.

This module contains tests for the utility functions provided by
the uuid-extension package, focusing on timestamp manipulation utilities.
"""

import time
from datetime import datetime, timedelta
from unittest import mock

import pytest

from uuid_extension.utils import generate_unix_ts_in_ms


class TestGenerateUnixTsInMs:
    """Tests for the generate_unix_ts_in_ms function."""

    def test_with_none_timestamp(self):
        """Test generating timestamp with None input (current time)."""
        # Mock time.time() to return a known value
        with mock.patch(
            "uuid_extension.utils.time",
            return_value=1621234567.89,
        ):
            result = generate_unix_ts_in_ms()
            # Expected: 1621234567.89 * 1000 = 1621234567890
            assert result == 1621234567890
            # Verify 48-bit constraint
            assert result <= 0xFFFFFFFFFFFF

    def test_with_float_timestamp(self):
        """Test generating timestamp from float seconds."""
        result = generate_unix_ts_in_ms(1621234567.89)
        assert result == 1621234567890
        assert result <= 0xFFFFFFFFFFFF

    def test_with_int_timestamp(self):
        """Test generating timestamp from integer seconds."""
        result = generate_unix_ts_in_ms(1621234567)
        assert result == 1621234567000
        assert result <= 0xFFFFFFFFFFFF

    def test_with_datetime_timestamp(self):
        """Test generating timestamp from datetime object."""
        dt = datetime(2021, 5, 17, 12, 34, 56)
        result = generate_unix_ts_in_ms(dt)
        # Convert datetime to expected timestamp
        expected = int(dt.timestamp() * 1000)
        assert result == expected
        assert result <= 0xFFFFFFFFFFFF

    def test_48bit_masking(self):
        """Test that timestamps are properly masked to 48 bits."""
        # Use a value just above the 48-bit limit to test masking
        # 0x1000000000000 = 2^48 = first 49-bit value
        ms_above_limit = 0x1000000000000  # 1 bit larger than 48 bits
        result = generate_unix_ts_in_ms(ms_above_limit / 1000)

        # Should be masked to zero (all bits beyond 48 are removed)
        assert result == 0

        # Test with value at exactly 48 bits (all 1's)
        max_48bit = 0xFFFFFFFFFFFF  # Max 48-bit value
        result_max = generate_unix_ts_in_ms(max_48bit / 1000)

        # Should remain unchanged after masking
        assert result_max == max_48bit
        assert hex(result_max) == "0xffffffffffff"

    def test_realtime_timestamp_range(self):
        """Test that current time is within expected range."""
        # Get timestamp slightly before our function call
        before = int(time.time() * 1000)

        result = generate_unix_ts_in_ms()

        # Get timestamp slightly after our function call
        after = int(time.time() * 1000)

        # Result should be between before and after
        assert before <= result <= after or before - 1 <= result <= after

    def test_invalid_timestamp_type(self):
        """Test handling of invalid timestamp types."""
        with pytest.raises(TypeError) as excinfo:
            generate_unix_ts_in_ms("invalid")

        assert "Unsupported timestamp type: str" in str(excinfo.value)

    def test_future_datetime(self):
        """Test with a future datetime."""
        future = datetime.now() + timedelta(days=30)
        result = generate_unix_ts_in_ms(future)
        expected = int(future.timestamp() * 1000) & 0xFFFFFFFFFFFF
        assert result == expected

    def test_past_datetime(self):
        """Test with a past datetime."""
        past = datetime(1970, 1, 2)  # One day after epoch
        result = generate_unix_ts_in_ms(past)
        expected = int(past.timestamp() * 1000) & 0xFFFFFFFFFFFF
        assert result == expected