code quality run

This commit is contained in:
2025-09-13 21:58:54 +02:00
parent 0dfd885847
commit 3eb18ef91e
63 changed files with 1802 additions and 1289 deletions

View File

@@ -1,9 +1,11 @@
# tests/test_csv_processor.py
from unittest.mock import Mock, patch
import pandas as pd
import pytest
import requests
import requests.exceptions
from unittest.mock import Mock, patch
import pandas as pd
from utils.csv_processor import CSVProcessor
@@ -11,7 +13,7 @@ class TestCSVProcessor:
def setup_method(self):
self.processor = CSVProcessor()
@patch('requests.get')
@patch("requests.get")
def test_download_csv_encoding_fallback(self, mock_get):
"""Test CSV download with encoding fallback"""
# Create content with special characters that would fail UTF-8 if not properly encoded
@@ -20,7 +22,7 @@ class TestCSVProcessor:
mock_response = Mock()
mock_response.status_code = 200
# Use latin-1 encoding which your method should try
mock_response.content = special_content.encode('latin-1')
mock_response.content = special_content.encode("latin-1")
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
@@ -30,14 +32,16 @@ class TestCSVProcessor:
assert isinstance(csv_content, str)
assert "Café Product" in csv_content
@patch('requests.get')
@patch("requests.get")
def test_download_csv_encoding_ignore_fallback(self, mock_get):
"""Test CSV download falls back to UTF-8 with error ignoring"""
# Create problematic bytes that would fail most encoding attempts
mock_response = Mock()
mock_response.status_code = 200
# Create bytes that will fail most encodings
mock_response.content = b"product_id,title,price\nTEST001,\xff\xfe Product,10.99"
mock_response.content = (
b"product_id,title,price\nTEST001,\xff\xfe Product,10.99"
)
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
@@ -49,7 +53,7 @@ class TestCSVProcessor:
assert "product_id,title,price" in csv_content
assert "TEST001" in csv_content
@patch('requests.get')
@patch("requests.get")
def test_download_csv_request_exception(self, mock_get):
"""Test CSV download with request exception"""
mock_get.side_effect = requests.exceptions.RequestException("Connection error")
@@ -57,18 +61,20 @@ class TestCSVProcessor:
with pytest.raises(requests.exceptions.RequestException):
self.processor.download_csv("http://example.com/test.csv")
@patch('requests.get')
@patch("requests.get")
def test_download_csv_http_error(self, mock_get):
"""Test CSV download with HTTP error"""
mock_response = Mock()
mock_response.status_code = 404
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError("404 Not Found")
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError(
"404 Not Found"
)
mock_get.return_value = mock_response
with pytest.raises(requests.exceptions.HTTPError):
self.processor.download_csv("http://example.com/nonexistent.csv")
@patch('requests.get')
@patch("requests.get")
def test_download_csv_failure(self, mock_get):
"""Test CSV download failure"""
# Mock failed HTTP response
@@ -95,26 +101,24 @@ TEST002,Test Product 2,15.99,TestMarket"""
@pytest.mark.asyncio
async def test_process_marketplace_csv_from_url(self, db):
"""Test complete marketplace CSV processing"""
with patch.object(self.processor, 'download_csv') as mock_download, \
patch.object(self.processor, 'parse_csv') as mock_parse:
with patch.object(
self.processor, "download_csv"
) as mock_download, patch.object(self.processor, "parse_csv") as mock_parse:
# Mock successful download and parsing
mock_download.return_value = "csv_content"
mock_df = pd.DataFrame({
"product_id": ["TEST001", "TEST002"],
"title": ["Product 1", "Product 2"],
"price": ["10.99", "15.99"],
"marketplace": ["TestMarket", "TestMarket"],
"shop_name": ["TestShop", "TestShop"]
})
mock_df = pd.DataFrame(
{
"product_id": ["TEST001", "TEST002"],
"title": ["Product 1", "Product 2"],
"price": ["10.99", "15.99"],
"marketplace": ["TestMarket", "TestMarket"],
"shop_name": ["TestShop", "TestShop"],
}
)
mock_parse.return_value = mock_df
result = await self.processor.process_marketplace_csv_from_url(
"http://example.com/test.csv",
"TestMarket",
"TestShop",
1000,
db
"http://example.com/test.csv", "TestMarket", "TestShop", 1000, db
)
assert "imported" in result