Skip to content

Commit 7c02e18

Browse files
initial commit
0 parents  commit 7c02e18

16 files changed

+501
-0
lines changed

Dockerfile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM python:3.9
2+
3+
WORKDIR /code
4+
5+
COPY ./setup.py /code/setup.py
6+
7+
COPY ./requirements.txt /code/requirements.txt
8+
9+
COPY ./README.md /code/README.md
10+
11+
COPY ./src /code/src
12+
13+
RUN pip install /code
14+
15+
COPY ./controller /code/controller
16+
17+
CMD ["uvicorn", "controller.controller:app", "--host", "0.0.0.0", "--port", "80"]

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TP2

controller/__init__.py

Whitespace-only changes.

controller/controller.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Fast API controller to query the module
3+
4+
5+
"""
6+
from fastapi import FastAPI
7+
from src.wallet import Wallet
8+
9+
app = FastAPI()
10+
11+
12+
@app.get("/")
13+
async def get_module():
14+
"""
15+
Query module
16+
"""
17+
return {
18+
"module": {
19+
"name": "TP2",
20+
"description": "Un exercice sur le CI/CD",
21+
"version": "1.0",
22+
}
23+
}
24+
25+
26+
@app.get("/wallet-with-100")
27+
async def get_wallet():
28+
"""
29+
Query module
30+
"""
31+
wallet = Wallet(100)
32+
return {"balance": wallet.balance}

instructions.md

+337
Large diffs are not rendered by default.

instructions.pdf

243 KB
Binary file not shown.

requirements.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pre-commit==2.20.0
2+
pytest==7.2.0
3+
pytest-cov==4.0.0
4+
coverage==7.0.5
5+
fastapi==0.92.0
6+
uvicorn==0.20.0

setup.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from setuptools import find_packages, setup
2+
3+
setup(
4+
name="TP2",
5+
version="0.1.0",
6+
description="Travail pratique sur la qualité de code et le CI/CD",
7+
long_description=open("README.md").read(),
8+
author="prenom.nom",
9+
package_dir={"TP2": "src"},
10+
install_requires=open("requirements.txt").readlines(),
11+
)

src/__init__.py

Whitespace-only changes.

src/wallet.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Module that lets you manipulate a wallet"""
2+
3+
4+
class InsufficientAmount(Exception):
5+
"""Exception triggered when you try to spend more money than a wallet has"""
6+
7+
pass
8+
9+
10+
class Wallet(object):
11+
"""Wallet class that lets your create a Wallet and add or spend money"""
12+
13+
def __init__(self, initial_amount=0):
14+
self.balance = initial_amount
15+
16+
def spend_cash(self, amount, deferred=False):
17+
"""Removes the specified amount of money from the current Wallet object"""
18+
if self.balance < amount:
19+
raise InsufficientAmount(f"Not enough available to spend {amount}")
20+
21+
self.balance -= amount
22+
23+
def add_cash(self, amount):
24+
"""Adds the specified amount of money to the current Wallet object"""
25+
self.balance += amount
26+
27+
def spend_money(self, amount, deferred=False):
28+
"""Removes the specified amount of money from the current Wallet object"""
29+
if self.balance < amount:
30+
raise InsufficientAmount(f"Not enough available to spend {amount}")
31+
32+
self.balance -= amount

test_wheel.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# import depuis les sources
2+
from src.wallet import Wallet
3+
4+
# import depuis le .whl TP2
5+
# from TP2.wallet import Wallet
6+
7+
if __name__ == "__main__":
8+
my_wallet = Wallet(100)
9+
my_wallet.add_cash(120)
10+
print("Congratulations. You successfully installed the TP2 package !")

tests/__init__.py

Whitespace-only changes.

tests/fixtures/__init__.py

Whitespace-only changes.

tests/fixtures/wallet_fixtures.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Necessary fixtures to test the wallet.py package"""
2+
import pytest
3+
from src.wallet import Wallet
4+
5+
# Wallet Fixtures
6+
@pytest.fixture
7+
def empty_wallet():
8+
"""Returns a Wallet instance with a zero balance"""
9+
return Wallet()
10+
11+
12+
@pytest.fixture
13+
def wallet():
14+
"""Returns a Wallet instance with a balance of 20"""
15+
return Wallet(20)
16+
17+
18+
@pytest.fixture
19+
def wallet_with_50_balance():
20+
"""Returns a Wallet instance with a balance of 50"""
21+
return Wallet(50)

tests/unit/__init__.py

Whitespace-only changes.

tests/unit/test_wallet.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Unit tests for wallet.py package
3+
"""
4+
import pytest
5+
from src.wallet import Wallet, InsufficientAmount
6+
from tests.fixtures.wallet_fixtures import empty_wallet, wallet
7+
8+
9+
def test_default_initial_amount(empty_wallet):
10+
"""Tests that initial balance of empty_wallet is 0"""
11+
assert empty_wallet.balance == 0
12+
13+
14+
def test_setting_initial_amount(wallet):
15+
"""Tests that initial ballance of wallet is 20"""
16+
assert wallet.balance == 20
17+
18+
19+
def test_wallet_add_cash(wallet):
20+
"""Tests add_cash funciton"""
21+
wallet.add_cash(80)
22+
assert wallet.balance == 100
23+
24+
25+
def test_wallet_spend_cash(wallet):
26+
"""Tests spend_cash function"""
27+
wallet.spend_cash(10)
28+
assert wallet.balance == 10
29+
30+
31+
def test_wallet_spend_cash_raises_exception_on_insufficient_amount(empty_wallet):
32+
"""Tests that InsufficientAmount exception is raised when trying to spend more thant the wallet's balance"""
33+
with pytest.raises(InsufficientAmount):
34+
empty_wallet.spend_cash(100)

0 commit comments

Comments
 (0)