Skip to content

Commit

Permalink
tests and bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispenGari committed Feb 16, 2024
1 parent d0fa850 commit 8590da1
Show file tree
Hide file tree
Showing 8 changed files with 764 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
- [Filters](#filters)
- [Utilities](#utilities)
- [`inspect`](#inspect)
- [Associations](#associations)
- [What is coming next?](#what-is-coming-next)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -1167,6 +1168,10 @@ Output:

The `inspect` function take the following arguments.

### Associations

Eager

### What is coming next?

1. Associations
Expand Down
2 changes: 1 addition & 1 deletion dataloom/keys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
push = False
push = True


class PgConfig:
Expand Down
29 changes: 21 additions & 8 deletions dataloom/loom/subqueries.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,28 @@ def get_find_by_pk_relations(self, parent: Model, pk, includes: list[Include] =
_, parent_pk_name, parent_fks, _ = get_table_fields(
parent, dialect=self.dialect
)
_pk = relations[key][re.sub(r'`|"', "", parent_pk_name)]

relations[key] = {
**relations[key],
**self.get_find_by_pk_relations(
include.model, _pk, includes=include.include
),
}

if isinstance(relations[key], dict):
_pk = relations[key][re.sub(r'`|"', "", parent_pk_name)]
relations[key] = {
**relations[key],
**self.get_find_by_pk_relations(
include.model, _pk, includes=include.include
),
}
else:
_pk = (
relations[key][0][re.sub(r'`|"', "", parent_pk_name)]
if len(relations[key]) != 0
else None
)
if _pk is not None:
relations[key] = {
**relations[key],
**self.get_find_by_pk_relations(
include.model, _pk, includes=include.include
),
}
return relations

def get_one(
Expand Down
239 changes: 239 additions & 0 deletions dataloom/tests/mysql/test_eager_loading_mysql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
class TestEagerLoadingOnMySQL:
def test_find_by_pk(self):
from dataloom import (
Dataloom,
Model,
Column,
PrimaryKeyColumn,
CreatedAtColumn,
TableColumn,
ForeignKeyColumn,
ColumnValue,
Include,
Order,
)
from dataloom.keys import MySQLConfig

mysql_loom = Dataloom(
dialect="mysql",
database=MySQLConfig.database,
password=MySQLConfig.password,
user=MySQLConfig.user,
)

class User(Model):
__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)
tokenVersion = Column(type="int", default=0)

class Profile(Model):
__tablename__: TableColumn = TableColumn(name="profiles")
id = PrimaryKeyColumn(type="int", auto_increment=True)
avatar = Column(type="text", nullable=False)
userId = ForeignKeyColumn(
User,
maps_to="1-1",
type="int",
required=True,
onDelete="CASCADE",
onUpdate="CASCADE",
)

class Post(Model):
__tablename__: TableColumn = TableColumn(name="posts")
id = PrimaryKeyColumn(
type="int", auto_increment=True, nullable=False, unique=True
)
completed = Column(type="boolean", default=False)
title = Column(type="varchar", length=255, nullable=False)
# timestamps
createdAt = CreatedAtColumn()
# relations
userId = ForeignKeyColumn(
User,
maps_to="1-N",
type="int",
required=True,
onDelete="CASCADE",
onUpdate="CASCADE",
)

class Category(Model):
__tablename__: TableColumn = TableColumn(name="categories")
id = PrimaryKeyColumn(
type="int", auto_increment=True, nullable=False, unique=True
)
type = Column(type="varchar", length=255, nullable=False)

postId = ForeignKeyColumn(
Post,
maps_to="N-1",
type="int",
required=True,
onDelete="CASCADE",
onUpdate="CASCADE",
)

conn, tables = mysql_loom.connect_and_sync(
[User, Profile, Post, Category], drop=True, force=True
)

userId = mysql_loom.insert_one(
instance=User,
values=ColumnValue(name="username", value="@miller"),
)

userId2 = mysql_loom.insert_one(
instance=User,
values=ColumnValue(name="username", value="bob"),
)

profileId = mysql_loom.insert_one(
instance=Profile,
values=[
ColumnValue(name="userId", value=userId),
ColumnValue(name="avatar", value="hello.jpg"),
],
)
for title in ["Hey", "Hello", "What are you doing", "Coding"]:
mysql_loom.insert_one(
instance=Post,
values=[
ColumnValue(name="userId", value=userId),
ColumnValue(name="title", value=title),
],
)

for cat in ["general", "education", "tech", "sport"]:
mysql_loom.insert_one(
instance=Category,
values=[
ColumnValue(name="postId", value=1),
ColumnValue(name="type", value=cat),
],
)

profile = mysql_loom.find_by_pk(
instance=Profile,
pk=profileId,
include=[
Include(
model=User, select=["id", "username", "tokenVersion"], has="one"
)
],
)
assert profile == {
"avatar": "hello.jpg",
"id": 1,
"userId": 1,
"user": {"id": 1, "username": "@miller", "tokenVersion": 0},
}

user = mysql_loom.find_by_pk(
instance=User,
pk=userId,
include=[Include(model=Profile, select=["id", "avatar"], has="one")],
)
assert user == {
"id": 1,
"name": "Bob",
"tokenVersion": 0,
"username": "@miller",
"profile": {"id": 1, "avatar": "hello.jpg"},
}

user = mysql_loom.find_by_pk(
instance=User,
pk=userId,
include=[
Include(
model=Post,
select=["id", "title"],
has="many",
offset=0,
limit=2,
order=[
Order(column="createdAt", order="DESC"),
Order(column="id", order="DESC"),
],
),
Include(model=Profile, select=["id", "avatar"], has="one"),
],
)
assert user == {
"id": 1,
"name": "Bob",
"tokenVersion": 0,
"username": "@miller",
"posts": [
{"id": 4, "title": "Coding"},
{"id": 3, "title": "What are you doing"},
],
"profile": {"id": 1, "avatar": "hello.jpg"},
}

post = mysql_loom.find_by_pk(
instance=Post,
pk=1,
select=["title", "id"],
include=[
Include(
model=User,
select=["id", "username"],
has="one",
include=[
Include(model=Profile, select=["avatar", "id"], has="one")
],
),
Include(
model=Category,
select=["id", "type"],
has="many",
order=[Order(column="id", order="DESC")],
),
],
)

assert post == {
"title": "Hey",
"id": 1,
"user": {
"id": 1,
"username": "@miller",
"profile": {"avatar": "hello.jpg", "id": 1},
},
"categories": [
{"id": 4, "type": "sport"},
{"id": 3, "type": "tech"},
{"id": 2, "type": "education"},
{"id": 1, "type": "general"},
],
}

user = mysql_loom.find_by_pk(
instance=User,
pk=userId2,
select=["username", "id"],
include=[
Include(
model=Post,
select=["id", "title"],
has="many",
include=[
Include(
model=Category,
select=["type", "id"],
has="many",
order=[Order(column="id", order="DESC")],
limit=2,
offset=0,
)
],
),
],
)
assert user == {"username": "bob", "id": 2, "posts": []}

conn.close()
Loading

0 comments on commit 8590da1

Please sign in to comment.