-
-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: nvarchar field filtering bug for non-ascii characters #1441
Draft
mm74noroozi
wants to merge
5
commits into
tortoise:develop
Choose a base branch
from
mm74noroozi:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
16fd8a5
feat: add mssql nvarchar
mm74noroozi 377f904
update CONTRIBUTORS.rst
mm74noroozi b1cffdd
fix: logical and typo mistakes
mm74noroozi 4b608b6
feat: add filter test on nvarchar field
mm74noroozi c64798f
update change log
mm74noroozi 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 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 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 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from tests import testmodels_mssql as testmodels | ||
from tortoise.contrib import test | ||
from tortoise.exceptions import IntegrityError, OperationalError | ||
from tortoise.exceptions import ConfigurationError | ||
|
||
@test.requireCapability(dialect="mssql") | ||
class TestNVARCHARFields(test.IsolatedTestCase): | ||
tortoise_test_modules = ["tests.testmodels_mssql"] | ||
|
||
async def test_max_length_missing(self): | ||
with self.assertRaisesRegex( | ||
TypeError, "missing 1 required positional argument: 'max_length'" | ||
): | ||
testmodels.NVARCHAR() # pylint: disable=E1120 | ||
|
||
async def test_max_length_bad(self): | ||
with self.assertRaisesRegex(ConfigurationError, "'max_length' must be >= 1"): | ||
testmodels.NVARCHAR(max_length=0) | ||
|
||
async def _setUpDB(self) -> None: | ||
try: | ||
await super()._setUpDB() | ||
except OperationalError: | ||
raise test.SkipTest("Works only with MSSQLServer") | ||
|
||
async def test_empty(self): | ||
with self.assertRaises(IntegrityError): | ||
await testmodels.NVARCHAR.create() | ||
|
||
async def test_filtering(self): | ||
testmodels.NVARCHAR.create(nvarchar="سلام").sql() | ||
|
||
async def test_create(self): | ||
obj0 = await testmodels.NVARCHAR.create(nvarchar="سلام") | ||
obj = await testmodels.NVARCHAR.get(id=obj0.id) | ||
self.assertEqual(obj.nvarchar, "سلام") | ||
self.assertIs(obj.nvarchar_null, None) | ||
await obj.save() | ||
obj2 = await testmodels.NVARCHAR.get(id=obj.id) | ||
self.assertEqual(obj, obj2) | ||
|
||
async def test_filterUTF8Chars(self): | ||
await testmodels.NVARCHAR.create(nvarchar="سلام") | ||
obj = await testmodels.NVARCHAR.get(nvarchar="سلام") | ||
self.assertIsNotNone(obj) | ||
self.assertEqual(obj.nvarchar, "سلام") | ||
|
||
async def test_update(self): | ||
obj0 = await testmodels.NVARCHAR.create(nvarchar="سلام") | ||
await testmodels.NVARCHAR.filter(id=obj0.id).update(nvarchar="non-utf8") | ||
obj = await testmodels.NVARCHAR.get(id=obj0.id) | ||
self.assertEqual(obj.nvarchar, "non-utf8") | ||
self.assertIs(obj.nvarchar_null, None) | ||
|
||
async def test_cast(self): | ||
obj0 = await testmodels.NVARCHAR.create(nvarchar=33) | ||
obj = await testmodels.NVARCHAR.get(id=obj0.id) | ||
self.assertEqual(obj.nvarchar, "33") | ||
|
||
async def test_values(self): | ||
obj0 = await testmodels.NVARCHAR.create(nvarchar="سلام") | ||
values = await testmodels.NVARCHAR.get(id=obj0.id).values("nvarchar") | ||
self.assertEqual(values["nvarchar"], "سلام") | ||
|
||
async def test_values_list(self): | ||
obj0 = await testmodels.NVARCHAR.create(nvarchar="سلام") | ||
values = await testmodels.NVARCHAR.get(id=obj0.id).values_list("nvarchar", flat=True) | ||
self.assertEqual(values, "سلام") | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from tortoise import Model, fields | ||
from tortoise.contrib.mssql.fields import NVARCHAR | ||
|
||
class NVARCHARFields(Model): | ||
id = fields.IntField(pk=True) | ||
nvarchar = NVARCHAR(max_length=255) | ||
nvarchar_null = NVARCHAR(max_length=255,null=True) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class NVARCHAR: | ||
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. What about |
||
def __init__(self,value): | ||
self._value = value | ||
def __str__(self): | ||
return f"N'{self._value}'" |
This file contains 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from tortoise.fields.data import CharField | ||
from tortoise.contrib.mssql.field_types import NVARCHAR | ||
|
||
|
||
class NVARCHAR(CharField): | ||
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. What about |
||
|
||
field_type = NVARCHAR | ||
|
||
@property | ||
def SQL_TYPE(self) -> str: | ||
return f"NVARCHAR({self.field.max_length})" |
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.
Just add in 0.20.0 because which is not released yet