| 708 | |
| 709 | #[test] |
| 710 | fn test_realistic_flask_app() { |
| 711 | let mut parser = PythonParser::new(); |
| 712 | let source = r#" |
| 713 | from flask import Flask, jsonify, request |
| 714 | from typing import Optional |
| 715 | |
| 716 | app = Flask(__name__) |
| 717 | |
| 718 | MAX_PAGE_SIZE = 100 |
| 719 | DEFAULT_PAGE_SIZE = 20 |
| 720 | |
| 721 | class UserNotFoundError(Exception): |
| 722 | pass |
| 723 | |
| 724 | def get_pagination() -> tuple[int, int]: |
| 725 | page = request.args.get("page", 1, type=int) |
| 726 | size = min(request.args.get("size", DEFAULT_PAGE_SIZE, type=int), MAX_PAGE_SIZE) |
| 727 | return page, size |
| 728 | |
| 729 | @app.route("/api/users") |
| 730 | def list_users(): |
| 731 | page, size = get_pagination() |
| 732 | return jsonify({"users": [], "page": page, "size": size}) |
| 733 | |
| 734 | @app.route("/api/users/<user_id>") |
| 735 | def get_user(user_id: str): |
| 736 | return jsonify({"id": user_id, "name": "Alice"}) |
| 737 | |
| 738 | class UserService: |
| 739 | def __init__(self, db): |
| 740 | self.db = db |
| 741 | |
| 742 | async def find_user(self, user_id: str) -> Optional[dict]: |
| 743 | return await self.db.users.find_one({"_id": user_id}) |
| 744 | |
| 745 | async def create_user(self, data: dict) -> dict: |
| 746 | result = await self.db.users.insert_one(data) |
| 747 | return {**data, "_id": str(result.inserted_id)} |
| 748 | "#; |
| 749 | let entities = parser.extract(source, "app.py"); |
| 750 | |
| 751 | let names: Vec<&str> = entities.iter().map(|e| e.name.as_str()).collect(); |
| 752 | |
| 753 | // Functions |
| 754 | assert!( |
| 755 | names.contains(&"get_pagination"), |
| 756 | "Should find get_pagination" |
| 757 | ); |
| 758 | assert!(names.contains(&"list_users"), "Should find list_users"); |
| 759 | assert!(names.contains(&"get_user"), "Should find get_user"); |
| 760 | |
| 761 | // Classes |
| 762 | assert!( |
| 763 | names.contains(&"UserNotFoundError"), |
| 764 | "Should find UserNotFoundError" |
| 765 | ); |
| 766 | assert!(names.contains(&"UserService"), "Should find UserService"); |
| 767 | |