|
| 1 | +"""Tests for delete_support module.""" |
| 2 | +import logging |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from src.db.delete_support import delete_support_by_name |
| 7 | +from src.db.models.support_listing import Support, SupportListing |
| 8 | +from tests.src.db.models import factories |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def caplog_info(caplog): |
| 13 | + """Configure caplog to capture INFO level logs.""" |
| 14 | + caplog.set_level(logging.INFO) |
| 15 | + return caplog |
| 16 | + |
| 17 | + |
| 18 | +"""Tests for delete_support_by_name function.""" |
| 19 | + |
| 20 | + |
| 21 | +def test_delete_single_support_deletes_listing(enable_factory_create, db_session, caplog_info): |
| 22 | + """Test that deleting the only support also deletes the listing.""" |
| 23 | + # Create a support listing with a single support |
| 24 | + support_listing = factories.SupportListingFactory.create(name="Test Listing") |
| 25 | + support = factories.SupportFactory.create(name="Test Support", support_listing=support_listing) |
| 26 | + db_session.flush() |
| 27 | + |
| 28 | + support_id = support.id |
| 29 | + listing_id = support_listing.id |
| 30 | + |
| 31 | + # Delete the support |
| 32 | + result = delete_support_by_name(db_session, "Test Support") |
| 33 | + |
| 34 | + assert result is True |
| 35 | + assert "Deleted Support 'Test Support'" in caplog_info.text |
| 36 | + assert "No remaining supports for SupportListing" in caplog_info.text |
| 37 | + assert "Deleted SupportListing 'Test Listing'" in caplog_info.text |
| 38 | + |
| 39 | + # Verify support and listing are deleted |
| 40 | + assert db_session.query(Support).filter_by(id=support_id).one_or_none() is None |
| 41 | + assert db_session.query(SupportListing).filter_by(id=listing_id).one_or_none() is None |
| 42 | + |
| 43 | + |
| 44 | +def test_delete_support_keeps_listing_with_remaining_supports( |
| 45 | + enable_factory_create, db_session, caplog_info |
| 46 | +): |
| 47 | + """Test that deleting a support keeps the listing if other supports remain.""" |
| 48 | + # Create a support listing with multiple supports |
| 49 | + support_listing = factories.SupportListingFactory.create(name="Test Listing 1") |
| 50 | + support1 = factories.SupportFactory.create( |
| 51 | + name="Test Support 1", support_listing=support_listing |
| 52 | + ) |
| 53 | + support2 = factories.SupportFactory.create( |
| 54 | + name="Test Support 2", support_listing=support_listing |
| 55 | + ) |
| 56 | + db_session.flush() |
| 57 | + |
| 58 | + support1_id = support1.id |
| 59 | + support2_id = support2.id |
| 60 | + listing_id = support_listing.id |
| 61 | + |
| 62 | + # Delete one support |
| 63 | + result = delete_support_by_name(db_session, "Test Support 1") |
| 64 | + |
| 65 | + assert result is True |
| 66 | + assert "Deleted Support 'Test Support 1'" in caplog_info.text |
| 67 | + assert "has 1 remaining support(s), keeping it" in caplog_info.text |
| 68 | + |
| 69 | + # Verify first support is deleted but listing and second support remain |
| 70 | + assert db_session.query(Support).filter_by(id=support1_id).one_or_none() is None |
| 71 | + assert db_session.query(Support).filter_by(id=support2_id).one_or_none() is not None |
| 72 | + assert db_session.query(SupportListing).filter_by(id=listing_id).one_or_none() is not None |
| 73 | + |
| 74 | + |
| 75 | +def test_delete_nonexistent_support(enable_factory_create, db_session, caplog_info): |
| 76 | + """Test that deleting a non-existent support returns False.""" |
| 77 | + result = delete_support_by_name(db_session, "Nonexistent Support") |
| 78 | + |
| 79 | + assert result is False |
| 80 | + assert "Support with name 'Nonexistent Support' not found" in caplog_info.text |
0 commit comments