Skip to content

Commit b6c072a

Browse files
Merge pull request #54 from sustech-cs304/backend
Backend
2 parents 4972224 + 811208a commit b6c072a

File tree

3 files changed

+149
-7
lines changed

3 files changed

+149
-7
lines changed

backend/test/test_data_generation.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ def generate_random_bookmarklist(material_id, user_id):
218218
Note_id_list = []
219219
Code_snippet_id_list = []
220220
Bookmarklist_id_list = []
221-
File_id_list = []
221+
File_id_list_individual = []
222+
File_id_list_group = []
222223
Assignment_id_list = []
223224
Envrionment_id_list = []
224225

@@ -506,13 +507,38 @@ def test_create_file():
506507
assert response.status_code == 200 or response.status_code == 201
507508
data = response.json()
508509
assert "file_id" in data
509-
File_id_list.append(data["file_id"])
510+
File_id_list_individual.append(data["file_id"])
511+
assert data["message"] == "File created successfully"
512+
513+
files_path_group = [
514+
os.path.join(os.path.dirname(__file__), "./test_group_environment/dijkstra.py"),
515+
os.path.join(os.path.dirname(__file__), "./test_group_environment/README.md"),
516+
]
517+
518+
for file_path in files_path_group:
519+
assert os.path.exists(file_path), "Sample PDF file does not exist."
520+
with open(file_path, "rb") as f:
521+
files = {"file": (os.path.basename(file_path), f, "application/pdf")}
522+
data = {
523+
"file_name": os.path.basename(file_path),
524+
"file_path": "/code" if file_path.endswith(".py") else "/",
525+
}
526+
response = client.post(
527+
"/api/file",
528+
headers=headers,
529+
data=data,
530+
files=files,
531+
)
532+
assert response.status_code == 200 or response.status_code == 201
533+
data = response.json()
534+
assert "file_id" in data
535+
File_id_list_group.append(data["file_id"])
510536
assert data["message"] == "File created successfully"
511537

512538
def test_update_file():
513539
headers = {"Authorization": f"Bearer {Teacher_id_list[0]['token']}"}
514540
update_resp = client.put(
515-
f"/api/file/{File_id_list[3]}",
541+
f"/api/file/{File_id_list_individual[3]}",
516542
headers=headers,
517543
data={
518544
"file_name": "hello_world.py",
@@ -521,14 +547,14 @@ def test_update_file():
521547
)
522548
updated_file = update_resp.json()
523549
assert update_resp.status_code == 200
524-
assert updated_file["file_id"] == File_id_list[3]
550+
assert updated_file["file_id"] == File_id_list_individual[3]
525551
assert updated_file["file_name"] == "hello_world.py"
526552
assert updated_file["file_path"] == "/test_code_files"
527553

528554
def test_delete_file():
529555
headers = {"Authorization": f"Bearer {Teacher_id_list[0]['token']}"}
530556
delete_resp = client.delete(
531-
f"/api/file/{File_id_list[2]}",
557+
f"/api/file/{File_id_list_individual[2]}",
532558
headers=headers,
533559
)
534560
assert delete_resp.status_code == 200
@@ -542,9 +568,9 @@ def test_create_assignment():
542568
"course_id": Course_id_list[0],
543569
"name": "Homework 1",
544570
"description": "This is the first homework assignment.",
545-
"deadline": "2025-06-01 23:59:59",
571+
"deadline": "2025-06-03 23:59:59",
546572
"is_group_assign": False,
547-
"files": File_id_list,
573+
"files": File_id_list_individual,
548574
}
549575
response = client.post("/api/assignment", data=assignment_data, headers=headers)
550576
assert response.status_code == 200 or response.status_code == 201
@@ -553,6 +579,21 @@ def test_create_assignment():
553579
Assignment_id_list.append(data["assignment_id"])
554580
assert data["message"] == "Assignment created successfully."
555581

