class CreateSearchUseCaseTests(unittest.TestCase):
    def setUp(self):
        super(CreateSearchUseCaseTests, self).setUp()
        self.repository = FakeSearchRepository()

        self.subject = CreateSearchUseCase(search_repository=self.repository)

    def test_create_search_with_valid_county_and_address_notifies_observer(self):
        observer = FakeCreateObserver()
        self.subject.execute(address="125 Jones St", county="sanfrancisco", observer=observer)

        search = None

        def fetch_success(incoming_search):
            nonlocal search
            search = incoming_search

        fetch_failure = lambda x, y: None
        self.repository.fetch_search(
            identifier=observer.created_identifier, success=fetch_success, failure=fetch_failure
        )

        expected_fetched_search = Search(county="sanfrancisco", address="125 Jones St")

        assert_searches_equal(observer.created_search, expected_fetched_search, self)
class SearchRepositoryTests(unittest.TestCase):

    def setUp(self):
        super(SearchRepositoryTests, self).setUp()
        self.subject = FakeSearchRepository()


    def test_create_search_with_valid_county_and_address(self):
        search_identifier = None
        search = None
        def create_success(incoming_search_identifier, incoming_search):
            nonlocal search_identifier
            nonlocal search
            search_identifier = incoming_search_identifier
            search = incoming_search

        failure_called = False
        def create_failure(x, y):
            nonlocal failure_called
            failure_called = True

        self.subject.create_search(county='sanfrancisco',
                                   address='125 Jones St',
                                   success=create_success,
                                   failure=create_failure)
        self.assertFalse(failure_called)
        self.assertIsNotNone(search_identifier)

        expected_search = Search(address='125 Jones St',
                                 county='sanfrancisco',)

        assert_searches_equal(search, expected_search, self)

        search = None
        def fetch_success(incoming_search):
            nonlocal search
            search = incoming_search

        exception = None
        context = None
        def fetch_failure(incoming_exception, incoming_context):
            nonlocal search
            exception = incoming_exception
            context = incoming_context

        self.subject.fetch_search(identifier=search_identifier,
                                  success=fetch_success,
                                  failure=fetch_failure)

        assert_searches_equal(search, expected_search, self)
        self.assertIsNone(exception)
        self.assertIsNone(context)