Skip to content

Commit

Permalink
Fix(profile save): Fixed failing tests and prettified profile_save a …
Browse files Browse the repository at this point in the history
…little bit
  • Loading branch information
Petzys committed Mar 18, 2024
1 parent 8278199 commit 8c44ac5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 33 deletions.
1 change: 0 additions & 1 deletion Client/Data/test/test_profiles.json

This file was deleted.

21 changes: 6 additions & 15 deletions Client/profile_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ class Profile:
This class is used to handle the profiles.json file. It is used to get, set, and add profiles to the file.
"""


@staticmethod
def get_profiles():
global path
"""
This method returns all the profiles from the file
:return: An array of all profiles
"""
if exists(path):
with open(path, 'r') as file:
data = json.load(file)
if not data:
return [], 0
output = []
profile_data = data[0]
selected = data[1]
Expand All @@ -29,26 +30,16 @@ def get_profiles():
else:
return [], 0

def set_profiles( players: list, selected: int):
global path
"""
This method sets the profile name and/or color by the uuid
:param profile_uuid:
:param profile_name:
:param profile_color:
"""

#try:
@staticmethod
def set_profiles(players: list, selected: int):
with open(path, 'w') as file:
entry = []
for player in players:
entry.append(player.as_dict())
json.dump([entry, selected], file)
#except:
# raise RuntimeError("json error: Make sure profiles.json is formatted correctly")

@staticmethod
def delete_all_profiles():
global path
"""
This method deletes all profiles
"""
Expand Down
26 changes: 9 additions & 17 deletions Client/test_profile_save.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import unittest
from Client.profile_save import Profile
import os
from Server.player import Player
from os.path import exists

from uuid import uuid4

class TestProfileSave(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.profile = Profile(os.path.abspath("Client/Data/test/test_profiles.json"))
cls.player1 = Player("test", 0, "test", False)
cls.player2 = Player("test2", 0, "test2", False)

def setUp(self):
self.profile.delete_all_profiles()
self.player1 = Player("test", 0, uuid4(), False)
self.player2 = Player("test2", 0, uuid4(), False)
Profile.delete_all_profiles()

def test_all(self):
if exists(self.profile.path):
os.remove(self.profile.path)
data = [self.player1, self.player2]
self.profile.set_profiles(data)
self.assertEqual(self.profile.get_profiles(), data)
self.profile.delete_all_profiles()
self.assertEqual(self.profile.get_profiles(), [])
data = ([self.player1, self.player2], 0)
Profile.set_profiles(data[0], data[1])
self.assertEqual(Profile.get_profiles(), data)
Profile.delete_all_profiles()
self.assertEqual(Profile.get_profiles(), ([], 0))

0 comments on commit 8c44ac5

Please sign in to comment.