Esempio n. 1
0
    def test_init_method(self):
        """
        tests the __init__ method for instantiating new objects
        both new and from kwargs
        __init__ method calls on inherited BaseModel with super()
        """
        # creates new instance of Profile
        new_obj1 = Profile()
        # tests that the new object is of type Profile
        self.assertIs(type(new_obj1), Profile)
        # adds all attributes for testing
        # (id should be set by primary key)
        # (created_at, updated_at should be set by datetime)
        new_obj1.name = "test_name"
        new_obj1.email = "*****@*****.**"
        new_obj1.password = "******"
        new_obj1.company_school_name = "123"
        new_obj1.about_me = "This is for testing purposes"
        new_obj1.linkedin = "https://www.linkedin.com/in/test"
        new_obj1.social_media = "Social media links would go here"
        # attributes_dict sets up dictionary of attribute names and types
        attributes_dict = {
            "id": str,
            "created_at": datetime,
            "updated_at": datetime,
            "name": str,
            "email": str,
            "password": str,
            "company_school_name": str,
            "about_me": str,
            "linkedin": str,
            "social_media": str
        }
        # loops through attributes_dict as subTests to check each attribute
        for attr, attr_type in attributes_dict.items():
            with self.subTest(attr=attr, attr_type=attr_type):
                # tests the expected attribute is in the object's dict
                self.assertIn(attr, new_obj1.__dict__)
                # tests the attribute is the expected type
                self.assertIs(type(new_obj1.__dict__[attr]), attr_type)
        # sets kwargs using object's dict and uses to create new object
        kwargs = new_obj1.__dict__
        new_obj2 = Profile(**kwargs)
        # tests that the new object is of type Profile
        self.assertIs(type(new_obj2), Profile)
        # loops through attributes_dict as subTests to check each attribute
        for attr, attr_type in attributes_dict.items():
            with self.subTest(attr=attr, attr_type=attr_type):
                # tests the expected attribute is in the object's dict
                self.assertIn(attr, new_obj2.__dict__)
                # tests the attribute is the expected type
                self.assertIs(type(new_obj2.__dict__[attr]), attr_type)
                # tests the value of name attribute matches the original object
                self.assertEqual(new_obj1.__dict__[attr],
                                 new_obj2.__dict__[attr])

        # tests that __class__ is not set in object 2
        self.assertNotIn('__class__', new_obj2.__dict__)
Esempio n. 2
0
 def test_attribute_types(self):
     """
     tests the class attributes exist and are of correct type
     also tests instantiation of new object
     """
     # creates new instance of Profile
     new_obj = Profile()
     # adds name and email as required attribute for database
     # adds optional attributes for testing
     # (id should be set by primary key)
     # (created_at, updated_at should be set by datetime)
     new_obj.name = "test_name"
     new_obj.email = "*****@*****.**"
     new_obj.password = "******"
     new_obj.company_school_name = "123"
     new_obj.about_me = "This is for testing purposes"
     new_obj.linkedin = "https://www.linkedin.com/in/test"
     new_obj.social_media = "Social media links would go here"
     # attributes_dict sets up dictionary of attribute names and types
     attributes_dict = {
         "id": str,
         "created_at": datetime,
         "updated_at": datetime,
         "name": str,
         "email": str,
         "password": str,
         "company_school_name": str,
         "about_me": str,
         "linkedin": str,
         "social_media": str
     }
     # loops through attributes_dict as subTests to check each attribute
     for attr, attr_type in attributes_dict.items():
         with self.subTest(attr=attr, attr_type=attr_type):
             # tests the expected attribute is in the instance's dict
             self.assertIn(attr, new_obj.__dict__)
             # tests the attribute is the expected type
             self.assertIs(type(new_obj.__dict__[attr]), attr_type)