"""Test models referentiel compétences

Utiliser par exemple comme: 
    pytest tests/unit/test_refcomp.py

"""
import io
from flask import g
import app
from app import db
from app import models
from app.but.import_refcomp import orebut_import_refcomp
from app.models.but_refcomp import (
    ApcReferentielCompetences,
    ApcCompetence,
    ApcSituationPro,
)


def test_but_refcomp(test_client):
    """modèles ref. comp."""
    xml_data = open("tests/data/but-RT-refcomp-exemple.xml").read()
    dept_id = models.Departement.query.first().id
    ref = orebut_import_refcomp(xml_data, dept_id)
    assert ref.competences.count() == 13
    assert ref.competences[0].situations.count() == 3
    assert ref.competences[0].situations[0].libelle.startswith("Conception ")
    assert (
        ref.competences[-1].situations[-1].libelle
        == "Administration des services multimédia"
    )
    # test cascades on delete
    db.session.delete(ref)
    db.session.commit()
    assert ApcCompetence.query.count() == 0
    assert ApcSituationPro.query.count() == 0
