From 4f492eda1e98d6dfb934e1e0376d02d521283457 Mon Sep 17 00:00:00 2001 From: Shrikant Giri Date: Fri, 24 Oct 2025 22:08:45 +0530 Subject: [PATCH 1/4] Fix Windows test failures and remove debug prints for DRF --- tests/schemas/test_openapi.py | 7 ------- tests/test_model_serializer.py | 13 +++++++------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index a168cb4661..666f9d03ce 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -257,7 +257,6 @@ class View(generics.GenericAPIView): inspector.view = view request_body = inspector.get_request_body(path, method) - print(request_body) assert request_body['content']['application/json']['schema']['$ref'] == '#/components/schemas/Item' components = inspector.get_components(path, method) @@ -928,7 +927,6 @@ def test_repeat_operation_ids(self): request = create_request('/') schema = generator.get_schema(request=request) schema_str = str(schema) - print(schema_str) assert schema_str.count("operationId") == 2 assert schema_str.count("newExample") == 1 assert schema_str.count("oldExample") == 1 @@ -948,7 +946,6 @@ def test_duplicate_operation_id(self): assert len(w) == 1 assert issubclass(w[-1].category, UserWarning) - print(str(w[-1].message)) assert 'You have a duplicated operationId' in str(w[-1].message) def test_operation_id_viewset(self): @@ -960,7 +957,6 @@ def test_operation_id_viewset(self): request = create_request('/') schema = generator.get_schema(request=request) - print(schema) assert schema['paths']['/account/']['get']['operationId'] == 'listExampleViewSets' assert schema['paths']['/account/']['post']['operationId'] == 'createExampleViewSet' assert schema['paths']['/account/{id}/']['get']['operationId'] == 'retrieveExampleViewSet' @@ -1284,7 +1280,6 @@ def test_serializer_model(self): request = create_request('/') schema = generator.get_schema(request=request) - print(schema) assert 'components' in schema assert 'schemas' in schema['components'] @@ -1299,7 +1294,6 @@ def test_authtoken_serializer(self): request = create_request('/') schema = generator.get_schema(request=request) - print(schema) route = schema['paths']['/api-token-auth/']['post'] body_schema = route['requestBody']['content']['application/json']['schema'] @@ -1327,7 +1321,6 @@ def test_component_name(self): request = create_request('/') schema = generator.get_schema(request=request) - print(schema) assert 'components' in schema assert 'schemas' in schema['components'] assert 'Ulysses' in schema['components']['schemas'] diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index eac51ae704..a3468cf0c1 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -174,7 +174,7 @@ class Meta: big_integer_field = IntegerField\(.*\) boolean_field = BooleanField\(required=False\) char_field = CharField\(max_length=100\) - comma_separated_integer_field = CharField\(max_length=100, validators=\[\]\) + comma_separated_integer_field = CharField\(max_length=100, validators=\[\]\) date_field = DateField\(\) datetime_field = DateTimeField\(\) decimal_field = DecimalField\(decimal_places=1, max_digits=3\) @@ -186,14 +186,15 @@ class Meta: positive_small_integer_field = IntegerField\(.*\) slug_field = SlugField\(allow_unicode=False, max_length=100\) small_integer_field = IntegerField\(.*\) - text_field = CharField\(max_length=100, style={'base_template': 'textarea.html'}\) + text_field = CharField\(max_length=100, style=\{.*\}\) file_field = FileField\(max_length=100\) time_field = TimeField\(\) url_field = URLField\(max_length=100\) - custom_field = ModelField\(model_field=\) - file_path_field = FilePathField\(path=%r\) - """ % tempfile.gettempdir()) - assert re.search(expected, repr(TestSerializer())) is not None + custom_field = ModelField\(model_field=<.*CustomField: custom_field>\) + file_path_field = FilePathField\(path='%s'\) + """ % tempfile.gettempdir().replace('\\', r'\\\\')) + + assert re.search(expected, repr(TestSerializer()), re.DOTALL) is not None def test_field_options(self): class TestSerializer(serializers.ModelSerializer): From 340065121a2bc3735829a825eb5cb051db1f24cc Mon Sep 17 00:00:00 2001 From: Shrikant Giri Date: Fri, 24 Oct 2025 22:23:58 +0530 Subject: [PATCH 2/4] Fix style issues in test_openapi.py (flake8 E303) --- tests/schemas/test_openapi.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 666f9d03ce..13746bb903 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -1280,7 +1280,6 @@ def test_serializer_model(self): request = create_request('/') schema = generator.get_schema(request=request) - assert 'components' in schema assert 'schemas' in schema['components'] assert 'ExampleModel' in schema['components']['schemas'] @@ -1294,7 +1293,6 @@ def test_authtoken_serializer(self): request = create_request('/') schema = generator.get_schema(request=request) - route = schema['paths']['/api-token-auth/']['post'] body_schema = route['requestBody']['content']['application/json']['schema'] From 297ad12c061127896740ea9d048c1e61572f28a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asif=20Saif=20Uddin=20=7B=22Auvi=22=3A=22=E0=A6=85?= =?UTF-8?q?=E0=A6=AD=E0=A6=BF=22=7D?= Date: Sat, 25 Oct 2025 13:41:32 +0600 Subject: [PATCH 3/4] Update tests/test_model_serializer.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tests/test_model_serializer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index a3468cf0c1..ee510c52f6 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -192,7 +192,7 @@ class Meta: url_field = URLField\(max_length=100\) custom_field = ModelField\(model_field=<.*CustomField: custom_field>\) file_path_field = FilePathField\(path='%s'\) - """ % tempfile.gettempdir().replace('\\', r'\\\\')) + """ % re.escape(tempfile.gettempdir())) assert re.search(expected, repr(TestSerializer()), re.DOTALL) is not None From 455f2bd11acce28bc2f74f4ed2c5864119d08493 Mon Sep 17 00:00:00 2001 From: Shrikant Giri Date: Sat, 25 Oct 2025 14:43:42 +0530 Subject: [PATCH 4/4] Use platform-specific path escaping with test evidence Windows requires manual backslash replacement for repr() matching: - repr() displays C:\Users as 'C:\Users' (double backslashes) - Regex needs \\ to match \ (four backslashes in pattern) - re.escape() only produces \ (matches single backslash) Test evidence on Windows: re.escape(path) in repr(path): None (fails) path.replace('\', r'\\') in repr(path): Match (works) Solution: - Windows: Manual replacement for repr() double-escaping - Unix: re.escape() for special character handling Addresses @auvipy's review with detailed testing and explanation. --- tests/test_model_serializer.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index a3468cf0c1..2aa35cf36a 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -168,6 +168,32 @@ class Meta: model = RegularFieldsModel fields = '__all__' + # Cross-platform path handling for regex matching against repr() output + # + # Challenge: repr() output escapes backslashes, so Windows paths like + # C:\Users become 'C:\\Users' in the repr string. To match \\ in regex, + # we need \\\\ in the pattern. + # + # Why re.escape() doesn't work for Windows: + # - re.escape(r'C:\Users') → 'C:\\Users' (matches single \) + # - But repr() shows 'C:\\Users' (double \\) + # - We need pattern 'C:\\\\Users' (to match double \\) + # + # Testing on Windows confirms: + # >>> path = r'C:\Users\Temp' + # >>> re.search(re.escape(path), repr(path)) + # None # Fails + # >>> re.search(path.replace('\\', r'\\\\'), repr(path)) + # # Works + # + # For Unix paths (no backslashes), re.escape() works correctly. + temp_path = tempfile.gettempdir() + if '\\' in temp_path: + # Windows: Manual replacement needed for repr() matching + escaped_temp_path = temp_path.replace('\\', r'\\\\') + else: + # Unix: re.escape() handles any special regex characters + escaped_temp_path = re.escape(temp_path) expected = dedent(r""" TestSerializer\(\): auto_field = IntegerField\(read_only=True\) @@ -192,7 +218,7 @@ class Meta: url_field = URLField\(max_length=100\) custom_field = ModelField\(model_field=<.*CustomField: custom_field>\) file_path_field = FilePathField\(path='%s'\) - """ % tempfile.gettempdir().replace('\\', r'\\\\')) + """) % escaped_temp_path assert re.search(expected, repr(TestSerializer()), re.DOTALL) is not None