0

I am using Flask and Flask SQLAlchemy and Flask Test. I define an example model with a string column with max length of 100. I thought I should get an exception if I try to insert a string with a length bigger than 100. SQLAlchemy even allows me to insert other data types like an int. Even if I have defined this column to be of type string. Or if I try to insert an empty string I also do not raise an Exception. Although I have set the column not to be nullable.

Why does SQLAlchemy just accepts everything. Shouldn't there be at least one exception raised?

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_testing import TestCase


db = SQLAlchemy()


def create_app(self):
    app = Flask(__name__)
    app.config['TESTING'] = True
    app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite://"

    db.init_app(app)

    return app


class Example(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)


class MyTest(TestCase):
    def create_app(self):
        return create_app(self)

    def setUp(self):
        db.create_all()

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

    def test_example_that_should_work(self):
        example = Example(name="ABC")

        db.session.add(example)
        db.session.commit()

        self.assertEqual(Example.query.count(), 1)

    def test_example__do_not_insert_strings_of_bigger_size(self):
        example = Example(name="ABC"*100)

        db.session.add(example)
        db.session.commit()

        self.assertEqual(Example.query.count(), 0)  # AssertionError: 1 != 0

    def test_example_do_not_insert_different_data_type(self):
        example = Example(name=42)

        db.session.add(example)
        db.session.commit()

        self.assertEqual(Example.query.count(), 0)  # AssertionError: 1 != 0

    def test_example_do_not_insert_nullable(self):
        example = Example(name="")

        db.session.add(example)
        db.session.commit()

        self.assertEqual(Example.query.count(), 0)  # AssertionError: 1 != 0
6
  • 2
    No, because the underlying database is sqlite and it will accept pretty much anything Commented Jun 9, 2018 at 16:10
  • Is there a way that I can define that SQLAlchemy checks the data type before adding to the database. Because I like testing with sqlite. Commented Jun 9, 2018 at 16:12
  • 2
    I'm trying to dig out the comment I want from the docs that's basically "SQLite ultimately doesn't care about datatypes on inserts". As for SQLAlchemy, I can't say. But it's not boundless on SQLite so I'd like to find the definitive comments. Commented Jun 9, 2018 at 16:16
  • 1
    I'm not able to find the comment I want right now, but the intro of this says that the types are dynamic. Commented Jun 9, 2018 at 16:28
  • Thanks, maybe I then use PostgresSQL for testing. Commented Jun 9, 2018 at 16:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.