def test_set_slicevalue(self, mocker, deep): # NOTE: This somehow did not work (i.e., does not seem to propagate # everywhere it is needed) # mocker.patch("comma.settings", # SLICE_DEEP_COPY_DATA=deep, # SLICE_DEEP_COPY_PARENT=False) # assert comma.settings.SLICE_DEEP_COPY_DATA == deep # backup global setting backup_sdcd = comma.settings.SLICE_DEEP_COPY_DATA comma.settings.SLICE_DEEP_COPY_DATA = deep assert comma.settings.SLICE_DEEP_COPY_DATA == deep cf = comma.load("col1,col2,col3,col4,col5\n" "row1col1,row1col2,row1col3,row1col4,row1col5\n" "row2col1,row2col2,row2col3,row2col4,row2col5\n") actual_slice = ["row1col1", "row2col1"] modified_slice = ["ROW1COL1", "ROW2COL1"] # query check assert list(cf["col1"]) == actual_slice # modification check cf["col1"][0] = modified_slice[0] # verification if deep: # change should not have propagated because of deep copy assert cf["col1"][0] == actual_slice[0] else: assert cf["col1"][0] == modified_slice[0] # restore original global setting comma.settings.SLICE_DEEP_COPY_DATA = backup_sdcd
def _load( self, vars: typing.Optional[typing.Dict[str, str]], ) -> list: # revalidate fields with 'vars' in case the validation of filename was skipped self._validate_fields(vars=vars) raw_data = None # getting the data if "file" in self: file_str = slacktivate.input.helpers.render_jinja2( jinja2_pattern=self.get("file"), data=None, vars=vars, ) files = glob.glob(file_str) # by default, sort is in increasing order of mtime # but if we want newest, we want the largest mtime first, # so that is reverse reverse_sort = (self.get( "sort", SLACKTIVATE_DEFAULT_SORT) == SLACKTIVATE_SORT_NEWEST) files.sort( key=os.path.getmtime, reverse=reverse_sort, ) # guaranteed to exist otherwise we would have raised an exception # above when validating parameters file = files[0] self._source_name = file raw_data = open(file).read() elif "contents" in self: raw_data = self.get("contents") data = None # converting it in right format if self.get("type") == "json": data = json.loads(raw_data) elif self.get("type") == "yaml": data = yaml.load(raw_data) elif self.get("type") == "csv": data = list(map(dict, comma.load(raw_data, force_header=True))) return data
def test_slicing_parent(): obj = comma.load(SOME_CSV_STRING) # make sure we know what we are getting assert len(obj) == 2 assert obj.header is not None assert len(obj.header) == 3 # get reference to parent object assert obj._parent is not None ref_id = id(obj._parent) # check this is propagated to slices # use hex(id(...)) to get an easier to read number assert (ref_id == id(obj[0]._parent)) assert (ref_id == id(obj[0:10]._parent)) assert (ref_id == id(obj[0:10][0]._parent))
def table(self) -> comma.classes.table.CommaTable: """ Returns (as a pytest fixture) a newly loaded `CommaTable` object from this class' test data file. """ return comma.load(self.FILEPATH)