コード例 #1
0
 def test_delete_profile(self):
     """
     tests the storage.delete() method removes and commits obj to database
     for an object from the Profile class
     """
     # connect to MySQL database through MySQLdb and get initial count
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM profiles""")
     objs_for_count1 = cur.fetchall()
     # creates new instance of Profile
     new_obj = Profile()
     # tests that the new object is of type Profile
     self.assertIs(type(new_obj), Profile)
     # adds all attributes required 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 = "*****@*****.**"
     # save the object with BaseModel save method
     # save method calls storage.new() and storage.save()
     new_obj.save()
     # closes connection to database and restarts connection with MySQLdb
     cur.close()
     db.close()
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM profiles""")
     objs_for_count2 = cur.fetchall()
     # tests that there is one more obj saved to profiles table in db
     self.assertEqual(len(objs_for_count1) + 1, len(objs_for_count2))
     # delete the object with BaseModel delete method
     # delete instance method calls storage.delete() and storage.save()
     new_obj.delete()
     # closes connection to database and restarts connection with MySQLdb
     cur.close()
     db.close()
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM profiles""")
     objs_for_count3 = cur.fetchall()
     # tests that there is one less obj in profiles table in db
     self.assertEqual(len(objs_for_count2) - 1, len(objs_for_count3))
     self.assertEqual(len(objs_for_count1), len(objs_for_count3))
     # closes the connection
     cur.close()
     db.close()
コード例 #2
0
 def test_all_profiles_dict(self):
     """
     tests return of all method when class is Profile
     """
     # connect to MySQL database through MySQLdb and get initial count
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM profiles""")
     profile_objs = cur.fetchall()
     total_profile_count = len(profile_objs)
     # call storage.all() method
     all_profile_objs = storage.all(Profile)
     profile_count1 = len(all_profile_objs.keys())
     # tests that all method returns same count of Identity objects
     self.assertEqual(total_profile_count, profile_count1)
     # tests that all method returns dictionary
     self.assertIsInstance(all_profile_objs, dict)
     # creates new Profile obj to test with
     new_obj = Profile()
     # adds all attributes required 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 = "*****@*****.**"
     # saves new object to the database
     new_obj.save()
     # re-call storage.all() method and test that count increased by 1
     all_profile_objs = storage.all(Profile)
     profile_count2 = len(all_profile_objs.keys())
     self.assertEqual(profile_count1 + 1, profile_count2)
     # tests that newly created obj is in dictionary with correct key
     self.assertIsInstance(storage.all(), dict)
     dict_key = "{}.{}".format("Profile", new_obj.id)
     self.assertIn(dict_key, storage.all())
     # get obj attributes from stroage.all() dictionary using obj id
     # test that retrieved attributes match expected values
     obj_class = storage.all().get("Profile.{}".format(
         new_obj.id)).__class__.__name__
     self.assertEqual("Profile", obj_class)
     obj_name = storage.all().get("Profile.{}".format(new_obj.id)).name
     self.assertEqual("test_name", obj_name)
     obj_email = storage.all().get("Profile.{}".format(new_obj.id)).email
     self.assertEqual("*****@*****.**", obj_email)
     # delete new object from the database
     new_obj.delete()
     # re-call storage.all() method and test that count decreased by 1
     all_profile_objs = storage.all(Profile)
     profile_count3 = len(all_profile_objs.keys())
     self.assertEqual(profile_count2 - 1, profile_count3)
     self.assertEqual(profile_count1, profile_count3)
     # tests that new object is no longer in return dictionary
     self.assertNotIn(dict_key, storage.all())
コード例 #3
0
 def test_all_profiles_count(self):
     """
     tests all method retrieves all objects when class is Profile
     """
     # connect to MySQL database through MySQLdb and get initial count
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM identities""")
     identity_objs = cur.fetchall()
     cur.execute("""SELECT * FROM profiles""")
     profile_objs = cur.fetchall()
     cur.execute("""SELECT * FROM skills""")
     skills_objs = cur.fetchall()
     total_count = len(identity_objs) + len(profile_objs) + len(skills_objs)
     total_profile_count = len(profile_objs)
     # call storage.all() method, both with and without class specified
     all_objs = storage.all()
     count1 = len(all_objs.keys())
     all_profile_objs = storage.all(Profile)
     profile_count1 = len(all_profile_objs.keys())
     # tests that counts from all method match current database
     self.assertEqual(total_count, count1)
     self.assertEqual(total_profile_count, profile_count1)
     # creates new Profile obj to test with
     new_obj = Profile()
     # adds all attributes required 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 = "*****@*****.**"
     # saves new object to the database
     new_obj.save()
     # re-call storage.all() method
     all_objs = storage.all()
     count2 = len(all_objs.keys())
     all_profile_objs = storage.all(Profile)
     profile_count2 = len(all_profile_objs.keys())
     # tests that counts increased by 1
     self.assertEqual(count1 + 1, count2)
     self.assertEqual(profile_count1 + 1, profile_count2)
     # deletes new object from the database
     new_obj.delete()
     # re-call storage.all() method
     all_objs = storage.all()
     count3 = len(all_objs.keys())
     all_profile_objs = storage.all(Profile)
     profile_count3 = len(all_profile_objs.keys())
     # tests that count decreased by 1
     self.assertEqual(count2 - 1, count3)
     self.assertEqual(count1, count3)
     self.assertEqual(profile_count2 - 1, profile_count3)
     self.assertEqual(profile_count1, profile_count3)
コード例 #4
0
 def test_delete_method(self, mock_storage):
     """
     tests that the delete() method inherited from BaseModel calls on
     storage.delete() to remove and commit the object from the database
     """
     # creates new instance of Profile
     new_obj = Profile()
     # adds name and email as required attribute for database
     # (id should be set by primary key)
     # (created_at, updated_at should be set by datetime)
     new_obj.name = "test_name"
     new_obj.email = "*****@*****.**"
     # saves to database before attempting to delete
     new_obj.save()
     self.assertTrue(mock_storage.new.called)
     # deletes new instance and tests if storage method deleted is called
     new_obj.delete()
     self.assertTrue(mock_storage.delete.called)