mirror of
https://github.com/newtype256/esgi-devops.git
synced 2025-06-04 04:56:26 +02:00
cours: n°2
This commit is contained in:
parent
2dbcc1d3a6
commit
c8fafe2ce3
13 changed files with 230 additions and 0 deletions
1
illustrations/1_tester/.envrc
Normal file
1
illustrations/1_tester/.envrc
Normal file
|
@ -0,0 +1 @@
|
|||
use nix
|
24
illustrations/1_tester/default.nix
Normal file
24
illustrations/1_tester/default.nix
Normal file
|
@ -0,0 +1,24 @@
|
|||
let nixpkgs = <nixpkgs>;
|
||||
in
|
||||
{
|
||||
pkgs ? import nixpkgs {}
|
||||
, pythonPackageName ? "python3"
|
||||
, python ? pkgs.${pythonPackageName}}:
|
||||
|
||||
rec {
|
||||
pythonDependencies = (python.withPackages
|
||||
(ps: [
|
||||
ps.pytest
|
||||
ps.pytest_xdist
|
||||
ps.pytest-mock
|
||||
ps.pytestcov
|
||||
ps.hypothesis
|
||||
]));
|
||||
|
||||
shell = pkgs.mkShell {
|
||||
buildInputs = with pkgs; [
|
||||
pythonDependencies
|
||||
];
|
||||
};
|
||||
}
|
||||
|
2
illustrations/1_tester/pytest.ini
Normal file
2
illustrations/1_tester/pytest.ini
Normal file
|
@ -0,0 +1,2 @@
|
|||
[pytest]
|
||||
addopts = --doctest-modules
|
1
illustrations/1_tester/shell.nix
Normal file
1
illustrations/1_tester/shell.nix
Normal file
|
@ -0,0 +1 @@
|
|||
(import ./default.nix {}).shell
|
24
illustrations/1_tester/test_integration.py
Normal file
24
illustrations/1_tester/test_integration.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
import os
|
||||
import tempfile
|
||||
|
||||
class UnixFS:
|
||||
@staticmethod
|
||||
def rm(filename):
|
||||
os.remove(filename)
|
||||
|
||||
# Sans intégration
|
||||
|
||||
def test_monkeypatche(mocker):
|
||||
mocker.patch('os.remove')
|
||||
UnixFS.rm('contrôle complet')
|
||||
os.remove.assert_called_once_with('contrôle complet')
|
||||
|
||||
# Avec intégration
|
||||
|
||||
def test_db_with_db():
|
||||
with tempfile.NamedTemporaryFile(delete=False) as fp:
|
||||
fp.write(b'Bonjour!')
|
||||
fp.seek(0)
|
||||
assert fp.read() == b'Bonjour!'
|
||||
UnixFS.rm(fp.name)
|
||||
# assert que le fichier a été supprimé.
|
35
illustrations/1_tester/test_intelligents.py
Normal file
35
illustrations/1_tester/test_intelligents.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
from hypothesis import given
|
||||
from hypothesis.strategies import text
|
||||
|
||||
def encode(input_string):
|
||||
count = 1
|
||||
prev = ''
|
||||
lst = []
|
||||
for character in input_string:
|
||||
if character != prev:
|
||||
if prev:
|
||||
entry = (prev,count)
|
||||
lst.append(entry)
|
||||
#print lst
|
||||
count = 1
|
||||
prev = character
|
||||
else:
|
||||
count += 1
|
||||
else:
|
||||
try:
|
||||
entry = (character,count)
|
||||
lst.append(entry)
|
||||
return (lst, 0)
|
||||
except Exception as e:
|
||||
print("Exception encountered {e}".format(e=e))
|
||||
return (e, 1)
|
||||
|
||||
def decode(lst):
|
||||
q = ""
|
||||
for character, count in lst:
|
||||
q += character * count
|
||||
return q
|
||||
|
||||
@given(text())
|
||||
def test_decode_inverts_encode(s):
|
||||
assert decode(encode(s)) == s
|
32
illustrations/1_tester/test_paradoxe.py
Normal file
32
illustrations/1_tester/test_paradoxe.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
from hypothesis import given, assume
|
||||
from hypothesis.strategies import lists, permutations
|
||||
from collections import Counter, defaultdict
|
||||
|
||||
# Ici, on va observer un paradoxe classique en théorie des votes juste en utilisant Hypothesis.
|
||||
# Imaginez, ici que vous fabriquez un algorithme de couplage, type APB/Admissions Parallèles/Parcoursup.
|
||||
|
||||
# On se donne par Hypothesis au moins trois candidats et trois voteurs.
|
||||
@given(lists(permutations(["A", "B", "C"]), min_size=3))
|
||||
def test_condorcet_paradox(election):
|
||||
all_candidates = {"A", "B", "C"}
|
||||
|
||||
# On calcule les préférences paires à paires (u, v) de chaque candidat.
|
||||
counts = Counter()
|
||||
for vote in election:
|
||||
for i, vi in enumerate(vote):
|
||||
for j in range(i + 1, len(vote)):
|
||||
counts[(vi, vote[j])] += 1
|
||||
|
||||
# On regarde quelle paires de candidats a la majorité.
|
||||
graph = defaultdict(set)
|
||||
for i in all_candidates:
|
||||
for j in all_candidates:
|
||||
if counts[(i, j)] > counts[(j, i)]:
|
||||
graph[i].add(j)
|
||||
|
||||
# Maintenant, on vérifie que les préférences sont transitives, i.e. si A préfère B, B préfère C, alors A préfère C non?
|
||||
for x in all_candidates:
|
||||
for y in graph[x]:
|
||||
for z in graph[y]:
|
||||
# vérifions que x n'est pas dans graph[z]
|
||||
assert x not in graph[z]
|
15
illustrations/1_tester/test_unitaire.py
Normal file
15
illustrations/1_tester/test_unitaire.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
def est_premier(n):
|
||||
"""
|
||||
>>> est_premier(3)
|
||||
True
|
||||
>>> est_premier(5)
|
||||
True
|
||||
>>> est_premier(4)
|
||||
True
|
||||
"""
|
||||
for k in range(n):
|
||||
if k % n == 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue