|
1 | 1 | """Class to test the `created_at` and `updated_at` timestamps."""
|
2 | 2 |
|
| 3 | +from datetime import datetime, timezone |
| 4 | + |
3 | 5 | from tests.conftest import ExampleModel
|
4 | 6 |
|
5 | 7 |
|
@@ -48,3 +50,214 @@ def test_update_timestamps(self, db_mock, mocker) -> None:
|
48 | 50 | assert (
|
49 | 51 | returned_instance.updated_at == 1234567891
|
50 | 52 | ) # Should be updated to the new timestamp
|
| 53 | + |
| 54 | + def test_insert_with_provided_timestamps(self, db_mock, mocker) -> None: |
| 55 | + """Test that user-provided timestamps are respected on insert.""" |
| 56 | + # Mock time.time() to return a fixed timestamp |
| 57 | + mocker.patch("time.time", return_value=1234567890) |
| 58 | + |
| 59 | + # User-provided timestamps |
| 60 | + new_instance = ExampleModel( |
| 61 | + slug="test", |
| 62 | + name="Test", |
| 63 | + content="Test content", |
| 64 | + created_at=1111111111, # User-provided |
| 65 | + updated_at=1111111111, # User-provided |
| 66 | + ) |
| 67 | + |
| 68 | + # Perform the insert operation |
| 69 | + returned_instance = db_mock.insert( |
| 70 | + new_instance, timestamp_override=True |
| 71 | + ) |
| 72 | + |
| 73 | + # Assert that the user-provided timestamps are respected |
| 74 | + assert returned_instance.created_at == 1111111111 |
| 75 | + assert returned_instance.updated_at == 1111111111 |
| 76 | + |
| 77 | + def test_insert_with_default_timestamps(self, db_mock, mocker) -> None: |
| 78 | + """Test that timestamps are set when created_at and updated_at are 0.""" |
| 79 | + # Mock time.time() to return a fixed timestamp |
| 80 | + mocker.patch("time.time", return_value=1234567890) |
| 81 | + |
| 82 | + # Create instance with default (0) timestamps |
| 83 | + new_instance = ExampleModel( |
| 84 | + slug="test", |
| 85 | + name="Test", |
| 86 | + content="Test content", |
| 87 | + created_at=0, |
| 88 | + updated_at=0, |
| 89 | + ) |
| 90 | + |
| 91 | + # Perform the insert operation |
| 92 | + returned_instance = db_mock.insert(new_instance) |
| 93 | + |
| 94 | + # Assert that timestamps are set to the mocked time |
| 95 | + assert returned_instance.created_at == 1234567890 |
| 96 | + assert returned_instance.updated_at == 1234567890 |
| 97 | + |
| 98 | + def test_insert_with_mixed_timestamps(self, db_mock, mocker) -> None: |
| 99 | + """Test a mix of user-provided and default timestamps work on insert.""" |
| 100 | + # Mock time.time() to return a fixed timestamp |
| 101 | + mocker.patch("time.time", return_value=1234567890) |
| 102 | + |
| 103 | + # Provide only created_at, leave updated_at as 0 |
| 104 | + new_instance = ExampleModel( |
| 105 | + slug="test", |
| 106 | + name="Test", |
| 107 | + content="Test content", |
| 108 | + created_at=1111111111, # User-provided |
| 109 | + updated_at=0, # Default to current time |
| 110 | + ) |
| 111 | + |
| 112 | + # Perform the insert operation |
| 113 | + returned_instance = db_mock.insert( |
| 114 | + new_instance, timestamp_override=True |
| 115 | + ) |
| 116 | + |
| 117 | + # Assert that created_at is respected, and updated_at is set to the |
| 118 | + # current time |
| 119 | + assert returned_instance.created_at == 1111111111 |
| 120 | + assert returned_instance.updated_at == 1234567890 |
| 121 | + |
| 122 | + def test_update_timestamps_on_change(self, db_mock, mocker) -> None: |
| 123 | + """Test that only `updated_at` changes on update.""" |
| 124 | + # Mock time.time() to return a fixed timestamp for the insert |
| 125 | + mocker.patch("time.time", return_value=1234567890) |
| 126 | + |
| 127 | + # Insert a new record |
| 128 | + new_instance = ExampleModel( |
| 129 | + slug="test", name="Test", content="Test content" |
| 130 | + ) |
| 131 | + returned_instance = db_mock.insert(new_instance) |
| 132 | + |
| 133 | + # Mock time.time() to return a new timestamp for the update |
| 134 | + mocker.patch("time.time", return_value=1234567891) |
| 135 | + |
| 136 | + # Update the record |
| 137 | + returned_instance.name = "Updated Test" |
| 138 | + db_mock.update(returned_instance) |
| 139 | + |
| 140 | + # Assert that created_at stays the same and updated_at is changed |
| 141 | + assert returned_instance.created_at == 1234567890 |
| 142 | + assert returned_instance.updated_at == 1234567891 |
| 143 | + |
| 144 | + def test_no_change_if_timestamps_already_set(self, db_mock, mocker) -> None: |
| 145 | + """Test timestamps are not modified if already set during insert.""" |
| 146 | + # Mock time.time() to return a fixed timestamp |
| 147 | + mocker.patch("time.time", return_value=1234567890) |
| 148 | + |
| 149 | + # User provides both timestamps |
| 150 | + new_instance = ExampleModel( |
| 151 | + slug="test", |
| 152 | + name="Test", |
| 153 | + content="Test content", |
| 154 | + created_at=1111111111, # Already set |
| 155 | + updated_at=1111111111, # Already set |
| 156 | + ) |
| 157 | + |
| 158 | + # Perform the insert operation |
| 159 | + returned_instance = db_mock.insert( |
| 160 | + new_instance, timestamp_override=True |
| 161 | + ) |
| 162 | + |
| 163 | + # Assert that timestamps are not modified |
| 164 | + assert returned_instance.created_at == 1111111111 |
| 165 | + assert returned_instance.updated_at == 1111111111 |
| 166 | + |
| 167 | + def test_override_but_no_timestamps_provided(self, db_mock, mocker) -> None: |
| 168 | + """Test missing timestamps always set to current time. |
| 169 | +
|
| 170 | + Even with `timestamp_override=True`. |
| 171 | + """ |
| 172 | + # Mock time.time() to return a fixed timestamp |
| 173 | + mocker.patch("time.time", return_value=1234567890) |
| 174 | + |
| 175 | + # User provides `0` for both timestamps, expecting them to be overridden |
| 176 | + new_instance = ExampleModel( |
| 177 | + slug="test", |
| 178 | + name="Test", |
| 179 | + content="Test content", |
| 180 | + created_at=0, # Should default to current time |
| 181 | + updated_at=0, # Should default to current time |
| 182 | + ) |
| 183 | + |
| 184 | + # Perform the insert with timestamp_override=True |
| 185 | + returned_instance = db_mock.insert( |
| 186 | + new_instance, timestamp_override=True |
| 187 | + ) |
| 188 | + |
| 189 | + # Assert that both timestamps are set to the current time, ignoring the |
| 190 | + # `0` |
| 191 | + assert returned_instance.created_at == 1234567890 |
| 192 | + assert returned_instance.updated_at == 1234567890 |
| 193 | + |
| 194 | + def test_partial_override_with_zero(self, db_mock, mocker) -> None: |
| 195 | + """Test changing `updated_at` only on create. |
| 196 | +
|
| 197 | + When `timestamp_override=True |
| 198 | + """ |
| 199 | + # Mock time.time() to return a fixed timestamp |
| 200 | + mocker.patch("time.time", return_value=1234567890) |
| 201 | + |
| 202 | + # User provides `created_at`, but leaves `updated_at` as 0 |
| 203 | + new_instance = ExampleModel( |
| 204 | + slug="test", |
| 205 | + name="Test", |
| 206 | + content="Test content", |
| 207 | + created_at=1111111111, # Provided by the user |
| 208 | + updated_at=0, # Should be set to current time |
| 209 | + ) |
| 210 | + |
| 211 | + # Perform the insert operation with timestamp_override=True |
| 212 | + returned_instance = db_mock.insert( |
| 213 | + new_instance, timestamp_override=True |
| 214 | + ) |
| 215 | + |
| 216 | + # Assert that `created_at` is respected, and `updated_at` is set to the |
| 217 | + # current time |
| 218 | + assert returned_instance.created_at == 1111111111 |
| 219 | + assert returned_instance.updated_at == 1234567890 |
| 220 | + |
| 221 | + def test_insert_with_override_disabled(self, db_mock, mocker) -> None: |
| 222 | + """Test that timestamp_override=False ignores provided timestamps.""" |
| 223 | + # Mock time.time() to return a fixed timestamp |
| 224 | + mocker.patch("time.time", return_value=1234567890) |
| 225 | + |
| 226 | + # User provides both timestamps, but they should be ignored |
| 227 | + new_instance = ExampleModel( |
| 228 | + slug="test", |
| 229 | + name="Test", |
| 230 | + content="Test content", |
| 231 | + created_at=1111111111, # Should be ignored |
| 232 | + updated_at=1111111111, # Should be ignored |
| 233 | + ) |
| 234 | + |
| 235 | + # Perform the insert with timestamp_override=False (default) |
| 236 | + returned_instance = db_mock.insert( |
| 237 | + new_instance, timestamp_override=False |
| 238 | + ) |
| 239 | + |
| 240 | + # Assert that both timestamps are set to the mocked current time |
| 241 | + assert returned_instance.created_at == 1234567890 |
| 242 | + assert returned_instance.updated_at == 1234567890 |
| 243 | + |
| 244 | + def test_time_is_in_utc(self, db_mock, mocker) -> None: |
| 245 | + """Test that timestamps generated with time.time() are in UTC.""" |
| 246 | + # Mock time.time() to return a fixed timestamp |
| 247 | + mocker.patch("time.time", return_value=1234567890) |
| 248 | + |
| 249 | + # Insert a new instance |
| 250 | + new_instance = ExampleModel( |
| 251 | + slug="test", name="Test", content="Test content" |
| 252 | + ) |
| 253 | + returned_instance = db_mock.insert(new_instance) |
| 254 | + |
| 255 | + # Convert created_at to UTC datetime and verify the conversion |
| 256 | + created_at_utc = datetime.fromtimestamp( |
| 257 | + returned_instance.created_at, tz=timezone.utc |
| 258 | + ) |
| 259 | + |
| 260 | + # Assert that the datetime is correctly interpreted as UTC |
| 261 | + assert created_at_utc == datetime( |
| 262 | + 2009, 2, 13, 23, 31, 30, tzinfo=timezone.utc |
| 263 | + ) |
0 commit comments