Now test-passing!
This commit is contained in:
@@ -3,6 +3,7 @@ from pickle import HIGHEST_PROTOCOL, dumps, loads
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import aiosqlite
|
||||
from aiosqlite import IntegrityError
|
||||
|
||||
from .constraints import Unique
|
||||
|
||||
@@ -158,7 +159,14 @@ def _tweaked_dump_value(self, value):
|
||||
|
||||
def _tweaked_dump(self, name):
|
||||
value = getattr(self, name)
|
||||
return _tweaked_dump_value(self, value)
|
||||
field_types = {key: value.type for key, value in self.__dataclass_fields__.items()}
|
||||
if (
|
||||
"NOT NULL UNIQUE" not in self.types_table.get(field_types[name], "")
|
||||
or value is not None
|
||||
):
|
||||
return _tweaked_dump_value(self, value)
|
||||
else:
|
||||
raise IntegrityError
|
||||
|
||||
|
||||
def _tweaked_load_value(data):
|
||||
|
||||
@@ -3,10 +3,10 @@ Defines the Datalite decorator that can be used to convert a dataclass to
|
||||
a class bound to an sqlite3 database.
|
||||
"""
|
||||
from dataclasses import asdict, fields
|
||||
from sqlite3.dbapi2 import IntegrityError
|
||||
from typing import Callable, Dict, Optional
|
||||
|
||||
import aiosqlite
|
||||
from aiosqlite import IntegrityError
|
||||
|
||||
from .commons import _create_table, _tweaked_create_table, _tweaked_dump, type_table
|
||||
from .constraints import ConstraintFailedError
|
||||
@@ -36,6 +36,9 @@ async def _create_entry(self) -> None:
|
||||
await con.commit()
|
||||
except IntegrityError:
|
||||
raise ConstraintFailedError("A constraint has failed.")
|
||||
finally:
|
||||
await cur.close()
|
||||
await con.close()
|
||||
|
||||
|
||||
async def _tweaked_create_entry(self) -> None:
|
||||
@@ -55,6 +58,9 @@ async def _tweaked_create_entry(self) -> None:
|
||||
await con.commit()
|
||||
except IntegrityError:
|
||||
raise ConstraintFailedError("A constraint has failed.")
|
||||
finally:
|
||||
await cur.close()
|
||||
await con.close()
|
||||
|
||||
|
||||
async def _update_entry(self) -> None:
|
||||
|
||||
Reference in New Issue
Block a user