1+ from tabnanny import verbose
12from pytrackunit .webcache import WebCache , get_from_file
23import os .path
34import hashlib
910DUMMY_URL = 'https://pokeapi.co/api/v2/pokemon/ditto'
1011DUMMY_URL_HASH = "70551c7a431274e4617c94ad307346d2"
1112
13+ CACHE_DIR = "pytest-web-cache"
14+
1215# This will remove error "message loop closed" under windows
1316if system () == "Windows" :
1417 asyncio .set_event_loop_policy (asyncio .WindowsSelectorEventLoopPolicy ())
1518
1619def get_hash (x ):
1720 return hashlib .sha256 (x .encode ('ascii' )).hexdigest ()
1821def test_Clean ():
19- cache = WebCache (_dir = "pytest-web-cache" )
22+ cache = WebCache (webcache_dir = CACHE_DIR , verbose = True )
2023 cache .clean ()
21- assert os .path .exists (cache . dir )
22- assert len (listdir (cache . dir )) == 0
23- fname = os .path .join (cache . dir ,"asdf.txt" )
24+ assert os .path .exists (CACHE_DIR )
25+ assert len (listdir (CACHE_DIR )) == 0
26+ fname = os .path .join (CACHE_DIR ,"asdf.txt" )
2427 f = open (fname ,"w" ,encoding = 'utf8' )
2528 f .write ('{"data": "data"}' )
2629 f .flush ()
2730 f .close ()
2831 cache .clean ()
29- assert os .path .exists (cache . dir )
30- assert len (listdir (cache . dir )) == 0
32+ assert os .path .exists (CACHE_DIR )
33+ assert len (listdir (CACHE_DIR )) == 0
3134 cache .clean ()
32- assert os .path .exists (cache . dir )
33- assert len (listdir (cache . dir )) == 0
35+ assert os .path .exists (CACHE_DIR )
36+ assert len (listdir (CACHE_DIR )) == 0
3437def test_GetFromFile_Clean ():
35- cache = WebCache (_dir = "pytest-web-cache" )
38+ cache = WebCache (webcache_dir = CACHE_DIR )
3639 cache .clean ()
3740 data = asyncio .run (get_from_file ('asdf' ))
3841 assert data is None
3942 cache .clean ()
4043def test_GetFromFile_WithFile ():
41- cache = WebCache (_dir = "pytest-web-cache" )
44+ cache = WebCache (webcache_dir = CACHE_DIR )
4245 cache .clean ()
43- fname = os .path .join (cache . dir ,"asdf.txt" )
46+ fname = os .path .join (CACHE_DIR ,"asdf.txt" )
4447 f = open (fname ,"w" ,encoding = 'utf8' )
4548 f .write ('{"data": "data"}' )
4649 f .flush ()
@@ -50,9 +53,9 @@ def test_GetFromFile_WithFile():
5053 assert data ["data" ] == "data"
5154 cache .clean ()
5255def test_GetFromFile_With_incorrect_File ():
53- cache = WebCache (_dir = "pytest-web-cache" ,verbose = True )
56+ cache = WebCache (webcache_dir = CACHE_DIR ,verbose = True )
5457 cache .clean ()
55- fname = os .path .join (cache . dir ,"asdf.txt" )
58+ fname = os .path .join (CACHE_DIR ,"asdf.txt" )
5659 f = open (fname ,"w" ,encoding = 'utf8' )
5760 f .write ('{"data": "data"' )
5861 f .flush ()
@@ -61,35 +64,35 @@ def test_GetFromFile_With_incorrect_File():
6164 data = asyncio .run (get_from_file (fname ))
6265 assert data is None
6366def test_Get_Clean ():
64- cache = WebCache (_dir = "pytest-web-cache" )
67+ cache = WebCache (webcache_dir = CACHE_DIR )
6568 cache .clean ()
6669 data = asyncio .run (cache .get (DUMMY_URL ))
6770 cache .clean ()
6871def test_Get_WithFile ():
69- cache = WebCache (_dir = "pytest-web-cache" )
72+ cache = WebCache (webcache_dir = CACHE_DIR )
7073 cache .clean ()
7174 data = asyncio .run (cache .get (DUMMY_URL ))
7275 assert data ["abilities" ][0 ]["ability" ]["name" ] == "limber"
7376 data = asyncio .run (cache .get (DUMMY_URL ))
7477 assert data ["abilities" ][0 ]["ability" ]["name" ] == "limber"
7578 cache .clean ()
7679def test_Get_no_file_read ():
77- cache = WebCache (_dir = "pytest-web-cache" ,dont_read_files = True )
80+ cache = WebCache (webcache_dir = CACHE_DIR ,dont_read_files = True )
7881 cache .clean ()
7982 data = asyncio .run (cache .get (DUMMY_URL ))
8083 assert data ["abilities" ][0 ]["ability" ]["name" ] == "limber"
8184 data = asyncio .run (cache .get (DUMMY_URL ))
8285 assert data == {}
8386 cache .clean ()
8487def test_Get_write_file ():
85- cache = WebCache (_dir = "pytest-web-cache" ,verbose = True )
86- fname = os .path .join (cache . dir ,DUMMY_URL_HASH + ".json" )
88+ cache = WebCache (webcache_dir = CACHE_DIR ,verbose = True )
89+ fname = os .path .join (CACHE_DIR ,DUMMY_URL_HASH + ".json" )
8790 cache .clean ()
8891 data1 = asyncio .run (cache .get (DUMMY_URL ))
8992 data2 = asyncio .run (get_from_file (fname ))
9093 assert data2 ["abilities" ][0 ]["ability" ]["name" ] == "limber"
9194def test_Get_verbose (capsys ):
92- cache = WebCache (_dir = "pytest-web-cache" ,verbose = True )
95+ cache = WebCache (webcache_dir = CACHE_DIR ,verbose = True )
9396 cache .clean ()
9497 data1 = asyncio .run (cache .get (DUMMY_URL ))
9598 captured = capsys .readouterr ()
@@ -101,7 +104,7 @@ def test_Get_verbose(capsys):
101104 assert captured .out .split ('\n ' )[1 ].split (' ' )[0 ] == DUMMY_URL
102105 assert captured .out .split ('\n ' )[1 ].split (' ' )[2 ] == "C"
103106def test_return_hashes ():
104- cache = WebCache (_dir = "pytest-web-cache" ,verbose = True ,return_only_cache_files = True )
107+ cache = WebCache (webcache_dir = CACHE_DIR ,verbose = True ,return_only_cache_files = True )
105108 cache .clean ()
106109 data = asyncio .run (cache .get (DUMMY_URL ))
107110 assert data == DUMMY_URL_HASH + ".json"
0 commit comments