简体   繁体   中英

python: How can I test my database model using in-memory db?

I am developing a project in Flask and using SQLAlchemy as ORM
I have a User model as

class User(UserMixin, db.Model):
    __tablename__ = 'users'
    # noinspection PyShadowingBuiltins
    uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True,
                  unique=True)
    email = Column('email', String, nullable=False, unique=True)
    _password = Column('password', String, nullable=False)
    created_on = Column('created_on', sa.types.DateTime(timezone=True),
                        default=datetime.utcnow())
    last_login = Column('last_login', sa.types.DateTime(timezone=True),
                        onupdate=datetime.utcnow())
  • I create a migration script using Alembic .
  • My database of choice is PostgreSQL but to test my models I preferred to use SQLite to make tests faster
  • Problem came in when there are some types in PostgreSQL and not in SQLite , in this case UUID
  • When I run migration on sqlite:// , it gives me error

AttributeError: 'module' object has no attribute 'GUID'

Question
- How can I keep my schema with GUID and run in-memory tests with SQLite? seems like SQLite doesn't have anything like it
- What are some ways people run tests faster(in-memory) when their database is PostgreSQL

How comes the tests faster with SQLite?

Anyway, if you want to keep things in memory...well, so keep your database in memory. Actually PostgreSQL moves frequently accessed data to memory, but anyway, you can install some RAMFS/RAMHDD solution and put your database there, so it will be extremely fast

You can google for solutions "postgresql on ramdisk"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM