def test_replaceRegion_found_middle(self):
        """Test the replaceRegion method when the matched region not first nor last in the list."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        firstRegion = RectangularRegion(x1=20,
                                        y1=20,
                                        x2=30,
                                        y2=30,
                                        id="firstId")
        regionToMatch = RectangularRegion(x1=10,
                                          y1=10,
                                          x2=20,
                                          y2=20,
                                          id="matchId")
        lastRegion = RectangularRegion(x1=30, y1=30, x2=40, y2=40, id="lastId")
        newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="matchId")

        unit.addRegion(firstRegion)
        unit.addRegion(regionToMatch)
        unit.addRegion(lastRegion)

        unit.replaceRegion(newRegion)

        self.assertEqual(
            unit.excludedRegions, [firstRegion, newRegion, lastRegion],
            "The excluded regions should be updated by replaceRegion if the ID is found (last)"
        )
    def test_replaceRegion_found_last(self):
        """Test the replaceRegion method when the region matches the last in the list."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        regionToMatch = RectangularRegion(x1=10,
                                          y1=10,
                                          x2=20,
                                          y2=20,
                                          id="matchId")
        otherRegion = RectangularRegion(x1=20,
                                        y1=20,
                                        x2=30,
                                        y2=30,
                                        id="otherId")
        newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="matchId")

        unit.addRegion(otherRegion)
        unit.addRegion(regionToMatch)

        unit.replaceRegion(newRegion)

        self.assertEqual(
            unit.excludedRegions, [otherRegion, newRegion],
            "The excluded regions should be updated by replaceRegion if the ID is found (last)"
        )
    def test_replaceRegion_noRegions(self):
        """Test the replaceRegion method when no regions are defined."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="someId")

        with self.assertRaises(ValueError):
            unit.replaceRegion(newRegion)
    def test_replaceRegion_missingId(self):
        """Test the replaceRegion method when the region procided doesn't have an assigned ID."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100)
        newRegion.id = None

        with self.assertRaises(ValueError):
            unit.replaceRegion(newRegion)
    def test_replaceRegion_notFound(self):
        """Test the replaceRegion method when the region is not found."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        existingRegion = RectangularRegion(x1=10,
                                           y1=10,
                                           x2=20,
                                           y2=20,
                                           id="otherId")
        newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="someId")

        unit.addRegion(existingRegion)

        with self.assertRaises(ValueError):
            unit.replaceRegion(newRegion)
    def test_replaceRegion_found_mustContain_contained(self):
        """Test the replaceRegion method when mustContainOldRegion is True and a match is found."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        regionToMatch = RectangularRegion(x1=10,
                                          y1=10,
                                          x2=20,
                                          y2=20,
                                          id="matchId")
        newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="matchId")

        unit.addRegion(regionToMatch)

        unit.replaceRegion(newRegion, True)

        self.assertEqual(
            unit.excludedRegions, [newRegion],
            "The excluded regions should be updated by replaceRegion if the ID is found (contained)"
        )
    def test_replaceRegion_found_single(self):
        """Test the replaceRegion method when the region matches the only one defined."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        regionToMatch = RectangularRegion(x1=10,
                                          y1=10,
                                          x2=20,
                                          y2=20,
                                          id="matchId")
        newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="matchId")

        unit.addRegion(regionToMatch)

        unit.replaceRegion(newRegion)

        self.assertEqual(
            unit.excludedRegions, [newRegion],
            "The excluded regions should be updated by replaceRegion if the ID is found (single)"
        )
    def test_replaceRegion_found_mustContain_notContained(self):
        """Test the replaceRegion method when mustContainOldRegion is True and no match is found."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        regionToMatch = RectangularRegion(x1=10,
                                          y1=10,
                                          x2=20,
                                          y2=20,
                                          id="matchId")
        newRegion = RectangularRegion(x1=0, y1=0, x2=5, y2=5, id="matchId")

        unit.addRegion(regionToMatch)

        with self.assertRaises(ValueError):
            unit.replaceRegion(newRegion, True)

        self.assertEqual(
            unit.excludedRegions, [regionToMatch],
            "The excluded regions should not be modified by replaceRegion (match, not contained)"
        )