def test_strip_comments_ignores_url(self): """test strips comment but doesn't affect http://...""" params = """ // my comment { "hello": "http://world" } """ assert read_json(params) == {"hello": "http://world"}
def test_read_invalid(self): with pytest.raises(ValueError): read_json('{"hello": "world"') with pytest.raises(ValueError): read_json(f":{['a'] * 200}") with pytest.raises(TypeError): read_json(("hello", "world")) with pytest.raises(TypeError): read_json(None)
def test_strip_comments_simple(self): """test strip comment""" params = """ // my comment // another { "hello": "world" } """ assert read_json(params) == {"hello": "world"}
def test_strip_comments_multiline(self): """test strip comment""" params = """ /* my comment another */ { "hello": "world" } """ assert read_json(params) == {"hello": "world"}
def test_read_string(self): res = read_json('{"hello": "world"}') assert isinstance(res, dict)
def test_read_file_path(self): CURRENT_PATH = os.path.abspath(os.path.dirname(__file__)) defaults_path = os.path.join(CURRENT_PATH, "defaults.json") res = read_json(defaults_path) assert isinstance(res, dict)
def test_read_github(self): gh_path = "github://*****:*****@master/paramtools/tests/defaults.json" res = read_json(gh_path) assert isinstance(res, dict)
def test_read_http(self): http_path = ( "https://raw.githubusercontent.com/PSLmodels/ParamTools/master/" "paramtools/tests/defaults.json") res = read_json(http_path) assert isinstance(res, dict)
def test_read_gcp(self): res = read_json("gs://paramtools-dev/defaults.json", {"token": "anon"}) assert isinstance(res, dict)
def test_read_s3(self): res = read_json("s3://paramtools-test/defaults.json", {"anon": True}) assert isinstance(res, dict)
def _read_json_revision(obj, topkey): """ Read JSON revision specified by ``obj`` and ``topkey`` returning a single revision dictionary suitable for use with the ``Parameters._update`` or ``Parameters.adjust`` methods. The obj function argument can be ``None`` or a string, where the string can be: - Path for a local file - Link pointing to a valid JSON file - Valid JSON text The ``topkey`` argument must be a string containing the top-level key in a compound-revision JSON text for which a revision dictionary is returned. If the specified ``topkey`` is not among the top-level JSON keys, the ``obj`` is assumed to be a non-compound-revision JSON text for the specified ``topkey``. Some examples of valid links are: - HTTP: ``https://raw.githubusercontent.com/PSLmodels/Tax-Calculator/master/taxcalc/reforms/2017_law.json`` - Github API: ``github://PSLmodels:Tax-Calculator@master/taxcalc/reforms/2017_law.json`` Checkout the ParamTools `docs <https://paramtools.dev/_modules/paramtools/parameters.html#Parameters.read_params>`_ for more information on valid file URLs. """ # noqa # embedded function used only in _read_json_revision staticmethod def convert_year_to_int(syr_dict): """ Converts specified syr_dict, which has string years as secondary keys, into a dictionary with the same structure but having integer years as secondary keys. """ iyr_dict = dict() for pkey, sdict in syr_dict.items(): assert isinstance(pkey, str) iyr_dict[pkey] = dict() assert isinstance(sdict, dict) for skey, val in sdict.items(): assert isinstance(skey, str) year = int(skey) iyr_dict[pkey][year] = val return iyr_dict # end of embedded function # process the main function arguments if obj is None: return dict() full_dict = pt.read_json(obj) # check top-level key contents of dictionary if topkey in full_dict.keys(): single_dict = full_dict[topkey] else: single_dict = full_dict if is_paramtools_format(single_dict): return single_dict # convert string year to integer year in dictionary and return return convert_year_to_int(single_dict)
def test_get_title(): data = paramtools.read_json("cs://*****:*****@47517/title") assert data == {"title": "Test TB after updates"}
def test_get_owner(): data = paramtools.read_json("cs://*****:*****@47517/owner") assert data == {"owner": "hdoupe"}
def test_get_outputs(): data = paramtools.read_json("cs://*****:*****@47517/outputs") for output in data: assert isinstance(pd.read_csv(io.StringIO(output["data"])), pd.DataFrame)