mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-02-02 11:04:06 +00:00
- Handle missing or empty databases.csv gracefully with warnings and empty DataFrame - Add unit tests for robust databases.csv loading behavior - Adjust seed tests to assert warnings across multiple print calls - Replace Debian docker.io with docker-ce-cli to avoid Docker API version mismatch - Install required build tools (curl, gnupg) for Docker repo setup https://chatgpt.com/share/697e6d9d-6458-800f-9d12-1e337509be4e
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
import io
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from contextlib import redirect_stderr
|
|
|
|
import pandas as pd
|
|
|
|
# Adjust if your package name/import path differs.
|
|
from baudolo.backup.app import _load_databases_df
|
|
|
|
|
|
EXPECTED_COLUMNS = ["instance", "database", "username", "password"]
|
|
|
|
|
|
class TestLoadDatabasesDf(unittest.TestCase):
|
|
def test_missing_csv_is_handled_with_warning_and_empty_df(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
missing_path = os.path.join(td, "does-not-exist.csv")
|
|
|
|
buf = io.StringIO()
|
|
with redirect_stderr(buf):
|
|
df = _load_databases_df(missing_path)
|
|
|
|
stderr = buf.getvalue()
|
|
self.assertIn("WARNING:", stderr)
|
|
self.assertIn("databases.csv not found", stderr)
|
|
|
|
self.assertIsInstance(df, pd.DataFrame)
|
|
self.assertListEqual(list(df.columns), EXPECTED_COLUMNS)
|
|
self.assertTrue(df.empty)
|
|
|
|
def test_empty_csv_is_handled_with_warning_and_empty_df(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
empty_path = os.path.join(td, "databases.csv")
|
|
# Create an empty file (0 bytes)
|
|
with open(empty_path, "w", encoding="utf-8") as f:
|
|
f.write("")
|
|
|
|
buf = io.StringIO()
|
|
with redirect_stderr(buf):
|
|
df = _load_databases_df(empty_path)
|
|
|
|
stderr = buf.getvalue()
|
|
self.assertIn("WARNING:", stderr)
|
|
self.assertIn("exists but is empty", stderr)
|
|
|
|
self.assertIsInstance(df, pd.DataFrame)
|
|
self.assertListEqual(list(df.columns), EXPECTED_COLUMNS)
|
|
self.assertTrue(df.empty)
|
|
|
|
def test_valid_csv_loads_without_warning(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
csv_path = os.path.join(td, "databases.csv")
|
|
|
|
content = "instance;database;username;password\nmyapp;*;dbuser;secret\n"
|
|
with open(csv_path, "w", encoding="utf-8") as f:
|
|
f.write(content)
|
|
|
|
buf = io.StringIO()
|
|
with redirect_stderr(buf):
|
|
df = _load_databases_df(csv_path)
|
|
|
|
stderr = buf.getvalue()
|
|
self.assertEqual(stderr, "") # no warning expected
|
|
|
|
self.assertIsInstance(df, pd.DataFrame)
|
|
self.assertListEqual(list(df.columns), EXPECTED_COLUMNS)
|
|
self.assertEqual(len(df), 1)
|
|
self.assertEqual(df.loc[0, "instance"], "myapp")
|
|
self.assertEqual(df.loc[0, "database"], "*")
|
|
self.assertEqual(df.loc[0, "username"], "dbuser")
|
|
self.assertEqual(df.loc[0, "password"], "secret")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|