Skip to content

Commit

Permalink
insert-bulk with testing
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispenGari committed Feb 7, 2024
1 parent a85d98b commit 4b6f4d1
Show file tree
Hide file tree
Showing 18 changed files with 900 additions and 345 deletions.
33 changes: 24 additions & 9 deletions dataloom/loom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,24 @@ def insert_one(self, instance: Model, values: ColumnValue | list[ColumnValue]):
)
return row[0] if type(row) in [list, tuple] else row

def insert_bulk(
self, instance: Model, values: list[list[ColumnValue] | ColumnValue]
):
def insert_bulk(self, instance: Model, values: list[list[ColumnValue]]):
# ? ensure that the values that are passed is a list of a list and they inner list have the same length
if not isinstance(values, list):
raise InvalidColumnValuesException(
"The insert_bulk method takes in values as lists of lists."
)
all_list = [isinstance(v, list) for v in values]
if not all(all_list):
raise InvalidColumnValuesException(
"The insert_bulk method takes in values as lists of lists."
)
lengths = [len(v) for v in values]
_max = max(lengths)
if not all([_max == v for v in lengths]):
raise InvalidColumnValuesException(
"The insert_bulk method takes in values as lists of lists with equal ColumnValues."
)

columns = None
placeholders = None
data = []
Expand All @@ -296,13 +311,13 @@ def insert_bulk(
if placeholders is None:
placeholders = placeholder_values
data.append(_values)
print(data)

# sql = instance._get_insert_bulk_smt(
# dialect=self.dialect, column_names=columns, placeholder_values=placeholders
# )
# row_count = self._execute_sql(sql, args=tuple(data), fetchall=True, bulk=True)
# return row_count
sql = instance._get_insert_bulk_smt(
dialect=self.dialect, column_names=columns, placeholder_values=placeholders
)

row_count = self._execute_sql(sql, args=tuple(data), fetchall=True, bulk=True)
return row_count

def find_many(
self,
Expand Down
23 changes: 9 additions & 14 deletions dataloom/tests/mysql/test_create_tables_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class TestCreatingTableMysql:
def test_2_pk_error(self):
from dataloom import Column, PrimaryKeyColumn, Dataloom, TableColumn, Model
import pytest
from typing import Optional
from dataloom.keys import MySQLConfig

mysql_loom = Dataloom(
Expand All @@ -14,14 +13,14 @@ def test_2_pk_error(self):
conn = mysql_loom.connect()

class User(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="users")
__tablename__: TableColumn = TableColumn(name="users")
_id = PrimaryKeyColumn(type="int", auto_increment=True)
id = PrimaryKeyColumn(type="int", auto_increment=True)
username = Column(type="text", nullable=False, default="Hello there!!")
name = Column(type="varchar", unique=True, length=255)

with pytest.raises(Exception) as exc_info:
tables = mysql_loom.sync([User], drop=True, force=True)
_ = mysql_loom.sync([User], drop=True, force=True)
assert (
str(exc_info.value)
== "You have defined many field as primary keys which is not allowed. Fields (`_id`, `id`) are primary keys."
Expand All @@ -31,7 +30,6 @@ class User(Model):
def test_no_pk_error(self):
import pytest
from dataloom import Model, Dataloom, Column, TableColumn
from typing import Optional
from dataloom.keys import MySQLConfig

mysql_loom = Dataloom(
Expand All @@ -43,18 +41,17 @@ def test_no_pk_error(self):
conn = mysql_loom.connect()

class User(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="users")
__tablename__: TableColumn = TableColumn(name="users")
username = Column(type="text", nullable=False, default="Hello there!!")
name = Column(type="varchar", unique=True, length=255)

with pytest.raises(Exception) as exc_info:
tables = mysql_loom.sync([User], drop=True, force=True)
_ = mysql_loom.sync([User], drop=True, force=True)
assert str(exc_info.value) == "Your table does not have a primary key column."
conn.close()

def test_table_name(self):
from dataloom import Model, Dataloom, Column, PrimaryKeyColumn, TableColumn
from typing import Optional
from dataloom.keys import MySQLConfig

mysql_loom = Dataloom(
Expand All @@ -71,7 +68,7 @@ class Posts(Model):
title = Column(type="varchar", length=255, nullable=False)

class User(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="users")
__tablename__: TableColumn = TableColumn(name="users")
username = Column(type="text", nullable=False, default="Hello there!!")
name = Column(type="varchar", unique=True, length=255)

Expand All @@ -81,17 +78,16 @@ class User(Model):

def test_connect_sync(self):
from dataloom import Dataloom, Model, TableColumn, Column, PrimaryKeyColumn
from typing import Optional
from dataloom.keys import MySQLConfig

class User(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="users")
__tablename__: TableColumn = TableColumn(name="users")
id = PrimaryKeyColumn(type="int", nullable=False, auto_increment=True)
username = Column(type="text", nullable=False)
name = Column(type="varchar", unique=False, length=255)

class Post(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="posts")
__tablename__: TableColumn = TableColumn(name="posts")

id = PrimaryKeyColumn(type="int", nullable=False, auto_increment=True)
title = Column(type="text", nullable=False)
Expand All @@ -110,7 +106,6 @@ class Post(Model):

def test_syncing_tables(self):
from dataloom import Model, Dataloom, Column, PrimaryKeyColumn, TableColumn
from typing import Optional
from dataloom.keys import MySQLConfig

mysql_loom = Dataloom(
Expand All @@ -122,13 +117,13 @@ def test_syncing_tables(self):
conn = mysql_loom.connect()

class Post(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="posts")
__tablename__: TableColumn = TableColumn(name="posts")
id = PrimaryKeyColumn(type="int", auto_increment=True)
completed = Column(type="boolean", default=False)
title = Column(type="varchar", length=255, nullable=False)

class User(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="users")
__tablename__: TableColumn = TableColumn(name="users")
id = PrimaryKeyColumn(type="int", auto_increment=True)
username = Column(type="text", nullable=False, default="Hello there!!")
name = Column(type="varchar", unique=True, length=255)
Expand Down
84 changes: 58 additions & 26 deletions dataloom/tests/mysql/test_delete_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def test_delete_by_pk_fn(self):
CreatedAtColumn,
UpdatedAtColumn,
ForeignKeyColumn,
ColumnValue,
)
from dataloom.keys import MySQLConfig
from typing import Optional

mysql_loom = Dataloom(
dialect="mysql",
Expand All @@ -21,7 +21,7 @@ def test_delete_by_pk_fn(self):
)

class User(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="users")
__tablename__: TableColumn = TableColumn(name="users")
id = PrimaryKeyColumn(type="int", auto_increment=True)
name = Column(type="text", nullable=False, default="Bob")
username = Column(type="varchar", unique=True, length=255)
Expand All @@ -31,7 +31,7 @@ class User(Model):
updatedAt = UpdatedAtColumn()

class Post(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="posts")
__tablename__: TableColumn = TableColumn(name="posts")
id = PrimaryKeyColumn(
type="int", auto_increment=True, nullable=False, unique=True
)
Expand All @@ -47,8 +47,10 @@ class Post(Model):

conn, _ = mysql_loom.connect_and_sync([Post, User], drop=True, force=True)

user = User(name="Crispen", username="heyy")
userId = mysql_loom.insert_one(user)
userId = mysql_loom.insert_one(
User, ColumnValue(name="username", value="@miller")
)

affected_rows_1 = mysql_loom.delete_by_pk(User, userId)
affected_rows_2 = mysql_loom.delete_by_pk(User, 89)
assert affected_rows_1 == 1
Expand All @@ -68,9 +70,9 @@ def test_delete_one_fn(self):
ForeignKeyColumn,
UnknownColumnException,
Filter,
ColumnValue,
)
from dataloom.keys import MySQLConfig
from typing import Optional

mysql_loom = Dataloom(
dialect="mysql",
Expand All @@ -80,7 +82,7 @@ def test_delete_one_fn(self):
)

class User(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="users")
__tablename__: TableColumn = TableColumn(name="users")
id = PrimaryKeyColumn(type="int", auto_increment=True)
name = Column(type="text", nullable=False, default="Bob")
username = Column(type="varchar", unique=True, length=255)
Expand All @@ -90,7 +92,7 @@ class User(Model):
updatedAt = UpdatedAtColumn()

class Post(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="posts")
__tablename__: TableColumn = TableColumn(name="posts")
id = PrimaryKeyColumn(
type="int", auto_increment=True, nullable=False, unique=True
)
Expand All @@ -106,11 +108,21 @@ class Post(Model):

conn, _ = mysql_loom.connect_and_sync([Post, User], drop=True, force=True)
mysql_loom.insert_bulk(
[
User(name="Crispen", username="heyy"),
User(name="Crispen", username="who"),
User(name="Crispen", username="hi"),
]
User,
values=[
[
ColumnValue(name="name", value="Crispen"),
ColumnValue(name="username", value="hi"),
],
[
ColumnValue(name="name", value="Crispen"),
ColumnValue(name="username", value="heyy"),
],
[
ColumnValue(name="name", value="Crispen"),
ColumnValue(name="username", value="who"),
],
],
)
mysql_loom.delete_one(User, filters=[Filter(column="name", value="Crispen")])
rows_1 = mysql_loom.find_many(
Expand Down Expand Up @@ -165,9 +177,9 @@ def test_delete_bulk_fn(self):
ForeignKeyColumn,
UnknownColumnException,
Filter,
ColumnValue,
)
from dataloom.keys import MySQLConfig
from typing import Optional

mysql_loom = Dataloom(
dialect="mysql",
Expand All @@ -177,7 +189,7 @@ def test_delete_bulk_fn(self):
)

class User(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="users")
__tablename__: TableColumn = TableColumn(name="users")
id = PrimaryKeyColumn(type="int", auto_increment=True)
name = Column(type="text", nullable=False, default="Bob")
username = Column(type="varchar", unique=True, length=255)
Expand All @@ -187,7 +199,7 @@ class User(Model):
updatedAt = UpdatedAtColumn()

class Post(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="posts")
__tablename__: TableColumn = TableColumn(name="posts")
id = PrimaryKeyColumn(
type="int", auto_increment=True, nullable=False, unique=True
)
Expand All @@ -203,11 +215,21 @@ class Post(Model):

conn, _ = mysql_loom.connect_and_sync([Post, User], drop=True, force=True)
mysql_loom.insert_bulk(
[
User(name="Crispen", username="hi"),
User(name="Crispen", username="heyy"),
User(name="Crispen", username="hie"),
]
User,
values=[
[
ColumnValue(name="name", value="Crispen"),
ColumnValue(name="username", value="hi"),
],
[
ColumnValue(name="name", value="Crispen"),
ColumnValue(name="username", value="heyy"),
],
[
ColumnValue(name="name", value="Crispen"),
ColumnValue(name="username", value="hie"),
],
],
)
mysql_loom.delete_bulk(
User, filters=Filter(column="name", value="Crispen", operator="eq")
Expand All @@ -216,11 +238,21 @@ class Post(Model):
User, filters=Filter(column="name", value="Crispen", operator="eq")
)
mysql_loom.insert_bulk(
[
User(name="Crispen", username="hi"),
User(name="Crispen", username="heyy"),
User(name="Crispen", username="hie"),
]
User,
values=[
[
ColumnValue(name="name", value="Crispen"),
ColumnValue(name="username", value="hi"),
],
[
ColumnValue(name="name", value="Crispen"),
ColumnValue(name="username", value="heyy"),
],
[
ColumnValue(name="name", value="Crispen"),
ColumnValue(name="username", value="hie"),
],
],
)
mysql_loom.delete_bulk(
User,
Expand Down
Loading

0 comments on commit 4b6f4d1

Please sign in to comment.