Esempio n. 1
0
 def test_shot_number_prefix_attribute_initialization(self):
     """testing the shot_number_prefix attribute is initialized correctly
     for a newly created Project instance
     """
     new_proj = Project("TEST_PROJECT")
     new_proj.create()
     
     self.assertEqual(new_proj.shot_number_prefix, conf.shot_number_prefix)
Esempio n. 2
0
    def test_shot_number_prefix_attribute_initialization(self):
        """testing the shot_number_prefix attribute is initialized correctly
        for a newly created Project instance
        """
        new_proj = Project("TEST_PROJECT")
        new_proj.create()

        self.assertEqual(new_proj.shot_number_prefix, conf.shot_number_prefix)
Esempio n. 3
0
    def test_calling_commit_multiple_times(self):
        """testing if there is no problem of calling Project.save() multiple
        times
        """

        new_proj = Project("TEST_PROJECT")
        new_proj.create()
        new_proj.save()
        new_proj.save()
Esempio n. 4
0
 def test_project_creation_for_new_project(self):
     """testing if the project creation occurs without any problem
     """
     
     new_proj = Project("TEST_PROJECT")
     new_proj.create()
     
     # now check if the folder is created
     self.assertTrue(os.path.exists(new_proj.full_path))
Esempio n. 5
0
    def test_project_creation_for_new_project(self):
        """testing if the project creation occurs without any problem
        """

        new_proj = Project("TEST_PROJECT")
        new_proj.create()

        # now check if the folder is created
        self.assertTrue(os.path.exists(new_proj.full_path))
Esempio n. 6
0
    def test_calling_commit_multiple_times(self):
        """testing if there is no problem of calling Project.save() multiple
        times
        """

        new_proj = Project("TEST_PROJECT")
        new_proj.create()
        new_proj.save()
        new_proj.save()
Esempio n. 7
0
    def test_creating_two_different_projects_and_calling_create_in_mixed_order(self):
        """testing no error will be raised when creating two Project instances
        and calling their create method in mixed order
        """

        new_proj1 = Project("TEST_PROJECT1")
        new_proj2 = Project("TEST_PROJECT2")

        new_proj1.create()
        new_proj2.create()
Esempio n. 8
0
    def test_creating_two_different_projects_and_calling_create_in_mixed_order(
            self):
        """testing no error will be raised when creating two Project instances
        and calling their create method in mixed order
        """

        new_proj1 = Project("TEST_PROJECT1")
        new_proj2 = Project("TEST_PROJECT2")

        new_proj1.create()
        new_proj2.create()
Esempio n. 9
0
    def test_shot_number_prefix_attribute_initialization_from_DB(self):
        """testing if the shot_number_prefix attribute is initialized correctly
        for a Project which is already in the database
        """
        new_proj = Project("TEST_PROJECT")
        new_proj.shot_number_prefix = "PL"
        new_proj.create()
        new_proj.save()

        # now get it back from db
        new_proj = Project("TEST_PROJECT")
        self.assertEqual(new_proj.shot_number_prefix, "PL")
Esempio n. 10
0
    def test_calling_create_on_a_project_which_is_retrieved_from_db(self):
        """testing if there will be no error messages generated when the new
        project is retrieved from the database and the create method of this
        project is called
        """

        project_name = "TEST_PROJECT1"
        new_proj1 = Project(project_name)
        new_proj1.create()

        new_proj2 = Project(project_name)
        new_proj2.create()
Esempio n. 11
0
    def test_shot_number_prefix_attribute_initialization_from_DB(self):
        """testing if the shot_number_prefix attribute is initialized correctly
        for a Project which is already in the database
        """
        new_proj = Project("TEST_PROJECT")
        new_proj.shot_number_prefix = "PL"
        new_proj.create()
        new_proj.save()

        # now get it back from db
        new_proj = Project("TEST_PROJECT")
        self.assertEqual(new_proj.shot_number_prefix, "PL")
Esempio n. 12
0
    def test_calling_create_on_a_project_which_is_retrieved_from_db(self):
        """testing if there will be no error messages generated when the new
        project is retrieved from the database and the create method of this
        project is called
        """

        project_name = "TEST_PROJECT1"
        new_proj1 = Project(project_name)
        new_proj1.create()

        new_proj2 = Project(project_name)
        new_proj2.create()
Esempio n. 13
0
 def test_shot_is_CRUD_properly_in_the_database(self):
     """testing if the shot instance is created properly in the database
     """
     new_proj = Project("TEST_PROJ_FOR_CRUD")
     new_proj.create()
     
     new_seq = Sequence(new_proj, "TEST_SEQ103")
     new_seq.save()
     
     new_shot = Shot(new_seq, '1a')
     new_shot.save()
     
     # now query the database if it is created and read correctly
     self.assertEqual(
         new_shot,
         Shot.query()
             .filter(Shot.number==new_shot.number)
             .first()
     )
     
     # now update it
     new_shot.start_frame = 100
     new_shot.end_frame = 200
     new_shot.save()
     
     self.assertEqual(
         new_shot.start_frame,
         Shot.query()
             .filter(Shot.number==new_shot.number)
             .first()
             .start_frame
     )
     self.assertEqual(
         new_shot.end_frame,
         Shot.query()
             .filter(Shot.number==new_shot.number)
             .first()
             .end_frame
     )
     
     # now delete it
     db.session.delete(new_shot)
     db.session.commit()
     
     self.assertEqual(len(Shot.query().all()), 1)
Esempio n. 14
0
    def test_project_stored_and_retrieved_correctly(self):
        """testing if the project is stored and retrieved correctly
        """

        new_proj = Project(name="TEST_PROJECT")
        new_proj.create()

        name = new_proj.name
        path = new_proj.path
        full_path = new_proj.full_path

        del new_proj

        new_proj_DB = Project.query().first()

        self.assertEqual(new_proj_DB.name, name)
        self.assertEqual(new_proj_DB.path, path)
        self.assertEqual(new_proj_DB.full_path, full_path)
Esempio n. 15
0
    def test_project_stored_and_retrieved_correctly(self):
        """testing if the project is stored and retrieved correctly
        """

        new_proj = Project(name="TEST_PROJECT")
        new_proj.create()

        name = new_proj.name
        path = new_proj.path
        full_path = new_proj.full_path

        del new_proj

        new_proj_DB = Project.query().first()

        self.assertEqual(new_proj_DB.name, name)
        self.assertEqual(new_proj_DB.path, path)
        self.assertEqual(new_proj_DB.full_path, full_path)
Esempio n. 16
0
 def test_inequality_of_shots(self):
     """testing if the equality operator is working properly
     """
     proj1 = Project("TEST_EQ_PROJ")
     proj1.create()
     
     seq1 = Sequence(proj1, "TEST_SEQ1")
     seq2 = Sequence(proj1, "TEST_SEQ2")
     
     shot1 = Shot(seq1, 1)
     shot2 = Shot(seq1, 2)
     shot3 = Shot(seq2, 1)
     
     #shot1a = Shot(seq1, 1)
     shot1a = seq1.shots[0]
     
     self.assertFalse(shot1!=shot1a)
     self.assertTrue(shot1!=shot2)
     self.assertTrue(shot1a!=shot3)
Esempio n. 17
0
    def test_project_restores_from_database_1(self):
        """testing if a project restores it self from the database with all its
        connections
        """

        # we need to create a new project and a sequence
        new_proj = Project("TEST_PROJECT")
        new_proj.create()

        test_description = "test description"
        new_proj.description = test_description
        new_proj.save()

        del new_proj

        # now retrieve the project by recreating it
        new_proj2 = Project("TEST_PROJECT")

        self.assertEqual(new_proj2.description, test_description)
Esempio n. 18
0
    def test_project_restores_from_database_1(self):
        """testing if a project restores it self from the database with all its
        connections
        """

        # we need to create a new project and a sequence
        new_proj = Project("TEST_PROJECT")
        new_proj.create()

        test_description = "test description"
        new_proj.description = test_description
        new_proj.save()

        del new_proj

        # now retrieve the project by recreating it
        new_proj2 = Project("TEST_PROJECT")

        self.assertEqual(new_proj2.description, test_description)
Esempio n. 19
0
    def test_version_types_attribute_initialization_for_a_new_Project_instance(self):
        """testing if the version_types attribute initializes from the config
        correctly for a new Project instance
        """

        new_proj = Project("TEST_PROJECT")
        new_proj.create()

        # now check if the project has all the version types defined in the
        # config file

        for version_type in conf.version_types:
            version_type_name = version_type["name"]
            vtype_from_proj =\
                VersionType.query().\
                filter_by(name=version_type_name).\
                first()
            
            self.assertTrue(vtype_from_proj is not None)
Esempio n. 20
0
    def test_version_types_attribute_initialization_for_a_new_Project_instance(
            self):
        """testing if the version_types attribute initializes from the config
        correctly for a new Project instance
        """

        new_proj = Project("TEST_PROJECT")
        new_proj.create()

        # now check if the project has all the version types defined in the
        # config file

        for version_type in conf.version_types:
            version_type_name = version_type["name"]
            vtype_from_proj =\
                VersionType.query().\
                filter_by(name=version_type_name).\
                first()

            self.assertTrue(vtype_from_proj is not None)
Esempio n. 21
0
 def test_inequality_of_assets(self):
     """testing if two assets are inequal if their names are different and
     or their projects are different
     """
     
     proj1 = Project("EQUALITY_TEST_PROJECT_1")
     proj1.create()
     
     proj2 = Project("EQUALITY_TEST_PROJECT_2")
     proj2.create()
     
     asset1 = Asset(proj1, "TEST_ASSET1")
     asset2 = Asset(proj1, "TEST_ASSET1")
     
     asset3 = Asset(proj1, "TEST_ASSET3")
     asset4 = Asset(proj2, "TEST_ASSET3")
     
     self.assertFalse(asset1!=asset2)
     self.assertTrue(asset1!=asset3)
     self.assertTrue(asset3!=asset4)
