File size: 6,959 Bytes
8845c50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""Tests Phase 5 β€” csv_writer & calcul prix (format de rΓ©fΓ©rence CAMENGO)."""
import csv
import os
import tempfile

import pytest

from core.csv_writer import (
    _fmt_decimal,
    _fmt_remise,
    export_ebp,
    prix_achat_from_conseille,
    prix_ht_from_ttc,
)


# ── Calcul prix ───────────────────────────────────────────────────────────────

def test_prix_achat_remise_45():
    # 70,92 Γ— (1 - 45%) = 70,92 Γ— 0,55 = 39,006 β†’ arrondi 39,01
    assert prix_achat_from_conseille(70.92, 45.0) == pytest.approx(39.006, abs=0.01)


def test_prix_achat_remise_0():
    assert prix_achat_from_conseille(100.0, 0.0) == pytest.approx(100.0)


def test_prix_achat_zero_conseille():
    assert prix_achat_from_conseille(0.0, 45.0) == 0.0


def test_prix_ht_from_ttc_20():
    assert prix_ht_from_ttc(120.0, 20.0) == pytest.approx(100.0, abs=0.01)


def test_prix_ht_from_ttc_10():
    assert prix_ht_from_ttc(110.0, 10.0) == pytest.approx(100.0, abs=0.01)


# ── Formatage dΓ©cimal ─────────────────────────────────────────────────────────

def test_fmt_decimal_virgule():
    assert _fmt_decimal(70.92) == "70,92"


def test_fmt_decimal_no_euro():
    assert "€" not in _fmt_decimal(100.0)


def test_fmt_decimal_no_percent():
    assert "%" not in _fmt_decimal(100.0)


def test_fmt_decimal_empty():
    assert _fmt_decimal("") == ""
    assert _fmt_decimal(None) == ""


def test_fmt_remise_entier():
    assert _fmt_remise(45.0) == "45"
    assert _fmt_remise(45) == "45"


def test_fmt_remise_no_percent():
    assert "%" not in _fmt_remise(45)


# ── DonnΓ©es de test (format CAMENGO) ─────────────────────────────────────────

SAMPLE_ARTICLES = [
    {
        "nom_dessin":   "Adagio",
        "reference":    "3953",
        "collection":   "CHOREGRAPHIE",
        "prix_conseille": 70.92,
        "unite":        "ml",
        "prix_valide":  True,
    },
    {
        "nom_dessin":   "Ballerine",
        "reference":    "3936",
        "collection":   "CHOREGRAPHIE",
        "prix_conseille": 96.08,
        "unite":        "ml",
        "prix_valide":  True,
    },
]


# ── export_ebp β€” BOM, sΓ©parateur, virgule dΓ©cimale ────────────────────────────

def test_bom_present():
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001", remise=45.0)
        with open(files["3_articles.csv"], "rb") as f:
            assert f.read(3) == b"\xef\xbb\xbf", "BOM UTF-8 manquant"


def test_separateur_point_virgule():
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001", remise=45.0)
        with open(files["3_articles.csv"], encoding="utf-8-sig") as f:
            first_line = f.readline()
        assert ";" in first_line


def test_colonnes_format_camengo():
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001", remise=45.0)
        with open(files["3_articles.csv"], encoding="utf-8-sig") as f:
            header = f.readline().strip()
        cols = header.split(";")
        assert cols[0] == "Nom de dessin"
        assert cols[1] == "RΓ©fΓ©rence"
        assert cols[2] == "Collection"
        assert "Prix d'achat HT par ml" in cols
        assert "Prix conseillΓ© HT par ml" in cols
        assert "Remise" in cols


def test_decimal_virgule_dans_articles():
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001", remise=45.0)
        with open(files["3_articles.csv"], encoding="utf-8-sig") as f:
            content = f.read()
        assert "70,92" in content
        assert "." not in content.replace("Nom de dessin", "")  # pas de point dΓ©cimal


def test_pas_de_euro_dans_articles():
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001", remise=45.0)
        with open(files["3_articles.csv"], encoding="utf-8-sig") as f:
            content = f.read()
        assert "€" not in content


def test_pas_de_percent_dans_articles():
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001", remise=45.0)
        with open(files["3_articles.csv"], encoding="utf-8-sig") as f:
            content = f.read()
        assert "%" not in content


def test_trois_fichiers_produits():
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001", remise=45.0)
        assert len(files) == 3
        for name in ["1_familles_articles.csv", "2_fournisseur.csv", "3_articles.csv"]:
            assert os.path.exists(files[name])


def test_remise_ecrite_sans_symbole():
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001", remise=45.0)
        with open(files["3_articles.csv"], encoding="utf-8-sig") as f:
            reader = csv.DictReader(f, delimiter=";")
            for row in reader:
                assert row["Remise"] == "45"
                assert "%" not in row["Remise"]


def test_prix_achat_calcule_depuis_conseille():
    # Prix conseillΓ© 70,92 Γ— (1-45%) β‰ˆ 38,51
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001", remise=45.0)
        with open(files["3_articles.csv"], encoding="utf-8-sig") as f:
            reader = csv.DictReader(f, delimiter=";")
            row = next(reader)
        prix_achat = float(row["Prix d'achat HT par ml"].replace(",", "."))
        assert prix_achat == pytest.approx(39.006, abs=0.01)


def test_libelle_tronque_80():
    articles = [{
        "nom_dessin": "X" * 100,
        "reference": "T001",
        "collection": "TEST",
        "prix_conseille": 50.0,
        "unite": "ml",
    }]
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(articles, d, "TEST", "TST001", remise=45.0)
        with open(files["3_articles.csv"], encoding="utf-8-sig") as f:
            reader = csv.DictReader(f, delimiter=";")
            row = next(reader)
        assert len(row["Nom de dessin"]) <= 80


def test_code_famille_dans_articles():
    with tempfile.TemporaryDirectory() as d:
        files = export_ebp(SAMPLE_ARTICLES, d, "CASAMANCE", "CAS001",
                           remise=45.0, code_famille="FFR00001")
        with open(files["3_articles.csv"], encoding="utf-8-sig") as f:
            reader = csv.DictReader(f, delimiter=";")
            for row in reader:
                assert row["Code famille"] == "FFR00001"