Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Created by Vercel
POSTGRES_URL="postgres://default:Hs1ODnhjr2lU@ep-holy-sunset-a405zt5y-pooler.us-east-1.aws.neon.tech:5432/verceldb?sslmode=require"
POSTGRES_PRISMA_URL="postgres://default:Hs1ODnhjr2lU@ep-holy-sunset-a405zt5y-pooler.us-east-1.aws.neon.tech:5432/verceldb?sslmode=require&pgbouncer=true&connect_timeout=15"
POSTGRES_URL_NO_SSL="postgres://default:Hs1ODnhjr2lU@ep-holy-sunset-a405zt5y-pooler.us-east-1.aws.neon.tech:5432/verceldb"
POSTGRES_URL_NON_POOLING="postgres://default:[email protected]:5432/verceldb?sslmode=require"
POSTGRES_USER="default"
POSTGRES_HOST="ep-holy-sunset-a405zt5y-pooler.us-east-1.aws.neon.tech"
POSTGRES_PASSWORD="Hs1ODnhjr2lU"
POSTGRES_DATABASE="verceldb"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.venv
__pycache__/
4 changes: 4 additions & 0 deletions change sql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE taxis (
id SERIAL PRIMARY KEY,
plate_number VARCHAR(20) NOT NULL
);
Binary file added fleet-management-software-data.zip
Binary file not shown.
1 change: 1 addition & 0 deletions psql [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
psql "postgres://default:************@ep-holy-sunset-a405zt5y.us-east-1.aws.neon.tech:5432/verceldb?sslmode=require"
Empty file added src/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions src/conection_postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Database connection"""
import os
import psycopg2


try:
connection = psycopg2.connect(
host = os.getenv('POSTGRES_HOST'),
user = os.getenv('POSTGRES_USER'),
password = os.getenv('POSTGRES_PASSWORD'),
database = os.getenv('POSTGRES_DATABASE'),
port = 5432
)
print("Succefully connected")
cursor = connection.cursor()
cursor.execute("SELECT version()")
except Exception as ex:
print(ex)
82 changes: 82 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
from flasgger import Swagger

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://default:Hs1ODnhjr2lU@ep-holy-sunset-a405zt5y-pooler.us-east-1.aws.neon.tech/verceldb'
db = SQLAlchemy(app)

# Initialize Swagger
swagger = Swagger(app)

# Define the Taxi model
class Taxi(db.Model):
"""Model representing a Taxi."""
__tablename__ = 'taxis'
id = db.Column(db.Integer, primary_key=True)
plate = db.Column(db.String(20), nullable=False)

# Route to show all taxis
@app.route('/taxis', methods=['GET'])
def show_taxis():
"""
Endpoint to get all taxis.

---
parameters:
- name: query
in: query
type: string
required: false
description: Plate of the taxi.

- name: page
in: query
type: integer
required: false
description: Page number of the results to retrieve. Default is 1.

- name: limit
in: query
type: integer
required: false
description: Number of elements per page. Default is 10.

responses:
200 :
description: A JSON array of all taxis.
schema:
type: array
items:
type: object
properties:
id:
type: integer
description: The taxi ID.
plate:
type: string
description: Plate of the taxi.
"""

# Get pagination parameters from the request
limit = request.args.get("limit", default=10, type=int)
page = request.args.get("page", default=1, type=int)

# Calculate home index based on page and limit
start_index = (page - 1) * limit

# Query database with limit and starting index
taxis = Taxi.query.offset(start_index).limit(limit).all()

# Create list
taxis_list = [{'id': taxi.id, 'plate': taxi.plate} for taxi in taxis]

# create diccionario
response = {
'taxis': taxis_list
}

return jsonify(response)

if __name__ == "__main__":
app.run(debug=True)
Empty file added tests/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest
#from src.main import app


@pytest.fixture
def client():
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
with app.test_client() as client:
with app.app_context():
yield client
59 changes: 59 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest


@pytest.mark.parametrize("expected_status", [200])
def test_show_taxis(client, expected_status):
"""test for function taxis"""
# Make the GET request to the '/taxis' endpoint
response = client.get('/taxis')
assert response.status_code == expected_status


# import pytest
# from src.main import app

# @pytest.fixture
# def client():
# app.config['TESTING'] = True
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
# with app.test_client() as client:
# with app.app_context():
# yield client

# @pytest.mark.parametrize("mock_data, expected_status, expected_plates", [
# (
# [
# {
# "id": 9217,
# "plate": "MJDD-8286"
# },
# {
# "id": 6737,
# "plate": "DLEP-5452"
# }
# ],
# 200,
# ['MJDD-8286', 'DLEP-5452']
# )
# ])
# def test_show_taxis(client, mocker, mock_data, expected_status, expected_plates):
# # Mock of the query results
# mocker.patch('src.main.Taxi.query')
# mock_query = app.db.session.query.return_value

# mock_query.limit().all.return_value = [type('Taxi', (), data) for data in mock_data]

# # Make the GET request to the '/taxis' endpoint
# response = client.get('/taxis/')

# # Check code of the answer
# assert response.status_code == expected_status

# # Check the result of the answer
# data = response.json()
# assert 'taxis' in data
# assert len(data['taxis']) == len(expected_plates)

# # Check taxis' plates
# for taxi, expected_plate in zip(data['taxis'], expected_plates):
# assert taxi['plate'] == expected_plate