| 731 | |
| 732 | #[test] |
| 733 | fn test_realistic_flask_app() { |
| 734 | let mut parser = PythonParser::new(); |
| 735 | let source = r#" |
| 736 | from flask import Flask, jsonify, request |
| 737 | from typing import Optional |
| 738 | |
| 739 | app = Flask(__name__) |
| 740 | |
| 741 | MAX_PAGE_SIZE = 100 |
| 742 | DEFAULT_PAGE_SIZE = 20 |
| 743 | |
| 744 | class UserNotFoundError(Exception): |
| 745 | pass |
| 746 | |
| 747 | def get_pagination() -> tuple[int, int]: |
| 748 | page = request.args.get("page", 1, type=int) |
| 749 | size = min(request.args.get("size", DEFAULT_PAGE_SIZE, type=int), MAX_PAGE_SIZE) |
| 750 | return page, size |
| 751 | |
| 752 | @app.route("/api/users") |
| 753 | def list_users(): |
| 754 | page, size = get_pagination() |
| 755 | return jsonify({"users": [], "page": page, "size": size}) |
| 756 | |
| 757 | @app.route("/api/users/<user_id>") |
| 758 | def get_user(user_id: str): |
| 759 | return jsonify({"id": user_id, "name": "Alice"}) |
| 760 | |
| 761 | class UserService: |
| 762 | def __init__(self, db): |
| 763 | self.db = db |
| 764 | |
| 765 | async def find_user(self, user_id: str) -> Optional[dict]: |
| 766 | return await self.db.users.find_one({"_id": user_id}) |
| 767 | |
| 768 | async def create_user(self, data: dict) -> dict: |
| 769 | result = await self.db.users.insert_one(data) |
| 770 | return {**data, "_id": str(result.inserted_id)} |
| 771 | "#; |
| 772 | let entities = parser.extract(source, "app.py"); |
| 773 | |
| 774 | let names: Vec<&str> = entities.iter().map(|e| e.name.as_str()).collect(); |
| 775 | |
| 776 | // Functions |
| 777 | assert!( |
| 778 | names.contains(&"get_pagination"), |
| 779 | "Should find get_pagination" |
| 780 | ); |
| 781 | assert!(names.contains(&"list_users"), "Should find list_users"); |
| 782 | assert!(names.contains(&"get_user"), "Should find get_user"); |
| 783 | |
| 784 | // Classes |
| 785 | assert!( |
| 786 | names.contains(&"UserNotFoundError"), |
| 787 | "Should find UserNotFoundError" |
| 788 | ); |
| 789 | assert!(names.contains(&"UserService"), "Should find UserService"); |
| 790 | |