Esempio n. 22
0
 def test_equality_of_assets(self):
     """testing if two assets are equal if their names and projects are also
     equal
     """
     
     proj1 = Project("EQUALITY_TEST_PROJECT_1")
     proj1.create()
     
     proj2 = Project("EQUALITY_TEST_PROJECT_2")
     proj2.create()
     
     asset1 = Asset(proj1, "TEST_ASSET1")
     asset2 = Asset(proj1, "TEST_ASSET1")
     
     asset3 = Asset(proj1, "TEST_ASSET3")
     asset4 = Asset(proj2, "TEST_ASSET3")
     
     self.assertTrue(asset1==asset2)
     self.assertFalse(asset1==asset3)
     self.assertFalse(asset3==asset4)
Esempio n. 23
0
    def test_inequality_of_assets(self):
        """testing if two assets are inequal if their names are different and
        or their projects are different
        """

        proj1 = Project("EQUALITY_TEST_PROJECT_1")
        proj1.create()

        proj2 = Project("EQUALITY_TEST_PROJECT_2")
        proj2.create()

        asset1 = Asset(proj1, "TEST_ASSET1")
        asset2 = Asset(proj1, "TEST_ASSET1")

        asset3 = Asset(proj1, "TEST_ASSET3")
        asset4 = Asset(proj2, "TEST_ASSET3")

        self.assertFalse(asset1 != asset2)
        self.assertTrue(asset1 != asset3)
        self.assertTrue(asset3 != asset4)
Esempio n. 24
0
    def test_equality_of_assets(self):
        """testing if two assets are equal if their names and projects are also
        equal
        """

        proj1 = Project("EQUALITY_TEST_PROJECT_1")
        proj1.create()

        proj2 = Project("EQUALITY_TEST_PROJECT_2")
        proj2.create()

        asset1 = Asset(proj1, "TEST_ASSET1")
        asset2 = Asset(proj1, "TEST_ASSET1")

        asset3 = Asset(proj1, "TEST_ASSET3")
        asset4 = Asset(proj2, "TEST_ASSET3")

        self.assertTrue(asset1 == asset2)
        self.assertFalse(asset1 == asset3)
        self.assertFalse(asset3 == asset4)
Esempio n. 25
0
 def test_asset_attribute_returns_a_list_of_assets_related_to_this_project(self):
     """testing if the asset attribute returns a list of assets which are
     related to this project
     """
     new_proj1 = Project("Test Project 1")
     new_proj1.create()
     
     new_proj2 = Project("Test Project 2")
     new_proj2.create()
     
     new_proj3 = Project("Test Project 3")
     new_proj3.create()
     
     asset1 = Asset(new_proj1, "Asset 1")
     asset1.save()
     
     asset2 = Asset(new_proj1, "Asset 2")
     asset2.save()
     
     asset3 = Asset(new_proj2, "Asset 3")
     asset3.save()
     
     asset4 = Asset(new_proj2, "Asset 4")
     asset4.save()
     
     self.assertItemsEqual(new_proj1.assets, [asset1, asset2])
     self.assertItemsEqual(new_proj2.assets, [asset3, asset4])
     self.assertItemsEqual(new_proj3.assets, [])
Esempio n. 26
0
    def test_asset_attribute_returns_a_list_of_assets_related_to_this_project(
            self):
        """testing if the asset attribute returns a list of assets which are
        related to this project
        """
        new_proj1 = Project("Test Project 1")
        new_proj1.create()

        new_proj2 = Project("Test Project 2")
        new_proj2.create()

        new_proj3 = Project("Test Project 3")
        new_proj3.create()

        asset1 = Asset(new_proj1, "Asset 1")
        asset1.save()

        asset2 = Asset(new_proj1, "Asset 2")
        asset2.save()

        asset3 = Asset(new_proj2, "Asset 3")
        asset3.save()

        asset4 = Asset(new_proj2, "Asset 4")
        asset4.save()

        self.assertItemsEqual(new_proj1.assets, [asset1, asset2])
        self.assertItemsEqual(new_proj2.assets, [asset3, asset4])
        self.assertItemsEqual(new_proj3.assets, [])
Esempio n. 27
0
    def test_project_restores_from_database_2(self):
        """testing if a project restores it self from the database with all its
        connections
        """

        # we need to create a new project and a sequence
        new_proj = Project("TEST_PROJECT")
        new_proj.create()

        new_seq = Sequence(new_proj, "TEST_SEQ")
        new_seq.save()
        new_seq.create()

        db.session.add(new_proj)
        db.session.commit()

        del new_proj
        del new_seq

        # now retrieve the project by recreating it
        new_proj2 = Project(name="TEST_PROJECT")

        self.assertEqual(new_proj2.sequences[0].name, "TEST_SEQ")
Esempio n. 28
0
    def test_project_restores_from_database_2(self):
        """testing if a project restores it self from the database with all its
        connections
        """

        # we need to create a new project and a sequence
        new_proj = Project("TEST_PROJECT")
        new_proj.create()
        
        new_seq = Sequence(new_proj, "TEST_SEQ")
        new_seq.save()
        new_seq.create()
        
        db.session.add(new_proj)
        db.session.commit()

        del new_proj
        del new_seq

        # now retrieve the project by recreating it
        new_proj2 = Project(name="TEST_PROJECT")

        self.assertEqual(new_proj2.sequences[0].name, "TEST_SEQ")
Esempio n. 29
0
    def test_calling_create_over_and_over_again_will_not_cause_any_problem(self):
        """testing if calling the create over and over again will not create a
        problem
        """

        # we need to create a new project and a sequence
        new_proj = Project("TEST_PROJECT")
        new_proj.create()
        new_proj.create()
        new_proj.create()
        new_proj.create()
        new_proj.create()
        new_proj.create()
Esempio n. 30
0
    def test_calling_create_over_and_over_again_will_not_cause_any_problem(
            self):
        """testing if calling the create over and over again will not create a
        problem
        """

        # we need to create a new project and a sequence
        new_proj = Project("TEST_PROJECT")
        new_proj.create()
        new_proj.create()
        new_proj.create()
        new_proj.create()
        new_proj.create()
        new_proj.create()
