def test_grid_getitem(): grid = Grid() grid.column['test'] = {} row = {'test': 'This is a test'} grid.append(row) assert grid[0] is row
def test_grid_not_equal_col_with_new_metadata(): ref = Grid() ref.column['test'] = {} ref.extend([{'test': 1}, {'test': 2}, {'test': 3}]) diff: Grid = copy.deepcopy(ref) diff.column.add_item(key='test', value='add') # type: ignore assert ref != diff
def test_grid_not_equal_col_with_updated_metadata(): ref = Grid() ref.column['test'] = {} ref.extend([{'test': 1}, {'test': 2}, {'test': 3}]) diff = copy.deepcopy(ref) diff.column['test']['test'] = None assert ref != diff
def test_grid_equal_with_complex_datas(): ref = Grid() ref.column['test'] = {} ref.extend([ { 'test': datetime.datetime(2010, 11, 28, 7, 23, 2, 600000) }, { 'test': Quantity(500, 'kg') }, { 'test': Coordinate(100, 100) }, { 'test': 1.0 }, ]) similar = Grid() similar.column['test'] = {} similar.extend([ { 'test': datetime.datetime(2010, 11, 28, 7, 23, 2, 500000) }, { 'test': Quantity(500.000001, 'kg') }, { 'test': Coordinate(100.000001, 100.000001) }, { 'test': 1.000001 }, ]) assert ref == similar
def test_pop_key(): grid = Grid(columns=["id", "a"]) row = {"id": Ref("myid"), "a": 1, "b": 2} grid.append(row) old = grid.pop(Ref("myid")) assert not grid assert id(old) == id(row)
def test_pop_invalid_pos(): grid = Grid(columns=["id", "a"]) row = {"id": Ref("myid"), "a": 1, "b": 2} grid.append(row) old = grid.pop(-1) assert grid assert not old
def test_grid_new_row(): ref = Grid() ref.column['test'] = {} ref.extend([{'test': 1}, {'test': 2}, {'test': 3}]) diff = copy.deepcopy(ref) diff.append({'test': 4}) assert ref != diff
def test_pop_multiple_pos(): grid = Grid(columns=["id", "a"]) row = {"id": Ref("id2"), "a": 1, "b": 2} grid.append(row) old = grid.pop(0, 1) assert not grid assert id(old) == id(row)
def test_grid_not_equal_with_updated_value(): ref = Grid() ref.column['test'] = {"a": 1} ref.extend([{'test': 1}, {'test': 2}, {'test': 3}]) diff = copy.deepcopy(ref) diff.column['test'] = {"a": ""} assert ref != diff
def test_grid_not_equal__with_new_col(): ref = Grid() ref.column['test'] = {} ref.extend([{'test': 1}, {'test': 2}, {'test': 3}]) diff = copy.deepcopy(ref) diff.column["added"] = {} assert ref != diff
def test_grid_not_equal_row(): ref = Grid() ref.column['test'] = {} ref.extend([{'test': 1}, {'test': 2}, {'test': 3}]) diff = copy.deepcopy(ref) diff[0] = {'test': 4} assert ref != diff
def test_grid_equal_with_none(): ref = Grid() ref.column['test', 'none'] = {} ref.extend([{'test': 1}, {'test': 2}, {'test': 3}]) other = copy.deepcopy(ref) other[0]['none'] = None assert ref == other
def test_grid_equal_with_other_order(): left = Grid() left.column['test'] = {} left.extend([{'test': 1}, {'test': 2}, {'test': 3}]) right = Grid() right.column['test'] = {} right.extend([{'test': 2}, {'test': 1}, {'test': 3}]) assert left == copy.deepcopy(right)
def test_grid_not_equal_col_with_change_col_name(): ref = Grid() ref.column['test'] = {} ref.extend([{'test': 1}, {'test': 2}, {'test': 3}]) diff: Grid = copy.deepcopy(ref) diff.column.pop('test') diff.column.add_item(key='new', value='add') # type: ignore assert ref != diff
def test_grid_setitem(): grid = Grid() grid.column['test'] = {} row_1 = {'test': 'This is a test'} row_2 = {'test': 'This is another test'} grid.append(row_1) grid[0] = row_2 assert grid[0] is row_2
def test_grid_copy(): grid = Grid(columns=['test']) rows = [{'test': 1}, {'test': 2}, {'test': 3}] grid.extend(rows) grid_2 = grid.copy() assert len(grid_2) == 3 del grid[1] assert len(grid) == 2 assert len(grid_2) == 3
def test_pop_multiple_keys(): grid = Grid(columns=["id", "a"]) row1 = {"id": Ref("id1"), "a": 1, "b": 2} row2 = {"id": Ref("id2"), "a": 1, "b": 2} grid.append(row1) grid.append(row2) old = grid.pop(Ref("id1"), Ref("id2")) assert not grid assert id(old) == id(row1)
def test_grid_del(): grid = Grid(columns=['id', 'test']) rows = [{'test': 1}, {'test': 2}, {'test': 3}, {'id': Ref('myid')}] grid.extend(rows) assert len(grid) == 4 del grid[1] del grid[Ref('myid')] assert len(grid) == 2 assert grid[0] is rows[0] assert grid[1] is rows[2]
def test_grid_not_equal_metadata(): ref = Grid(metadata={"x": {}}) ref.column['test'] = {} ref.extend([{'test': 1}, {'test': 2}, {'test': 3}]) diff = copy.deepcopy(ref) diff.metadata.append('add') assert ref != diff diff = copy.deepcopy(ref) diff.metadata["x"] = 1 assert ref != diff
def _get_mock_s3(): sample_grid = Grid(version=VER_3_0, columns=["id", "col", "hisURI"]) sample_grid.append({"id": Ref("id1"), "col": 1, "hisURI": "hist.zinc"}) sample_grid.append({"id": Ref("id2"), "col": 2, "hisURI": "hist.zinc"}) version_1 = datetime(2020, 10, 1, 0, 0, 1, 0, tzinfo=pytz.UTC) version_2 = datetime(2020, 10, 1, 0, 0, 2, 0, tzinfo=pytz.UTC) version_3 = datetime(2020, 10, 1, 0, 0, 3, 0, tzinfo=pytz.UTC) # noinspection PyUnusedLocal class MockS3: __slots__ = "history", "his_count" def __init__(self): self.history = None self.his_count = 0 # noinspection PyMethodMayBeStatic def list_object_versions(self, **args): # pylint: disable=R0201, W0613 return { "Versions": [ { "VersionId": "1", "LastModified": version_1 }, { "VersionId": "2", "LastModified": version_2 }, { "VersionId": "3", "LastModified": version_3 }, ] } def download_fileobj(self, bucket, path, stream, **params): # pylint: disable=R0201, W0613 if path == "grid.zinc": grid = sample_grid.copy() if params.get("ExtraArgs", None): grid.metadata = {"v": params["ExtraArgs"]["VersionId"]} else: grid.metadata = {"v": "last"} for row in grid: row["hisURI"] = f"his{self.his_count}.zinc" self.his_count += 1 return stream.write(dump(grid, mode=MODE_ZINC).encode("utf-8")) return stream.write( dump(self.history, mode=MODE_ZINC).encode("utf-8")) return MockS3()
def test_grid_str(): grid = Grid(version=VER_3_0) rows = [{'test': 1}, {'test': 2}, {'test': 3}] grid.column['test'] = {} grid.extend(rows) assert repr(grid) == 'Grid\n' + \ '\tVersion: 3.0\n' + \ '\tColumns:\n' + \ '\t\ttest\n' + \ '\t---- Row 0:\n' + \ '\ttest=1\n' + \ '\t---- Row 1:\n' + \ '\ttest=2\n' + \ '\t---- Row 2:\n' + \ '\ttest=3\n'
def test_grid_setitem_notdict(): grid = Grid() grid.column['test'] = {} row = {'test': 'This is a test'} grid.append(row) try: # noinspection PyTypeChecker grid[0] = 'This is not a dict' # type: ignore assert False, 'Accepted a string' except TypeError: pass assert len(grid) == 1 assert grid[0]['test'] == 'This is a test'
def test_grid_given_column_list(): col_list = [('col1', [('c1m1', None), ('c1m2', None)]), ('col2', [('c2m1', None), ('c2m2', None)])] grid = Grid(columns=col_list) assert list(grid.column.keys()) == ['col1', 'col2'] for col, meta in col_list: assert list(grid.column[col].items()) == meta
def test_grid_given_column_dict(): cols = SortableDict([('col1', [('c1m1', None), ('c1m2', None)]), ('col2', [('c2m1', None), ('c2m2', None)])]) grid = Grid(columns=cols) assert list(grid.column.keys()) == ['col1', 'col2'] for col, meta in cols.items(): assert list(grid.column[col].items()) == meta
def test_grid_add(): left = Grid(columns=["id", "a", "b"]) left.append({"id": Ref("my_id"), "a": 1, "b": 2}) right = Grid(columns={"id": {}, "a": {}, "b": {}}) right.append({"id": Ref("my_id"), "a": 3, "c": 4}) add_grid = left + right assert isinstance(add_grid, Grid) assert len(add_grid) == 1
def test_select_grid(): grid = Grid(columns=["id", "a", "b"]) grid.append({"id": Ref("myid1"), "a": 1, "b": 2}) grid.append({"id": Ref("myid2"), "a": 1, "b": 2}) assert grid.select("id,a").column == {"id": {}, 'a': {}} assert grid.select("id,b").column == {"id": {}, 'b': {}} assert grid.select("!a").column == {"id": {}, 'b': {}} assert grid.select("!id,!b").column == {'a': {}}
def test_grid_sub(): left = Grid(columns=["id", "a", "b"]) left.append({"id": Ref("my_id"), "a": 1}) right = Grid(columns={"id": {}, "a": {}, "b": {}}) right.append({"id": Ref("my_id"), "a": 3, "b": 4}) diff = right - left assert isinstance(diff, Grid) assert len(diff) == 1 diff = left - right assert isinstance(diff, Grid) assert len(diff) == 1
def test_grid_given_metadata(): # Test that when passing in metadata, it is set as given. meta = SortableDict() meta['first'] = 1 meta['second'] = 2 meta['third'] = 3 meta['fourth'] = 4 assert list(Grid(metadata=meta).metadata.items()) == [('first', 1), ('second', 2), ('third', 3), ('fourth', 4)]
def test_grid_keys(): grid = Grid(columns=['id', 'site']) grid.append({ 'id': Ref('id1'), }) grid.append({'id': Ref('id2')}) grid.append({'id': Ref('id3')}) assert sorted(list(grid.keys())) == sorted( [Ref('id1'), Ref('id2'), Ref('id3')])
def test_grid_append_nover_list(): grid = Grid(version=None) assert grid.version == Version('3.0') grid.column['test'] = {} row_1 = {'test': 'This is a test'} row_2 = {'test': ['This should fail']} grid.append(row_1) assert grid.version == Version('3.0') grid.append(row_2) assert grid.version == Version('3.0')