Skip to content

Commit

Permalink
re-enable ColumnProxyContainer with some simple tests
Browse files Browse the repository at this point in the history
TODO: add some more dict-like functionality to replace the original dict
  • Loading branch information
icyveins7 committed Mar 15, 2024
1 parent eea7266 commit 5519c19
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion sew/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import re

from .formatSpec import FormatSpecifier
from .column import ColumnProxy
from .column import ColumnProxy, ColumnProxyContainer

#%% Basic container, the most barebones
class SqliteContainer:
Expand Down
39 changes: 24 additions & 15 deletions sew/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,37 @@ def __repr__(self):
def __str__(self):
return "%s %s" % (self._name, self._typehint)

def _requireType(self, x):
if not isinstance(x, self.typehint):
raise TypeError("Compared value must be of type %s" % str(self.typehint))
def _requireColumnProxy(self, x):
if not isinstance(x, self.__class__):
raise TypeError("Comparisons are only allowed between ColumnProxy objects.")

# def _requireType(self, x):
# if not isinstance(x, self.typehint):
# raise TypeError("Compared value must be of type %s" % str(self.typehint))

def __lt__(self, x):
self._requireType(x)
return "%s < %s" % (self._name, str(x))
self._requireColumnProxy(x)
return "%s < %s" % (self._name, x.name)

def __le__(self, x):
self._requireType(x)
return "%s <= %s" % (self._name, str(x))
self._requireColumnProxy(x)
return "%s <= %s" % (self._name, x.name)

def __gt__(self, x):
self._requireType(x)
return "%s > %s" % (self._name, str(x))
self._requireColumnProxy(x)
return "%s > %s" % (self._name, x.name)

def __ge__(self, x):
self._requireType(x)
return "%s >= %s" % (self._name, str(x))
self._requireColumnProxy(x)
return "%s >= %s" % (self._name, x.name)

def __eq__(self, x):
self._requireType(x)
return "%s = %s" % (self._name, str(x))
self._requireColumnProxy(x)
return "%s = %s" % (self._name, x.name)

def __ne__(self, x):
self._requireType(x)
return "%s != %s" % (self._name, str(x))
self._requireColumnProxy(x)
return "%s != %s" % (self._name, x.name)

#%%
class ColumnProxyContainer:
Expand All @@ -71,7 +75,12 @@ class ColumnProxyContainer:
mytable.cols.mycolname
"""
def __init__(self, columnProxies: list[ColumnProxy]):
lastTablename = None
for col in columnProxies:
if lastTablename is not None and lastTablename != col.tablename:
raise ValueError("ColumnProxyContainer can only contain columns from the same table.")
lastTablename = col.tablename
# Here is where we set the attribute directly
setattr(self, col.name, col)


Expand Down
22 changes: 21 additions & 1 deletion tests/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,24 @@ def test_columnProxy_properties(self):
self.assertEqual(proxy.name, "col1")
self.assertEqual(proxy.typehint, "int")
self.assertEqual(proxy.tablename, "mytbl")

self.assertEqual(str(proxy), "col1 int")


def test_columnProxyContainer(self):
# Just add some and reference
proxy = sew.ColumnProxy("col1", "int", "mytbl")
proxy2 = sew.ColumnProxy("col2", "int", "mytbl")
proxy3 = sew.ColumnProxy("col3", "int", "mytbl")
proxylist = [proxy, proxy2, proxy3]
container = sew.ColumnProxyContainer(proxylist)

self.assertEqual(container.col1, proxy)
self.assertEqual(container.col2, proxy2)
self.assertEqual(container.col3, proxy3)

# Check that they should all belong to same table
proxylist.append(
sew.ColumnProxy("col4", "int", "mytbl2")
)
with self.assertRaises(ValueError):
sew.ColumnProxyContainer(proxylist)

0 comments on commit 5519c19

Please sign in to comment.