-
-
Couldn't load subscription status.
- Fork 442
fix: nvarchar field filtering bug for non-ascii characters #1441
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, "سلام") | ||
|
|
||
|
|
| 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) |
| 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}'" | ||
| 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})" | ||
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