This repository was archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Convert timezone #20
Merged
Merged
Convert timezone #20
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,14 @@ | ||
| from typing import Callable, List | ||
| from datetime import datetime | ||
| import unittest | ||
| from datetime import datetime | ||
| from typing import Callable, List, Tuple | ||
|
|
||
| from .common import str_to_checksum, TEST_MYSQL_CONN_STRING | ||
| from .common import str_to_checksum, test_each_database_in_list, get_conn, random_table_suffix | ||
|
|
||
| from sqeleton.queries import table, current_timestamp | ||
| import pytz | ||
|
|
||
| from sqeleton import databases as dbs | ||
| from sqeleton import connect | ||
|
|
||
| from sqeleton import databases as dbs | ||
| from sqeleton.queries import table, current_timestamp, NormalizeAsString | ||
| from .common import TEST_MYSQL_CONN_STRING | ||
| from .common import str_to_checksum, test_each_database_in_list, get_conn, random_table_suffix | ||
|
|
||
| TEST_DATABASES = { | ||
| dbs.MySQL, | ||
|
|
@@ -81,6 +80,37 @@ def test_current_timestamp(self): | |
| res = db.query(current_timestamp(), datetime) | ||
| assert isinstance(res, datetime), (res, type(res)) | ||
|
|
||
| def test_correct_timezone(self): | ||
| name = "tbl_" + random_table_suffix() | ||
| db = get_conn(self.db_cls) | ||
| tbl = table(db.parse_table_name(name), schema={ | ||
| "id": int, "created_at": "timestamp_tz(9)", "updated_at": "timestamp_tz(9)" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }) | ||
|
|
||
| db.query(tbl.create()) | ||
|
|
||
| tz = pytz.timezone('Europe/Berlin') | ||
|
|
||
| now = datetime.now(tz) | ||
| db.query(table(db.parse_table_name(name)).insert_row("1", now, now)) | ||
| db.query(db.dialect.set_timezone_to_utc()) | ||
|
|
||
| t = db.table(name).query_schema() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is refreshing the schema, to actually get the correct timezones/datatype values. |
||
| t.schema["created_at"] = t.schema["created_at"].replace(precision=t.schema["created_at"].precision, rounds=True) | ||
|
|
||
| tbl = table(db.parse_table_name(name), schema=t.schema) | ||
|
|
||
| results = db.query(tbl.select(NormalizeAsString(tbl[c]) for c in ["created_at", "updated_at"]), List[Tuple]) | ||
|
|
||
| created_at = results[0][1] | ||
| updated_at = results[0][1] | ||
|
|
||
| utc = now.astimezone(pytz.UTC) | ||
|
|
||
| self.assertEqual(created_at, utc.__format__("%Y-%m-%d %H:%M:%S.%f")) | ||
| self.assertEqual(updated_at, utc.__format__("%Y-%m-%d %H:%M:%S.%f")) | ||
|
|
||
| db.query(tbl.drop()) | ||
|
|
||
| @test_each_database | ||
| class TestThreePartIds(unittest.TestCase): | ||
|
|
@@ -104,3 +134,4 @@ def test_three_part_support(self): | |
| d = db.query_table_schema(part.path) | ||
| assert len(d) == 1 | ||
| db.query(part.drop()) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was required to get the "NormalizeAsString" working, not sure if this is actually required?