|
| 1 | +# Copyright (c) 2022 spdx contributors |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | +from datetime import datetime |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from src.datetime_conversions import datetime_from_str |
| 16 | + |
| 17 | + |
| 18 | +def test_datetime_from_str(): |
| 19 | + date_str = "2010-03-04T05:45:11Z" |
| 20 | + |
| 21 | + date = datetime_from_str(date_str) |
| 22 | + |
| 23 | + assert date == datetime(2010, 3, 4, 5, 45, 11) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize("invalid_date_str, error_type, expected_message", |
| 27 | + [(5, TypeError, "Could not convert str to datetime, invalid type: int"), |
| 28 | + ("2010-02-03", ValueError, "time data '2010-02-03' does not match format '%Y-%m-%dT%H:%M:%SZ'")]) |
| 29 | +def test_datetime_from_str_error(invalid_date_str, error_type, expected_message): |
| 30 | + with pytest.raises(error_type, match=expected_message): |
| 31 | + datetime_from_str(invalid_date_str) |
0 commit comments