def test_row_str_and_repr(): r = Row({'a': 1, 'b': 2}) expected = ' a b\n--- ---\n 1 2' assert str(r) in expected assert repr(r) in expected # parse html representation with pandas html = pd.read_html(r._repr_html_())[0] assert html.to_dict() == {'a': {0: 1}, 'b': {0: 2}}
def test_table_auto_size(monkeypatch): TerminalSize = namedtuple('TerminalSize', ['columns']) monkeypatch.setattr(shutil, 'get_terminal_size', lambda: TerminalSize(80)) r = Row({'a': '1' * 60, 'b': '1' * 60}) table = Table([r, r], column_width='auto') assert max([len(line) for line in str(table).splitlines()]) == 80 # simulate resize monkeypatch.setattr(shutil, 'get_terminal_size', lambda: TerminalSize(120)) assert max([len(line) for line in str(table).splitlines()]) == 120
def test_table_str_and_repr(monkeypatch): mock = Mock() mock.get_terminal_size().columns = 6 monkeypatch.setattr(table, 'shutil', mock) r = Row({'a': 1, 'b': 2}) t = Table([r, r]) expected = ' a b\n--- ---\n 1 2\n 1 2' assert str(t) == expected assert repr(t) == expected # parse html representation with pandas html = pd.read_html(t._repr_html_())[0] assert html.to_dict(orient='list') == {'a': [1, 1], 'b': [2, 2]}
def status(self, return_code_diff=False, sections=None): """Prints the current task status Parameters ---------- sections : list, optional Sections to include. Defaults to "name", "last_run", "oudated", "product", "doc", "location" """ sections = sections or [ 'name', 'last_run', 'outdated', 'product', 'doc', 'location' ] p = self.product data = {} if 'name' in sections: data['name'] = self.name if 'type' in sections: data['type'] = type(self).__name__ if 'status' in sections: data['status'] = self.exec_status.name if 'client' in sections: # FIXME: all tasks should have a client property data['client'] = (repr(self.client) if hasattr(self, 'client') else None) if 'last_run' in sections: if p.metadata.timestamp is not None: dt = datetime.fromtimestamp(p.metadata.timestamp) date_h = dt.strftime('%b %d, %y at %H:%M') time_h = humanize.naturaltime(dt) data['Last run'] = '{} ({})'.format(time_h, date_h) else: data['Last run'] = 'Has not been run' outd_data = p._outdated_data_dependencies() outd_code = p._outdated_code_dependency() outd = False if outd_code: outd = 'Source code' if outd_data: if not outd: outd = 'Upstream' else: outd += ' & Upstream' if 'outdated' in sections: data['Outdated?'] = outd if 'outdated_dependencies' in sections: data['Outdated dependencies'] = outd_data if 'outdated_code' in sections: data['Outdated code'] = outd_code if outd_code and return_code_diff: data['Code diff'] = (self.dag.differ.get_diff( p.metadata.stored_source_code, str(self.source), extension=self.source.extension)) else: outd_code = '' if 'product_type' in sections: data['Product type'] = type(self.product).__name__ if 'product' in sections: data['Product'] = repr(self.product) if 'product_client' in sections: # FIXME: all products should have a client property data['Product client'] = (repr(self.product.client) if hasattr( self.product, 'client') else None) if 'doc' in sections: data['Doc (short)'] = _doc_short(self.source.doc) if 'location' in sections: data['Location'] = self.source.loc return Row(data)
def test_convert_to_dict(): d = {'a': 1, 'b': 2} r = Row(d) t = Table([r, r], column_width=None) assert t.to_dict() == {'a': [1, 1], 'b': [2, 2]}
def test_convert_to_pandas(): d = {'a': 1, 'b': 2} r = Row(d) t = Table([r, r], column_width=None) expected = pd.DataFrame({'a': [1, 1], 'b': [2, 2]}) assert expected.equals(t.to_pandas())
def test_create_build_report(): row = Row({'Elapsed (s)': 1}) report = BuildReport([row, row]) assert report == {'Elapsed (s)': [1, 1], 'Percentage': [50, 50]}
def test_table_values(): d = {'a': 1, 'b': 2} r = Row(d) t = Table([r, r], column_width=None) assert t.values == {'a': [1, 1], 'b': [2, 2]}
def test_select_multiple_cols_in_table(): d = {'a': 1, 'b': 2} r = Row(d) t = Table([r, r], column_width=None) assert t[['a', 'b']] == {'a': [1, 1], 'b': [2, 2]}
def test_select_col_in_table(): r = Row({'a': 1, 'b': 2}) t = Table([r, r], column_width=None) assert t['a'] == [1, 1]
def test_error_if_row_initialized_with_non_mapping(): with pytest.raises(TypeError): Row([])
def test_select_multiple_cols_in_row(): r = Row({'a': 1, 'b': 2}) assert r[['a', 'b']] == {'a': 1, 'b': 2}
def test_table_wrap(): r = Row({'a': 'abc d', 'b': 'abc d'}) table = Table([r, r], column_width=3) # Max expected length: 3 (col a) + 2 (whitespace) + 3 (col b) = 8 assert max([len(line) for line in str(table).splitlines()]) == 8
def test_table_iter(): r = Row({'a': 1, 'b': 2}) t = Table([r, r]) assert set(iter(t)) == {'a', 'b'}
def test_row_str_setitem(): r = Row({'a': 1, 'b': 2}) r['a'] = 10 assert r['a'] == 10
def test_rows2columns(): r1 = Row({'a': 1}) r2 = Row({'a': 2}) assert rows2columns([r1, r2]) == {'a': [1, 2]}