Пример #1
0
 def test_delete_skills(self):
     """
     tests the storage.delete() method removes and commits obj to database
     for an object from the Skills 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 skills""")
     objs_for_count1 = cur.fetchall()
     # creates new instance of Skills
     new_obj = Skills()
     # tests that the new object is of type Skills
     self.assertIs(type(new_obj), Skills)
     # 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"
     # 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 skills""")
     objs_for_count2 = cur.fetchall()
     # tests that there is one more obj saved to skills 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 skills""")
     objs_for_count3 = cur.fetchall()
     # tests that there is one less obj in skills 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_skills_count(self):
     """
     tests all method retrieves all objects when class is Skills
     """
     # 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_skills_count = len(skills_objs)
     # call storage.all() method, both with and without class specified
     all_objs = storage.all()
     count1 = len(all_objs.keys())
     all_skills_objs = storage.all(Skills)
     skills_count1 = len(all_skills_objs.keys())
     # tests that counts from all method match current database
     self.assertEqual(total_count, count1)
     self.assertEqual(total_skills_count, skills_count1)
     # creates new Skills obj to test with
     new_obj = Skills()
     # 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"
     # saves new object to the database
     new_obj.save()
     # re-call storage.all() method
     all_objs = storage.all()
     count2 = len(all_objs.keys())
     all_skills_objs = storage.all(Skills)
     skills_count2 = len(all_skills_objs.keys())
     # tests that counts increased by 1
     self.assertEqual(count1 + 1, count2)
     self.assertEqual(skills_count1 + 1, skills_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_skills_objs = storage.all(Skills)
     skills_count3 = len(all_skills_objs.keys())
     # tests that count decreased by 1
     self.assertEqual(count2 - 1, count3)
     self.assertEqual(count1, count3)
     self.assertEqual(skills_count2 - 1, skills_count3)
     self.assertEqual(skills_count1, skills_count3)
Пример #3
0
 def test_all_skills_dict(self):
     """
     tests return of all method when class is Skills
     """
     # 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 skills""")
     skills_objs = cur.fetchall()
     total_skills_count = len(skills_objs)
     # call storage.all() method
     all_skills_objs = storage.all(Skills)
     skills_count1 = len(all_skills_objs.keys())
     # tests that all method returns same count of Skills objects
     self.assertEqual(total_skills_count, skills_count1)
     # tests that all method returns dictionary
     self.assertIsInstance(all_skills_objs, dict)
     # creates new Skills obj to test with
     new_obj = Skills()
     # 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"
     # saves new object to the database
     new_obj.save()
     # re-call storage.all() method and test that count increased by 1
     all_skills_objs = storage.all(Skills)
     skills_count2 = len(all_skills_objs.keys())
     self.assertEqual(skills_count1 + 1, skills_count2)
     # tests that newly created obj is in dictionary with correct key
     self.assertIsInstance(storage.all(), dict)
     dict_key = "{}.{}".format("Skills", 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("Skills.{}".format(
         new_obj.id)).__class__.__name__
     self.assertEqual("Skills", obj_class)
     obj_name = storage.all().get("Skills.{}".format(new_obj.id)).name
     self.assertEqual("test_name", obj_name)
     # delete new object from the database
     new_obj.delete()
     # re-call storage.all() method and test that count decreased by 1
     all_skills_objs = storage.all(Skills)
     skills_count3 = len(all_skills_objs.keys())
     self.assertEqual(skills_count2 - 1, skills_count3)
     self.assertEqual(skills_count1, skills_count3)
     # tests that new object is no longer in return dictionary
     self.assertNotIn(dict_key, storage.all())
Пример #4
0
 def test_save_method(self, mock_storage):
     """
     tests that the save() method inherited from BaseModel calls on
     storage.new() to add and commit the object to the database
     """
     # creates new instance of Skills
     new_obj = Skills()
     # adds name 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"
     # saves new instance and tests if storage method new is called
     new_obj.save()
     self.assertTrue(mock_storage.new.called)
Пример #5
0
def create_skills():
    """
    create a new instance of Skills
    through POST request
    """
    # get JSON from POST request
    json = request.get_json(silent=True)
    # checks for missing attributes
    if json is None:
        abort(400, 'Not a JSON')
    if 'name' not in json:
        abort(400, 'Missing name attribute')
    # create new instance with **kwargs from json
    new_obj = Skills(**json)
    new_obj.save()
    # return json version of object's to_dict()
    return jsonify(new_obj.to_dict()), 201
Пример #6
0
 def test_reload(self):
     """
     tests that the reload function creates a new session
     linked only to current database tables
     """
     # 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 skills""")
     skills_objs = cur.fetchall()
     total_skills_count = len(skills_objs)
     # call storage.all() method
     all_skills_objs = storage.all(Skills)
     skills_count1 = len(all_skills_objs.keys())
     # tests that all method returns same count of Skills objects
     self.assertEqual(total_skills_count, skills_count1)
     # creates new Skills obj to test with; obj is saved
     new_obj1 = Skills()
     # adds all attributes required for testing
     # (id should be set by primary key)
     # (created_at, updated_at should be set by datetime)
     new_obj1.name = "test_name1"
     # saves new object to the database
     new_obj1.save()
     # creates new Skills obj to test with; obj is not saved
     new_obj2 = Skills()
     # adds all attributes required for testing
     # (id should be set by primary key)
     # (created_at, updated_at should be set by datetime)
     new_obj2.name = "test_name2"
     # call reload method to reconnect session to current database
     storage.reload()
     # re-call storage.all() method and test that count increased by only 1
     all_skills_objs = storage.all(Skills)
     skills_count2 = len(all_skills_objs.keys())
     self.assertEqual(skills_count1 + 1, skills_count2)
     # tests that newly created saved obj is in dictionary with correct key
     dict_key = "{}.{}".format("Skills", new_obj1.id)
     self.assertIn(dict_key, storage.all())
     # tests that newly created unsaved obj is not in dictionary
     dict_key = "{}.{}".format("Skills", new_obj2.id)
     self.assertNotIn(dict_key, storage.all())
Пример #7
0
 def test_updated_datetime_attributes(self):
     """
     tests that the datetime attribute updated_at changes
     when the save() method is implemented
     """
     first_time = datetime.now()
     new_obj = Skills()
     second_time = datetime.now()
     # tests if object's created_at time is between timestamps
     self.assertTrue(first_time <= new_obj.created_at <= second_time)
     # tests if object's updated_at is within the same timestamps
     self.assertTrue(first_time <= new_obj.updated_at <= second_time)
     # gets timestamps of current attributes and pauses a moment
     original_created_at = new_obj.created_at
     original_updated_at = new_obj.updated_at
     sleep(1)
     # adds required attributes so the object can be saved; saves object
     new_obj.name = "test_name"
     new_obj.save()
     # tests that the object's updated_at has changed and is later
     self.assertNotEqual(original_updated_at, new_obj.updated_at)
     self.assertTrue(original_updated_at < new_obj.updated_at)
     # tests that only the object's updated_at datetime has changed
     self.assertEqual(original_created_at, new_obj.created_at)