tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.37k stars 355 forks source link

Add `timezone` and `use_utc` parameters into `RegisterTortoise` class. #1649

Closed Abeautifulsnow closed 1 week ago

Abeautifulsnow commented 2 weeks ago

Description

A detailed description can be found here: #1632

Motivation and Context

There are no timezone and utc configurations which can be applied to RegisterTortoise class when using db_url to initialize a sql connection. I've made them available in RegisterTortoise, allowing users to conveniently specify these settings.

How Has This Been Tested?

There are no side effects on other areas of the code. It just introduces two parameters within the RegisterTortoise. I have tested it within my project, and it works with these settings specified. #1632

And can also be tested with:

cd examples/fastapi && PYTHONPATH=. pytest _tests.py

I have also added test cases for the modified parts and passed the self-test.

Checklist:

abondar commented 1 week ago

@Abeautifulsnow seems like tests are failing

Abeautifulsnow commented 1 week ago

@Abeautifulsnow seems like tests are failing

Ummm... It seems I know where the problem lies. My two test assertions timezones are both based on Asia/Shanghai. Then where is the timezone of the CI test environment? Or maybe I should add more code to adjust the timezone to ensure the test passes?

abondar commented 1 week ago

Yeah, you would need to somehow modify it, so it doesn't rely on what environment it is, as github action do not give any guarantees regarding their timezones

Abeautifulsnow commented 1 week ago

The changes has been Done! @abondar

Abeautifulsnow commented 1 week ago
_____________ TestModelMethods.test_update_or_create_with_defaults _____________
[gw3] linux -- Python 3.8.18 /opt/hostedtoolcache/Python/3.8.18/x64/bin/python3.8
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/case.py", line 60, in testPartExecutor
    yield
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/case.py", line 676, in run
    self._callTestMethod(testMethod)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/async_case.py", line 65, in _callTestMethod
    self._callMaybeAsync(method)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/async_case.py", line 88, in _callMaybeAsync
    return self._asyncioTestLoop.run_until_complete(fut)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/async_case.py", line 102, in _asyncioLoopRunner
    ret = await awaitable
  File "/home/runner/work/tortoise-orm/tortoise-orm/tests/test_model_methods.py", line 186, in test_update_or_create_with_defaults
    await self.cls.update_or_create(id=mdl.id, defaults=mdl_dict)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/case.py", line 227, in __exit__
    self._raiseFailure("{} not raised".format(exc_name))
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/case.py", line 164, in _raiseFailure
    raise self.test_case.failureException(msg)
AssertionError: ParamsError not raised

=========================== short test summary info ============================
FAILED tests/test_model_methods.py::TestModelMethods::test_update_or_create_with_defaults - AssertionError: ParamsError not raised
1 failed, 1102 passed, 80 skipped, 3 xfailed, 50 warnings in 58.54s
make: *** [Makefile:53: test_postgres_psycopg] Error 1
Error: Process completed with exit code 2.

It seems the issue is due to other test files, not my own. Could you please check it out? Thank you.

coveralls commented 1 week ago

Pull Request Test Coverage Report for Build 9559957832

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details


Files with Coverage Reduction New Missed Lines %
tortoise/contrib/fastapi/init.py 2 63.44%
<!-- Total: 2 -->
Totals Coverage Status
Change from base Build 9543663341: 0.006%
Covered Lines: 5870
Relevant Lines: 6494

💛 - Coveralls