def test_build_query_city_mismatch(self):
        """
        test building the address string with a mispelled city name
        """
        addr = Address.from_dict({
            "street": "",
            "zipcode": "",
            "city": "Bay Ridge",
            "state": "NY",
            "latitude": 0.0,
            "longitude": 0.0,
            "locality": "Brooklyn",
        })

        q_loc, q_apa = build_search_query(address=addr,
                                          orig_query="bayridge",
                                          min_price="",
                                          max_price="",
                                          bed_num="")
        query_result_dict = {
            "city__iexact": "Bay Ridge",
            "state__iexact": "NY",
            "apartment_set__is_rented": False,
        }
        query_result_apa_dict = {"is_rented": False}

        self.assertDictEqual(q_loc, query_result_dict)
        self.assertDictEqual(q_apa, query_result_apa_dict)
    def test_build_query_street_only(self):
        """
        test building the location query with only the street name
        """
        addr = Address.from_dict({
            "street": "Herkimer Street",
            "zipcode": "",
            "city": "Brooklyn",
            "state": "NY",
            "latitude": 0.0,
            "longitude": 0.0,
            "locality": "Brooklyn",
        })

        q_loc, q_apa = build_search_query(
            address=addr,
            orig_query="Herkimer Street",
            min_price="",
            max_price="",
            bed_num="",
        )
        query_result_dict = {
            "address__icontains": "Herkimer Street",
            "state__iexact": "NY",
            "apartment_set__is_rented": False,
        }
        query_result_apa_dict = {"is_rented": False}

        self.assertDictEqual(q_loc, query_result_dict)
        self.assertDictEqual(q_apa, query_result_apa_dict)
    def test_build_query_if_bed(self):
        """
        test building the address string with number of beds
        """
        addr = Address.from_dict({
            "street": "28-15 34th street",
            "zipcode": "11103",
            "city": "Long Island City",
            "state": "NY",
            "latitude": 0.0,
            "longitude": 0.0,
            "locality": "Queens",
        })

        q_loc, q_apa = build_search_query(
            address=addr,
            orig_query="28-15 34th st, Long Island City, NY 11103",
            min_price="",
            max_price="",
            bed_num="1",
        )
        query_result_dict = {
            "address__icontains": "28-15 34th street",
            "city__iexact": "Long Island City",
            "state__iexact": "NY",
            "zipcode": "11103",
            "apartment_set__is_rented": False,
            "apartment_set__number_of_bed": "1",
        }
        query_result_apa_dict = {"is_rented": False, "number_of_bed": "1"}
        self.assertDictEqual(q_loc, query_result_dict)
        self.assertDictEqual(q_apa, query_result_apa_dict)
    def test_build_query_locality_mismatch_no_state(self):
        """
        Tests the return value of build_search_query when
        the address locality is in the original query but
        the locality has spacing issues and there is no state
        """

        addr = Address.from_dict({
            "street": "",
            "zipcode": "",
            "city": "Staten Island",
            "state": "NY",
            "latitude": 40.6195067,
            "longitude": -73.9859414,
            "locality": "Staten Island",
        })

        q_loc, q_apa = build_search_query(
            address=addr,
            orig_query="StatenIsland",
            min_price="",
            max_price="",
            bed_num="",
        )
        query_result_dict = {
            "locality__iexact": "Staten Island",
            "state__iexact": "NY",
            "apartment_set__is_rented": False,
        }
        query_result_apa_dict = {"is_rented": False}

        self.assertDictEqual(q_loc, query_result_dict)
        self.assertDictEqual(q_apa, query_result_apa_dict)
    def test_build_query_zip_only(self):
        """
        Tests the return value of the search query when
        the original zip code is the exact query
        """

        addr = Address.from_dict({
            "street": "",
            "zipcode": "11204",
            "city": "Brooklyn",
            "state": "NY",
            "latitude": 40.6195067,
            "longitude": -73.9859414,
            "locality": "Brooklyn",
        })

        q_loc, q_apa = build_search_query(address=addr,
                                          orig_query="11204",
                                          min_price="",
                                          max_price="",
                                          bed_num="")
        query_result_dict = {"zipcode": "11204"}
        query_result_apa_dict = {}
        self.assertDictEqual(q_loc, query_result_dict)
        self.assertDictEqual(q_apa, query_result_apa_dict)
    def test_search_rented_apartment(self, mock_norm):
        """
        tests to make sure that apartments that have been marked as rented don't show up in
        the general search
        """
        mock_norm.return_value = Address(
            street="",
            city="Brooklyn",
            state="New York",
            zipcode=11218,
            latitude=0.0,
            longitude=0.0,
        )
        loc, apa = create_location_and_apartment()
        response = self.client.get("/search/?query=Brooklyn%2C+New+York+11218")
        self.assertEqual(
            len(response.context["search_data"]["locations"]),
            1,
            msg="Did not return result when it should have",
        )

        apa.is_rented = True
        apa.save()

        # once an apartment has been rented, it should no longer show up in the default search
        response = self.client.get("/search/?query=Brooklyn%2C+New+York+11218")
        self.assertEqual(
            len(response.context["search_data"]["locations"]),
            0,
            msg="Returned results when it shouldn't have",
        )
    def test_search_page_apartments(self, mock_norm):
        """
        tests to make sure that "Apartment(s)" is displayed for each location
        in the search results
        """
        mock_norm.return_value = Address(
            street="",
            city="Brooklyn",
            state="New York",
            zipcode=11218,
            latitude=0.0,
            longitude=0.0,
        )
        loc, apa = create_location_and_apartment()
        response = self.client.get("/search/?query=Brooklyn%2C+New+York+11218")

        self.assertContains(response, "Apartment(s)")