Skip to content

Commit

Permalink
Merge pull request #48 from CSSE6400/47-keep-fix-tests
Browse files Browse the repository at this point in the history
Completed test.
  • Loading branch information
EscapedShark authored Jun 2, 2024
2 parents d55b770 + 80b69d8 commit 26b6d81
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 23 deletions.
37 changes: 14 additions & 23 deletions backend/brewbucks/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,27 +447,21 @@ def test_get_active_orders(client, sample_user, sample_orders):


def test_get_finished_orders(client, sample_user, sample_orders):

response = client.get("/api/v1/orders/finished", json={"user_id": sample_user.user_id})
response = client.get(f"/api/v1/orders/finished?user_id={sample_user.user_id}")
assert response.status_code == 200
finished_orders = response.json
assert isinstance(finished_orders, list)
assert len(finished_orders) == 1
for order in finished_orders:
assert order["order_status"] == "Completed"
assert order["user_id"] == sample_user.user_id
assert len(finished_orders) == 1



def test_get_making_orders(client, sample_user, sample_orders):

response = client.get("/api/v1/orders/making", json={"user_id": sample_user.user_id})
response = client.get(f"/api/v1/orders/making?user_id={sample_user.user_id}")
assert response.status_code == 200
making_orders = response.json
assert isinstance(making_orders, list)
assert len(making_orders) == 2
for order in making_orders:
assert order["order_status"] == "Making"
assert order["user_id"] == sample_user.user_id
assert len(making_orders) == 2



def test_get_making_orders_all(client, sample_orders):
Expand All @@ -482,10 +476,9 @@ def test_get_making_orders_all(client, sample_orders):


def test_create_and_get_rewards(client, sample_user):

reward_data = {
"customer_id": sample_user.user_id,
"total_points": 300
"total_points": 300
}
new_reward = Rewards(
customer_id=reward_data["customer_id"],
Expand All @@ -497,18 +490,16 @@ def test_create_and_get_rewards(client, sample_user):
db.session.add(new_reward)
db.session.commit()


with client.application.app_context():
reward = Rewards.query.filter_by(customer_id=sample_user.user_id).first()
assert reward is not None
assert reward.total_points == 300


response = client.get(f"/api/v1/users/rewards", json={"user_id": sample_user.user_id})
response = client.get(f"/api/v1/users/rewards?user_id={sample_user.user_id}")
assert response.status_code == 200
data = response.json
assert "User points" in data
assert len(data["User points"]) == 1
assert data["User points"][0] == 300
assert len(data["Rewards details"]) == 1
assert data["Rewards details"][0]["total_points"] == 300
response_data = response.json
assert "User points" in response_data
assert "Rewards details" in response_data
assert len(response_data["User points"]) > 0
assert response_data["User points"][0] == 300

42 changes: 42 additions & 0 deletions k6_test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# macOS
brew install k6

# Windows (Using Chocolatey)
choco install k6

# Linux
sudo apt install k6



k6 run sign_up_and_delete_user.js
k6 run get_user_info.js
k6 run create_menu_item.js
k6 run get_menu_item.js
k6 run create_order.js

1. User Sign-Up and Deletion (sign_up_and_delete_user.js)
Total Requests: 7116
Successful Requests: 100.00%
Average HTTP Request Duration: 10.42ms
HTTP Request Failure Rate: 0.00%
2. Get User Info (get_user_info.js)
Total Requests: 3605
Successful Requests: 100.00%
Average HTTP Request Duration: 6.62ms
HTTP Request Failure Rate: 0.00%
3. Create Menu Item (create_menu_item.js)
Total Requests: 3606
Successful Requests: 100.00%
Average HTTP Request Duration: 7.34ms
HTTP Request Failure Rate: 0.00%
4. Get Menu Items (get_menu_item.js)
Total Requests: 1260
Successful Requests: 78.05%
Average HTTP Request Duration: 1.89s
HTTP Request Failure Rate: 0.00%
5. Create Order (create_order.js)
Total Requests: 3584
Successful Requests: 100.00%
Average HTTP Request Duration: 13.4ms
HTTP Request Failure Rate: 0.00%
42 changes: 42 additions & 0 deletions k6_test/create_menu_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import http from 'k6/http';
import { check, sleep } from 'k6';

export function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}


export const options = {
stages: [
{ duration: '1m', target: 20 },
{ duration: '2m', target: 20 },
{ duration: '1m', target: 0 },
],
};

export default function () {
let itemData = JSON.stringify({
user_id: 1,
name: `Item_${uuidv4()}`,
description: 'Test item',
price: 10.0,
orderable: true,
});

let createItemRes = http.post('http://localhost:8080/api/v1/menu_items', itemData, {
headers: { 'Content-Type': 'application/json' },
});

check(createItemRes, {
'item creation status is 201': (r) => r.status === 201,
});

let itemId = createItemRes.json().item_id;


sleep(1);
}
31 changes: 31 additions & 0 deletions k6_test/create_order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
stages: [
{ duration: '1m', target: 20 }, // Ramp-up to 20 users over 1 minute
{ duration: '2m', target: 20 }, // Stay at 20 users for 2 minutes
{ duration: '1m', target: 0 }, // Ramp-down to 0 users over 1 minute
],
};

export default function () {
let orderData = JSON.stringify({
user_id: 1,
order_items: [
{ item_id: 1, quantity: 2 },
{ item_id: 2, quantity: 1 },
],
rewards_added: 5,
});

let createOrderRes = http.post('http://localhost:8080/api/v1/users/orders', orderData, {
headers: { 'Content-Type': 'application/json' },
});

check(createOrderRes, {
'order creation status is 201': (r) => r.status === 201,
});

sleep(1);
}
23 changes: 23 additions & 0 deletions k6_test/get_menu_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
stages: [
{ duration: '1m', target: 20 },
{ duration: '2m', target: 20 },
{ duration: '1m', target: 0 },
],
};

export default function () {
let res = http.get('http://localhost:8080/api/v1/menu_items', {
headers: { 'Content-Type': 'application/json' },
});

check(res, {
'status is 200': (r) => r.status === 200,
'response time is < 500ms': (r) => r.timings.duration < 500,
});

sleep(1);
}
23 changes: 23 additions & 0 deletions k6_test/get_user_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
stages: [
{ duration: '1m', target: 20 },
{ duration: '2m', target: 20 },
{ duration: '1m', target: 0 },
],
};

export default function () {
let res = http.get('http://localhost:8080/api/v1/users/1', {
headers: { 'Content-Type': 'application/json' },
});

check(res, {
'status is 200': (r) => r.status === 200,
'response time is < 500ms': (r) => r.timings.duration < 500,
});

sleep(1);
}
Loading

0 comments on commit 26b6d81

Please sign in to comment.