<commit_before># Case Conductor is a Test Case Management system.
# Copyright (C) 2011-2012 Mozilla
#
# This file is part of Case Conductor.
#
# Case Conductor is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Case Conductor is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Case Conductor.  If not, see <http://www.gnu.org/licenses/>.
"""
Models for test-case library (cases, suites).

"""
from django.core.exceptions import ValidationError
from django.db import models

from ..attachments.models import Attachment
from ..ccmodel import CCModel, DraftStatusModel
from ..core.models import Product, ProductVersion
from ..environments.models import HasEnvironmentsModel
from ..tags.models import Tag



class Case(CCModel):
    """A test case for a given product."""
    product = models.ForeignKey(Product, related_name="cases")


    def __unicode__(self):
        return "case #%s" % (self.id,)


    def clone(self, *args, **kwargs):
        """Clone this Case with default cascade behavior: latest versions."""
        kwargs.setdefault("cascade", ["versions"])
        return super(Case, self).clone(*args, **kwargs)


    def set_latest_version(self, update_instance=None):
        """
        Mark latest version of this case in DB, marking all others non-latest.

        If ``update_instance`` is provided, its ``latest`` flag is updated
        appropriately.

        """
        try:
            latest_version = self.versions.order_by("-productversion__order")[0]
        except IndexError:
            pass
        else:
            self.versions.update(latest=False, notrack=True)
            latest_version.latest = True
            latest_version.save(
                force_update=True, skip_set_latest=True, notrack=True)
            if update_instance == latest_version:
                update_instance.latest = True
            elif update_instance is not None:
                update_instance.latest = False


    def all_versions(self):
        """
        Return list of (productversion, caseversion) tuples for this case.

        Includes all product versions; caseversion may be None if this case has
        no version for that product version.

        """
        caseversions_by_pv = dict(
            (cv.productversion, cv) for cv in self.versions.all())
        return(
            [
                (pv, caseversions_by_pv.get(pv, None))
                for pv in self.product.versions.all()
                ]
            )


    def latest_version(self):
        """Return latest version of this case."""
        return self.versions.get(latest=True)



    class Meta:
        permissions = [
            ("create_cases", "Can create new test cases."),
            ("manage_cases", "Can add/edit/delete test cases."),
            ]



class CaseVersion(CCModel, DraftStatusModel, HasEnvironmentsModel):
    """A version of a test case."""
    productversion = models.ForeignKey(
        ProductVersion, related_name="caseversions")
    case = models.ForeignKey(Case, related_name="versions")
    name = models.CharField(max_length=200)
    description = models.TextField(blank=True)

    # denormalized for queries
    latest = models.BooleanField(default=False, editable=False)

    tags = models.ManyToManyField(Tag, blank=True)
    # True if this case's envs have been narrowed from the product version.
    envs_narrowed = models.BooleanField(default=False)


    def __unicode__(self):
        return self.name


    class Meta:
        ordering = ["case", "productversion__order"]


    def save(self, *args, **kwargs):
        """Save CaseVersion, updating latest version."""
        skip_set_latest = kwargs.pop("skip_set_latest", False)
        super(CaseVersion, self).save(*args, **kwargs)
        if not skip_set_latest:
            self.case.set_latest_version(update_instance=self)


    def delete(self, *args, **kwargs):
        """Delete CaseVersion, updating latest version."""<commit_msg>Gave caseversion tags a related name.<commit_after># Case Conductor is a Test Case Management system.
# Copyright (C) 2011-2012 Mozilla
#
# This file is part of Case Conductor.
#
# Case Conductor is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Case Conductor is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Case Conductor.  If not, see <http://www.gnu.org/licenses/>.
"""
Models for test-case library (cases, suites).

"""
from django.core.exceptions import ValidationError
from django.db import models

from ..attachments.models import Attachment
from ..ccmodel import CCModel, DraftStatusModel
from ..core.models import Product, ProductVersion
from ..environments.models import HasEnvironmentsModel
from ..tags.models import Tag



class Case(CCModel):
    """A test case for a given product."""
    product = models.ForeignKey(Product, related_name="cases")


    def __unicode__(self):
        return "case #%s" % (self.id,)


    def clone(self, *args, **kwargs):
        """Clone this Case with default cascade behavior: latest versions."""
        kwargs.setdefault("cascade", ["versions"])
        return super(Case, self).clone(*args, **kwargs)


    def set_latest_version(self, update_instance=None):
        """
        Mark latest version of this case in DB, marking all others non-latest.

        If ``update_instance`` is provided, its ``latest`` flag is updated
        appropriately.

        """
        try:
            latest_version = self.versions.order_by("-productversion__order")[0]
        except IndexError:
            pass
        else:
            self.versions.update(latest=False, notrack=True)
            latest_version.latest = True
            latest_version.save(
                force_update=True, skip_set_latest=True, notrack=True)
            if update_instance == latest_version:
                update_instance.latest = True
            elif update_instance is not None:
                update_instance.latest = False


    def all_versions(self):
        """
        Return list of (productversion, caseversion) tuples for this case.

        Includes all product versions; caseversion may be None if this case has
        no version for that product version.

        """
        caseversions_by_pv = dict(
            (cv.productversion, cv) for cv in self.versions.all())
        return(
            [
                (pv, caseversions_by_pv.get(pv, None))
                for pv in self.product.versions.all()
                ]
            )


    def latest_version(self):
        """Return latest version of this case."""
        return self.versions.get(latest=True)



    class Meta:
        permissions = [
            ("create_cases", "Can create new test cases."),
            ("manage_cases", "Can add/edit/delete test cases."),
            ]



class CaseVersion(CCModel, DraftStatusModel, HasEnvironmentsModel):
    """A version of a test case."""
    productversion = models.ForeignKey(
        ProductVersion, related_name="caseversions")
    case = models.ForeignKey(Case, related_name="versions")
    name = models.CharField(max_length=200)
    description = models.TextField(blank=True)

    # denormalized for queries
    latest = models.BooleanField(default=False, editable=False)

    tags = models.ManyToManyField(Tag, blank=True, related_name="caseversions")
    # True if this case's envs have been narrowed from the product version.
    envs_narrowed = models.BooleanField(default=False)


    def __unicode__(self):
        return self.name


    class Meta:
        ordering = ["case", "productversion__order"]


    def save(self, *args, **kwargs):
        """Save CaseVersion, updating latest version."""
        skip_set_latest = kwargs.pop("skip_set_latest", False)
        super(CaseVersion, self).save(*args, **kwargs)
        if not skip_set_latest:
            self.case.set_latest_version(update_instance=self)


    def delete(self, *args, **kwargs):
        """Delete CaseVersion, updating latest version."""
{'fraction_non_alphanumeric': 0.09451046972269384, 'fraction_numerical': 0.002942840973401245, 'mean_word_length': 2.8184961106309423, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5493832', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2274, 'n_words': 793}