Skip to content

Commit

Permalink
chore: splite routes and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nuffin committed Oct 16, 2024
1 parent 0838ce0 commit dbba6a7
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 19 deletions.
2 changes: 1 addition & 1 deletion llmsearch/api/blueprint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from flask import Blueprint

bp = Blueprint('api', __name__, url_prefix='/api')
bp = Blueprint("api", __name__, url_prefix="/api")
10 changes: 5 additions & 5 deletions llmsearch/api/file.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import os

from flask import (current_app, jsonify, request)
from flask import current_app, jsonify, request
import hashlib
from werkzeug.utils import secure_filename

from env import getenv
from .blueprint import bp

@bp.route('/upload', methods=['POST'])

@bp.route("/upload", methods=["POST"])
def upload():
return jsonify(message='Search'), 200
return jsonify(message="Search"), 200


ALLOWED_EXTENSIONS = {
"txt",
Expand Down Expand Up @@ -69,5 +71,3 @@ def upload_file():
)
else:
return jsonify({"error": "File type not allowed"}), 400


6 changes: 3 additions & 3 deletions llmsearch/api/search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import jsonify
from .blueprint import bp

@bp.route('/hello', methods=['GET'])
def search():
return jsonify(message='Search'), 200

@bp.route("/hello", methods=["GET"])
def search():
return jsonify(message="Search"), 200
12 changes: 8 additions & 4 deletions llmsearch/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@


# Create a custom CLI group
cli = AppGroup('cli')
cli = AppGroup("cli")

@cli.command('config')

@cli.command("config")
def dump_config():
"""Show config."""
print("===================================================================")
Expand All @@ -45,7 +46,7 @@ def create_app():
template_folder=public_path,
static_url_path="/",
)
app.config.from_object('config.Config')
app.config.from_object("config.Config")
CORS(app)

dash_app = Dash(
Expand All @@ -64,7 +65,9 @@ def create_app():
[
dbc.Row(
[
dbc.Col(html.H1("Simple Dash App", className="text-center")),
dbc.Col(
html.H1("Simple Dash App", className="text-center")
),
dbc.Col(
html.P(
"This is a simple web app using Flask and Dash.",
Expand Down Expand Up @@ -98,6 +101,7 @@ def main():
## print(f"meme_type={meme_type}")

from env import getenv

host = getenv("HOST")
port = getenv("PORT")
app = create_app()
Expand Down
1 change: 1 addition & 0 deletions llmsearch/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

def init_db(app):
from models import File, User

__models = [File, User]

db.init_app(app)
Expand Down
37 changes: 37 additions & 0 deletions llmsearch/tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest
from app import app # Assuming your Flask app is in app.py


class FlaskAppTestCase(unittest.TestCase):
def setUp(self):
# Set up the test client
self.app = app.test_client()
self.app.testing = True

def test_hello(self):
# Test the /api/hello route
response = self.app.get("/api/hello")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"message": "Hello, World!"})

def test_greet_default(self):
# Test the /api/greet route with default name
response = self.app.post("/api/greet", json={})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"message": "Hello, Guest!"})

def test_greet_with_name(self):
# Test the /api/greet route with a name
response = self.app.post("/api/greet", json={"name": "Alice"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"message": "Hello, Alice!"})

def test_greet_with_empty_name(self):
# Test the /api/greet route with an empty name
response = self.app.post("/api/greet", json={"name": ""})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"message": "Hello, !"})


if __name__ == "__main__":
unittest.main()
12 changes: 6 additions & 6 deletions llmsearch/web/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from flask import (
Blueprint,
Blueprint,
redirect,
render_template_string,
request,
send_from_directory,
send_from_directory,
session,
url_for,
)
)

bp = Blueprint("web", __name__, url_prefix="")

bp = Blueprint('web', __name__, url_prefix='')

# @web_blueprint.route("/")
# def home():
Expand All @@ -24,6 +25,7 @@
def serve_react_app(path):
return send_from_directory(bp.static_folder, "index.html")


@bp.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
Expand Down Expand Up @@ -68,5 +70,3 @@ def about():
<p><a href="/">Back to home</a></p>
"""
)


0 comments on commit dbba6a7

Please sign in to comment.