582+
assignment_data2 = {
583+
"course_id": Course_id_list[0],
584+
"name": "Group Project",
585+
"description": "This is a group project assignment.",
586+
"deadline": "2025-06-15 23:59:59",
587+
"is_group_assign": True,
588+
"files": File_id_list_group,
589+
}
590+
response2 = client.post("/api/assignment", data=assignment_data2, headers=headers)
591+
assert response2.status_code == 200 or response2.status_code == 201
592+
data2 = response2.json()
593+
assert "assignment_id" in data2
594+
Assignment_id_list.append(data2["assignment_id"])
595+
assert data2["message"] == "Assignment created successfully."
596+
556597
def test_get_assignments():
557598
student_token = Student_id_list[0]["token"]
558599
headers = {"Authorization": f"Bearer {student_token}"}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Dijkstra Project Description
2+
3+
## Overview
4+
5+
This project implements **Dijkstra's Algorithm** for finding the shortest path in a weighted graph. Dijkstra's Algorithm is a classic solution in computer science for solving the single-source shortest path problem, where the goal is to determine the minimum distance from a starting node to all other nodes in a graph with non-negative edge weights.
6+
7+
## Features
8+
9+
- **Graph Representation:** Supports both adjacency list and adjacency matrix representations.
10+
- **Customizable Input:** Users can define their own graphs and specify the source node.
11+
- **Efficient Implementation:** Utilizes a priority queue (min-heap) for optimal performance.
12+
- **Path Reconstruction:** Returns both the shortest distances and the actual paths taken.
13+
- **Error Handling:** Handles disconnected graphs and invalid input gracefully.
14+
15+
## How It Works
16+
17+
1. **Initialization:**
18+
All node distances are set to infinity except the source node, which is set to zero.
19+
20+
2. **Processing:**
21+
The algorithm repeatedly selects the node with the smallest known distance, updates the distances to its neighbors, and continues until all nodes have been processed.
22+
23+
3. **Result:**
24+
The shortest path from the source to every other node is computed and can be retrieved.
25+
26+
## Applications
27+
28+
- Network routing protocols (e.g., OSPF)
29+
- GPS navigation systems
30+
- Robotics and AI pathfinding
31+
- Project scheduling and resource allocation
32+
33+
## Example Usage
34+
35+
```python
36+
# Example usage in Python
37+
graph = {
38+
'A': {'B': 1, 'C': 4},
39+
'B': {'C': 2, 'D': 5},
40+
'C': {'D': 1},
41+
'D': {}
42+
}
43+
distances, paths = dijkstra(graph, 'A')
44+
print(distances) # {'A': 0, 'B': 1, 'C': 3, 'D': 4}
45+
print(paths) # {'A': ['A'], 'B': ['A', 'B'], ...}
46+
```
47+
48+
## References
49+
50+
- [Dijkstra's Algorithm - Wikipedia](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)
51+
- [Introduction to Algorithms, Cormen et al.]
52+
53+
---
54+
55+
*This project demonstrates a fundamental algorithm used in many real-world applications, emphasizing both correctness and efficiency.*
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import heapq
2+
3+
"""
4+
Demonstrative Python file for a group algorithm project.
5+
Implements Dijkstra's algorithm for finding the shortest path in a graph.
6+
"""
7+
8+
9+
def dijkstra(graph, start):
10+
"""
11+
Finds the shortest paths from start node to all other nodes in the graph.
12+
:param graph: dict, adjacency list representation {node: [(neighbor, weight), ...]}
13+
:param start: starting node
14+
:return: dict, shortest distances to each node
15+
"""
16+
distances = {node: float('inf') for node in graph}
17+
distances[start] = 0
18+
queue = [(0, start)]
19+
20+
while queue:
21+
current_distance, current_node = heapq.heappop(queue)
22+
if current_distance > distances[current_node]:
23+
continue
24+
for neighbor, weight in graph[current_node]:
25+
distance = current_distance + weight
26+
if distance < distances[neighbor]:
27+
distances[neighbor] = distance
28+
heapq.heappush(queue, (distance, neighbor))
29+
return distances
30+
31+
def main():
32+
# Example graph
33+
graph = {
34+
'A': [('B', 1), ('C', 4)],
35+
'B': [('A', 1), ('C', 2), ('D', 5)],
36+
'C': [('A', 4), ('B', 2), ('D', 1)],
37+
'D': [('B', 5), ('C', 1)]
38+
}
39+
start_node = 'A'
40+
print(f"Shortest distances from {start_node}:")
41+
distances = dijkstra(graph, start_node)
42+
for node, distance in distances.items():
43+
print(f" {node}: {distance}")
44+
45+
if __name__ == "__main__":
46+
main()

0 commit comments

Comments
 (0)