1- import logging
21import json
2+ import logging
33import os
4+ import shutil
5+ import tempfile
6+ import time
7+ from datetime import datetime
8+ from unittest import mock
49
5- import pandas as pd
610import numpy as np
11+ import pandas as pd
712import pytest
8-
9- import tempfile
10-
13+ import synapseclient
14+ import synapseclient .core .cache as cache
1115from pandas .testing import assert_frame_equal
1216from synapseclient .core .exceptions import SynapseHTTPError
1317
14- from schematic .schemas .explorer import SchemaExplorer
15- from schematic .schemas import df_parser
16- from schematic .utils import general
17- from schematic .utils import cli_utils
18- from schematic .utils import io_utils
19- from schematic .utils import df_utils
20- from schematic .utils import validate_utils
21- from schematic .exceptions import (
22- MissingConfigValueError ,
23- MissingConfigAndArgumentValueError ,
24- )
2518from schematic import LOADER
19+ from schematic .exceptions import (MissingConfigAndArgumentValueError ,
20+ MissingConfigValueError )
21+ from schematic .schemas import df_parser
22+ from schematic .schemas .explorer import SchemaExplorer
2623from schematic .store .synapse import SynapseStorage
27- from schematic .utils .general import entity_type_mapping
24+ from schematic .utils import (cli_utils , df_utils , general , io_utils ,
25+ validate_utils )
26+ from schematic .utils .general import (calculate_datetime ,
27+ check_synapse_cache_size ,
28+ clear_synapse_cache , entity_type_mapping )
2829
2930logging .basicConfig (level = logging .DEBUG )
3031logger = logging .getLogger (__name__ )
3132
33+ IN_GITHUB_ACTIONS = os .getenv ("GITHUB_ACTIONS" )
3234
3335@pytest .fixture
3436def synapse_store ():
@@ -39,8 +41,69 @@ def synapse_store():
3941 synapse_store = SynapseStorage ()
4042 yield synapse_store
4143
42-
4344class TestGeneral :
45+ def test_clear_synapse_cache (self , tmp_path ):
46+ # define location of mock synapse cache
47+ mock_synapse_cache_dir = tmp_path / ".synapseCache/"
48+ mock_synapse_cache_dir .mkdir ()
49+ mock_sub_folder = mock_synapse_cache_dir / "123"
50+ mock_sub_folder .mkdir ()
51+ mock_table_query_folder = mock_sub_folder / "456"
52+ mock_table_query_folder .mkdir ()
53+
54+ # create mock table query csv and a mock cache map
55+ mock_synapse_table_query_csv = mock_table_query_folder / "mock_synapse_table_query.csv"
56+ mock_synapse_table_query_csv .write_text ("mock table query content" )
57+ mock_cache_map = mock_table_query_folder / ".cacheMap"
58+ mock_cache_map .write_text (f"{ mock_synapse_table_query_csv } : '2022-06-13T19:24:27.000Z'" )
59+
60+ assert os .path .exists (mock_synapse_table_query_csv )
61+
62+ # since synapse python client would compare last modified date and before date
63+ # we have to create a little time gap here
64+ time .sleep (1 )
65+
66+ # clear cache
67+ my_cache = cache .Cache (cache_root_dir = mock_synapse_cache_dir )
68+ clear_synapse_cache (my_cache , minutes = 0.0001 )
69+ # make sure that cache files are now gone
70+ assert os .path .exists (mock_synapse_table_query_csv ) == False
71+ assert os .path .exists (mock_cache_map ) == False
72+
73+ def test_calculate_datetime_before_minutes (self ):
74+ input_date = datetime .strptime ("07/20/23 17:36:34" , '%m/%d/%y %H:%M:%S' )
75+ minutes_before = calculate_datetime (input_date = input_date , minutes = 10 , before_or_after = "before" )
76+ expected_result_date_before = datetime .strptime ("07/20/23 17:26:34" , '%m/%d/%y %H:%M:%S' )
77+ assert minutes_before == expected_result_date_before
78+
79+ def test_calculate_datetime_after_minutes (self ):
80+ input_date = datetime .strptime ("07/20/23 17:36:34" , '%m/%d/%y %H:%M:%S' )
81+ minutes_after = calculate_datetime (input_date = input_date , minutes = 10 , before_or_after = "after" )
82+ expected_result_date_after = datetime .strptime ("07/20/23 17:46:34" , '%m/%d/%y %H:%M:%S' )
83+ assert minutes_after == expected_result_date_after
84+
85+ def test_calculate_datetime_raise_error (self ):
86+ with pytest .raises (ValueError ):
87+ input_date = datetime .strptime ("07/20/23 17:36:34" , '%m/%d/%y %H:%M:%S' )
88+ minutes = calculate_datetime (input_date = input_date , minutes = 10 , before_or_after = "error" )
89+
90+ # this test might fail for windows machine
91+ @pytest .mark .not_windows
92+ def test_check_synapse_cache_size (self ,tmp_path ):
93+ mock_synapse_cache_dir = tmp_path / ".synapseCache"
94+ mock_synapse_cache_dir .mkdir ()
95+
96+ mock_synapse_table_query_csv = mock_synapse_cache_dir / "mock_synapse_table_query.csv"
97+ mock_synapse_table_query_csv .write_text ("example file for calculating cache" )
98+
99+ file_size = check_synapse_cache_size (mock_synapse_cache_dir )
100+
101+ # For some reasons, when running in github action, the size of file changes.
102+ if IN_GITHUB_ACTIONS :
103+ assert file_size == 8000
104+ else :
105+ assert file_size == 4000
106+
44107 def test_find_duplicates (self ):
45108
46109 mock_list = ["foo" , "bar" , "foo" ]
@@ -84,6 +147,7 @@ def test_download_manifest_to_temp_folder(self):
84147 path_dir = general .create_temp_folder (tmpdir )
85148 assert os .path .exists (path_dir )
86149
150+
87151class TestCliUtils :
88152 def test_query_dict (self ):
89153
0 commit comments