Skip to content

Commit

Permalink
more realistic dummy data
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjinDedic committed Mar 19, 2023
1 parent 8a3ae99 commit 728d776
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 42 deletions.
Binary file modified __pycache__/oo_package.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/test_api_auto.cpython-311-pytest-7.2.2.pyc
Binary file not shown.
Binary file modified database.db
Binary file not shown.
38 changes: 32 additions & 6 deletions oo_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from main import app
import json
import os
import time

client = TestClient(app=app)

Expand Down Expand Up @@ -77,6 +78,17 @@ def print_status(self):
scoreboard = Scoreboard(teams_data = teams_data)
scoreboard.print_status(self.team_name)

def live(self):
if self.__logged_in == False:
print('login first')
return
teams_data = self.get_teams_table()
scoreboard = Scoreboard(teams_data = teams_data)
scoreboard.print_rankings()
time.sleep(1)
os.system('cls' if os.name == 'nt' else 'clear')
self.live()

def interactive_menu(self):
if self.__logged_in == False:
print('login first')
Expand Down Expand Up @@ -118,16 +130,26 @@ def interactive_menu(self):


class Team():
def __init__(self, name, score, solved_questions=[]):
def __init__(self, name, score, solved_questions=[],color='\033[1m'):
self.name = name
self.score = score
self.solved_questions = solved_questions
self.color = color

def __str__(self):
if len(self.name) < 8:
answer = "TEAM NAME: "+ self.name + "\t\t SCORE: " + str(self.score) + "\t ANSWERED QUESTIONS: " + str(len(self.solved_questions))
c = self.color
b = '\033[1m'
nb = '\033[0m'
first = "| "+ c + "NAME: "+b+ self.name + nb
second = "| "+ c + "SCORE: " + b + str(self.score) + nb
third = "| "+ c + "ANSWERED QUESTIONS: " + b + str(len(self.solved_questions)) + nb + "\t|" + '\033[0m'

if len(first) <= 8:
answer = first + "\t\t" + second + "\t\t" + third
elif len(first) <= 16:
answer = first + "\t" + second + "\t\t" + third
else:
answer = "TEAM NAME: "+ self.name + "\t SCORE: " + str(self.score) + + "\t ANSWERED QUESTIONS: " + str(len(self.solved_questions))
answer = first + "\t\t\t" + second + "\t\t" + third
return answer


Expand All @@ -140,15 +162,19 @@ def __init__(self,teams_data):
def load_teams(self):
for team in self.teams_data['teams']:
solved_qs = json.loads(team[3])
self.teams.append(Team(name=team[0],score=team[2],solved_questions=solved_qs))
color = team[4]
self.teams.append(Team(name=team[0],score=team[2],solved_questions=solved_qs,color=color))

def print_rankings(self):
#this function needs to add colors to the teams which are read from the json file
#os.system('cls' if os.name == 'nt' else 'clear')
ordered_teams = sorted(self.teams, key=lambda x: x.score, reverse=True)
headline = '\033[1m' + 'HERE IS THE CURRENT SCOREBOARD' + '\033[0m'
print(headline.center(97,'.'))
print('-'*89)
for team in ordered_teams:
print(team)

print('-'*89)
def print_status(self,team_name):
for team in self.teams:
if team.name == team_name:
Expand Down
8 changes: 4 additions & 4 deletions reset_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
solved_qs = json.dumps([])
print(solved_qs, type(solved_qs))

c.execute("INSERT INTO teams VALUES (?, ?, ?, ?, ?, ?)", ('team1', 'team1', 0, solved_qs, 'red', False))
c.execute("INSERT INTO teams VALUES (?, ?, ?, ?, ?, ?)", ('team2', 'team2', 0, solved_qs, 'blue', False))
c.execute("INSERT INTO teams VALUES (?, ?, ?, ?, ?, ?)", ('team3', 'team3', 0, solved_qs, 'green', False))
c.execute("INSERT INTO teams VALUES (?, ?, ?, ?, ?, ?)", ('team4', 'team4', 0, solved_qs, 'yellow', False))
c.execute("INSERT INTO teams VALUES (?, ?, ?, ?, ?, ?)", ('Mount Waverley', 'abc', 0, solved_qs, '\033[33m', False))
c.execute("INSERT INTO teams VALUES (?, ?, ?, ?, ?, ?)", ('Box Hill', 'abc', 0, solved_qs, '\033[34m', False))
c.execute("INSERT INTO teams VALUES (?, ?, ?, ?, ?, ?)", ('Melbourne High', 'abc', 0, solved_qs, '\033[92m', False))
c.execute("INSERT INTO teams VALUES (?, ?, ?, ?, ?, ?)", ('Wantirna', 'abc', 0, solved_qs, '\033[35m', False))

conn.commit()
conn.close()
Expand Down
40 changes: 15 additions & 25 deletions test_api_auto.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from fastapi.testclient import TestClient
from main import app
import sys
import time

sys.path.insert(0, 'G:\My Drive\PROJECTS\FAST_API_TEST\APP')
from app import main
client = TestClient(app=app)


