Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# This file lists all individuals having contributed content to the repository.


Jennifer Huang <133@holbertonschool.com>
Alexa Orrico <210@holbertonschool.com>
Joann Vuong <130@holbertonschool.com>
Thokozane Tshabalala <thokozanetek@gmail.com>
204 changes: 132 additions & 72 deletions tests/test_models/test_engine/test_db_storage.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,148 @@
#!/usr/bin/python3
"""
Contains the TestDBStorageDocs and TestDBStorage classes
"""

from datetime import datetime
import inspect
import models
from models.engine import db_storage
from models.amenity import Amenity
from models.base_model import BaseModel
from models.city import City
from models.place import Place
from models.review import Review
from models.state import State
from models.user import User
import json
import os
import pep8
'''
Testing the file_storage module.
'''
import time
import unittest
DBStorage = db_storage.DBStorage
classes = {"Amenity": Amenity, "City": City, "Place": Place,
"Review": Review, "State": State, "User": User}
import sys
from models.engine.db_storage import DBStorage
from models import storage
from models.user import User
from models.state import State
from models import storage
from console import HBNBCommand
from os import getenv
from io import StringIO

db = getenv("HBNB_TYPE_STORAGE")

class TestDBStorageDocs(unittest.TestCase):
"""Tests to check the documentation and style of DBStorage class"""

@unittest.skipIf(db != 'db', "Testing DBstorage only")
class test_DBStorage(unittest.TestCase):
'''
Testing the DB_Storage class
'''
@classmethod
def setUpClass(cls):
"""Set up for the doc tests"""
cls.dbs_f = inspect.getmembers(DBStorage, inspect.isfunction)
'''
Initializing classes
'''
cls.dbstorage = DBStorage()
cls.output = StringIO()
sys.stdout = cls.output

def test_pep8_conformance_db_storage(self):
"""Test that models/engine/db_storage.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
result = pep8s.check_files(['models/engine/db_storage.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")
@classmethod
def tearDownClass(cls):
'''
delete variables
'''
del cls.dbstorage
del cls.output

def test_pep8_conformance_test_db_storage(self):
"""Test tests/test_models/test_db_storage.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
result = pep8s.check_files(['tests/test_models/test_engine/\
test_db_storage.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")
def create(self):
'''
Create HBNBCommand()
'''
return HBNBCommand()

def test_db_storage_module_docstring(self):
"""Test for the db_storage.py module docstring"""
self.assertIsNot(db_storage.__doc__, None,
"db_storage.py needs a docstring")
self.assertTrue(len(db_storage.__doc__) >= 1,
"db_storage.py needs a docstring")
def test_new(self):
'''
Test DB new
'''
new_obj = State(name="California")
self.assertEqual(new_obj.name, "California")

def test_db_storage_class_docstring(self):
"""Test for the DBStorage class docstring"""
self.assertIsNot(DBStorage.__doc__, None,
"DBStorage class needs a docstring")
self.assertTrue(len(DBStorage.__doc__) >= 1,
"DBStorage class needs a docstring")
def test_dbstorage_user_attr(self):
'''
Testing User attributes
'''
new = User(email="melissa@hbtn.com", password="hello")
self.assertTrue(new.email, "melissa@hbtn.com")

def test_dbs_func_docstrings(self):
"""Test for the presence of docstrings in DBStorage methods"""
for func in self.dbs_f:
self.assertIsNot(func[1].__doc__, None,
"{:s} method needs a docstring".format(func[0]))
self.assertTrue(len(func[1].__doc__) >= 1,
"{:s} method needs a docstring".format(func[0]))
def test_dbstorage_check_method(self):
'''
Check methods exists
'''
self.assertTrue(hasattr(self.dbstorage, "all"))
self.assertTrue(hasattr(self.dbstorage, "__init__"))
self.assertTrue(hasattr(self.dbstorage, "new"))
self.assertTrue(hasattr(self.dbstorage, "save"))
self.assertTrue(hasattr(self.dbstorage, "delete"))
self.assertTrue(hasattr(self.dbstorage, "reload"))

def test_dbstorage_all(self):
'''
Testing all function
'''
storage.reload()
result = storage.all("")
self.assertIsInstance(result, dict)
self.assertEqual(len(result), 0)
new = User(email="adriel@hbtn.com", password="abc")
console = self.create()
console.onecmd("create State name=California")
result = storage.all("State")
self.assertTrue(len(result) > 0)

class TestFileStorage(unittest.TestCase):
"""Test the FileStorage class"""
@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_all_returns_dict(self):
"""Test that all returns a dictionaty"""
self.assertIs(type(models.storage.all()), dict)
def test_dbstorage_new_save(self):
'''
Testing save method
'''
new_state = State(name="NewYork")
storage.new(new_state)
save_id = new_state.id
result = storage.all("State")
temp_list = []
for k, v in result.items():
temp_list.append(k.split('.')[1])
obj = v
self.assertTrue(save_id in temp_list)
self.assertIsInstance(obj, State)

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_all_no_class(self):
"""Test that all returns all rows when no class is passed"""
def test_dbstorage_delete(self):
'''
Testing delete method
'''
new_user = User(email="haha@hehe.com", password="abc",
first_name="Adriel", last_name="Tolentino")
storage.new(new_user)
save_id = new_user.id
key = "User.{}".format(save_id)
self.assertIsInstance(new_user, User)
storage.save()
old_result = storage.all("User")
del_user_obj = old_result[key]
storage.delete(del_user_obj)
new_result = storage.all("User")
self.assertNotEqual(len(old_result), len(new_result))

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_new(self):
"""test that new adds an object to the database"""
def test_model_storage(self):
'''
Test to check if storage is an instance for DBStorage
'''
self.assertTrue(isinstance(storage, DBStorage))

def test_get(self):
'''
Test if get method retrieves obj requested
'''
new_state = State(name="NewYork")
storage.new(new_state)
key = "State.{}".format(new_state.id)
result = storage.get("State", new_state.id)
self.assertTrue(result.id, new_state.id)
self.assertIsInstance(result, State)

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_save(self):
"""Test that save properly saves objects to file.json"""
def test_count(self):
'''
Test if count method returns expected number of objects
'''
storage.reload()
old_count = storage.count("State")
new_state1 = State(name="NewYork")
storage.new(new_state1)
new_state2 = State(name="Virginia")
storage.new(new_state2)
new_state3 = State(name="California")
storage.new(new_state3)
self.assertEqual(old_count + 3, storage.count("State"))
Loading