Skip to content

support boolean as PK (make sense if it's part of composite key) #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions sqeleton/abcs/database_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ class PrecisionType(ColType):
rounds: Union[bool, Unknown] = Unknown


class Boolean(ColType):
precision = 0


class TemporalType(PrecisionType):
pass

Expand Down Expand Up @@ -82,6 +78,10 @@ def python_type(self) -> type:
return decimal.Decimal


class Boolean(ColType, IKey):
precision = 0
python_type = bool

@dataclass
class StringType(ColType):
python_type = str
Expand Down
28 changes: 26 additions & 2 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sqeleton.queries import table, current_timestamp, NormalizeAsString, ForeignKey, Compiler
from .common import TEST_MYSQL_CONN_STRING
from .common import str_to_checksum, make_test_each_database_in_list, get_conn, random_table_suffix
from sqeleton.abcs.database_types import TimestampTZ
from sqeleton.abcs.database_types import TimestampTZ, Boolean, IKey

TEST_DATABASES = {
dbs.MySQL,
Expand Down Expand Up @@ -95,7 +95,31 @@ def test_type_mapping(self):
db.query(tbl.drop())
assert not db.query(q)


@test_each_database
class TestColTypes(unittest.TestCase):
"""Test column type implementations, especially IKey interface"""

def test_boolean_as_ikey(self):
"""Test that Boolean type implements IKey interface correctly"""
boolean_type = Boolean()

# Test that Boolean is an instance of IKey
self.assertIsInstance(boolean_type, IKey)

# Test that python_type property returns bool
self.assertEqual(boolean_type.python_type, bool)

# Test precision is 0
self.assertEqual(boolean_type.precision, 0)

# Test make_value method converts values to bool
self.assertEqual(boolean_type.make_value(True), True)
self.assertEqual(boolean_type.make_value(False), False)
self.assertEqual(boolean_type.make_value(1), True)
self.assertEqual(boolean_type.make_value(0), False)
self.assertEqual(boolean_type.make_value("True"), True)
self.assertEqual(boolean_type.make_value(""), False)

@test_each_database
class TestQueries(unittest.TestCase):
def test_current_timestamp(self):
Expand Down