|
1 | 1 | import unittest |
2 | 2 | import os |
3 | 3 | import sys |
| 4 | +from collections import Counter |
4 | 5 | from scholarly import scholarly, ProxyGenerator |
5 | 6 | from scholarly.data_types import Mandate |
6 | 7 | from scholarly.publication_parser import PublicationParser |
@@ -549,6 +550,16 @@ def test_save_journal_leaderboard(self): |
549 | 550 | if os.path.exists(filename): |
550 | 551 | os.remove(filename) |
551 | 552 |
|
| 553 | + def test_bin_citations_by_year(self): |
| 554 | + """Test an internal optimization function to bin cites_per_year |
| 555 | + while keeping the citation counts less than 1000 per bin. |
| 556 | + """ |
| 557 | + cpy = {2022: 490, 2021: 340, 2020:327, 2019:298, 2018: 115, 2017: 49, 2016: 20, 2015: 8, 2014: 3, 2013: 1, 2012: 1} |
| 558 | + years = scholarly._bin_citations_by_year(cpy, 2022) |
| 559 | + for y_hi, y_lo in years: |
| 560 | + self.assertLessEqual(y_lo, y_hi) |
| 561 | + self.assertLessEqual(sum(cpy[y] for y in range(y_lo, y_hi+1)), 1000) |
| 562 | + |
552 | 563 |
|
553 | 564 | class TestScholarlyWithProxy(unittest.TestCase): |
554 | 565 | @classmethod |
@@ -795,5 +806,41 @@ def test_pubs_custom_url(self): |
795 | 806 | self.assertEqual(pub['bib']['pub_year'], '2009') |
796 | 807 | self.assertGreaterEqual(pub['num_citations'], 581) |
797 | 808 |
|
| 809 | + def check_citedby_1k(self, pub): |
| 810 | + """A common checking method to check |
| 811 | + """ |
| 812 | + original_citation_count = pub["num_citations"] |
| 813 | + # Trigger a different code path |
| 814 | + if original_citation_count <= 1000: |
| 815 | + pub["num_citations"] = 1001 |
| 816 | + citations = scholarly.citedby(pub) |
| 817 | + citation_list = list(citations) |
| 818 | + self.assertEqual(len(citation_list), original_citation_count) |
| 819 | + return citation_list |
| 820 | + |
| 821 | + @unittest.skipIf(os.getenv("CONNECTION_METHOD") in {None, "none", "freeproxy"}, reason="No robust proxy setup") |
| 822 | + def test_citedby_1k_citations(self): |
| 823 | + """Test that scholarly can fetch 1000+ citations from an author |
| 824 | + """ |
| 825 | + author = scholarly.search_author_id('QoX9bu8AAAAJ') |
| 826 | + scholarly.fill(author, sections=['publications']) |
| 827 | + pub = [_p for _p in author['publications'] if _p["author_pub_id"]=="QoX9bu8AAAAJ:L8Ckcad2t8MC"][0] |
| 828 | + scholarly.fill(pub) |
| 829 | + citation_list = self.check_citedby_1k(pub) |
| 830 | + |
| 831 | + yearwise_counter = Counter([c["bib"]["pub_year"] for c in citation_list]) |
| 832 | + for year, count in pub["cites_per_year"].items(): |
| 833 | + self.assertEqual(yearwise_counter.get(str(year), 0), count) |
| 834 | + |
| 835 | + @unittest.skipIf(os.getenv("CONNECTION_METHOD") in {None, "none", "freeproxy"}, reason="No robust proxy setup") |
| 836 | + def test_citedby_1k_scholar(self): |
| 837 | + """Test that scholarly can fetch 1000+ citations from a pub search. |
| 838 | + """ |
| 839 | + title = "Persistent entanglement in a class of eigenstates of quantum Heisenberg spin glasses" |
| 840 | + pubs = scholarly.search_pubs(title) |
| 841 | + pub = next(pubs) |
| 842 | + self.check_citedby_1k(pub) |
| 843 | + |
| 844 | + |
798 | 845 | if __name__ == '__main__': |
799 | 846 | unittest.main() |
0 commit comments