def load_articles_and_tags(data_path: str, repo: MemoryRepository): tags = dict() for data_row in read_csv_file(os.path.join(data_path, 'news_articles.csv')): article_key = int(data_row[0]) number_of_tags = len(data_row) - 6 article_tags = data_row[-number_of_tags:] # Add any new tags; associate the current article with tags. for tag in article_tags: if tag not in tags.keys(): tags[tag] = list() tags[tag].append(article_key) del data_row[-number_of_tags:] # Create Article object. article = Article(date=date.fromisoformat(data_row[1]), title=data_row[2], first_para=data_row[3], hyperlink=data_row[4], image_hyperlink=data_row[5], id=article_key) # Add the Article to the repository. repo.add_article(article) # Create Tag objects, associate them with Articles and add them to the repository. for tag_name in tags.keys(): tag = Tag(tag_name) for article_id in tags[tag_name]: article = repo.get_article(article_id) make_tag_association(article, tag) repo.add_tag(tag)
def test_repository_can_add_a_tag(session_factory): repo = SqlAlchemyRepository(session_factory) tag = Tag('Motoring') repo.add_tag(tag) assert tag in repo.get_tags()
def test_repository_can_add_a_tag(session): repo = SqlAlchemyRepository(session) tag = Tag('Motoring') repo.add_tag(tag) session.commit() assert tag in repo.get_tags()
def test_repository_can_retrieve_article(session_factory): repo = SqlAlchemyRepository(session_factory) article = repo.get_article(1) # Check that the Article has the expected title. assert article.title == 'Coronavirus: First case of virus in New Zealand' # Check that the Article is commented as expected. comment_one = [comment for comment in article.comments if comment.comment == 'Oh no, COVID-19 has hit New Zealand'][ 0] comment_two = [comment for comment in article.comments if comment.comment == 'Yeah Freddie, bad news'][0] assert comment_one.user.username == 'fmercury' assert comment_two.user.username == "thorke" # Check that the Article is tagged as expected. assert article.is_tagged_by(Tag('Health')) assert article.is_tagged_by(Tag('New Zealand'))
def test_make_tag_associations(): article = Article( date.fromisoformat('2020-03-15'), 'Coronavirus travel restrictions: Self-isolation deadline pushed back to give airlines breathing room', 'The self-isolation deadline has been pushed back', 'https://www.nzherald.co.nz/business/news/article.cfm?c_id=3&objectid=12316800', 'https://th.bing.com/th/id/OIP.0lCxLKfDnOyswQCF9rcv7AHaCz?w=344&h=132&c=7&o=5&pid=1.7' ) tag = Tag('New Zealand') make_tag_association(article, tag) # Check that the Article knows about the Tag. assert article.is_tagged() assert article.is_tagged_by(tag) # check that the Tag knows about the Article. assert tag.is_applied_to(article) assert article in tag.tagged_articles
def load_articles_and_tags(data_path: str, repo: MemoryRepository): tags = dict() for data_row in read_csv_file(os.path.join(data_path, 'Data1000Movies.csv')): article_key = int(data_row[0]) article_tags = data_row[2].split(",") # Add any new tags; associate the current article with tags. for tag in article_tags: if tag not in tags.keys(): tags[tag] = list() tags[tag].append(article_key) # Create Article object. article = Article( date=int(data_row[6]), title=data_row[1], first_para=data_row[3], director=data_row[4], actors=data_row[5], #hyperlink=data_row[4], #image_hyperlink=data_row[5], id=article_key) # Add the Article to the repository. repo.add_article(article) # Create Tag objects, associate them with Articles and add them to the repository. for tag_name in tags.keys(): tag = Tag(tag_name) for article_id in tags[tag_name]: article = repo.get_article(article_id) make_tag_association(article, tag) repo.add_tag(tag)
def test_tag_construction(): tag = Tag('New Zealand') assert tag.tag_name == 'New Zealand' assert len(tag.tagged_articles) == 0 assert not tag.is_applied_to(Article(None, None, None, None, None, None))
def tag(): return Tag('New Zealand')
def make_tag(): tag = Tag("News") return tag
def test_repository_can_add_a_tag(in_memory_repo): tag = Tag('Motoring') in_memory_repo.add_tag(tag) assert tag in in_memory_repo.get_tags()