0

I am reading the tutorial here

But this tutorial uses sqlalchemy as following:

from datetime import datetime, timedelta
import unittest
from app import app, db
from app.models import User, Post

class UserModelCase(unittest.TestCase):
    def setUp(self):
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
        db.create_all()

    def tearDown(self):
        db.session.remove()
        db.drop_all()

in fact, I already wrote a web app that uses from flask_mysqldb import MySQL

the __init__.py in the tutorial looks like this:

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)

from app import routes, models

I have mysql=MySQL(app) in my own __init__.py

can I replace db by mysql in the first part of the code to test my app?

and what about app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'?

I haven't use the sqlalchemy,just use flask_mysqldb

Yiling Liu
  • 666
  • 1
  • 6
  • 21

1 Answers1

0

Sol:

use testing.mysqld module

here

Reference: Mock a MySQL database in Python

Yiling Liu
  • 666
  • 1
  • 6
  • 21