Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating tables using SQLAlchemy model #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/rush/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class AuditMixin(Base):
performed_by = Column(Integer, nullable=True)
created_at = Column(TIMESTAMP, default=get_current_ist_time(), nullable=False)
updated_at = Column(TIMESTAMP, default=get_current_ist_time(), nullable=False)
performed_by = Column(Integer, default=1, nullable=True)

@classmethod
def snapshot(
Expand Down Expand Up @@ -87,7 +86,6 @@ def get_or_create(session, model, defaults=None, **kwargs):

class User(AuditMixin):
__tablename__ = "users"
user_id = Column(Integer, primary_key=True)
name = Column(String(50))
email = Column(String(100))
fullname = Column(String(50))
Expand All @@ -102,7 +100,7 @@ class AuditMixinPy:

@py_dataclass
class UserPy(AuditMixinPy):
user_id: str
id: str
name: str
email: str
fullname: str
Expand Down
15 changes: 1 addition & 14 deletions src/test/alembic_config/versions/1a5c43fc5b50_add_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,7 @@


def upgrade() -> None:
op.create_table(
"users",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("performed_by", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("email", sa.String(), nullable=False),
sa.Column("fullname", sa.String(), nullable=False),
sa.Column("nickname", sa.String(), nullable=False),
sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
# pass
pass


def downgrade() -> None:
Expand Down
4 changes: 3 additions & 1 deletion src/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from sqlalchemy.engine import Engine
from sqlalchemy.orm import sessionmaker

# from rush.pg_function import PGFunction
from rush.models import Base

REPO_ROOT = Path(os.path.abspath(os.path.dirname(__file__))).parent.parent.resolve()
TEST_RESOURCE_ROOT = REPO_ROOT / "src" / "test" / "resources"
Expand Down Expand Up @@ -127,8 +127,10 @@ def postgres_server(docker_client: docker.DockerClient) -> Iterator[Dict[str, st

@pytest.fixture(scope="session")
def pg(postgres_server: Dict[str, str]) -> Iterator[Dict[str, str]]:
Base.metadata.create_all(postgres_server["engine"])
upgrade_db(postgres_server, "head")
yield postgres_server
Base.metadata.drop_all(postgres_server["engine"])
downgrade_db(postgres_server, "base")


Expand Down
22 changes: 7 additions & 15 deletions src/test/test_current.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ def test_current(getAlembic: alembic.config.Config) -> None:

def test_user2(session: sqlalchemy.orm.session.Session) -> None:
u = User(
# id=101,
performed_by=123,
user_id=101,
id=101,
name="dfd",
fullname="dfdf",
nickname="dfdd",
Expand All @@ -48,7 +47,6 @@ def test_user2(session: sqlalchemy.orm.session.Session) -> None:
id=a.id,
performed_by=123,
email="sss",
user_id=101,
name="dfd",
fullname="dfdf",
nickname="dfdd",
Expand All @@ -57,9 +55,8 @@ def test_user2(session: sqlalchemy.orm.session.Session) -> None:

def test_user(session: sqlalchemy.orm.session.Session) -> None:
u = User(
# id=100,
performed_by=123,
user_id=101,
id=102,
name="dfd",
fullname="dfdf",
nickname="dfdd",
Expand All @@ -68,25 +65,22 @@ def test_user(session: sqlalchemy.orm.session.Session) -> None:
session.add(u)
session.commit()
a = session.query(User).first()
print(a.id)
u = UserPy(
id=a.id,
performed_by=123,
email="sss",
user_id=101,
id=a.id,
name="dfd",
fullname="dfdf",
nickname="dfdd",
)


# if you remove min_value=2, you will see the errors
@given(integers(min_value=2, max_value=999999))
def test_user3(session: sqlalchemy.orm.session.Session, user_id: int) -> None:
@given(integers(min_value=103, max_value=999999))
def test_user3(session: sqlalchemy.orm.session.Session, id: int) -> None:
u = User(
# id=100,
performed_by=123,
user_id=user_id,
id=id,
name="dfd",
fullname="dfdf",
nickname="dfdd",
Expand All @@ -95,12 +89,10 @@ def test_user3(session: sqlalchemy.orm.session.Session, user_id: int) -> None:
session.add(u)
session.commit()
a = session.query(User).first()
print(a.id)
u = UserPy(
id=a.id,
performed_by=123,
email="sss",
user_id=101,
id=a.id,
name="dfd",
fullname="dfdf",
nickname="dfdd",
Expand Down