<commit_before>"""
Tests for Case and related models (CaseVersion, CaseStep).

"""
from datetime import datetime

from django.core.exceptions import ValidationError

from mock import patch

from tests import case



class CaseTest(case.DBTestCase):
    def test_unicode(self):
        c = self.F.CaseFactory()

        self.assertEqual(unicode(c), u"case #%s" % c.id)


    def test_clone_versions(self):
        """Cloning a case clones all versions."""
        cv = self.F.CaseVersionFactory.create(name="CV 1")

        new = cv.case.clone()

        self.assertEqual(new.versions.get().name, "Cloned: CV 1")


    def test_all_versions(self):
        """Returns ordered product versions paired with caseversion or None."""
        c = self.F.CaseFactory()
        pv1 = self.F.ProductVersionFactory(product=c.product, version="1")
        pv2 = self.F.ProductVersionFactory(product=c.product, version="2")
        pv3 = self.F.ProductVersionFactory(product=c.product, version="3")
        cv2 = self.F.CaseVersionFactory(productversion=pv2, case=c)

        self.assertEqual(
            c.all_versions(), [(pv1, None), (pv2, cv2), (pv3, None)])



class CaseVersionTest(case.DBTestCase):
    def test_unicode(self):
        cv = self.F.CaseVersionFactory(name="Foo")

        self.assertEqual(unicode(cv), u"Foo")


    def test_clone_clones_case(self):
        """Cloning caseversion w/o case or productversion clones case."""
        cv = self.F.CaseVersionFactory(name="one")
        self.F.CaseVersionFactory(case=cv.case)

        new = cv.clone()

        self.assertIsInstance(new, cv.__class__)
        self.assertEqual(
            [v.name for v in new.case.versions.all()], ["Cloned: one"])


    def test_clone_override_name(self):
        """Providing name override prevents 'Cloned: ' prefix."""
        cv = self.F.CaseVersionFactory(name="one")

        new = cv.clone(overrides={"name": "two"})

        self.assertEqual(new.name, "two")


    def test_clone_gets_source_state(self):
        """Clone of active caseversion is still active."""
        cv = self.F.CaseVersionFactory(status="active")

        new = cv.clone()

        self.assertEqual(new.status, "active")


    def test_clone_steps(self):
        """Cloning a caseversion clones its steps."""
        cs = self.F.CaseStepFactory.create()
        pv = self.F.ProductVersionFactory.create(
            product=cs.caseversion.case.product, version="2.0")

        new = cs.caseversion.clone(overrides={"productversion": pv})

        cloned_step = new.steps.get()
        self.assertNotEqual(cloned_step, cs)
        self.assertEqual(cloned_step.instruction, cs.instruction)


    def test_clone_attachments(self):
        """Cloning a caseversion clones its attachments."""
        ca = self.F.CaseAttachmentFactory.create()
        pv = self.F.ProductVersionFactory.create(
            product=ca.caseversion.case.product, version="2.0")<commit_msg>add 2 test cases for cloning w/ suites<commit_after>"""
Tests for Case and related models (CaseVersion, CaseStep).

"""
from datetime import datetime

from django.core.exceptions import ValidationError

from mock import patch

from tests import case

from moztrap.model import SuiteCase



class CaseTest(case.DBTestCase):
    def test_unicode(self):
        c = self.F.CaseFactory()

        self.assertEqual(unicode(c), u"case #%s" % c.id)


    def test_clone_versions(self):
        """Cloning a case clones all versions."""
        cv = self.F.CaseVersionFactory.create(name="CV 1")

        new = cv.case.clone()

        self.assertEqual(new.versions.get().name, "Cloned: CV 1")


    def test_all_versions(self):
        """Returns ordered product versions paired with caseversion or None."""
        c = self.F.CaseFactory()
        pv1 = self.F.ProductVersionFactory(product=c.product, version="1")
        pv2 = self.F.ProductVersionFactory(product=c.product, version="2")
        pv3 = self.F.ProductVersionFactory(product=c.product, version="3")
        cv2 = self.F.CaseVersionFactory(productversion=pv2, case=c)

        self.assertEqual(
            c.all_versions(), [(pv1, None), (pv2, cv2), (pv3, None)])



class CaseVersionTest(case.DBTestCase):
    def test_unicode(self):
        cv = self.F.CaseVersionFactory(name="Foo")

        self.assertEqual(unicode(cv), u"Foo")


    def test_clone_clones_case(self):
        """Cloning caseversion w/o case or productversion clones case."""
        cv = self.F.CaseVersionFactory(name="one")
        self.F.CaseVersionFactory(case=cv.case)

        new = cv.clone()

        self.assertIsInstance(new, cv.__class__)
        self.assertEqual(
            [v.name for v in new.case.versions.all()], ["Cloned: one"])


    def test_clone_override_name(self):
        """Providing name override prevents 'Cloned: ' prefix."""
        cv = self.F.CaseVersionFactory(name="one")

        new = cv.clone(overrides={"name": "two"})

        self.assertEqual(new.name, "two")


    def test_clone_gets_source_state(self):
        """Clone of active caseversion is still active."""
        cv = self.F.CaseVersionFactory(status="active")

        new = cv.clone()

        self.assertEqual(new.status, "active")


    def test_clone_single_suite(self):
        cv = self.F.CaseVersionFactory()
        suite = self.F.SuiteFactory(product=cv.case.product)
        self.F.SuiteCaseFactory(suite=suite, case=cv.case)

        user = self.F.UserFactory.create(username='tester')
        new = cv.clone(user=user)

        self.assertEqual(len(SuiteCase.objects.filter(suite=suite)), 2)

    def test_clone_multiple_suites(self):
        cv = self.F.CaseVersionFactory()
        num_suites = 3
        suites = []
        for i in range(0, num_suites):
            suites.append(self.F.SuiteFactory(name='Suite {0}'.format(i), product=cv.case.product))
            self.F.SuiteCaseFactory(suite=suites[i], case=cv.case)

        user = self.F.UserFactory.create(username='tester')
        new = cv.clone(user=user)

        for i in range(0, num_suites):
            self.assertEqual(len(SuiteCase.objects.filter(suite=suites[i])), 2)


    def test_clone_steps(self):
        """Cloning a caseversion clones its steps."""
        cs = self.F.CaseStepFactory.create()
        pv = self.F.ProductVersionFactory.create(
            product=cs.caseversion.case.product, version="2.0")

        new = cs.caseversion.clone(overrides={"productversion": pv})

        cloned_step = new.steps.get()
        self.assertNotEqual(cloned_step, cs)
        self.assertEqual(cloned_step.instruction, cs.instruction)


    def test_clone_attachments(self):
        """Cloning a caseversion clones its attachments."""
        ca = self.F.CaseAttachmentFactory.create()
        pv = self.F.ProductVersionFactory.create(
            product=ca.caseversion.case.product, version="2.0")
{'fraction_non_alphanumeric': 0.12999708199591478, 'fraction_numerical': 0.006273708783192297, 'mean_word_length': 2.967013888888889, 'pattern_counts': {'":': 4, '<': 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': '5987708', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2027, 'n_words': 422}