Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions python/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,32 @@
from tsfile import schema, Field
from tsfile import Tablet
from tsfile.constants import *
from tsfile.schema import *


def test_tablet():
column_names = ["temp1", "temp2", "value1", "value2"]
data_types = [TSDataType.INT32, TSDataType.INT64, TSDataType.FLOAT, TSDataType.DOUBLE]
column_names = ["temp1", "temp2", "value1", "value2", "string1"]
data_types = [TSDataType.INT32, TSDataType.INT64, TSDataType.FLOAT, TSDataType.DOUBLE, TSDataType.STRING]
tablet = Tablet(column_names, data_types)
tablet.set_table_name("test")

assert "test" == tablet.get_target_name()
assert 4 == len(tablet.get_column_name_list())
assert 5 == len(tablet.get_column_name_list())
assert TSDataType.INT32 == tablet.get_data_type_list()[0]

tablet.add_timestamp(0, 10)
tablet.add_value_by_name("temp1", 0, 10)
tablet.add_value_by_name("temp2", 0, 100)
tablet.add_value_by_index(2, 0, 0.1)
tablet.add_value_by_index(3, 0, 0.1)
tablet.add_value_by_index(4, 0, "test")
tablet.add_value_by_name("string1", 0, "test")
# Illegal column name
with pytest.raises(ValueError):
tablet.add_value_by_name("temp3", 0, 10)
# Illegal exists column index
with pytest.raises(IndexError):
tablet.add_value_by_index(4, 0, 10)
tablet.add_value_by_index(5, 0, 10)
# Illegal row index
with pytest.raises(IndexError):
tablet.add_value_by_name("temp1", 2048, 10)
Expand Down Expand Up @@ -87,6 +90,29 @@ def test_field():
with pytest.raises(OverflowError):
field_int64.get_int_value()

def test_schema():
column1 = ColumnSchema("device", TSDataType.STRING, ColumnCategory.TAG)
column2 = ColumnSchema("sensor", TSDataType.STRING, ColumnCategory.TAG)
# Default by FIELD.
column3 = ColumnSchema("value1", TSDataType.DOUBLE)
column4 = ColumnSchema("value2", TSDataType.INT32, ColumnCategory.FIELD)
table = TableSchema("test_table", [column1, column2, column3, column4])

assert column3.get_category() == ColumnCategory.FIELD
assert column4.__str__() == "ColumnSchema(value2, INT32, FIELD)"

with pytest.raises(ValueError):
tablet = TableSchema("", [column1, column2, column3, column4])

with pytest.raises(ValueError):
tablet = TableSchema("test_table", [])

with pytest.raises(ValueError):
column = ColumnSchema("test_column",None, ColumnCategory.TAG)

with pytest.raises(ValueError):
tablet = TableSchema("test_table", [ColumnSchema("", TSDataType.DOUBLE)])




Expand Down
8 changes: 8 additions & 0 deletions python/tsfile/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ class ColumnSchema:
data_type = None

def __init__(self, column_name: str, data_type: TSDataType, category: ColumnCategory = ColumnCategory.FIELD):
if column_name is None or len(column_name) == 0:
raise ValueError("Column name cannot be None")
self.column_name = column_name.lower()
if data_type is None:
raise ValueError("Data type cannot be None")
self.data_type = data_type
self.category = category

Expand All @@ -97,7 +101,11 @@ class TableSchema:
columns = None

def __init__(self, table_name: str, columns: List[ColumnSchema]):
if table_name is None or len(table_name) == 0:
raise ValueError("Table name cannot be None")
self.table_name = table_name.lower()
if len(columns) == 0:
raise ValueError("Columns cannot be empty")
self.columns = columns

def get_table_name(self):
Expand Down
3 changes: 2 additions & 1 deletion python/tsfile/tablet.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def add_value_by_index(self, col_index: int, row_index: int, value: Union[int, f
if not isinstance(value, expected_type.to_py_type()):
raise TypeError(f"Expected {expected_type.to_py_type()} got {type(value)}")

self._check_numeric_range(value, expected_type)
if expected_type in (TSDataType.INT32, TSDataType.INT64, TSDataType.FLOAT, TSDataType.DOUBLE):
self._check_numeric_range(value, expected_type)

self.data_list[col_index][row_index] = value

Expand Down