Skip to content

Commit

Permalink
Merge pull request #41 from AnswerDotAI/test-insert
Browse files Browse the repository at this point in the history
Test `insert()` and `insert_all()`
  • Loading branch information
jph00 authored Nov 20, 2024
2 parents 23993b1 + e33b43b commit bfdac10
Show file tree
Hide file tree
Showing 3 changed files with 766 additions and 2 deletions.
1 change: 1 addition & 0 deletions fastlite/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@
'fastlite.core.create_mod': ('core.html#create_mod', 'fastlite/core.py'),
'fastlite.core.diagram': ('core.html#diagram', 'fastlite/core.py'),
'fastlite.core.get_typ': ('core.html#get_typ', 'fastlite/core.py')},
'fastlite.db_dc': {},
'fastlite.kw': {}}}
10 changes: 8 additions & 2 deletions fastlite/kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def transform_sql(
drop_foreign_keys=drop_foreign_keys, add_foreign_keys=add_foreign_keys, foreign_keys=foreign_keys,
column_order=column_order, keep_table=keep_table)

def _process_row(row): return {k:(v.value if isinstance(v, Enum) else v) for k,v in asdict(row).items() if v is not UNSET}
def _process_row(row):
if row is None: return {}
return {k:(v.value if isinstance(v, Enum) else v) for k,v in asdict(row).items() if v is not UNSET}

@patch
def update(self:Table, updates: dict|None=None, pk_values: list|tuple|str|int|float|None=None,
Expand Down Expand Up @@ -147,6 +149,10 @@ def insert_all(
**kwargs) -> Table:
if not xtra: xtra = getattr(self,'xtra_id',{})
records = [_process_row(o) for o in records]
records = [x for x in records if x]
if not any(records):
self.result = []
return self
records = [{**o, **xtra} for o in records]
return self._orig_insert_all(
records=records, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null,
Expand All @@ -172,9 +178,9 @@ def insert(
columns: Union[Dict[str, Any], Default, None]=DEFAULT,
strict: opt_bool=DEFAULT,
**kwargs) -> Table:
if not record: record={}
record = _process_row(record)
record = {**record, **kwargs}
if not record: return {}
self._orig_insert(
record=record, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null,
defaults=defaults, hash_id=hash_id, hash_id_columns=hash_id_columns, alter=alter, ignore=ignore,
Expand Down
Loading

0 comments on commit bfdac10

Please sign in to comment.