def test_db_operators_fetch_rows(): db.connect(DB_URI) class User(db.BaseModel): __tablename__ = "users" id = Column(Integer, primary_key=True) username = Column(String(50), default='') age = Column(Integer) db.create_all() lucy = User.create(**{ 'username': '******', 'age': 13, }) jack = User.create(**{ 'username': '******', 'age': 20, }) groot = User.create(**{ 'username': '******', 'age': 32, }) assert lucy.id > 0 users = User.query().filter(User.username.like('%c%'), User.age > 0).all() assert len( users) == 2, 'Fetch users count in common way should work properly' filters = { 'username__like': '%c%', 'age__gt': 0, } users = User.query().filter(*get_filters_expr(User, **filters)).all() assert len(users) == 2
def test_model_to_schema_from_orm(): db.connect(DB_URI) class User(db.BaseModel): __tablename__ = "users" id = Column(Integer, primary_key=True) db.create_all() user = User.create() # noinspection PyPep8Naming UserSchema = model_to_schema(User) user_schema = UserSchema.from_orm(user) assert user_schema.id == 1 assert user_schema.created_time <= user_schema.updated_time assert user_schema.is_active expected = { 'id': user.id, 'created_time': user.created_time, 'updated_time': user.updated_time, 'is_active': user.is_active, } assert user_schema.dict( ) == expected, 'Schema to dict value should equal origin model'
def test_base_model_create_entity(): db.connect(DB_URI) class User(db.BaseModel): __tablename__ = "users" id = Column(Integer, primary_key=True) db.create_all() user = User.create() assert user.id > 0 # create successfully assert user.created_time <= user.updated_time assert user.is_active
def test_base_model(): db.connect(DB_URI) class User(db.BaseModel): __tablename__ = "users" id = Column(Integer, primary_key=True) db.create_all() # using the exists columns users = db.query(User.created_time, User.updated_time, User.is_active).all() assert users == []
def test_model_to_schema(): db.connect(DB_URI) class User(db.BaseModel): __tablename__ = "users" id = Column(Integer, primary_key=True) db.create_all() # noinspection PyPep8Naming UserSchema = model_to_schema(User) user_schema = UserSchema(id=1) assert user_schema.id == 1
import locale from bali.db import db from pydantic.env_settings import BaseSettings from redis import Redis class Settings(BaseSettings): class Config: case_sensitive = True env_file = '.env' TITLE: str = "{{cookiecutter.repo_name}}" LOCALE: str TZ: str DATABASE: str RPC_PORT: int RPC_THREAD_POOL_SIZE: int MQ_HOST: str MQ_ACCESS_ID: str MQ_ACCESS_KEY: str MQ_INSTANCE: str REDIS: str settings = Settings() db.connect(settings.DATABASE) locale.setlocale(locale.LC_ALL, settings.LOCALE) redis_cli = Redis.from_url(settings.REDIS)
from typing import Optional from pydantic import BaseModel from sqlalchemy import Column, Integer, String from bali.db import db from bali.db.operators import get_filters_expr from bali.decorators import action from bali.resource import ModelResource from bali.schemas import ListRequest from permissions import IsAuthenticated DB_URI = 'sqlite:///:memory:' db.connect(DB_URI) class User(db.BaseModel): __tablename__ = "users" id = Column(Integer, primary_key=True) username = Column(String(50), default='') age = Column(Integer) # generate records db.create_all() lucy = User.create(**{ 'username': '******', 'age': 13, }) jack = User.create(**{
def test_db_connect(): db.connect(DB_URI) assert db._session is not None