Esempio n. 31
0
class AssetTester(unittest.TestCase):
    """tests the Asset class
    """
    def setUp(self):
        """setup the test settings with environment variables
        """
        # -----------------------------------------------------------------
        # start of the setUp
        # create the environment variable and point it to a temp directory
        conf.database_url = "sqlite://"

        self.temp_config_folder = tempfile.mkdtemp()
        self.temp_projects_folder = tempfile.mkdtemp()

        os.environ["OYPROJECTMANAGER_PATH"] = self.temp_config_folder
        os.environ[conf.repository_env_key] = self.temp_projects_folder

        self.test_proj = Project("TEST_PROJ1")
        self.test_proj.create()

        self.kwargs = {
            "project": self.test_proj,
            "name": "Test Asset",
            "code": "TEST_ASSET",
            'type': 'Prop',
        }

        self.test_asset = Asset(**self.kwargs)
        self.test_asset.save()

        self._name_test_values = [
            ("Test Asset", "Test Asset"),
            ("23Test_Asset", "23Test_Asset"),
            ("TEST_ASSET", "TEST_ASSET"),
            ("£#$£#$AB", "AB"),
            ("'^+'^%^+%&&AB3£#$£½'^+'3'^+'4", "AB334"),
            ("afasfas fasf asdf67", "Afasfas fasf asdf67"),
            ("45a", "45a"),
            ("45acafs", "45acafs"),
            ("45'^+'^+a 234", "45a 234"),
            ("45asf78wr", "45asf78wr"),
            ("'^+'afsd2342'^+'asdFGH", "Afsd2342asdFGH"),
        ]

        self._code_test_values = [
            ("Test Asset", "Test_Asset"),
            ("23Test_Asset", "23Test_Asset"),
            ("TEST_ASSET", "TEST_ASSET"),
            ("£#$£#$AB", "AB"),
            ("'^+'^%^+%&&AB3£#$£½'^+'3'^+'4", "AB334"),
            ("afasfas fasf asdf67", "Afasfas_fasf_asdf67"),
            ("45a", "45a"),
            ("45acafs", "45acafs"),
            ("45'^+'^+a 234", "45a_234"),
            ("45asf78wr", "45asf78wr"),
            ("'^+'afsd2342'^+'asdFGH", "Afsd2342asdFGH"),
        ]

    def tearDown(self):
        """cleanup the test
        """

        # set the db.session to None
        db.session = None

        # delete the temp folder
        shutil.rmtree(self.temp_config_folder)
        shutil.rmtree(self.temp_projects_folder)

    def test_name_argument_is_skipped(self):
        """testing if a TypeError will be raised when the name argument is
        skipped
        """
        self.kwargs.pop("name")
        self.assertRaises(TypeError, Asset, **self.kwargs)

    def test_name_argument_is_None(self):
        """testing if a TypeError will be raised when the name argument is None
        """
        self.kwargs["name"] = None
        self.assertRaises(TypeError, Asset, **self.kwargs)

    def test_name_attribute_is_None(self):
        """testing if a TypeError will be raised when the name attribute is set
        to None
        """
        self.assertRaises(TypeError, setattr, self.test_asset, "name", None)

    def test_name_argument_is_not_a_string(self):
        """testing if a TypeError will be raised when the name argument is not
        a string
        """
        self.kwargs["name"] = 123445
        self.assertRaises(TypeError, Asset, **self.kwargs)

    def test_name_attribute_is_not_a_string(self):
        """testing if a TypeError will be raised when the name attribute is not
        a string
        """
        self.assertRaises(TypeError, setattr, self.test_asset, "name", 123456)

    def test_name_argument_is_working_properly(self):
        """test if the name attribute initialized correctly with the value of
        the name argument
        """
        test_value = "Test Value"
        self.kwargs["name"] = test_value
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.name, test_value)

    def test_name_attribute_is_working_properly(self):
        """testing if the name attribute is working properly
        """
        test_value = "Test Value"
        self.test_asset.name = test_value
        self.assertEqual(self.test_asset.name, test_value)

    def test_name_argument_formatting(self):
        """testing if the name argument will be formatted correctly
        """
        for test_value in self._name_test_values:
            self.kwargs["name"] = test_value[0]
            new_asset = Asset(**self.kwargs)
            self.assertEqual(new_asset.name, test_value[1])

    def test_name_attribute_formatting(self):
        """testing if the name attribute will be formatted correctly
        """
        for test_value in self._name_test_values:
            self.test_asset.name = test_value[0]
            self.assertEqual(self.test_asset.name, test_value[1])

    def test_name_argument_is_empty_string_after_formatting(self):
        """testing if a ValueError will be raised when the name argument is an
        empty string after formatting
        """
        self.kwargs["name"] = "£#$£'^+'"
        self.assertRaises(ValueError, Asset, **self.kwargs)

    def test_name_attribute_is_empty_string_after_formatting(self):
        """testing if a ValueError will be raised when the name attribugte is
        an empty string after formatting
        """
        self.assertRaises(ValueError, setattr, self.test_asset, "name",
                          "£#$£'^+'")

    def test_name_argument_is_not_unique(self):
        """testing if a IntegrityError will be raised when the name is unique
        """
        # create an asset with the same name
        new_asset = Asset(**self.kwargs)
        self.assertRaises(IntegrityError, new_asset.save)

    def test_code_argument_is_skipped(self):
        """testing if the code attribute will be get from the name attribute if
        the code argument is skipped
        """
        self.kwargs["name"] = "Test Value"
        self.kwargs.pop("code")
        expected_value = "Test_Value"
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.code, expected_value)

    def test_code_argument_is_None(self):
        """testing if the code attribute will be get from the name attribute if
        the code argument is None
        """
        self.kwargs["name"] = "Test Value"
        self.kwargs["code"] = None
        expected_value = "Test_Value"
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.code, expected_value)

    def test_code_attribute_is_None(self):
        """testing if the code attribute will be get from the name attribute if
        it is set to None
        """
        self.test_asset.name = "Test Value"
        self.test_asset.code = None
        expected_value = "Test_Value"
        self.assertEqual(self.test_asset.code, expected_value)

    def test_code_argument_is_not_a_string_instance(self):
        """testing if a TypeError will be raised when the code argument is not
        an instance of string or unicode
        """
        self.kwargs["code"] = 2134
        self.assertRaises(TypeError, Asset, **self.kwargs)

    def test_code_attribute_is_not_a_string_instance(self):
        """testing if a TypeError will be raised when the code attribute is set
        to a value which is not a string or unicode
        """
        self.assertRaises(TypeError, setattr, self.test_asset, "code", 2342)

    def test_code_argument_is_working_properly(self):
        """testing if the code attribute is set from the code argument
        """
        self.kwargs["code"] = "TEST_VALUE"
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.code, self.kwargs["code"])

    def test_code_attribute_is_working_properly(self):
        """testing if the code attribute is working properly
        """
        test_value = "TEST_VALUE"
        self.test_asset.code = test_value
        self.assertEqual(self.test_asset.code, test_value)

    def test_code_argument_formatting(self):
        """testing if the code argument is formatted correctly
        """
        for test_value in self._code_test_values:
            self.kwargs["code"] = test_value[0]
            new_asset = Asset(**self.kwargs)
            self.assertEqual(new_asset.code, test_value[1])

    def test_code_attribute_formatting(self):
        """testing if the code attribute is formatted correctly
        """
        for test_value in self._code_test_values:
            self.test_asset.code = test_value[0]
            self.assertEqual(self.test_asset.code, test_value[1])

    def test_code_argument_is_empty_string_after_formatting(self):
        """testing if a ValueError will be raised when the code argument is an
        empty string after formatting
        """
        self.kwargs["code"] = "'^+'%+%"
        self.assertRaises(ValueError, Asset, **self.kwargs)

    def test_code_attribute_is_empty_string_after_formatting(self):
        """testing if a ValueError will be raised when the code attribugte is
        an empty string after formatting
        """
        self.assertRaises(ValueError, setattr, self.test_asset, "code",
                          "'^+'%+%")

    def test_code_argument_is_not_unique(self):
        """testing if an IntegrityError will be raised when the code argument
        is not unique
        """
        self.kwargs["name"] = "Another_Asset_Name"
        new_asset = Asset(**self.kwargs)
        self.assertRaises(IntegrityError, new_asset.save)

    def test_save_method_saves_the_asset_to_the_database(self):
        """testing if the save method saves the asset to the database
        """
        self.test_asset.save()
        self.assertTrue(self.test_asset in db.session)

    def test_equality_of_assets(self):
        """testing if two assets are equal if their names and projects are also
        equal
        """

        proj1 = Project("EQUALITY_TEST_PROJECT_1")
        proj1.create()

        proj2 = Project("EQUALITY_TEST_PROJECT_2")
        proj2.create()

        asset1 = Asset(proj1, "TEST_ASSET1")
        asset2 = Asset(proj1, "TEST_ASSET1")

        asset3 = Asset(proj1, "TEST_ASSET3")
        asset4 = Asset(proj2, "TEST_ASSET3")

        self.assertTrue(asset1 == asset2)
        self.assertFalse(asset1 == asset3)
        self.assertFalse(asset3 == asset4)

    def test_inequality_of_assets(self):
        """testing if two assets are inequal if their names are different and
        or their projects are different
        """

        proj1 = Project("EQUALITY_TEST_PROJECT_1")
        proj1.create()

        proj2 = Project("EQUALITY_TEST_PROJECT_2")
        proj2.create()

        asset1 = Asset(proj1, "TEST_ASSET1")
        asset2 = Asset(proj1, "TEST_ASSET1")

        asset3 = Asset(proj1, "TEST_ASSET3")
        asset4 = Asset(proj2, "TEST_ASSET3")

        self.assertFalse(asset1 != asset2)
        self.assertTrue(asset1 != asset3)
        self.assertTrue(asset3 != asset4)

    def test_type_argument_is_skipped(self):
        """testing if skipping the type argument the type attribute will be set
        to conf.default_asset_type_name
        """
        self.kwargs.pop('type')
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.type, conf.default_asset_type_name)

    def test_type_argument_is_None(self):
        """testing if setting the type argument to None will set the type
        attribute to conf.default_asset_type_name
        """
        self.kwargs['type'] = None
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.type, conf.default_asset_type_name)

    def test_type_attribute_is_set_to_None(self):
        """testing if setting the type attribute to None will set the type to
        conf.default_asset_type_name
        """
        self.test_asset.type = None
        self.assertEqual(self.test_asset.type, conf.default_asset_type_name)

    def test_type_argument_accepts_string_or_unicode_only(self):
        """testing if a TypeError will be raised when the type argument is set
        to a value other than string or unicode value
        """
        self.kwargs['type'] = 12312
        self.assertRaises(TypeError, Asset, **self.kwargs)

    def test_type_attribute_accepts_string_or_unicode_only(self):
        """testing if a TypeError will be raised when the type attribute is set
        to a value other than string or unicode
        """
        self.assertRaises(TypeError, setattr, self.test_asset, 'type', 2342)

    def test_type_argument_is_working_properly(self):
        """testing if the type attribute is set with the type argument
        """
        self.kwargs['type'] = "Test_Type_1"
        new_asset = Asset(**self.kwargs)
        self.assertEqual(self.kwargs['type'], new_asset.type)

    def test_type_attribute_is_working_properly(self):
        """testing if the type attribute is working properly
        """
        test_value = "Test_Type_1"
        self.test_asset.type = test_value
        self.assertEqual(self.test_asset.type, test_value)

    def test_type_argument_formatting(self):
        """testing if the type argument is formatted correctly
        """
        for test_values in self._code_test_values:
            input_value = test_values[0]
            expected_value = test_values[1]
            self.kwargs['type'] = input_value
            new_asset = Asset(**self.kwargs)
            self.assertEqual(new_asset.type, expected_value)

    def test_type_argument_is_invalid_after_formatting(self):
        """testing if a ValueError will be raised when the type argument is
        invalid after formatting
        """
        self.kwargs['type'] = '@#$@#$'
        self.assertRaises(ValueError, Asset, **self.kwargs)

    def test_type_attribute_is_invalid_after_formatting(self):
        """testing if a ValueError will be raised when the type attribute is
        invalid after formatting
        """
        self.assertRaises(ValueError, setattr, self.test_asset, 'type', '#@$#')

    def test_type_attribute_formatting(self):
        """testing if the type attribute is formatted correctly
        """
        for test_values in self._code_test_values:
            input_value = test_values[0]
            expected_value = test_values[1]
            self.test_asset.type = input_value
            self.assertEqual(self.test_asset.type, expected_value)

    def test_deleting_an_asset_will_not_delete_the_related_project(self):
        """testing if deleting an asset will not delete the related project
        """
        proj1 = Project('test project 1')
        proj1.save()

        asset = Asset(proj1, 'Test asset')
        asset.save()

        # check if they are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(asset, db.session)

        # delete the asset
        db.session.delete(asset)
        db.session.commit()

        # check if it is removed from the session
        self.assertNotIn(asset, db.session)

        # and the project is there
        self.assertIn(proj1, db.session)

    def test_deleting_an_asset_will_not_delete_the_other_assets_in_the_related_project(
            self):
        """testing if deleting an asset will not delete the other assets in the
        related project
        """
        proj1 = Project('test project 1')
        proj1.save()

        asset1 = Asset(proj1, 'test asset 1')
        asset1.save()

        asset2 = Asset(proj1, 'test asset 2')
        asset2.save()

        asset3 = Asset(proj1, 'test asset 3')
        asset3.save()

        # check if they are properly in the db.session
        self.assertIn(proj1, db.session)
        self.assertIn(asset1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(asset3, db.session)

        # delete asset1
        db.session.delete(asset1)
        db.session.commit()

        # check if the asset1 is deleted
        self.assertNotIn(asset1, db.session)

        # and the others are in place
        self.assertIn(proj1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(asset3, db.session)

    def test_deleting_an_asset_will_delete_all_the_related_versions(self):
        """testing if deleting an asset will also delete the related versions
        """
        proj1 = Project('test project 1')
        proj1.save()

        asset1 = Asset(proj1, 'test asset 1')
        asset1.save()

        asset2 = Asset(proj1, 'test asset 2')
        asset2.save()

        asset_vtypes = VersionType.query().filter_by(type_for="Asset").all()

        user = User.query().first()

        vers1 = Version(version_of=asset1,
                        base_name=asset1.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers1.save()

        vers2 = Version(version_of=asset1,
                        base_name=asset1.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers2.save()

        vers3 = Version(version_of=asset1,
                        base_name=asset1.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers3.save()

        vers4 = Version(version_of=asset2,
                        base_name=asset2.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers4.save()

        vers5 = Version(version_of=asset2,
                        base_name=asset2.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers5.save()

        vers6 = Version(version_of=asset2,
                        base_name=asset2.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers6.save()

        # check if all are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(asset1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(vers1, db.session)
        self.assertIn(vers2, db.session)
        self.assertIn(vers3, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)

        # delete the asset
        db.session.delete(asset1)
        db.session.commit()

        # check if it is not in the session anymore
        self.assertNotIn(asset1, db.session)

        # check if the versions are also deleted
        self.assertNotIn(vers1, db.session)
        self.assertNotIn(vers2, db.session)
        self.assertNotIn(vers3, db.session)

        # check if the others are still there
        self.assertIn(proj1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)

    def test_deleting_an_asset_will_delete_all_the_related_versions_but_keep_references(
            self):
        """testing if deleting an asset will only delete the version of that
        asset and will keep the referenced versions.
        """
        proj1 = Project('test project 1')
        proj1.save()

        asset1 = Asset(proj1, 'test asset 1')
        asset1.save()

        asset2 = Asset(proj1, 'test asset 2')
        asset2.save()

        asset_vtypes = VersionType.query().filter_by(type_for="Asset").all()

        user = User.query().first()

        vers1 = Version(version_of=asset1,
                        base_name=asset1.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers1.save()

        vers2 = Version(version_of=asset1,
                        base_name=asset1.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers2.save()

        vers3 = Version(version_of=asset1,
                        base_name=asset1.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers3.save()

        vers4 = Version(version_of=asset2,
                        base_name=asset2.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers4.save()

        vers5 = Version(version_of=asset2,
                        base_name=asset2.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers5.save()

        vers6 = Version(version_of=asset2,
                        base_name=asset2.code,
                        type=asset_vtypes[0],
                        created_by=user)
        vers6.save()

        # reference vers4, vers5 and vers6 to vers1, vers2 and vers3
        vers1.references.append(vers4)
        vers1.references.append(vers5)
        vers1.references.append(vers6)

        vers2.references.append(vers4)
        vers2.references.append(vers5)
        vers2.references.append(vers6)

        vers3.references.append(vers4)
        vers3.references.append(vers5)
        vers3.references.append(vers6)

        # check if all are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(asset1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(vers1, db.session)
        self.assertIn(vers2, db.session)
        self.assertIn(vers3, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)

        # there should be 9 entries in the secondary table
        result = db.session\
            .query('referencer_id', 'reference_id')\
            .from_statement("SELECT referencer_id, reference_id "
                            "FROM Version_References").all()
        self.assertEqual(len(result), 9)

        # delete the asset
        db.session.delete(asset1)
        db.session.commit()

        # check if it is not in the session anymore
        self.assertNotIn(asset1, db.session)

        # check if the versions are also deleted
        self.assertNotIn(vers1, db.session)
        self.assertNotIn(vers2, db.session)
        self.assertNotIn(vers3, db.session)

        # check if the others are still there
        self.assertIn(proj1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)

        # to be sure check the secondary table
        result = db.session\
            .query('referencer_id', 'reference_id')\
            .from_statement("SELECT referencer_id, reference_id "
                            "FROM Version_References").all()
        self.assertEqual(len(result), 0)
Esempio n. 32
0
class ShotTester(unittest.TestCase):
    """tests the Shot class
    """
    
    def setUp(self):
        """setup the test settings with environment variables
        """
        # -----------------------------------------------------------------
        # start of the setUp
        conf.database_url = "sqlite://"
        
        # create the environment variable and point it to a temp directory
        self.temp_config_folder = tempfile.mkdtemp()
        self.temp_projects_folder = tempfile.mkdtemp()
        
        os.environ["OYPROJECTMANAGER_PATH"] = self.temp_config_folder
        os.environ[conf.repository_env_key] = self.temp_projects_folder
        
        self.test_proj = Project("TEST_PROJ1")
        self.test_proj.create()
        
        self.test_seq = Sequence(self.test_proj, "TEST_SEQ")
        self.test_seq.save()
        self.test_seq.create()
        
        self.kwargs = {
            "sequence": self.test_seq,
            "number": 1,
            "start_frame": 1,
            "end_frame": 100,
            "description": "Test shot"
        }
        
        self.test_shot = Shot(**self.kwargs)
        
        self._number_test_values = [
            (23, "23"),
            ("24", "24"),
            ("324ASF", "324ASF"),
            ("AD43", "AD43"),
            ("AS43A", "AS43A"),
            ("afasfas fasf    asdf67", "AFASFAS_FASF_ASDF67"),
            ("45a", "45A"),
            ("45acafs","45ACAFS"),
            ("45'^+'^+b", "45B"),
            ("45asf78wr", "45ASF78WR"),
            ("'^+'afsd2342'^+'asdFGH", "AFSD2342ASDFGH"),
            ("46B-3-B", "46B-3-B"),
            ("XB58P-4-C", "XB58P-4-C"),
            ("A143AN-04-D", "A143AN-04-D"),
            ("xb58q-2-d", "XB58Q-2-D"),
            ("underscores_are_allowed", "UNDERSCORES_ARE_ALLOWED"),
            ("304_sb_0403_0040", "304_SB_0403_0040"),
            #("0001", "1"),
        ]
    
    def tearDown(self):
        """cleanup the test
        """
        # set the db.session to None
        db.session = None
        
        # delete the temp folder
        shutil.rmtree(self.temp_config_folder)
        shutil.rmtree(self.temp_projects_folder)
    
    def test_number_argument_is_skipped(self):
        """testing if a TypeError will be raised when the number argument is
        skipped
        """
        self.kwargs.pop("number")
        self.assertRaises(TypeError, Shot, **self.kwargs)
    
    def test_number_argument_is_None(self):
        """testing if a TypeError will be raised when the number argument is
        None
        """
        self.kwargs["number"] = None
        self.assertRaises(TypeError, Shot, **self.kwargs)

    def test_number_attribute_is_None(self):
        """testing if a TypeError will be raised when the number attribute is
        set to None
        """
        self.assertRaises(TypeError, setattr, self.test_shot, "number", None)
    
    def test_number_argument_is_empty_string(self):
        """testing if a ValueError will be raised when the number argument is
        an empty string
        """
        self.kwargs["number"] = ""
        self.assertRaises(ValueError, Shot, **self.kwargs)
    
    def test_number_attribute_is_set_to_empty_string(self):
        """testing if a ValueError will be raised when the number attribute is
        set to an empty string
        """
        self.assertRaises(ValueError, setattr, self.test_shot, "number", "")
    
    def test_number_argument_is_not_a_string_or_integer(self):
        """testing if a TypeError will be raised when the number argument is
        not a string or integer
        """
        self.kwargs["number"] = [123]
        self.assertRaises(TypeError, Shot, **self.kwargs)
    
    def test_number_attribute_is_not_a_string_integer(self):
        """testing if a TypeError will be raised when the number attribute is
        not a string or integer
        """
        self.assertRaises(TypeError, setattr, self.test_shot, "number", [])
    
    def test_number_argument_formatted_correctly(self):
        """testing if the number attribute is formatted correctly when the class
        is initialized
        """
        for test_value in self._number_test_values:
            self.kwargs["number"] = test_value[0]
            new_shot = Shot(**self.kwargs)
            self.assertEqual(test_value[1], new_shot.number)
            
    
#    def test_number_attribute_formatted_correctly(self):
#        """testing if the number attribute is formatted correctly
#        """
#        for test_value in self._number_test_values:
#            self.kwargs["number"] = test_value[0]
#            new_shot = Shot(**self.kwargs)
#            self.assertEqual(test_value[1], new_shot.number)
    
    def test_number_is_already_defined_in_the_sequence(self):
        """testing if a ValueError will be raised when the shot number is
        already defined in the given Sequence
        """
        self.kwargs["number"] = 101
        new_shot1 = Shot(**self.kwargs)
        self.assertRaises(
            ValueError,
            Shot,
            **self.kwargs
        )
    
    def test_number_is_already_defined_in_the_sequence_for_an_already_created_one(self):
        """testing if a ValueError will be raised when the number is already
        defined for a Shot in the same Sequence instance
        """
        self.kwargs["number"] = 101
        new_shot1 = Shot(**self.kwargs)
        new_shot1.save()
        
        self.assertRaises(ValueError, Shot, **self.kwargs)
    
    def test_number_argument_is_string_or_integer(self):
        """testing if both strings and integers are ok to pass to the number
        argument
        """
        self.kwargs["number"] = 10
        new_shot1 = Shot(**self.kwargs)
        self.assertEqual(new_shot1.number, "10")
        
        self.kwargs["number"] = "11A"
        new_shot2 = Shot(**self.kwargs)
        self.assertEqual(new_shot2.number, "11A")
    
    def test_code_attribute_is_calculated_from_the_number_argument(self):
        """testing if the code attribute is calculated from the number
        argument
        """
        
        self.kwargs["number"] = 10
        new_shot1 = Shot(**self.kwargs)
        self.assertEqual(new_shot1.code, "SH010")
        
        self.kwargs["number"] = "10A"
        new_shot1 = Shot(**self.kwargs)
        self.assertEqual(new_shot1.code, "SH010A")
        
        self.kwargs["number"] = "A143AN-04-D"
        new_shot1 = Shot(**self.kwargs)
        self.assertEqual(new_shot1.code, "SHA143AN-04-D")
        
        self.kwargs["number"] = "A143AN-04-DB"
        self.test_proj.shot_number_prefix = ""
        new_shot1 = Shot(**self.kwargs)
        self.assertEqual(new_shot1.code, "A143AN-04-DB")
        
        self.kwargs['number'] = '304_sb_0403_0040'
        self.test_proj.shot_number_prefix = ""
        new_shot1 = Shot(**self.kwargs)
        self.assertEqual(new_shot1.code, '304_SB_0403_0040')
    
    def test_code_attribute_is_calculated_from_the_number_attribute(self):
        """testing if the code attribute is calculated from the number
        attribute
        """
        
        self.test_shot.number = 10
        self.assertEqual(self.test_shot.code, "SH010")
        
        self.test_shot.number = "10A"
        self.assertEqual(self.test_shot.code, "SH010A")
    
    def test_code_attribute_is_read_only(self):
        """testing if the code attribute is read_only
        """
        self.assertRaises(AttributeError, setattr, self.test_shot, "code",
                          "SH010A")
    
#    def test_code_argument_is_not_in_good_format(self):
#        """testing if a ValueError will be raised when the code argument format
#        is not correct
#        """
#        self.kwargs["code"] = "wrong format"
#        self.assertRaises(ValueError, Shot, **self.kwargs)
#    
#    def test_code_attribute_is_not_in_good_format(self):
#        """testing if a ValueError will be raised when the code attribute
#        format is not correct
#        """
#        self.assertRaises(ValueError, setattr, self.test_shot, "code",
#                          "wrong format")
#    
#    def test_code_argument_is_formatted_correctly(self):
#        """testing if the code argument is formatted correctly
#        """
#        for test_value in self._code_test_values:
#            self.kwargs["code"] = test_value[0]
#            new_shot = Shot(**self.kwargs)
#            self.assertEqual(new_shot.code, test_value[1])
#    
#    def test_code_attribute_is_formatted_correctly(self):
#        """testing if the code attribute is formatted correctly
#        """
#        for test_value in self._code_test_values:
#            self.test_shot.code = test_value[0]
#            self.assertEqual(self.test_shot.code, test_value[1])
    
    def test_sequence_argument_is_skipped(self):
        """testing if a TypeError will be raised when the sequence argument
        is skipped
        """
        self.kwargs.pop("sequence")
        self.assertRaises(TypeError, Shot, **self.kwargs)
    
    def test_sequence_argument_is_None(self):
        """testing if a TypeError will be raised when the sequence argument
        is None
        """
        self.kwargs["sequence"] = None
        self.assertRaises(TypeError, Shot, **self.kwargs)
    
    def test_sequence_argument_is_not_a_Sequence_instance(self):
        """testing if a TypeError will be raised when the sequence argument is
        not a Sequence instance
        """
        self.kwargs["sequence"] = "not a sequence instance"
        self.assertRaises(TypeError, Shot, **self.kwargs)
    
    def test_sequence_argument_is_working_properly(self):
        """testing if the sequence argument is working correctly
        """
        self.assertTrue(self.test_shot.sequence is self.test_seq)
    
    def test_sequence_attribute_is_read_only(self):
        """testing if the sequence attribute is read-only
        """
        new_seq = Sequence(self.test_proj, "TEST_SEQ2")
        new_seq.save()
        self.assertRaises(AttributeError, setattr, self.test_shot, "sequence",
                          new_seq)
    
    def test_start_frame_argument_is_skipped(self):
        """testing if the start_frame attribute will be set to the default
        value when the start_frame argument is skipped
        """
        self.kwargs['number'] = '1a'
        self.kwargs.pop("start_frame")
        new_shot = Shot(**self.kwargs)
        self.assertEqual(new_shot.start_frame, 1)
    
    def test_start_frame_argument_is_None(self):
        """testing if the start_frame attribute will be set to the default
        value when the start_frame argument is skipped
        """
        self.kwargs['number'] = '1a'
        self.kwargs["start_frame"] = None
        new_shot = Shot(**self.kwargs)
        self.assertEqual(new_shot.start_frame, 1)
    
    def test_start_frame_attribute_is_None(self):
        """testing if the start_frame attribute will be set to the default
        value when it is set to None
        """
        self.test_shot.start_frame = None
        self.assertEqual(self.test_shot.start_frame, 1)
    
    def test_start_frame_argument_is_not_integer(self):
        """testing if a TypeError will be raised when the start_frame argument
        is not an integer
        """
        self.kwargs['number'] = '1a'
        self.kwargs["start_frame"] = "asdfa"
        self.assertRaises(TypeError, Shot, **self.kwargs)
    
    def test_start_frame_attribute_is_not_integer(self):
        """testing if a TypeError will be raised when the start_frame attribute
        is not set to an integer value
        """
        self.assertRaises(TypeError, setattr, self.test_shot, "start_frame",
                          "asdfs")
    
    def test_start_frame_attribute_is_working_properly(self):
        """testing if the start_frame attribute is working properly
        """
        test_value = 10
        self.test_shot.start_frame = test_value
        self.assertEqual(self.test_shot.start_frame, test_value)
    
    def test_start_frame_attribute_updates_the_duration_attribute(self):
        """testing if the start_frame attribute updates the duration attribute
        value
        """
        self.test_shot.start_frame = 10
        self.assertEqual(self.test_shot.end_frame, 100)
        self.assertEqual(self.test_shot.duration, 91)
    
    def test_end_frame_argument_is_skipped(self):
        """testing if the end_frame attribute will be set to the default
        value when the end_frame argument is skipped
        """
        self.kwargs['number'] = '1a'
        self.kwargs.pop("end_frame")
        new_shot = Shot(**self.kwargs)
        self.assertEqual(new_shot.end_frame, 1)
    
    def test_end_frame_argument_is_None(self):
        """testing if the end_frame attribute will be set to the default
        value when the end_frame argument is skipped
        """
        self.kwargs['number'] = '1a'
        self.kwargs["end_frame"] = None
        new_shot = Shot(**self.kwargs)
        self.assertEqual(new_shot.end_frame, 1)
    
    def test_end_frame_attribute_is_Non(self):
        """testing if the end_frame attribute will be set to the default
        value when it is set to None
        """
        self.test_shot.end_frame = None
        self.assertEqual(self.test_shot.end_frame, 1)
    
    def test_end_frame_argument_is_not_integer(self):
        """testing if a TypeError will be raised when the end_frame argument
        is not an integer
        """
        self.kwargs['number'] = '1a'
        self.kwargs["end_frame"] = "asdfa"
        self.assertRaises(TypeError, Shot, **self.kwargs)
    
    def test_end_frame_attribute_is_not_integer(self):
        """testing if a TypeError will be raised when the end_frame attribute
        is not set to an integer value
        """
        self.assertRaises(TypeError, setattr, self.test_shot, "end_frame",
                          "asdfs")
    
    def test_end_frame_attribute_is_working_properly(self):
        """testing if the end_frame attribute is working properly
        """
        test_value = 10
        self.test_shot.end_frame = test_value
        self.assertEqual(self.test_shot.end_frame, test_value)
    
    def test_end_frame_attribute_updates_the_duration_attribute(self):
        """testing if the end_frame attribute updates the duration attribute
        value
        """
        self.test_shot.end_frame = 200
        self.assertEqual(self.test_shot.start_frame, 1)
        self.assertEqual(self.test_shot.duration, 200)
    
    def test_duration_attribute_is_initialized_correctly(self):
        """testing if the duration attribute is initialized correctly 
        """
        self.assertEqual(
            self.test_shot.duration,
            self.test_shot.end_frame - self.test_shot.start_frame + 1
        ) 
    
    def test_duration_attribute_is_updated_correctly(self):
        """testing if the duration attribute is updated correctly with the
        changing values of start_frame and end_frame values
        """
        self.kwargs['number'] = '1duration'
        new_shot = Shot(**self.kwargs)
        self.assertEqual(new_shot.start_frame, 1)
        self.assertEqual(new_shot.end_frame, 100)
        new_shot.start_frame = 10
        self.assertEqual(new_shot.duration, 91)
        
        new_shot.end_frame = 110
        self.assertEqual(new_shot.duration, 101)
    
    def test_project_attribute_is_initialized_correctly(self):
        """testing if the project attribute is initialized correctly
        """
        self.assertTrue(self.test_shot.project is
                        self.kwargs["sequence"].project)

    def test_shot_is_CRUD_properly_in_the_database(self):
        """testing if the shot instance is created properly in the database
        """
        new_proj = Project("TEST_PROJ_FOR_CRUD")
        new_proj.create()
        
        new_seq = Sequence(new_proj, "TEST_SEQ103")
        new_seq.save()
        
        new_shot = Shot(new_seq, '1a')
        new_shot.save()
        
        # now query the database if it is created and read correctly
        self.assertEqual(
            new_shot,
            Shot.query()
                .filter(Shot.number==new_shot.number)
                .first()
        )
        
        # now update it
        new_shot.start_frame = 100
        new_shot.end_frame = 200
        new_shot.save()
        
        self.assertEqual(
            new_shot.start_frame,
            Shot.query()
                .filter(Shot.number==new_shot.number)
                .first()
                .start_frame
        )
        self.assertEqual(
            new_shot.end_frame,
            Shot.query()
                .filter(Shot.number==new_shot.number)
                .first()
                .end_frame
        )
        
        # now delete it
        db.session.delete(new_shot)
        db.session.commit()
        
        self.assertEqual(len(Shot.query().all()), 1)
    
    def test_equality_of_shots(self):
        """testing if the equality operator is working properly
        """
        proj1 = Project("TEST_EQ_PROJ")
        proj1.create()
        
        seq1 = Sequence(proj1, "TEST_SEQ1")
        seq2 = Sequence(proj1, "TEST_SEQ2")
        
        shot1 = Shot(seq1, 1)
        shot2 = Shot(seq1, 2)
        shot3 = Shot(seq2, 1)
        
        #shot1a = Shot(seq1, 1)
        shot1a = seq1.shots[0]
        
        self.assertTrue(shot1==shot1a)
        self.assertFalse(shot1==shot2)
        self.assertFalse(shot1a==shot3)
    
    def test_inequality_of_shots(self):
        """testing if the equality operator is working properly
        """
        proj1 = Project("TEST_EQ_PROJ")
        proj1.create()
        
        seq1 = Sequence(proj1, "TEST_SEQ1")
        seq2 = Sequence(proj1, "TEST_SEQ2")
        
        shot1 = Shot(seq1, 1)
        shot2 = Shot(seq1, 2)
        shot3 = Shot(seq2, 1)
        
        #shot1a = Shot(seq1, 1)
        shot1a = seq1.shots[0]
        
        self.assertFalse(shot1!=shot1a)
        self.assertTrue(shot1!=shot2)
        self.assertTrue(shot1a!=shot3)
    
    def test_handle_at_start_attribute_not_an_integer(self):
        """testing if a TypeError will be raised when the handles_at_start
        attribute is set anything other then an integer
        """
        
        self.assertRaises(
            TypeError, setattr, self.test_shot, "handle_at_start",
            "test value"
        )
    
    def test_handle_at_start_defaults_to_zero(self):
        """testing if the default value of handle_at_start is 0
        """
        
        self.assertEqual(self.test_shot.handle_at_start, 0)
    
    def test_handle_at_start_is_negative(self):
        """testing if a ValueError will be raised when the handle_at_start is
        a negative value
        """
        
        self.assertRaises(
            ValueError, setattr, self.test_shot, "handle_at_start", -10
        )
    
    def test_handle_at_start_is_bigger_then_duration_minus_one(self):
       """testing if a ValueError will be raised when the handle_at_start is
       bigger then the duration-1
       """
       
       self.assertRaises(
           ValueError, setattr, self.test_shot, "handle_at_start",
           self.test_shot.duration
       )
    
    def test_handles_together_are_bigger_then_duration_minus_one_while_setting_handle_at_start(self):
       """testing if a ValueError will be raised when the handle_at_start and
       handle_at_end together are greater then duration-1
       """
       
       self.test_shot.start_frame = 1
       self.test_shot.end_frame = 100
       self.test_shot.handle_at_end = 50
       self.assertRaises(
           ValueError, setattr, self.test_shot, "handle_at_start", 50
       )
    
    def test_handle_at_end_attribute_not_an_integer(self):
        """testing if a TypeError will be raised when the handles_at_start
        attribute is set anything other then an integer
        """
        
        self.assertRaises(
            TypeError, setattr, self.test_shot, "handle_at_end",
            "test value"
        )
    
    def test_handle_at_end_defaults_to_zero(self):
        """testing if the default value of handle_at_end is 0
        """
        self.assertEqual(self.test_shot.handle_at_end, 0)
    
    def test_handle_at_end_is_negative(self):
        """testing if a ValueError will be raised when the handle_at_end is
        a negative value
        """
        self.assertRaises(
            ValueError, setattr, self.test_shot, "handle_at_end", -10
        )
    
    def test_handle_at_end_is_bigger_then_duration_minus_one(self):
       """testing if a ValueError will be raised when the handle_at_end is
       bigger then the duration-1
       """
       self.assertRaises(
           ValueError, setattr, self.test_shot, "handle_at_end",
           self.test_shot.duration
       )
    
    def test_handles_together_are_bigger_then_duration_minus_one_while_setting_handle_at_end(self):
       """testing if a ValueError will be raised when the handle_at_start and
       handle_at_end together are greater then duration-1
       """
       
       self.test_shot.start_frame = 1
       self.test_shot.end_frame = 100
       self.test_shot.handle_at_start = 50
       self.assertRaises(
           ValueError, setattr, self.test_shot, "handle_at_end", 50
       )
    
    def test_deleting_a_shot_will_not_delete_the_related_project(self):
        """testing if deleting a shot will not delete the related project
        """
        proj1 = Project('test project 1')
        proj1.save()
        
        seq1 = Sequence(proj1, 'test seq 1')
        seq1.save()
        
        shot1 = Shot(seq1, 1)
        shot1.save()
        
        # check if they are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(seq1, db.session)
        self.assertIn(shot1, db.session)
        
        # delete the shot
        db.session.delete(shot1)
        db.session.commit()
        
        self.assertNotIn(shot1, db.session)
        
        self.assertIn(proj1, db.session)
    
    def test_deleting_a_shot_will_not_delete_the_related_sequence(self):
        """testing if deleting a shot will not delete the related sequence
        """
        proj1 = Project('test project 1')
        proj1.save()
        
        seq1 = Sequence(proj1, 'test seq 1')
        seq1.save()
        
        shot1 = Shot(seq1, 1)
        shot1.save()
        
        # check if they are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(seq1, db.session)
        self.assertIn(shot1, db.session)
        
        # delete the shot
        db.session.delete(shot1)
        db.session.commit()
        
        self.assertNotIn(shot1, db.session)
        self.assertIn(seq1, db.session)
    
    def test_deleting_a_shot_will_not_delete_the_other_shots_in_the_related_sequence(self):
        """testing if deleting a shot will not delete the other shots in the
        sequence
        """
        proj1 = Project('test project 1')
        proj1.save()
        
        seq1 = Sequence(proj1, 'test seq 1')
        seq1.save()
        
        shot1 = Shot(seq1, 1)
        shot1.save()
        
        shot2 = Shot(seq1, 2)
        shot2.save()
        
        # check if they are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(seq1, db.session)
        self.assertIn(shot1, db.session)
        self.assertIn(shot2, db.session)
        
        # delete the shot
        db.session.delete(shot1)
        db.session.commit()
        
        self.assertNotIn(shot1, db.session)
        self.assertIn(shot2, db.session)
    
    def test_deleting_a_shot_will_delete_the_related_versions(self):
        """testing if deleting a shot will delete the related versions
        """
        
        proj1 = Project('test project 1')
        proj1.save()
        
        seq1 = Sequence(proj1, 'test sequence 1')
        seq1.save()
        
        shot1 = Shot(seq1, 1)
        shot1.save()
        
        shot2 = Shot(seq1, 2)
        shot2.save()
        
        user = User.query().first()
        shot_vtypes = VersionType.query().filter_by(type_for="Shot").all()
        
        # versions for shot1
        vers1 = Version(
            shot1,
            base_name=shot1.code,
            type=shot_vtypes[0],
            created_by=user
        )
        vers1.save()
        
        vers2 = Version(
            shot1,
            base_name=shot1.code,
            type=shot_vtypes[0],
            created_by=user
        )
        vers2.save()
        
        vers3 = Version(
            shot1,
            base_name=shot1.code,
            type=shot_vtypes[0],
            created_by=user
        )
        vers3.save()
        
        # versions for shot2
        vers4 = Version(
            shot2,
            base_name=shot2.code,
            type=shot_vtypes[0],
            created_by=user
        )
        vers4.save()
        
        vers5 = Version(
            shot2,
            base_name=shot2.code,
            type=shot_vtypes[0],
            created_by=user
        )
        vers5.save()
        
        vers6 = Version(
            shot2,
            base_name=shot2.code,
            type=shot_vtypes[0],
            created_by=user
        )
        vers6.save()
        
        # test all are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(seq1, db.session)
        self.assertIn(shot1, db.session)
        self.assertIn(shot2, db.session)
        self.assertIn(vers1, db.session)
        self.assertIn(vers2, db.session)
        self.assertIn(vers3, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)
        
        # delete shot1
        db.session.delete(shot1)
        db.session.commit()
        
        # check if versions are deleted
        self.assertNotIn(shot1, db.session)
        self.assertNotIn(vers1, db.session)
        self.assertNotIn(vers2, db.session)
        self.assertNotIn(vers3, db.session)
        
        # check if all the others are still there
        self.assertIn(proj1, db.session)
        self.assertIn(seq1, db.session)
        self.assertIn(shot2, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)
Esempio n. 33
0
class AssetTester(unittest.TestCase):
    """tests the Asset class
    """
    
    def setUp(self):
        """setup the test settings with environment variables
        """
        # -----------------------------------------------------------------
        # start of the setUp
        # create the environment variable and point it to a temp directory
        conf.database_url = "sqlite://"
        
        self.temp_config_folder = tempfile.mkdtemp()
        self.temp_projects_folder = tempfile.mkdtemp()
        
        os.environ["OYPROJECTMANAGER_PATH"] = self.temp_config_folder
        os.environ[conf.repository_env_key] = self.temp_projects_folder
        
        self.test_proj = Project("TEST_PROJ1")
        self.test_proj.create()
        
        self.kwargs = {
            "project": self.test_proj,
            "name": "Test Asset",
            "code": "TEST_ASSET",
            'type': 'Prop',
        }
        
        self.test_asset = Asset(**self.kwargs)
        self.test_asset.save()
        
        self._name_test_values = [
            ("Test Asset", "Test Asset"),
            ("23Test_Asset", "23Test_Asset"),
            ("TEST_ASSET", "TEST_ASSET"),
            ("£#$£#$AB", "AB"),
            ("'^+'^%^+%&&AB3£#$£½'^+'3'^+'4", "AB334"),
            ("afasfas fasf asdf67", "Afasfas fasf asdf67"),
            ("45a", "45a"),
            ("45acafs","45acafs"),
            ("45'^+'^+a 234", "45a 234"),
            ("45asf78wr", "45asf78wr"),
            ("'^+'afsd2342'^+'asdFGH", "Afsd2342asdFGH"),
        ]
        
        self._code_test_values = [
            ("Test Asset", "Test_Asset"),
            ("23Test_Asset", "23Test_Asset"),
            ("TEST_ASSET", "TEST_ASSET"),
            ("£#$£#$AB", "AB"),
            ("'^+'^%^+%&&AB3£#$£½'^+'3'^+'4", "AB334"),
            ("afasfas fasf asdf67", "Afasfas_fasf_asdf67"),
            ("45a", "45a"),
            ("45acafs","45acafs"),
            ("45'^+'^+a 234", "45a_234"),
            ("45asf78wr", "45asf78wr"),
            ("'^+'afsd2342'^+'asdFGH", "Afsd2342asdFGH"),
        ]
    
    def tearDown(self):
        """cleanup the test
        """
        
        # set the db.session to None
        db.session = None
        
        # delete the temp folder
        shutil.rmtree(self.temp_config_folder)
        shutil.rmtree(self.temp_projects_folder)
    
    def test_name_argument_is_skipped(self):
        """testing if a TypeError will be raised when the name argument is
        skipped
        """
        self.kwargs.pop("name")
        self.assertRaises(TypeError, Asset, **self.kwargs)
    
    def test_name_argument_is_None(self):
        """testing if a TypeError will be raised when the name argument is None
        """
        self.kwargs["name"] = None
        self.assertRaises(TypeError, Asset, **self.kwargs)
    
    def test_name_attribute_is_None(self):
        """testing if a TypeError will be raised when the name attribute is set
        to None
        """
        self.assertRaises(TypeError, setattr, self.test_asset, "name", None)
    
    def test_name_argument_is_not_a_string(self):
        """testing if a TypeError will be raised when the name argument is not
        a string
        """
        self.kwargs["name"] = 123445
        self.assertRaises(TypeError, Asset, **self.kwargs)
    
    def test_name_attribute_is_not_a_string(self):
        """testing if a TypeError will be raised when the name attribute is not
        a string
        """
        self.assertRaises(TypeError, setattr, self.test_asset, "name", 123456)
    
    def test_name_argument_is_working_properly(self):
        """test if the name attribute initialized correctly with the value of
        the name argument
        """
        test_value = "Test Value"
        self.kwargs["name"] = test_value
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.name, test_value)
    
    def test_name_attribute_is_working_properly(self):
        """testing if the name attribute is working properly
        """
        test_value = "Test Value"
        self.test_asset.name = test_value
        self.assertEqual(self.test_asset.name, test_value)
    
    def test_name_argument_formatting(self):
        """testing if the name argument will be formatted correctly
        """
        for test_value in self._name_test_values:
            self.kwargs["name"] = test_value[0]
            new_asset = Asset(**self.kwargs)
            self.assertEqual(new_asset.name, test_value[1])
    
    def test_name_attribute_formatting(self):
        """testing if the name attribute will be formatted correctly
        """
        for test_value in self._name_test_values:
            self.test_asset.name = test_value[0]
            self.assertEqual(self.test_asset.name, test_value[1])
    
    def test_name_argument_is_empty_string_after_formatting(self):
        """testing if a ValueError will be raised when the name argument is an
        empty string after formatting
        """
        self.kwargs["name"] = "£#$£'^+'"
        self.assertRaises(ValueError, Asset, **self.kwargs)
    
    def test_name_attribute_is_empty_string_after_formatting(self):
        """testing if a ValueError will be raised when the name attribugte is
        an empty string after formatting
        """
        self.assertRaises(ValueError, setattr, self.test_asset, "name",
                          "£#$£'^+'")
    
    def test_name_argument_is_not_unique(self):
        """testing if a IntegrityError will be raised when the name is unique
        """
        # create an asset with the same name
        new_asset = Asset(**self.kwargs)
        self.assertRaises(IntegrityError, new_asset.save)
    
    def test_code_argument_is_skipped(self):
        """testing if the code attribute will be get from the name attribute if
        the code argument is skipped
        """
        self.kwargs["name"] = "Test Value"
        self.kwargs.pop("code")
        expected_value = "Test_Value"
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.code, expected_value)
    
    def test_code_argument_is_None(self):
        """testing if the code attribute will be get from the name attribute if
        the code argument is None
        """
        self.kwargs["name"] = "Test Value"
        self.kwargs["code"] = None
        expected_value = "Test_Value"
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.code, expected_value)
    
    def test_code_attribute_is_None(self):
        """testing if the code attribute will be get from the name attribute if
        it is set to None
        """
        self.test_asset.name = "Test Value"
        self.test_asset.code = None
        expected_value = "Test_Value"
        self.assertEqual(self.test_asset.code, expected_value)
    
    def test_code_argument_is_not_a_string_instance(self):
        """testing if a TypeError will be raised when the code argument is not
        an instance of string or unicode
        """
        self.kwargs["code"] = 2134
        self.assertRaises(TypeError, Asset, **self.kwargs)
    
    def test_code_attribute_is_not_a_string_instance(self):
        """testing if a TypeError will be raised when the code attribute is set
        to a value which is not a string or unicode
        """
        self.assertRaises(TypeError, setattr, self.test_asset, "code", 2342)
    
    def test_code_argument_is_working_properly(self):
        """testing if the code attribute is set from the code argument
        """
        self.kwargs["code"] = "TEST_VALUE"
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.code, self.kwargs["code"])
    
    def test_code_attribute_is_working_properly(self):
        """testing if the code attribute is working properly
        """
        test_value = "TEST_VALUE"
        self.test_asset.code = test_value
        self.assertEqual(self.test_asset.code, test_value)
    
    def test_code_argument_formatting(self):
        """testing if the code argument is formatted correctly
        """
        for test_value in self._code_test_values:
            self.kwargs["code"] = test_value[0]
            new_asset = Asset(**self.kwargs)
            self.assertEqual(new_asset.code, test_value[1])
    
    def test_code_attribute_formatting(self):
        """testing if the code attribute is formatted correctly
        """
        for test_value in self._code_test_values:
            self.test_asset.code = test_value[0]
            self.assertEqual(self.test_asset.code, test_value[1])
    
    def test_code_argument_is_empty_string_after_formatting(self):
        """testing if a ValueError will be raised when the code argument is an
        empty string after formatting
        """
        self.kwargs["code"] = "'^+'%+%"
        self.assertRaises(ValueError, Asset, **self.kwargs)
    
    def test_code_attribute_is_empty_string_after_formatting(self):
        """testing if a ValueError will be raised when the code attribugte is
        an empty string after formatting
        """
        self.assertRaises(ValueError, setattr, self.test_asset, "code",
                          "'^+'%+%")
    
    def test_code_argument_is_not_unique(self):
        """testing if an IntegrityError will be raised when the code argument
        is not unique
        """
        self.kwargs["name"] = "Another_Asset_Name"
        new_asset = Asset(**self.kwargs)
        self.assertRaises(IntegrityError, new_asset.save)
    
    def test_save_method_saves_the_asset_to_the_database(self):
        """testing if the save method saves the asset to the database
        """
        self.test_asset.save()
        self.assertTrue(self.test_asset in db.session)
    
    def test_equality_of_assets(self):
        """testing if two assets are equal if their names and projects are also
        equal
        """
        
        proj1 = Project("EQUALITY_TEST_PROJECT_1")
        proj1.create()
        
        proj2 = Project("EQUALITY_TEST_PROJECT_2")
        proj2.create()
        
        asset1 = Asset(proj1, "TEST_ASSET1")
        asset2 = Asset(proj1, "TEST_ASSET1")
        
        asset3 = Asset(proj1, "TEST_ASSET3")
        asset4 = Asset(proj2, "TEST_ASSET3")
        
        self.assertTrue(asset1==asset2)
        self.assertFalse(asset1==asset3)
        self.assertFalse(asset3==asset4)
    
    def test_inequality_of_assets(self):
        """testing if two assets are inequal if their names are different and
        or their projects are different
        """
        
        proj1 = Project("EQUALITY_TEST_PROJECT_1")
        proj1.create()
        
        proj2 = Project("EQUALITY_TEST_PROJECT_2")
        proj2.create()
        
        asset1 = Asset(proj1, "TEST_ASSET1")
        asset2 = Asset(proj1, "TEST_ASSET1")
        
        asset3 = Asset(proj1, "TEST_ASSET3")
        asset4 = Asset(proj2, "TEST_ASSET3")
        
        self.assertFalse(asset1!=asset2)
        self.assertTrue(asset1!=asset3)
        self.assertTrue(asset3!=asset4)
    
    def test_type_argument_is_skipped(self):
        """testing if skipping the type argument the type attribute will be set
        to conf.default_asset_type_name
        """
        self.kwargs.pop('type')
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.type, conf.default_asset_type_name)
    
    def test_type_argument_is_None(self):
        """testing if setting the type argument to None will set the type
        attribute to conf.default_asset_type_name
        """
        self.kwargs['type'] = None
        new_asset = Asset(**self.kwargs)
        self.assertEqual(new_asset.type, conf.default_asset_type_name)
    
    def test_type_attribute_is_set_to_None(self):
        """testing if setting the type attribute to None will set the type to
        conf.default_asset_type_name
        """
        self.test_asset.type = None
        self.assertEqual(self.test_asset.type, conf.default_asset_type_name)
    
    def test_type_argument_accepts_string_or_unicode_only(self):
        """testing if a TypeError will be raised when the type argument is set
        to a value other than string or unicode value
        """
        self.kwargs['type'] = 12312
        self.assertRaises(TypeError, Asset, **self.kwargs)
    
    def test_type_attribute_accepts_string_or_unicode_only(self):
        """testing if a TypeError will be raised when the type attribute is set
        to a value other than string or unicode
        """
        self.assertRaises(TypeError, setattr, self.test_asset, 'type', 2342)
    
    def test_type_argument_is_working_properly(self):
        """testing if the type attribute is set with the type argument
        """
        self.kwargs['type'] = "Test_Type_1"
        new_asset = Asset(**self.kwargs)
        self.assertEqual(self.kwargs['type'], new_asset.type)
    
    def test_type_attribute_is_working_properly(self):
        """testing if the type attribute is working properly
        """
        test_value = "Test_Type_1"
        self.test_asset.type = test_value
        self.assertEqual(self.test_asset.type, test_value)
    
    def test_type_argument_formatting(self):
        """testing if the type argument is formatted correctly
        """
        for test_values in self._code_test_values:
            input_value = test_values[0]
            expected_value = test_values[1]
            self.kwargs['type'] = input_value
            new_asset = Asset(**self.kwargs)
            self.assertEqual(new_asset.type, expected_value)
    
    def test_type_argument_is_invalid_after_formatting(self):
        """testing if a ValueError will be raised when the type argument is
        invalid after formatting
        """
        self.kwargs['type'] = '@#$@#$'
        self.assertRaises(ValueError, Asset, **self.kwargs)
    
    def test_type_attribute_is_invalid_after_formatting(self):
        """testing if a ValueError will be raised when the type attribute is
        invalid after formatting
        """
        self.assertRaises(ValueError, setattr, self.test_asset, 'type', '#@$#')
    
    def test_type_attribute_formatting(self):
        """testing if the type attribute is formatted correctly
        """
        for test_values in self._code_test_values:
            input_value = test_values[0]
            expected_value = test_values[1]
            self.test_asset.type = input_value
            self.assertEqual(self.test_asset.type, expected_value)
    
    def test_deleting_an_asset_will_not_delete_the_related_project(self):
        """testing if deleting an asset will not delete the related project
        """
        proj1 = Project('test project 1')
        proj1.save()
        
        asset = Asset(proj1, 'Test asset')
        asset.save()
        
        # check if they are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(asset, db.session)
        
        # delete the asset
        db.session.delete(asset)
        db.session.commit()
        
        # check if it is removed from the session
        self.assertNotIn(asset, db.session)
        
        # and the project is there
        self.assertIn(proj1, db.session)
    
    def test_deleting_an_asset_will_not_delete_the_other_assets_in_the_related_project(self):
        """testing if deleting an asset will not delete the other assets in the
        related project
        """
        proj1 = Project('test project 1')
        proj1.save()
        
        asset1 = Asset(proj1, 'test asset 1')
        asset1.save()
        
        asset2 = Asset(proj1, 'test asset 2')
        asset2.save()
        
        asset3 = Asset(proj1, 'test asset 3')
        asset3.save()
        
        # check if they are properly in the db.session
        self.assertIn(proj1, db.session)
        self.assertIn(asset1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(asset3, db.session)
        
        # delete asset1
        db.session.delete(asset1)
        db.session.commit()
        
        # check if the asset1 is deleted
        self.assertNotIn(asset1, db.session)
        
        # and the others are in place
        self.assertIn(proj1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(asset3, db.session)
    
    def test_deleting_an_asset_will_delete_all_the_related_versions(self):
        """testing if deleting an asset will also delete the related versions
        """
        proj1 = Project('test project 1')
        proj1.save()
        
        asset1 = Asset(proj1, 'test asset 1')
        asset1.save()
        
        asset2 = Asset(proj1, 'test asset 2')
        asset2.save()
        
        asset_vtypes = VersionType.query().filter_by(type_for="Asset").all()
        
        user = User.query().first()
        
        vers1 = Version(
            version_of=asset1,
            base_name=asset1.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers1.save()
        
        vers2 = Version(
            version_of=asset1,
            base_name=asset1.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers2.save()
        
        vers3 = Version(
            version_of=asset1,
            base_name=asset1.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers3.save()
    
        vers4 = Version(
            version_of=asset2,
            base_name=asset2.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers4.save()
        
        vers5 = Version(
            version_of=asset2,
            base_name=asset2.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers5.save()
        
        vers6 = Version(
            version_of=asset2,
            base_name=asset2.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers6.save()    
        
        # check if all are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(asset1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(vers1, db.session)
        self.assertIn(vers2, db.session)
        self.assertIn(vers3, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)
        
        # delete the asset
        db.session.delete(asset1)
        db.session.commit()
        
        # check if it is not in the session anymore
        self.assertNotIn(asset1, db.session)
        
        # check if the versions are also deleted
        self.assertNotIn(vers1, db.session)
        self.assertNotIn(vers2, db.session)
        self.assertNotIn(vers3, db.session)
        
        # check if the others are still there
        self.assertIn(proj1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)
    
    def test_deleting_an_asset_will_delete_all_the_related_versions_but_keep_references(self):
        """testing if deleting an asset will only delete the version of that
        asset and will keep the referenced versions.
        """
        proj1 = Project('test project 1')
        proj1.save()
        
        asset1 = Asset(proj1, 'test asset 1')
        asset1.save()
        
        asset2 = Asset(proj1, 'test asset 2')
        asset2.save()
        
        asset_vtypes = VersionType.query().filter_by(type_for="Asset").all()
        
        user = User.query().first()
        
        vers1 = Version(
            version_of=asset1,
            base_name=asset1.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers1.save()
        
        vers2 = Version(
            version_of=asset1,
            base_name=asset1.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers2.save()
        
        vers3 = Version(
            version_of=asset1,
            base_name=asset1.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers3.save()
    
        vers4 = Version(
            version_of=asset2,
            base_name=asset2.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers4.save()
        
        vers5 = Version(
            version_of=asset2,
            base_name=asset2.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers5.save()
        
        vers6 = Version(
            version_of=asset2,
            base_name=asset2.code,
            type=asset_vtypes[0],
            created_by=user
        )
        vers6.save()
        
        # reference vers4, vers5 and vers6 to vers1, vers2 and vers3
        vers1.references.append(vers4)
        vers1.references.append(vers5)
        vers1.references.append(vers6)
        
        vers2.references.append(vers4)
        vers2.references.append(vers5)
        vers2.references.append(vers6)
        
        vers3.references.append(vers4)
        vers3.references.append(vers5)
        vers3.references.append(vers6)
        
        # check if all are in the session
        self.assertIn(proj1, db.session)
        self.assertIn(asset1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(vers1, db.session)
        self.assertIn(vers2, db.session)
        self.assertIn(vers3, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)
        
        # there should be 9 entries in the secondary table
        result = db.session\
            .query('referencer_id', 'reference_id')\
            .from_statement("SELECT referencer_id, reference_id "
                            "FROM Version_References").all()
        self.assertEqual(len(result), 9)
        
        # delete the asset
        db.session.delete(asset1)
        db.session.commit()
        
        # check if it is not in the session anymore
        self.assertNotIn(asset1, db.session)
        
        # check if the versions are also deleted
        self.assertNotIn(vers1, db.session)
        self.assertNotIn(vers2, db.session)
        self.assertNotIn(vers3, db.session)
        
        # check if the others are still there
        self.assertIn(proj1, db.session)
        self.assertIn(asset2, db.session)
        self.assertIn(vers4, db.session)
        self.assertIn(vers5, db.session)
        self.assertIn(vers6, db.session)
        
        # to be sure check the secondary table
        result = db.session\
            .query('referencer_id', 'reference_id')\
            .from_statement("SELECT referencer_id, reference_id "
                            "FROM Version_References").all()
        self.assertEqual(len(result), 0)