Coder Social home page Coder Social logo

python-p4-validation-flask-sqlalchemy-validations-lab's Introduction

Flask-SQLAlchemy Validations Lab

Learning Goals

  • Define validations in data processing.
  • Ensure that only acceptable input is sent to the database using validations.

Key Vocab

  • Constraint: a rule enforced on the data columns of a table. Ensures that only appropriate data is saved to the database.
  • Validation: an automatic check to ensure that data entered is sensible and feasible.
  • Forms: A web form (or HTML form) is a place where users enter data or personal information that's then sent to a server for processing.

Introduction

This is a test-driven lab.

Run pipenv install to create your virtual environment and pipenv shell to enter the virtual environment.

pipenv install && pipenv shell

This project has starter code for a couple of models, Author and Post. To get create the database from the initial migration, run:

$ cd server
$ flask db upgrade
$ python seed.py

Basic Validations

Add validators to the Author and Post models such that:

  1. All authors have a name.
  2. No two authors have the same name.
  3. Author phone numbers are exactly ten digits.
  4. Post content is at least 250 characters long.
  5. Post summary is a maximum of 250 characters.
  6. Post category is either Fiction or Non-Fiction.
  7. Post title is sufficiently clickbait-y and must contain one of the following:
    • "Won't Believe"
    • "Secret"
    • "Top"
    • "Guess"

You should not need to run another migration, unless you altered model constraints.

Run pytest -x to run your tests. Use these instructions and pytest's error messages to complete your work in the server/ folder.


Resources

python-p4-validation-flask-sqlalchemy-validations-lab's People

Contributors

dado3899 avatar jlboba avatar lambert-stephen avatar linda-seiter avatar lizbur10 avatar pgill97 avatar professor-ben avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-p4-validation-flask-sqlalchemy-validations-lab's Issues

Possible error in content/summary validation?

Canvas Link

https://learning.flatironschool.com/courses/6463/assignments/250503?module_item_id=591896

Concern

Wouldn't len(content) <= 250 and len(summary) >= 250 raise an error if string is equal to 250 characters? The solution error message is "String must be greater/lesser than or equal to 250 characters long"
If I am misunderstanding something, please let me know!

@validates('content', 'summary')
def validate_length(self, key, string):
if( key == 'content'):
if len(string) <= 250:
raise ValueError("Post content must be greater than or equal 250 characters long.")
if( key == 'summary'):
if len(string) >= 250:
raise ValueError("Post summary must be less than or equal to 250 characters long.")
return string

Additional Context

No response

Suggested Changes

No response

TEST 2 FOR VALIDATIONS LAB ALWAYS FAILS BEFORE RUNNING THE TEST AND THERE IS NO SEED FILE

Canvas Link

https://learning.flatironschool.com/courses/6560/assignments/243007?module_item_id=571712

Concern

I see run the following commands:

  1. pipenv install
  2. pipenv shell
  3. cd server
  4. flask db upgrade
    5. python seed.py

ISSUE #1: The 5th command errors out because the file does not exist. It is not needed in the lab so it should be removed.

That is not the biggest problem with this lab.

For the lab there are seven (7) tasks we need to do. We need to make sure that:

  1. All authors have a name.
  2. No two authors have the same name.
  3. Author phone numbers are exactly ten digits.
  4. Post content is at least 250 characters long.
  5. Post summary is a maximum of 250 characters.
  6. Post category is either Fiction or Non-Fiction.
  7. Post title is sufficiently clickbait-y and must contain one of the following:
  • "Won't Believe"
  • "Secret"
  • "Top"
  • "Guess"

That is all fine and dandy except for one MAJOR problem. The test for unique names is wrong. The test initially goes into creating and adding an Author called "Ben". The problem is on the database the Author Ben already exists, so it fails to add Ben in for a successful test. So it does not even get to testing our code when it tries to add Ben again. Test 2 always fails. The only time it does not fail is when the DB is empty. So that means we need to remove all authors from it before running this test.

There is a simple solution:

def test_requires_unique_name(self):
'''requires each record to have a unique name.'''

    **with app.app_context():
        db.session.query(Author).delete()
        db.session.commit()**

    with app.app_context():
        author_a = Author(name = 'Ben', phone_number = '1231144321')
        db.session.add(author_a)
        db.session.commit()
        
        with pytest.raises(ValueError):
            author_b = Author(name = 'Ben', phone_number = '1231144321')
            
        db.session.query(Author).delete()
        db.session.commit()

The solution is the bolded portion.

Additional Context

I reported the issue to a TC today (1-10-2024) and noticed someone else had run into a similar problem with the test and the lab on 12-26-2023.

Suggested Changes

REMOVE the command:

python seed.py

The file does not exist and is not needed for the lab.

ADD THIS TO THE TEST FILE.

There is a simple solution:

def test_requires_unique_name(self):
'''requires each record to have a unique name.'''

    **with app.app_context():
        db.session.query(Author).delete()
        db.session.commit()**

    with app.app_context():
        author_a = Author(name = 'Ben', phone_number = '1231144321')
        db.session.add(author_a)
        db.session.commit()
        
        with pytest.raises(ValueError):
            author_b = Author(name = 'Ben', phone_number = '1231144321')
            
        db.session.query(Author).delete()
        db.session.commit()

The solution is the bolded portion.

The seed file needs to come out, it's interfering with the tests.

Canvas Link

https://learning.flatironschool.com/courses/6551/assignments/237721?module_item_id=558463

Concern

The test checking the name validation is broken because of the seed file. The test is checking for the name validation functionality that names are unique. Well the test creates a user named ben and THEN tries to create a second ben and measure if the error was raised on the second attempt but the seed file adds a user named ben to the db so when the student runs pytest, that first ben raises the error instead of the second one.

Additional Context

No response

Suggested Changes

either remove the seed file completely or just remove ben from the db since that is the name that is being used to test the uniqueness of the name attribute.

Models are not set up

Canvas Link

https://learning.flatironschool.com/courses/6182/assignments/211188?module_item_id=486871

Concern

Our instructions indicate that the models are already set up for students, and that all they have to do is run alembic upgrade head. We actually haven't filled out any information for the models thus far. Also our directions are weirdly worded:

This project has starter code for a couple of models, Author and Post. To get set up, run:

This is a test-driven lab. Run pipenv install to create your virtual environment and pipenv shell to enter the virtual environment.

To generate a database using the provided models, from lib/ run:

$ alembic upgrade head

Additional Context

No response

Suggested Changes

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.