|
1 | | -# tests/test_snowflake_utils.py |
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2021-present Pycord Development |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +copy of this software and associated documentation files (the "Software"), |
| 8 | +to deal in the Software without restriction, including without limitation |
| 9 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +Software is furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +DEALINGS IN THE SOFTWARE. |
| 23 | +""" |
2 | 24 |
|
3 | 25 | import datetime |
4 | 26 | import pytest |
|
18 | 40 |
|
19 | 41 | @pytest.mark.parametrize(("dt", "expected_ms"), DATETIME_CASES) |
20 | 42 | def test_generate_snowflake_realistic(dt, expected_ms): |
21 | | - """Realistic mode should set lower 22 bits to 0x3FFFFF.""" |
22 | 43 | sf = generate_snowflake(dt, mode="realistic") |
23 | | - # top bits are the timestamp |
24 | 44 | assert (sf >> 22) == expected_ms |
25 | | - # lower 22 bits are all ones in realistic mode |
26 | 45 | assert (sf & ((1 << 22) - 1)) == 0x3FFFFF |
27 | 46 |
|
28 | 47 |
|
29 | 48 | @pytest.mark.parametrize(("dt", "expected_ms"), DATETIME_CASES) |
30 | 49 | def test_generate_snowflake_boundary_low(dt, expected_ms): |
31 | | - """Boundary mode low should zero out lower 22 bits.""" |
32 | 50 | sf = generate_snowflake(dt, mode="boundary", high=False) |
33 | 51 | assert (sf >> 22) == expected_ms |
34 | 52 | assert (sf & ((1 << 22) - 1)) == 0 |
35 | 53 |
|
36 | 54 |
|
37 | 55 | @pytest.mark.parametrize(("dt", "expected_ms"), DATETIME_CASES) |
38 | 56 | def test_generate_snowflake_boundary_high(dt, expected_ms): |
39 | | - """Boundary mode high should set lower 22 bits to max.""" |
40 | 57 | sf = generate_snowflake(dt, mode="boundary", high=True) |
41 | 58 | assert (sf >> 22) == expected_ms |
42 | 59 | assert (sf & ((1 << 22) - 1)) == (2**22 - 1) |
43 | 60 |
|
44 | 61 |
|
45 | 62 | @pytest.mark.parametrize(("dt", "expected_ms"), DATETIME_CASES) |
46 | 63 | def test_snowflake_time_roundtrip_boundary(dt, expected_ms): |
47 | | - """Converting boundary snowflake back to datetime yields the original dt.""" |
48 | 64 | sf_low = generate_snowflake(dt, mode="boundary", high=False) |
49 | 65 | sf_high = generate_snowflake(dt, mode="boundary", high=True) |
50 | | - # snowflake_time ignores low bits, so both should map to dt |
51 | 66 | assert snowflake_time(sf_low) == dt |
52 | 67 | assert snowflake_time(sf_high) == dt |
53 | 68 |
|
54 | 69 |
|
55 | 70 | @pytest.mark.parametrize(("dt", "expected_ms"), DATETIME_CASES) |
56 | 71 | def test_snowflake_time_roundtrip_realistic(dt, expected_ms): |
57 | | - """Converting realistic snowflake back to datetime yields the original dt.""" |
58 | 72 | sf = generate_snowflake(dt, mode="realistic") |
59 | 73 | assert snowflake_time(sf) == dt |
60 | 74 |
|
61 | 75 |
|
62 | 76 | def test_generate_snowflake_invalid_mode(): |
63 | | - """Passing an invalid mode should raise ValueError.""" |
64 | 77 | with pytest.raises(ValueError, match="Invalid mode 'nope'. Must be 'realistic' or 'boundary'"): |
65 | 78 | generate_snowflake(datetime.datetime.now(tz=UTC), mode="nope") |
0 commit comments