refactored code
This commit is contained in:
1 parent
2faa94ec14
commit
c1c13022ee
4 files changed
+83
-7
No files matched your search
Binary file not shown.
@@ -4,6 +4,12 @@ from pathlib import Path
|
||||
|
||||
|
||||
class FileManager:
|
||||
"""
|
||||
Cette classe fait plusieurs choses:
|
||||
- 4 méthodes
|
||||
- lire/écrire fichier
|
||||
- compresser/décompresser fichier
|
||||
"""
|
||||
def __init__(self, filename: str | Path):
|
||||
self.path = Path(filename)
|
||||
|
||||
@@ -20,3 +26,28 @@ class FileManager:
|
||||
def decompress(self) -> None:
|
||||
decompressed_data = zlib.decompress(self.path.read_bytes())
|
||||
self.path.write_bytes(decompressed_data)
|
||||
|
||||
|
||||
class RawFileManager:
|
||||
def __init__(self, filename: str | Path):
|
||||
self.path = Path(filename)
|
||||
|
||||
def read(self) -> str:
|
||||
return self.path.read_text("utf-8")
|
||||
|
||||
def write(self, data: str) -> None:
|
||||
self.path.write_text(data, "utf-8")
|
||||
|
||||
|
||||
class CompressionFileManager:
|
||||
def __init__(self, filename: str | Path):
|
||||
self.path = Path(filename)
|
||||
|
||||
def compress(self) -> None:
|
||||
compressed_data = zlib.compress(self.path.read_bytes())
|
||||
self.path.write_bytes(compressed_data)
|
||||
|
||||
def decompress(self) -> None:
|
||||
decompressed_data = zlib.decompress(self.path.read_bytes())
|
||||
self.path.write_bytes(decompressed_data)
|
||||
|
||||
@@ -27,11 +27,38 @@ users = [
|
||||
]
|
||||
|
||||
|
||||
def get_user(user_name: str, is_admin: bool = False, consider_old_users: bool=False) -> User | User2 | Admin | None:
|
||||
def verify_admin(user: User2 | User) -> bool:
|
||||
return isinstance(user, User2) and user.is_admin
|
||||
|
||||
def verify_regular_user(user: User2 | User) -> bool:
|
||||
return isinstance(user, User2) and user.is_admin is False
|
||||
|
||||
|
||||
def get_admin(user_name: str) -> Admin | None:
|
||||
for user in users:
|
||||
if user_name == user.name:
|
||||
if isinstance(user, User) and not consider_old_users:
|
||||
continue
|
||||
if isinstance(user, User2) and user.is_admin and not is_admin:
|
||||
continue
|
||||
if verify_admin(user) and user.name == user_name:
|
||||
return user
|
||||
|
||||
|
||||
def get_old_user(user_name: str) -> User:
|
||||
for user in users:
|
||||
if isinstance(user, User) and user.name == user_name:
|
||||
return user
|
||||
|
||||
|
||||
def get_regular_user(user_name: str) -> User2:
|
||||
for user in users:
|
||||
if verify_regular_user(user) and user.name == user_name:
|
||||
return user
|
||||
|
||||
|
||||
def get_user(user_name: str,
|
||||
is_admin: bool = False,
|
||||
consider_old_users: bool = False) -> User | User2 | Admin | None:
|
||||
if is_admin:
|
||||
return get_admin(user_name)
|
||||
if consider_old_users:
|
||||
return get_old_user(user_name)
|
||||
return get_regular_user(user_name)
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from solid.single_responsability.simple_function import get_user
|
||||
from solid.single_responsability.simple_function import get_user, get_admin, get_old_user, get_regular_user
|
||||
|
||||
|
||||
class TestGetUser:
|
||||
@@ -16,3 +16,21 @@ class TestGetUser:
|
||||
assert get_user("jonathan", is_admin=True) is None
|
||||
assert get_user("jonathan", consider_old_users=True) is not None
|
||||
assert get_user("jonathan", consider_old_users=True).name == "jonathan"
|
||||
|
||||
|
||||
|
||||
|
||||
class TestRefactoredGetUser:
|
||||
def test_happy_path(self):
|
||||
assert get_regular_user("marie-eve") is None
|
||||
assert get_regular_user("walid").name == 'walid'
|
||||
|
||||
def test_admin(self):
|
||||
assert get_admin("walid") is None
|
||||
assert get_admin("tristan") is not None
|
||||
assert get_admin("tristan").name == "tristan"
|
||||
|
||||
def test_old_users(self):
|
||||
assert get_old_user("didier") is None
|
||||
assert get_old_user("jonathan") is not None
|
||||
assert get_old_user("jonathan").name == "jonathan"
|
||||
Reference in new issue
Block a user