Skip to content

Commit de20034

Browse files
authored
Merge branch 'main' into main
2 parents c5d902c + f106fe3 commit de20034

File tree

9 files changed

+187
-7
lines changed

9 files changed

+187
-7
lines changed

.github/scripts/test_homework3.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# .github/scripts/test_homework3.py
2+
from fastapi.testclient import TestClient
3+
4+
try:
5+
from main import app # Assuming the FastAPI application is in main.py
6+
except ImportError:
7+
from app.main import app # If unable to import from the main module, try importing from app.main
8+
9+
client = TestClient(app)
10+
11+
# Test the correctness of GET /items/{item_id}
12+
def test_get_item_valid():
13+
response = client.get("/items/123?q=test_query&sort_order=asc")
14+
assert response.status_code == 200
15+
assert response.json() == {
16+
"item_id": 123,
17+
"description": "This is a sample item that matches the query test_query",
18+
"sort_order": "asc"
19+
}
20+
21+
# Test GET /items/{item_id} with invalid path parameter
22+
def test_get_item_invalid_id():
23+
response = client.get("/items/1001")
24+
assert response.status_code == 422 # Should return 422 Unprocessable Entity
25+
assert "detail" in response.json()
26+
27+
# Test GET /items/{item_id} with invalid query parameter q
28+
def test_get_item_invalid_query():
29+
response = client.get("/items/123?q=ab&sort_order=asc")
30+
assert response.status_code == 422 # Should return 422 Unprocessable Entity
31+
assert "detail" in response.json()
32+
33+
# Test GET /items/{item_id} without query parameters
34+
def test_get_item_no_query():
35+
response = client.get("/items/123")
36+
assert response.status_code == 200
37+
assert response.json() == {
38+
"item_id": 123,
39+
"description": "This is a sample item.",
40+
"sort_order": "asc"
41+
}
42+
43+
# Test PUT /items/{item_id} to update item data
44+
def test_put_item_valid():
45+
data = {
46+
"name": "Updated Item",
47+
"description": "Updated item description",
48+
"price": 20.0,
49+
"tax": 2.5
50+
}
51+
response = client.put("/items/123", json=data)
52+
assert response.status_code == 200
53+
assert response.json() == {
54+
"item_id": 123,
55+
"name": "Updated Item",
56+
"description": "Updated item description",
57+
"price": 20.0,
58+
"tax": 2.5
59+
}
60+
61+
# Test PUT /items/{item_id} with valid query parameter q
62+
def test_put_item_with_query():
63+
data = {
64+
"name": "Updated Item",
65+
"description": "Updated item description",
66+
"price": 20.0,
67+
"tax": 2.5
68+
}
69+
response = client.put("/items/123?q=test_query", json=data)
70+
assert response.status_code == 200
71+
assert response.json() == {
72+
"item_id": 123,
73+
"name": "Updated Item",
74+
"description": "Updated item description",
75+
"price": 20.0,
76+
"tax": 2.5,
77+
"q": "test_query"
78+
}
79+
80+
# Test PUT /items/{item_id} with invalid path parameter
81+
def test_put_item_invalid_id():
82+
data = {
83+
"name": "Updated Item",
84+
"description": "Updated item description",
85+
"price": 20.0,
86+
"tax": 2.5
87+
}
88+
response = client.put("/items/1001", json=data)
89+
assert response.status_code == 422
90+
assert "detail" in response.json()
91+
92+
# Test PUT /items/{item_id} with invalid query parameter q
93+
def test_put_item_invalid_query():
94+
data = {
95+
"name": "Updated Item",
96+
"description": "Updated item description",
97+
"price": 20.0,
98+
"tax": 2.5
99+
}
100+
response = client.put("/items/123?q=ab", json=data)
101+
assert response.status_code == 422
102+
assert "detail" in response.json()

.github/workflows/test_homework3.yml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Test Homework 3
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'members/**'
7+
8+
permissions:
9+
contents: read
10+
pull-requests: read
11+
12+
jobs:
13+
test:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v3
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Set up Python 3.10
23+
uses: actions/setup-python@v4
24+
with:
25+
python-version: '3.10'
26+
27+
- name: Install global dependencies
28+
run: |
29+
pip install fastapi pytest uvicorn
30+
31+
- name: Get list of changed submodules
32+
id: changed_submodules
33+
uses: actions/github-script@v6
34+
with:
35+
script: |
36+
const pull_number = context.payload.pull_request.number;
37+
const owner = context.repo.owner;
38+
const repo = context.repo.repo;
39+
const { data: files } = await github.rest.pulls.listFiles({
40+
owner,
41+
repo,
42+
pull_number,
43+
});
44+
const changedSubmodules = files
45+
.map(file => file.filename)
46+
.filter(filename => filename.startsWith('members/'))
47+
.map(filename => filename.split('/')[1])
48+
.filter((value, index, self) => self.indexOf(value) === index);
49+
50+
if (changedSubmodules.length > 1) {
51+
core.setFailed(`More than one submodule changed: ${changedSubmodules.join(', ')}`);
52+
} else if (changedSubmodules.length === 0) {
53+
core.setFailed('No submodules were changed.');
54+
} else {
55+
core.setOutput('submodule', changedSubmodules[0]);
56+
}
57+
58+
- name: Install dependencies
59+
if: steps.changed_submodules.outputs.submodule != ''
60+
run: |
61+
submodule=${{ steps.changed_submodules.outputs.submodule }}
62+
echo "Testing $submodule"
63+
64+
- name: Install dependencies
65+
if: steps.changed_submodules.outputs.submodule != ''
66+
run: |
67+
submodule=${{ steps.changed_submodules.outputs.submodule }}
68+
git submodule update --init --recursive members/$submodule
69+
pip install -r members/$submodule/requirements.txt
70+
71+
- name: Test submodule
72+
if: steps.changed_submodules.outputs.submodule != ''
73+
run: |
74+
submodule=${{ steps.changed_submodules.outputs.submodule }}
75+
cd members/$submodule
76+
cp ../../.github/scripts/test_homework3.py .
77+
pytest test_homework3.py
78+

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
[submodule "members/Lyc1103"]
6262
path = members/Lyc1103
6363
url = https://github.com/Lyc1103/SDC-backend-training.git
64-
6564
[submodule "members/weng1002"]
6665
path = members/weng1002
6766
url = https://github.com/Weng1002/sdc-backend-training.git
67+

members/AlHIO

Submodule AlHIO updated 1 file

members/YourFath3r

Submodule YourFath3r updated 1 file

members/gigi-igig

Submodule gigi-igig updated 1 file

members/ian60509

Submodule ian60509 updated 1 file

members/tiffanyfan1015

0 commit comments

Comments
 (0)