def test_write_schema(empty_testsuite, tmp_path): f = tsdb.Field r = OrderedDict([ ('item', [f('i-id', ':integer', (':key',)), f('i-input', ':string')]), ('fold', [f('fold-id', ':integer', (':key',))]), ('run', [f('run-id', ':integer', (':key',), 'unique test run identifier')]), ('parse', [f('parse-id', ':integer', (':key',), 'unique parse identifier'), f('run-id', ':integer', (':key',), 'test run for this parse'), f('i-id', ':integer', (':key',), 'item parsed')]), ('result', [f('parse-id', ':integer', (':key',), 'parse for this result'), f('result-id', ':integer', (), 'result identifier'), f('mrs', ':string', (), 'MRS for this reading')]) ]) tmp_dir = tmp_path.joinpath('test_write_schema') tmp_dir.mkdir() tsdb.write_schema(str(tmp_dir), r) orig = pathlib.Path(empty_testsuite).joinpath('relations') new = tmp_dir.joinpath('relations') assert orig.read_text() == new.read_text()
def _mkprof_from_database(destination, db, schema, where, full, gzip): if schema is None: schema = db.schema destination.mkdir(exist_ok=True) tsdb.write_schema(destination, schema) to_copy = set(schema if full else tsdb.TSDB_CORE_FILES) where = '' if where is None else 'where ' + where for table in schema: if table not in to_copy or _no_such_relation(db, table): records = [] elif where: # filter the data, but use all if the query fails # (e.g., if the filter and table cannot be joined) try: records = _tsql_distinct( tsql.select(f'* from {table} {where}', db)) except tsql.TSQLError: records = list(db[table]) else: records = list(db[table]) tsdb.write(destination, table, records, schema[table], gzip=gzip)
def test_bad_date_issue_279(tmp_path, empty_alt_testsuite): tmp_ts = tmp_path.joinpath('test_bad_date_issue_279') tmp_ts.mkdir() schema = tsdb.read_schema(empty_alt_testsuite) fields = schema['item'] tsdb.write_schema(tmp_ts, schema) tsdb.write(tmp_ts, 'item', [(0, 'The cat meows.', datetime(1999, 9, 8))], fields) db = tsdb.Database(tmp_ts) assert list(db['item']) == [('0', 'The cat meows.', '8-sep-1999')] tsdb.write(tmp_ts, 'item', [(0, 'The cat meows.', 'September 8, 1999')], fields) assert list(db['item']) == [('0', 'The cat meows.', 'September 8, 1999')]
def test_bad_date_issue_279b(tmp_path, empty_alt_testsuite): tmp_ts = tmp_path.joinpath('test_bad_date_issue_279b') tmp_ts.mkdir() schema = tsdb.read_schema(empty_alt_testsuite) fields = schema['item'] tsdb.write_schema(tmp_ts, schema) tsdb.write(tmp_ts, 'item', [(0, 'The cat meows.', 'September 8, 1999')], fields) ts = itsdb.TestSuite(tmp_ts) assert list(ts['item'].select('i-date', cast=False)) == [('September 8, 1999', )] with pytest.warns(tsdb.TSDBWarning): ts['item'][0]['i-date']
def __init__(self, path: util.PathLike = None, schema: tsdb.SchemaLike = None, encoding: str = 'utf-8') -> None: # Virtual test suites use a temporary directory if path is None: self._tempdir = tempfile.TemporaryDirectory() path = Path(self._tempdir.name) else: path = Path(path).expanduser() path.mkdir(exist_ok=True) # can fail if path is a file # Ensure test suite directory has a relations file if not path.joinpath(tsdb.SCHEMA_FILENAME).is_file(): if schema is None: raise ITSDBError( '*schema* argument is required for new test suites') elif isinstance(schema, (str, Path)): schema = tsdb.read_schema(schema) tsdb.write_schema(path, schema) super().__init__(path, autocast=False, encoding=encoding) self._data: Dict[str, Table] = {}