Skip to content

Commit

Permalink
Update test to use pytest (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed Aug 9, 2023
1 parent af0138d commit 0f6b3e3
Show file tree
Hide file tree
Showing 8 changed files with 732 additions and 828 deletions.
12 changes: 6 additions & 6 deletions holonote/annotate/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class PrimaryKey(param.Parameterized):

connector_class = param.String(default='SQLiteDB')

def __call__(self, connector, key_list=[]):
def __call__(self, connector, key_list=None):
"""
The key list is the current list of index values that are
outstanding (i.e. have not been comitted).
Expand Down Expand Up @@ -74,7 +74,7 @@ class AutoIncrementKey(PrimaryKey):
schema = param.String(default='INTEGER PRIMARY KEY AUTOINCREMENT',
constant=True, allow_None=False)

def __call__(self, connector, key_list=[]):
def __call__(self, connector, key_list=None):
key_list_max = max(key_list) if key_list else 0
connector_max = connector.max_rowid()
connector_max = 0 if connector_max is None else connector_max
Expand All @@ -101,9 +101,7 @@ class UUIDHexStringKey(PrimaryKey): # Probably the better default

length = param.Integer(default=32, bounds=(4,32))



def __call__(self, connector, key_list=[]):
def __call__(self, connector, key_list=None):
return uuid.uuid4().hex[:self.length]

def cast(self, value):
Expand All @@ -124,9 +122,11 @@ class UUIDBinaryKey(PrimaryKey):

schema = param.String('BINARY PRIMARY KEY', constant=True, allow_None=False)

def __call__(self, connector, key_list=[]):
def __call__(self, connector, key_list=None):
return uuid.uuid4().bytes

def cast(self, value):
return bytes(value)

class WidgetKey(PrimaryKey):
"""
Expand Down
Binary file removed holonote/tests/annotations.db
Binary file not shown.
97 changes: 97 additions & 0 deletions holonote/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from __future__ import annotations

from typing import Iterator

import numpy as np
import pytest

from holonote.annotate import Annotator, SQLiteDB, UUIDHexStringKey


@pytest.fixture()
def conn_sqlite_uuid(tmp_path) -> Iterator[SQLiteDB]:
conn = SQLiteDB(filename=str(tmp_path / "test.db"), primary_key=UUIDHexStringKey())
yield conn
try:
conn.cursor.close()
except Exception:
pass
try:
conn.con.close()
except Exception:
pass


@pytest.fixture()
def annotator_range1d(conn_sqlite_uuid) -> Annotator:
anno = Annotator(
{"TIME": np.datetime64},
fields=["description"],
region_types=["Range"],
connector=conn_sqlite_uuid,
)
return anno


@pytest.fixture()
def annotator_point1d(conn_sqlite_uuid) -> Annotator:
anno = Annotator(
{"TIME": np.datetime64},
fields=["description"],
region_types=["Point"],
connector=conn_sqlite_uuid,
)
return anno


@pytest.fixture()
def annotator_range2d(conn_sqlite_uuid) -> Annotator:
anno = Annotator(
{"x": float, "y": float},
fields=["description"],
region_types=["Range"],
connector=conn_sqlite_uuid,
)
return anno


@pytest.fixture()
def annotator_point2d(conn_sqlite_uuid) -> Annotator:
anno = Annotator(
{"x": float, "y": float},
fields=["description"],
region_types=["Point"],
connector=conn_sqlite_uuid,
)
return anno


@pytest.fixture()
def multiple_region_annotator(annotator_range1d) -> Annotator:
annotator_range1d.region_types = ["Point", "Range"]
return annotator_range1d


@pytest.fixture()
def multiple_annotators(
conn_sqlite_uuid, annotator_range1d, annotator_range2d
) -> dict[str, Annotator | SQLiteDB]:
annotator_range1d.connector = conn_sqlite_uuid
annotator_range2d.connector = conn_sqlite_uuid
output = {
"annotation1d": annotator_range1d,
"annotation2d": annotator_range2d,
"conn": conn_sqlite_uuid,
}
return output


@pytest.fixture()
def multiple_fields_annotator(conn_sqlite_uuid) -> Annotator:
conn_sqlite_uuid.fields = ["field1", "field2"]
anno = Annotator(
{"TIME": np.datetime64},
fields=["field1", "field2"],
connector=conn_sqlite_uuid,
)
return anno
42 changes: 15 additions & 27 deletions holonote/tests/test_annotation_table.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
import unittest
import numpy as np
import pandas as pd

from holonote.annotate import AnnotationTable


def test_table_region_df():
table = AnnotationTable()
table.load(primary_key_name='id', fields=['test_description'])
assert len(table._region_df) == 0, 'Should be initialized empty'
assert tuple(table._region_df.columns) == AnnotationTable.columns

class TestBasicTableLoad(unittest.TestCase):
start = pd.Timestamp('2022-06-17 18:32:48.623476')
end = pd.Timestamp('2022-06-19 04:44:09.306402')
regions = [ {'region_type': 'Range',
'value': (start, end),
'dim1': 'TIME',
'dim2': None} ]
table.add_annotation(regions, id=100, test_description='A test')

def setUp(self):
self.table = AnnotationTable()

# Load some metadata and region data
expected = pd.DataFrame([{'region_type':'Range', 'dim1':'TIME', 'dim2': None,
'value':(start, end), '_id':100}]).astype({'_id':object})

def test_table_region_df(self):
self.table.load(primary_key_name='id', fields=['test_description'])
assert len(self.table._region_df) == 0, 'Should be initialized empty'
assert tuple(self.table._region_df.columns) == AnnotationTable.columns

start = pd.Timestamp('2022-06-17 18:32:48.623476')
end = pd.Timestamp('2022-06-19 04:44:09.306402')
regions = [ {'region_type': 'Range',
'value': (start, end),
'dim1': 'TIME',
'dim2': None} ]
self.table.add_annotation(regions, id=100, test_description='A test')


expected = pd.DataFrame([{'region_type':'Range', 'dim1':'TIME', 'dim2': None,
'value':(start, end), '_id':100}]).astype({'_id':object})

pd.testing.assert_frame_equal(self.table._region_df, expected)


# Test other metadata field
pd.testing.assert_frame_equal(table._region_df, expected)
Loading

0 comments on commit 0f6b3e3

Please sign in to comment.