def test_has_area(): """Test `has_area` method of a site instance.""" has_area = Site("boston").has_area() expected_has_area = True assert has_area == expected_has_area has_area = Site("eugene").has_area() expected_has_area = False assert has_area == expected_has_area
def craigsearch(CraigslistObject, site_keys, **kwargs): for key in site_keys: site = Site(key) if site.has_area(): for area in site: yield CraigslistObject(site=site.key(), area=area.key(), **kwargs) else: yield CraigslistObject(site=site.key(), **kwargs)
def get_urls(): """Get every Craigslist url using craigslist_meta API.""" all_urls = [] for site in Site.all(): if site.has_area(): for area in site: all_urls.append(area.url) else: all_urls.append(site.url) return all_urls
def area(): """Get an instance of Area.""" area = next(iter(Site(site_key))) global area_key area_key = area.key yield area
def test_key_raises(): """Constructing Site with an invalid key should raise an exception.""" with pytest.raises(ValueError): Site("invalid_key")
def test_all(): """Test `all` method yields all site instances.""" site_instances = [site for site in Site.all()] assert all(isinstance(item, Site) for item in site_instances)
def test_url(get_url): """Test `url` attribute of site instance.""" site_url = Site(key).url expected_url = get_url(selector, key) assert site_url == expected_url
def test_title(get_title): """Test `title` attribute of site instance.""" site_title = Site(key).title expected_title = get_title(selector, key) assert site_title == expected_title
def test_children(get_children): """Test `children` attribute of site instance.""" site_children = Site(key).children expected_children = list(get_children(selector, key)) assert site_children == expected_children
def test_key(): """Test `key` attribute of site instance.""" site_key = Site(key).key expected_key = key assert site_key == expected_key