def tearDownClass(cls): """resets the database changes""" cursor = User.db_connect() # connects to db cursor.execute( """UPDATE [Users] SET [NumberOfWinnings]=0 WHERE [ID]=0""") cursor.execute("""UPDATE [Users] SET [NumberOfGames]=0 WHERE [ID]=0""") cursor.commit() # executes SQL command
def test_statistics_won(self): """testing that the statistics are updated accordingly if the user won""" cursor = User.db_connect() cursor.execute("SELECT * FROM [Users] WHERE [ID]=0" ) # selects the first row in the db (test row) rows = cursor.fetchall() User.add_score(0, won=True) cursor = User.db_connect() cursor.execute("SELECT * FROM [Users] WHERE [ID]=0" ) # selects the updated first row in the db (test row) updated_rows = cursor.fetchall() self.assertEqual(updated_rows[0].NumberOfWinnings, rows[0].NumberOfWinnings + 1)
def test_db_connection(self): """testing the connection to the database""" cursor = User.db_connect() # connects to db cursor.execute("SELECT * FROM [Users] WHERE [ID]=0" ) # selects the first row in the db (test row) rows = cursor.fetchall() self.assertEqual(rows[0].FirstName, 'testFirstName') self.assertEqual(rows[0].LastName, 'testLastName') self.assertEqual(rows[0].Username, 'testUsername')
def test_statistics_lost(self): """testing that the statistics are updated accordingly if the user lost""" cursor = User.db_connect() cursor.execute("SELECT * FROM [Users] WHERE [ID]=0" ) # selects the first row in the db (test row) rows = cursor.fetchall() User.add_score(0, won=False) cursor = User.db_connect() cursor.execute("SELECT * FROM [Users] WHERE [ID]=0" ) # selects the updated first row in the db (test row) updated_rows = cursor.fetchall() self.assertEqual(updated_rows[0].NumberOfGames, rows[0].NumberOfGames + 1) # the number of games should grow by 1 self.assertEqual(updated_rows[0].NumberOfWinnings, rows[0].NumberOfWinnings)
def stats_screen(): """in this screen the user can see the top 5 players in the game and watch his game statistics """ cursor = User.db_connect() # connecting to the database cursor.execute( "SELECT TOP(5) [Username], [NumberofWinnings] FROM [Users] WHERE [ID]!=0 ORDER BY [NumberofWinnings]" " DESC" ) # selects the top 5 players with the highest number of winnings rows = cursor.fetchall() # gets the values that were selected stats_win = ThemedTk( theme="Smog") # created the statistics window themed "smog" style = ttk.Style(stats_win) style.configure("Treeview.Heading", font=("Calibri Bold", 14)) # styles the tables headings style.configure("Treeview", font=("Calibri", 12)) # styles the tables fields ttk.Label(stats_win, text='Top Players', font=("Cooper black", 32), foreground='black', background='light blue')\ .pack(pady=30) frame = ttk.Frame(stats_win) # frames the top players table frame.pack(padx=20, pady=20) table = ttk.Treeview(frame, columns=(1, 2), show="headings", height='5') # creates the table table.column(1, anchor="c") table.column(2, anchor="c") table.pack(fill="both", expand=True) # centers the table columns and adds the table to the window table.heading(1, text="Player") table.heading(2, text="Number of Winnings") # writes the table headings for row in rows: table.insert( "", "end", values=list(row) ) # inserts the values that were given by the database to the table stats_win.geometry('540x540') stats_win.title("© Battleship by Shani Daniel ©") stats_win.configure(background='light blue') num_games = GameUI.logged_in_user.num_of_games # gets the number of games the current user has played from the db num_wins = GameUI.logged_in_user.num_of_wins # gets the number of games the current user has won from the db if num_games != 0: win_per = int( 100 * (num_wins / num_games)) # calculates the winnings percentage else: # if the user's number of games is zero, return 0 win_per = 0 ttk.Label(stats_win, text='Your Statistics:', font=("Cooper Black", 20), foreground='black', background='light blue').pack(pady=20) ttk.Label(stats_win, text='Number of games you played: %s' % num_games, font=("Calibri Bold", 16), foreground='black', background='light blue').pack(pady=5) ttk.Label(stats_win, text='Number of games you won: %s' % num_wins, font=("Calibri Bold", 16), foreground='black', background='light blue').pack(pady=5) ttk.Label(stats_win, text='Winning percentage: %s%%' % win_per, font=("Calibri Bold", 16), foreground='black', background='light blue').pack(pady=5) # creates the labels and adds the statistics we got for the current user stats_win.mainloop()