From c2248b40e00126a618cf8d326483899856b2d381 Mon Sep 17 00:00:00 2001 From: Tom Donoghue Date: Fri, 30 Aug 2024 09:37:42 -0400 Subject: [PATCH] fix ArticlesAll for empty colelction --- lisc/data/articles_all.py | 18 ++++++++++++------ lisc/tests/data/test_articles_all.py | 28 ++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lisc/data/articles_all.py b/lisc/data/articles_all.py index ffc5372..987c0ac 100644 --- a/lisc/data/articles_all.py +++ b/lisc/data/articles_all.py @@ -144,12 +144,18 @@ def create_summary(self): self.summary['label'] = self.label self.summary['n_articles'] = str(self.n_articles) - self.summary['top_author_name'] = ' '.join(self.authors.most_common()[0][0]) - self.summary['top_author_count'] = str(self.authors.most_common()[0][1]) - self.summary['top_journal_name'] = self.journals.most_common()[0][0] - self.summary['top_journal_count'] = str(self.journals.most_common()[0][1]) - self.summary['top_keywords'] = [freq[0] for freq in self.keywords.most_common()[0:5]] - self.summary['first_publication'] = str(min(self.years.keys())) + if self.has_data: + self.summary['top_author_name'] = ' '.join(self.authors.most_common()[0][0]) + self.summary['top_author_count'] = str(self.authors.most_common()[0][1]) + self.summary['top_journal_name'] = self.journals.most_common()[0][0] + self.summary['top_journal_count'] = str(self.journals.most_common()[0][1]) + self.summary['top_keywords'] = [freq[0] for freq in self.keywords.most_common()[0:5]] + self.summary['first_publication'] = str(min(self.years.keys())) + else: + labels = ['top_author_name', 'top_author_count', 'top_journal_name', + 'top_journal_count', 'top_keywords', 'first_publication'] + for label in labels: + self.summary[label] = None def print_summary(self): diff --git a/lisc/tests/data/test_articles_all.py b/lisc/tests/data/test_articles_all.py index 3d83294..9e87ef9 100755 --- a/lisc/tests/data/test_articles_all.py +++ b/lisc/tests/data/test_articles_all.py @@ -10,11 +10,6 @@ def test_articles_all(tarts_data): data_all = ArticlesAll(tarts_data) assert data_all -def test_articles_all_none(tarts_none): - - data_all = ArticlesAll(tarts_none) - assert data_all - def test_check(tarts_all): tarts_all.check_frequencies(data_type='words') @@ -23,8 +18,29 @@ def test_check(tarts_all): def test_summary(tdb, tarts_all): tarts_all.create_summary() - assert tarts_all.summary tarts_all.print_summary() tarts_all.save_summary(directory=tdb) + +def test_articles_all_none(tarts_none): + + data_all = ArticlesAll(tarts_none) + assert data_all + + data_all.check_frequencies(data_type='words') + data_all.check_frequencies(data_type='keywords') + + data_all.create_summary() + assert data_all.summary + +def test_articles_all_empty(tarts): + + data_all = ArticlesAll(tarts) + assert not data_all.has_data + + data_all.check_frequencies(data_type='words') + data_all.check_frequencies(data_type='keywords') + + data_all.create_summary() + assert data_all.summary