<commit_before>import pytest

from boites.models import App, Boite
from app_parcel.models import AppParcel


@pytest.fixture
def boite(admin_user):
    boite, _ = Boite.objects.get_or_create(
        name="Test boite",
        user=admin_user,
        api_key = "123")
    yield boite


@pytest.fixture
def parcel(boite):
    parcel = AppParcel.objects.create(
        boite=boite,
        parcel="parcel to track",
        parcel_carrier="chronopost")
    yield parcel


def test_get_app_dictionnary(parcel):
    assert parcel.get_app_dictionary() == {
        "arrival": None,
        "info": None,
        "parcel": "parcel to track",
        "status": None,
        "parcel_carrier": "chronopost",
    }


def test_get_apps_dictionnary_single_app(parcel):
    assert parcel.boite.get_apps_dictionary() == {
        "app_parcel": [
            {
                "arrival": None,
                "info": None,
                "parcel": "parcel to track",
                "status": None,
                "parcel_carrier": "chronopost",
            }
        ]
    }


def test_get_apps_dictionnary_duplicated_apps(parcel):
    parcel2 = parcel
    parcel2.id = None
    parcel2.save()  # Hack: save parcel2 as a new instance.
    parcel2.parcel = "another parcel to track"
    parcel2.parcel_carrier = "gls"
    parcel2.save()
    assert parcel.boite.get_apps_dictionary() == {
        "app_parcel": [
            {
                "arrival": None,
                "info": None,
                "parcel": "parcel to track",
                "status": None,
                "parcel_carrier": "chronopost",
            },
            {
                "arrival": None,
                "info": None,
                "parcel": "another parcel to track",
                "status": None,
                "parcel_carrier": "gls",
            },
        ]
    }


def test_boite_api_key(admin_user):
    """Make sure the api_key is properly set."""
    boite = Boite(name="test boite", user=admin_user)
    # Before saving the boite, no api_key is generated.
    assert boite.api_key == ''
    # One the boite is saved, an api_key is generated.
    boite.save()
    api_key = boite.api_key
    assert api_key
    # Any subsequent saves won't overwrite the generated api_key.
    boite.save()
    assert boite.api_key == api_key


def test_get_app_dictionnary_base_app(boite):
    app = App(boite=boite)
    with pytest.raises(NotImplementedError):
        # The App base class should not be used.<commit_msg>Fix typo in the tests<commit_after>import pytest

from boites.models import App, Boite
from app_parcel.models import AppParcel


@pytest.fixture
def boite(admin_user):
    boite, _ = Boite.objects.get_or_create(
        name="Test boite",
        user=admin_user,
        api_key = "123")
    yield boite


@pytest.fixture
def parcel(boite):
    parcel = AppParcel.objects.create(
        boite=boite,
        parcel="parcel to track",
        parcel_carrier="chronopost")
    yield parcel


def test_get_app_dictionary(parcel):
    assert parcel.get_app_dictionary() == {
        "arrival": None,
        "info": None,
        "parcel": "parcel to track",
        "status": None,
        "parcel_carrier": "chronopost",
    }


def test_get_apps_dictionary_single_app(parcel):
    assert parcel.boite.get_apps_dictionary() == {
        "app_parcel": [
            {
                "arrival": None,
                "info": None,
                "parcel": "parcel to track",
                "status": None,
                "parcel_carrier": "chronopost",
            }
        ]
    }


def test_get_apps_dictionary_duplicated_apps(parcel):
    parcel2 = parcel
    parcel2.id = None
    parcel2.save()  # Hack: save parcel2 as a new instance.
    parcel2.parcel = "another parcel to track"
    parcel2.parcel_carrier = "gls"
    parcel2.save()
    assert parcel.boite.get_apps_dictionary() == {
        "app_parcel": [
            {
                "arrival": None,
                "info": None,
                "parcel": "parcel to track",
                "status": None,
                "parcel_carrier": "chronopost",
            },
            {
                "arrival": None,
                "info": None,
                "parcel": "another parcel to track",
                "status": None,
                "parcel_carrier": "gls",
            },
        ]
    }


def test_boite_api_key(admin_user):
    """Make sure the api_key is properly set."""
    boite = Boite(name="test boite", user=admin_user)
    # Before saving the boite, no api_key is generated.
    assert boite.api_key == ''
    # One the boite is saved, an api_key is generated.
    boite.save()
    api_key = boite.api_key
    assert api_key
    # Any subsequent saves won't overwrite the generated api_key.
    boite.save()
    assert boite.api_key == api_key


def test_get_app_dictionary_base_app(boite):
    app = App(boite=boite)
    with pytest.raises(NotImplementedError):
        # The App base class should not be used.
{'fraction_non_alphanumeric': 0.10671462829736211, 'fraction_numerical': 0.003996802557953637, 'mean_word_length': 2.0425531914893615, 'pattern_counts': {'":': 44, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1333428', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1580, 'n_words': 365}