def test_builder_assigns_address_components_if_formatted_address_present(): address_parser_mock = MagicMock(spec=address_parser) parsed_address_mock = MagicMock() address = 'fake_address' city = 'fake_city' state = 'fake_state' zip_code = 'fake_zip_code' formatted_address = 'fake_formatted_address ' parsed_address_mock.address1 = address parsed_address_mock.city = city parsed_address_mock.state = state parsed_address_mock.zip_code = zip_code parsed_address_mock.formatted_address = formatted_address address_parser_mock.parse_address = MagicMock(return_value=parsed_address_mock) builder = ListingBuilder(address_parser_mock, formatted_address='x') builder.listing_attrs_output = MagicMock() builder._build_formatted_address() call_list = [ call(listing_builder.ADDRESS, address), call(listing_builder.CITY, city), call(listing_builder.STATE, state), call(listing_builder.ZIP_CODE, zip_code), call(listing_builder.FORMATTED_ADDRESS, formatted_address), ] assert builder.listing_attrs_output.__setitem__.call_args_list == call_list
def test_builder_gets_amenities_from_desc_if_not_in_list(): amenity_service_mock = MagicMock(spec=amenity_service) builder = ListingBuilder(amenity_service=amenity_service_mock) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = 'IGNORE ME' builder._build_amenities() builder.listing_attrs_output.get.assert_called_with(listing_builder.DESCRIPTION)
def test_builder_gets_correct_price_from_text(): home_parser_mock = MagicMock(spec=home_parser) expected_price = 5.0 home_parser_mock.get_price = MagicMock(return_value=expected_price) builder = ListingBuilder(home_parser=home_parser_mock, price='something') builder.listing_attrs_output = MagicMock() builder._build_price() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call(listing_builder.PRICE, expected_price)
def test_builder_gets_correct_sqfeet_from_text(): home_parser_mock = MagicMock(spec=home_parser) expected_sqfeet = 2 home_parser_mock.get_sqfeet = MagicMock(return_value=expected_sqfeet) builder = ListingBuilder(home_parser=home_parser_mock, sqfeet='something') builder.listing_attrs_output = MagicMock() builder._build_sqfeet() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call(listing_builder.SQFEET, expected_sqfeet)
def test_builder_gets_correct_bathroom_from_text(): home_parser_mock = MagicMock(spec=home_parser) expected_bathroom_count = 2 home_parser_mock.get_bathroom_count = MagicMock(return_value=expected_bathroom_count) builder = ListingBuilder(home_parser=home_parser_mock, bathroom_count='something') builder.listing_attrs_output = MagicMock() builder._build_bathroom_count() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call(listing_builder.BATHROOM_COUNT, expected_bathroom_count)
def test_builder_address_only_sanitized_if_untrustworthy(): geo_service_mock = MagicMock(spec=listing_geo_service) listing_source_mock = MagicMock(trusted_geo_data=True) builder = ListingBuilder(listing_geo_service=geo_service_mock) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = listing_source_mock builder._sanitize_address() assert 0 == len(geo_service_mock.get_sanitized_address.call_args_list)
def test_builder_delegates_address_sanitization(): geo_service_mock = MagicMock(spec=listing_geo_service) builder = ListingBuilder(listing_geo_service=geo_service_mock) listing_source_mock = MagicMock(trusted_geo_data=False) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = listing_source_mock builder._sanitize_address() geo_service_mock.get_sanitized_address.assert_called_with(ANY, ANY, ANY)
def test_builder_uses_description_for_email_if_not_available(): contact_parser_mock = MagicMock(spec=contact_parser) expected_email = '*****@*****.**' contact_parser_mock.get_contact_email_address = MagicMock(return_value=expected_email) builder = ListingBuilder(contact_parser=contact_parser_mock) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = True builder._build_contact_email_address() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call(listing_builder.CONTACT_EMAIL_ADDRESS, expected_email)
def test_builder_uses_description_for_phone_if_not_available(): contact_parser_mock = MagicMock(spec=contact_parser) expected_phone = '(555) 555-5555' contact_parser_mock.get_contact_phone_number = MagicMock(return_value=expected_phone) builder = ListingBuilder(contact_parser=contact_parser_mock) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = True builder._build_contact_phone_number() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call(listing_builder.CONTACT_PHONE_NUMBER, expected_phone)
def test_builder_gets_correct_broker_fee_from_url_if_not_in_list(): home_parser_mock = MagicMock(spec=home_parser) expected_fee = True home_parser_mock.get_broker_fee_from_url = MagicMock(return_value=expected_fee) builder = ListingBuilder(home_parser=home_parser_mock) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = True builder._build_broker_fee() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call(listing_builder.BROKER_FEE, expected_fee)
def test_builder_gets_correct_price_title_if_not_in_list(): home_parser_mock = MagicMock(spec=home_parser) expected_price = 100.0 home_parser_mock.get_price = MagicMock(return_value=expected_price) builder = ListingBuilder(home_parser=home_parser_mock) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = True builder._build_price() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call(listing_builder.PRICE, expected_price)
def test_builder_gets_correct_bathroom_from_title_if_not_in_list(): home_parser_mock = MagicMock(spec=home_parser) expected_sqfeet = 100.0 home_parser_mock.get_sqfeet = MagicMock(return_value=expected_sqfeet) builder = ListingBuilder(home_parser=home_parser_mock) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = True builder._build_sqfeet() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call(listing_builder.SQFEET, expected_sqfeet)
def test_builder_gets_correct_bathroom_even_if_0(): home_parser_mock = MagicMock(spec=home_parser) expected_bathroom_count = 0 home_parser_mock.get_bathroom_count = MagicMock(return_value=expected_bathroom_count) builder = ListingBuilder(home_parser=home_parser_mock) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = True builder._build_bathroom_count() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call(listing_builder.BATHROOM_COUNT, expected_bathroom_count)
def test_builder_only_sets_last_updated_date_if_greater_than_posted(): datetime_parser_mock = MagicMock(spec=datetime_parser) datetime_parser_mock.get_datetime = MagicMock( return_value=listing_test_data.cl_listing_3952467416_expected_posted_date ) builder = ListingBuilder(datetime_parser=datetime_parser_mock, last_updated_date='IGNOREME') builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = listing_test_data.cl_listing_3952467416_expected_last_updated_date builder._build_last_updated_date() assert len(builder.listing_attrs_output.__setitem__.call_args_list) == 0
def test_builder_sets_last_updated_date_to_correct_date(): datetime_parser_mock = MagicMock(spec=datetime_parser) datetime_parser_mock.get_datetime = MagicMock( return_value=listing_test_data.cl_listing_3952467416_expected_last_updated_date ) builder = ListingBuilder(datetime_parser=datetime_parser_mock, last_updated_date='IGNOREME') builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = listing_test_data.cl_listing_3952467416_expected_posted_date builder._build_last_updated_date() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call( listing_builder.LAST_UPDATED_DATE, listing_test_data.cl_listing_3952467416_expected_last_updated_date )
def test_builder_delegates_amenitiy_lookup_to_parser(): expected_amenities = [1, 2, 3] text_parser_mock = MagicMock(spec=text_parser) text_parser_mock.get_canonical_name_from_keywords = MagicMock(return_value=expected_amenities) amenity_service_mock = MagicMock(spec=amenity_service) builder = ListingBuilder(text_parser=text_parser_mock, amenity_service=amenity_service_mock) builder.listing_attrs_output = MagicMock() builder.listing_attrs_output.get.return_value = 'IGNORE ME' builder._build_amenities() assert builder.listing_attrs_output.__setitem__.call_args_list[0] == call( listing_builder.AMENITIES, expected_amenities )