77import random
88import json
99import csv
10+ import requests
11+ from bs4 import BeautifulSoup
1012from contextlib import contextmanager
1113
1214
@@ -640,13 +642,14 @@ def test_download_mandates_csv(self):
640642 # Delete the file with a finally block no matter what happens
641643 try :
642644 scholarly .download_mandates_csv (filename )
643- funder , policy , percentage2020 = [], [], []
645+ funder , policy , percentage2020 , percentageOverall = [], [], [], []
644646 with open (filename , "r" ) as f :
645647 csv_reader = csv .DictReader (f )
646648 for row in csv_reader :
647649 funder .append (row ['\ufeff Funder' ])
648650 policy .append (row ['Policy' ])
649651 percentage2020 .append (row ['2020' ])
652+ percentageOverall .append (row ['Overall' ])
650653
651654 agency_policy = {
652655 "US National Science Foundation" : "https://www.nsf.gov/pubs/2015/nsf15052/nsf15052.pdf" ,
@@ -661,10 +664,17 @@ def test_download_mandates_csv(self):
661664 "Swedish Research Council for Environment, Agricultural Sciences and Spatial Planning" : "88%"
662665 }
663666
664- for agency in agency_policy :
667+ response = requests .get ("https://scholar.google.com/citations?view_op=mandates_leaderboard&hl=en" )
668+ soup = BeautifulSoup (response .text , "html.parser" )
669+ agency_overall = soup .find_all ("td" , class_ = "gsc_mlt_n gsc_mlt_bd" )
670+
671+ for agency , index in zip (agency_policy , [4 - 1 ,10 - 1 , 19 - 1 , 64 - 1 ]):
665672 agency_index = funder .index (agency )
666673 self .assertEqual (policy [agency_index ], agency_policy [agency ])
667- self .assertEqual (percentage2020 [agency_index ], agency_2020 [agency ])
674+ # Check that the percentage values from CSV and on the page agree.
675+ self .assertEqual (percentageOverall [agency_index ], agency_overall [index ].text )
676+ # The percentage fluctuates, so we can't check the exact value.
677+ self .assertAlmostEquals (int (percentage2020 [agency_index ][:- 1 ]), int (agency_2020 [agency ][:- 1 ]), delta = 2 )
668678 finally :
669679 if os .path .exists (filename ):
670680 os .remove (filename )
0 commit comments