Expand All @@ -26,31 +27,31 @@ def test_question_contains_attachment():


def test_submit_answer():
response = client.post("/submit_answer", json={"id": "1", "answer": "Sanjin", "team_name": "team1"})
response = client.post("/submit_answer", json={"id": "1", "answer": "Sanjin", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert "Correct" in response.json()["message"]


def test_submit_second_answer():
response = client.post("/submit_answer", json={"id": "2", "answer": "38", "team_name": "team1"})
response = client.post("/submit_answer", json={"id": "2", "answer": "38", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert "Correct" in response.json()["message"]


def test_submit_answer_again():
response = client.post("/submit_answer", json={"id": "1", "answer": "Sanjin", "team_name": "team1"})
response = client.post("/submit_answer", json={"id": "1", "answer": "Sanjin", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert "Already solved" in response.json()["message"]


def test_submit_answer_wrong():
response = client.post("/submit_answer", json={"id": "1", "answer": "Bob", "team_name": "team1"})
response = client.post("/submit_answer", json={"id": "1", "answer": "Bob", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert response.json() == {"message": "Incorrect"}


def test_submit_answer_nonexisting():
response = client.post("/submit_answer", json={"id": "10000", "answer": "Bob", "team_name": "team1"})
response = client.post("/submit_answer", json={"id": "10000", "answer": "Bob", "team_name": "Mount Waverley"})
assert response.status_code == 200
assert response.json() == {"message": "Question not found"}

Expand All @@ -68,13 +69,13 @@ def test_download_starter_code():


def test_valid_login():
response = client.post("/login", json={"name": "team1", "password": "team1"})
response = client.post("/login", json={"name": "Mount Waverley", "password": "abc"})
assert response.status_code == 200
assert "Login successful" in response.json()["message"]


def test_invalid_login():
response = client.post("/login", json={"name": "team1", "password": "xxxxx"})
response = client.post("/login", json={"name": "Mount Waverley", "password": "xxxxx"})
print(response.json())
assert response.status_code == 200
assert "Login failed" in response.json()["message"]
Expand All @@ -83,26 +84,16 @@ def test_invalid_login():
def test_get_teams_table():
response = client.get("/get_teams_table")
assert response.status_code == 200
assert "team1" in response.json()["teams"][0][0]
assert "team2" in response.json()["teams"][1][0]
assert "team3" in response.json()["teams"][2][0]
assert "team4" in response.json()["teams"][3][0]
assert "Mount Waverley" in response.json()["teams"][0][0]
assert "Box Hill" in response.json()["teams"][1][0]
assert "Melbourne High" in response.json()["teams"][2][0]
assert "Wantirna" in response.json()["teams"][3][0]
assert response.json()["teams"][0][2] == 20
assert response.json()["teams"][1][2] == 0
assert response.json()["teams"][2][2] == 0
assert response.json()["teams"][3][2] == 0
assert response.json()["teams"][0][3] == '[1, 2]'

def submit_answer1():
response = client.post("/submit_answer", json={"id": "1", "answer": "Sanjin", "team_name": "team1"})
print(response.json())
print(response.status_code)
print(response.content)

def get_teams_table():
response = client.get("/get_teams_table")
print(response.json())


#this always needs to be the last test
def reset_db_just_run_file():
Expand All @@ -112,5 +103,4 @@ def reset_db_just_run_file():
time.sleep(1)


reset_db_just_run_file()
get_teams_table()
reset_db_just_run_file()
5 changes: 5 additions & 0 deletions test_live_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from oo_package import Quiz

competition = Quiz(team_name='Mount Waverley',team_password='abc')

competition.interactive_menu()
14 changes: 7 additions & 7 deletions test_oo_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
import time

time.sleep(1)
with open("reset_db.py") as f:
exec(f.read())
#with open("reset_db.py") as f:
# exec(f.read())
time.sleep(1)


competition = Quiz(team_name='team1',team_password='team1')

competition = Quiz(team_name='Mount Waverley',team_password='abc')
print(competition.get_question(1))
competition.submit_answer(1,'Sanjin')
competition.print_rankings()
time.sleep(2)


competition = Quiz(team_name='team2',team_password='team2')
competition = Quiz(team_name='Box Hill',team_password='abc')
competition.submit_answer(2,'38')
competition.submit_answer(3,'Python')
competition.print_rankings()
time.sleep(2)

competition = Quiz(team_name='team3',team_password='team3')
competition = Quiz(team_name='Melbourne High',team_password='abc')
competition.submit_answer(2,'38')
competition.submit_answer(3,'Python')
sol = '''cerpxazeceaqacth
Expand All @@ -31,7 +31,7 @@
time.sleep(2)


competition = Quiz(team_name='team4',team_password='team4')
competition = Quiz(team_name='Wantirna',team_password='abc')
competition.submit_answer(1,'Sanjin')
competition.submit_answer(2,'38')
competition.submit_answer(3,'Python')
Expand Down

0 comments on commit 728d776

Please sign in to comment.