-
-
Notifications
You must be signed in to change notification settings - Fork 46
chore: db fixture #432
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
chore: db fixture #432
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 | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,19 +1,15 @@ | ||||||||||||||||||||||||||||||||||||||
| import allure | ||||||||||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||||||||||
| from assertpy import assert_that | ||||||||||||||||||||||||||||||||||||||
| from mysql.connector import MySQLConnection | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @pytest.mark.skip(reason="requires database connection") | ||||||||||||||||||||||||||||||||||||||
| class TestDatabaseExample: | ||||||||||||||||||||||||||||||||||||||
| @allure.title("Verify population amounts") | ||||||||||||||||||||||||||||||||||||||
| def test_verify_population_amount(self): | ||||||||||||||||||||||||||||||||||||||
| with MySQLConnection( | ||||||||||||||||||||||||||||||||||||||
| user="root", password="1234", database="world" | ||||||||||||||||||||||||||||||||||||||
| ) as connection: | ||||||||||||||||||||||||||||||||||||||
| cursor = connection.cursor() | ||||||||||||||||||||||||||||||||||||||
| cursor.execute("select Population from city where CountryCode='DNK'") | ||||||||||||||||||||||||||||||||||||||
| population_amount: list[int] = [item[0] for item in cursor.fetchall()] | ||||||||||||||||||||||||||||||||||||||
| assert_that(population_amount).described_as("population amount").is_equal_to( | ||||||||||||||||||||||||||||||||||||||
| [495699, 284846, 183912, 161161, 90327] | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| def test_verify_population_amount(self, db_connection): | ||||||||||||||||||||||||||||||||||||||
| with db_connection.cursor() as cursor: | ||||||||||||||||||||||||||||||||||||||
| cursor.execute("SELECT Population FROM city WHERE CountryCode='DNK'") | ||||||||||||||||||||||||||||||||||||||
| population_amount = [item[0] for item in cursor.fetchall()] | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+12
Contributor
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. 🛠️ Refactor suggestion Enhance database query security and error handling. While the context manager usage is good, the SQL query should be parameterized to prevent SQL injection risks, even in tests. Additionally, consider adding error handling for database operations. Consider this improvement: with db_connection.cursor() as cursor:
- cursor.execute("SELECT Population FROM city WHERE CountryCode='DNK'")
+ try:
+ cursor.execute(
+ "SELECT Population FROM city WHERE CountryCode = %s",
+ ('DNK',)
+ )
+ except Exception as e:
+ pytest.fail(f"Database query failed: {str(e)}")📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| assert_that(population_amount).described_as( | ||||||||||||||||||||||||||||||||||||||
| "population amount" | ||||||||||||||||||||||||||||||||||||||
| ).is_equal_to([495699, 284846, 183912, 161161, 90327]) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+13
to
+15
Contributor
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. 🛠️ Refactor suggestion Consider making population assertions more maintainable. The hardcoded population values might become outdated and the test doesn't verify the order of results. Consider making the test more robust and maintainable. Consider these improvements: - assert_that(population_amount).described_as(
- "population amount"
- ).is_equal_to([495699, 284846, 183912, 161161, 90327])
+ # Verify we have the expected number of cities
+ assert_that(population_amount).described_as(
+ "number of Danish cities"
+ ).is_length(5)
+
+ # Verify population values are in descending order
+ sorted_population = sorted(population_amount, reverse=True)
+ assert_that(population_amount).described_as(
+ "population amounts in descending order"
+ ).is_equal_to(sorted_population)
+
+ # Verify population ranges
+ assert_that(population_amount).described_as(
+ "population amounts within expected range"
+ ).all_match(lambda x: 50000 <= x <= 500000)📝 Committable suggestion
Suggested change
Comment on lines
+9
to
+15
Contributor
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. 💡 Codebase verification Database credentials are hardcoded in the fixture The database connection details are indeed not properly externalized. Found hardcoded credentials in connection = MySQLConnection(user="root", password="1234", database="world")
🔗 Analysis chainVerify database configuration management. The test relies on database connection, but we should ensure that connection details are properly externalized and not hardcoded in the fixture. Let me gather more specific information about the database configuration and fixture setup. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check for database configuration management
# Look for configuration files or environment variables
echo "Checking for database configuration files..."
fd -e ini -e cfg -e yaml -e env
# Check for potential hardcoded credentials
echo -e "\nChecking for potential hardcoded database credentials..."
rg -i "password|user|host|root|localhost|3306" --type py
Length of output: 7146 Script: #!/bin/bash
# Check for database fixture and configuration setup
echo "Checking database fixture setup..."
rg -A 10 "def db_connection" --type py
echo -e "\nChecking for environment variable usage in database connection..."
rg "os\.getenv.*(?i)(db|database|sql)" --type py
echo -e "\nChecking for any config files in tests directory..."
fd . tests/ -e ini -e cfg -e yaml -e env -e json
echo -e "\nChecking for pytest configuration..."
fd pytest -e ini -e cfg
Length of output: 1131 |
||||||||||||||||||||||||||||||||||||||
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.
💡 Codebase verification
Security and configuration concerns in database fixture need to be addressed
The review comment is valid. The search results confirm:
.env,.yaml, etc.)tests/conftest.pyare the only database configuration presentSuggested improvements remain valid:
.env.examplefile for test configurationThe original suggestion with environment variables and error handling should be implemented as proposed in the review comment.
🔗 Analysis chain
Security and configuration concerns in database fixture
Several issues need to be addressed:
Here's a suggested improvement:
Let's check if environment variables are already defined in the project:
Let's search more broadly to understand the database configuration patterns in the project and any existing environment variable usage.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 228
Script:
Length of output: 5202