17 : Get Request to Retreive a Job



I believe you still remember the very first post, where I told you the difference between get, post, put, patch, and delete requests. In this post, we are going to explore get requests. First, we will implement a route that gives back data of a job post given its id. This time we already have all the files existing. Just add the following code in apis > versions > route_jobs.py

from fastapi import APIRouter
from sqlalchemy.orm import Session
from fastapi import Depends,HTTPException,status

from db.session import get_db
from db.models.jobs import Job
from schemas.jobs import JobCreate,ShowJob
from db.repository.jobs import create_new_job,retreive_job  #new import retrieve_job

router = APIRouter()

def create_job(job: JobCreate,db: Session = Depends(get_db)):

#new function
@router.get("/get/{id}",response_model=ShowJob) # if we keep just "{id}" . it would stat catching all routes
def read_job(id:int,db:Session = Depends(get_db)):
    job = retreive_job(id=id,db=db)
    if not job:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=f"Job with this id {id} does not exist")
    return job

Since our database and orm logic will be in the repository folder. So, let's add the following lines in file db > repository > jobs.py

def retreive_job(id:int,db:Session):
    item = db.query(Job).filter(Job.id == id).first()
    return item

Testing our new route.
Add the below test to tests > test_routes > test_jobs.py

import json

def test_create_job(client):
    data = {
        "title": "SDE super",
        "company": "google",

def test_read_job(client):     #new test
    data = {
        "title": "SDE super",
        "company": "doogle",
        "company_url": "www.doogle.com",
        "location": "USA,NY",
        "description": "python",
        "date_posted": "2022-03-20"
    response = client.post("/jobs/create-job/",json.dumps(data))

    response = client.get("/jobs/get/1/")
    assert response.status_code == 200
    assert response.json()['title'] == "SDE super"

But why are we again making a post request to test a get request? It's because our tests are independent. After every single tests, our tables are in a brand new state. So, we need to have a record to test the fetching of a record. That's why we are having the POST part. This is not the only test possible, It is one of the many tests that are possible. We could have also tested that our web app should return 404 with a message "Job with this id {id} does not exist". One more thing that I have learnt is the quality of tests matters but not the quantity. There are a thousand ways to test the same thing but testing many possible aspects is considered good testing. Let's use pytest now.

Final git commit: implement get request for job along with test · nofoobar/JobBoard-Fastapi@6767761 (github.com)

Prev: 16 : Post … Next: 18 : Cleaning …

Brige the gap between Tutorial hell and Industry. We want to bring in the culture of Clean Code, Test Driven Development.

We know, we might make it hard for you but definitely worth the efforts.



Refund Policy

Follow us on our social media channels to stay updated.

© Copyright 2022-23 Team FastAPITutorial