-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c97bfc1
commit e453ce7
Showing
12 changed files
with
629 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
ForeignKeyColumn, | ||
UpdatedAtColumn, | ||
Column, | ||
TableColumn, | ||
) | ||
|
||
Dataloom = Dataloom | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
class TestCreatingTableMysql: | ||
def test_2_pk_error(self): | ||
from dataloom import Column, PrimaryKeyColumn, Dataloom, TableColumn, Model | ||
import pytest | ||
from typing import Optional | ||
|
||
mysql_loom = Dataloom( | ||
dialect="mysql", database="hi", password="root", user="root" | ||
) | ||
conn = mysql_loom.connect() | ||
|
||
class User(Model): | ||
__tablename__: Optional[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) | ||
assert ( | ||
str(exc_info.value) | ||
== "You have defined many field as primary keys which is not allowed. Fields (`_id`, `id`) are primary keys." | ||
) | ||
conn.close() | ||
|
||
def test_no_pk_error(self): | ||
import pytest | ||
from dataloom import Model, Dataloom, Column, TableColumn | ||
from typing import Optional | ||
|
||
mysql_loom = Dataloom( | ||
dialect="mysql", database="hi", password="root", user="root" | ||
) | ||
conn = mysql_loom.connect() | ||
|
||
class User(Model): | ||
__tablename__: Optional[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) | ||
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 | ||
|
||
mysql_loom = Dataloom( | ||
dialect="mysql", database="hi", password="root", user="root" | ||
) | ||
conn = mysql_loom.connect() | ||
|
||
class Posts(Model): | ||
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") | ||
username = Column(type="text", nullable=False, default="Hello there!!") | ||
name = Column(type="varchar", unique=True, length=255) | ||
|
||
assert User._get_table_name() == "users" | ||
assert Posts._get_table_name() == "posts" | ||
conn.close() | ||
|
||
def test_connect_sync(self): | ||
from dataloom import Dataloom, Model, TableColumn, Column, PrimaryKeyColumn | ||
from typing import Optional | ||
|
||
class User(Model): | ||
__tablename__: Optional[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") | ||
|
||
id = PrimaryKeyColumn(type="int", nullable=False, auto_increment=True) | ||
title = Column(type="text", nullable=False) | ||
|
||
mysql_loom = Dataloom( | ||
dialect="mysql", database="hi", password="root", user="root" | ||
) | ||
conn, tables = mysql_loom.connect_and_sync([User, Post], drop=True, force=True) | ||
assert len(tables) == 2 | ||
assert sorted(tables) == sorted(["users", "posts"]) | ||
|
||
conn.close() | ||
|
||
def test_syncing_tables(self): | ||
from dataloom import Model, Dataloom, Column, PrimaryKeyColumn, TableColumn | ||
from typing import Optional | ||
|
||
mysql_loom = Dataloom( | ||
dialect="mysql", database="hi", password="root", user="root" | ||
) | ||
conn = mysql_loom.connect() | ||
|
||
class Post(Model): | ||
__tablename__: Optional[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") | ||
id = PrimaryKeyColumn(type="int", auto_increment=True) | ||
username = Column(type="text", nullable=False, default="Hello there!!") | ||
name = Column(type="varchar", unique=True, length=255) | ||
|
||
tables = mysql_loom.sync([User, Post], drop=True, force=True) | ||
assert len(tables) == 2 | ||
assert sorted(tables) == sorted(["users", "posts"]) | ||
conn.close() |
Oops, something went wrong.