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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
task_tests
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ EOF all create destroy help quit show update
No known bugs at this time.

## Authors
Alexa Orrico - [Github](https://github.com/alexaorrico) / [Twitter](https://twitter.com/alexa_orrico)
Jennifer Huang - [Github](https://github.com/jhuang10123) / [Twitter](https://twitter.com/earthtojhuang)
* [Muhammad Hussein](https://github.com/muhammadSWE)
* [Daniel Kamel](https://github.com/daniel-kamel)

Second part of Airbnb: Joann Vuong
## License
Expand Down
Empty file added api/__init__.py
Empty file.
Empty file added api/v1/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions api/v1/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from api.v1.views import app_views
from flask import Flask
from models import storage

app = Flask(__name__)

app.register_blueprint(app_views)

@app.teardown_appcontext
def teardown_db(exception):
storage.close()

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, threaded=True)
4 changes: 4 additions & 0 deletions api/v1/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from flask import Blueprint
from api.v1.views import *

app_views = Blueprint("app_views", __name__, url_prefix="/api/v1")
6 changes: 6 additions & 0 deletions api/v1/views/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from api.v1.views.index import app_views
from flask import jsonify

@app_views.route('/status', methods=['GET'], strict_slashes=False)
def status():
return jsonify({"status": "OK"})
20 changes: 20 additions & 0 deletions models/engine/db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def __init__(self):
if HBNB_ENV == "test":
Base.metadata.drop_all(self.__engine)

Base.metadata.create_all(self.__engine)

def all(self, cls=None):
"""query on the current database session"""
new_dict = {}
Expand Down Expand Up @@ -74,3 +76,21 @@ def reload(self):
def close(self):
"""call remove() method on the private session attribute"""
self.__session.remove()

def get(self, cls, id):
"""retrieve one object"""
if cls in classes.values():
return self.__session.query(cls).filter(cls.id == id).first()
else:
return None

def count(self, cls=None):
"""
Returns the number of objects in storage matching
the given class. If no class is passed, returns
the count of all objects in storage.
"""
if cls is not None:
return len(self.all(cls))
else:
return len(self.all())
19 changes: 19 additions & 0 deletions models/engine/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,22 @@ def delete(self, obj=None):
def close(self):
"""call reload() method for deserializing the JSON file to objects"""
self.reload()

def get(self, cls, id):
"""retrieve one object"""
if cls in classes.values():
for key in self.__objects:
if key == cls.__name__ + '.' + id:
return self.__objects[key]
return None

def count(self, cls=None):
"""
Returns the number of objects in storage matching
the given class. If no class is passed, returns
the count of all objects in storage.
"""
if cls is not None:
return len(self.all(cls))
else:
return len(self.all())
42 changes: 39 additions & 3 deletions tests/test_models/test_engine/test_db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from models.user import User
import json
import os
import pep8
import pycodestyle
import unittest
DBStorage = db_storage.DBStorage
classes = {"Amenity": Amenity, "City": City, "Place": Place,
Expand All @@ -32,14 +32,14 @@ def setUpClass(cls):

def test_pep8_conformance_db_storage(self):
"""Test that models/engine/db_storage.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
pep8s = pycodestyle.StyleGuide(quiet=True)
result = pep8s.check_files(['models/engine/db_storage.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")

def test_pep8_conformance_test_db_storage(self):
"""Test tests/test_models/test_db_storage.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
pep8s = pycodestyle.StyleGuide(quiet=True)
result = pep8s.check_files(['tests/test_models/test_engine/\
test_db_storage.py'])
self.assertEqual(result.total_errors, 0,
Expand Down Expand Up @@ -78,11 +78,47 @@ def test_all_returns_dict(self):
@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"""
self.assertIs(type(models.storage.all()), dict)
self.assertEqual(len(models.storage.all()), 0)

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_new(self):
"""test that new adds an object to the database"""
new = State(name="California")
models.storage.new(new)
models.storage.save()
self.assertIn(new, models.storage.all().values())

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_save(self):
"""Test that save properly saves objects to file.json"""
new = State(name="Oregon")
models.storage.new(new)
models.storage.save()
models.storage.reload()
self.assertIn(new, models.storage.all().values())

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_get(self):
"""Test that get properly gets an object from the database"""
new = State(name="Oregon")
models.storage.new(new)
models.storage.save()
models.storage.reload()
self.assertIn(new, models.storage.all().values())
self.assertEqual(models.storage.get(State, new.id), new)

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_count(self):
"""Test that count returns the number of objects in storage"""
new = State(name="Oregon")
models.storage.new(new)
models.storage.save()
models.storage.reload()
self.assertEqual(models.storage.count(), 1)
self.assertEqual(models.storage.count(State), 1)
self.assertEqual(models.storage.count(User), 0)
self.assertEqual(models.storage.count(City), 0)
self.assertEqual(models.storage.count(Amenity), 0)
self.assertEqual(models.storage.count(Place), 0)
self.assertEqual(models.storage.count(Review), 0)
35 changes: 32 additions & 3 deletions tests/test_models/test_engine/test_file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from models.user import User
import json
import os
import pep8
import pycodestyle
import unittest
FileStorage = file_storage.FileStorage
classes = {"Amenity": Amenity, "BaseModel": BaseModel, "City": City,
Expand All @@ -32,14 +32,14 @@ def setUpClass(cls):

def test_pep8_conformance_file_storage(self):
"""Test that models/engine/file_storage.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
pep8s = pycodestyle.StyleGuide(quiet=True)
result = pep8s.check_files(['models/engine/file_storage.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")

def test_pep8_conformance_test_file_storage(self):
"""Test tests/test_models/test_file_storage.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
pep8s = pycodestyle.StyleGuide(quiet=True)
result = pep8s.check_files(['tests/test_models/test_engine/\
test_file_storage.py'])
self.assertEqual(result.total_errors, 0,
Expand Down Expand Up @@ -113,3 +113,32 @@ def test_save(self):
with open("file.json", "r") as f:
js = f.read()
self.assertEqual(json.loads(string), json.loads(js))

@unittest.skipIf(models.storage_t == 'db', "not testing file storage")
def test_get(self):
"""Test that get properly gets an object from the FileStorage.__objects
attr"""
storage = FileStorage()
for key, value in classes.items():
instance = value()
instance_key = instance.__class__.__name__ + "." + instance.id
storage.new(instance)
self.assertEqual(storage.get(instance.__class__, instance.id),
instance)
self.assertIsNone(storage.get(None, None))

@unittest.skipIf(models.storage_t == 'db', "not testing file storage")
def test_count(self):
"""Test that count returns the number of objects in storage"""
storage = FileStorage()
for key, value in classes.items():
instance = value()
instance_key = instance.__class__.__name__ + "." + instance.id
storage.new(instance)
self.assertEqual(storage.count(), 6)
self.assertEqual(storage.count(User), 1)
self.assertEqual(storage.count(State), 1)
self.assertEqual(storage.count(City), 1)
self.assertEqual(storage.count(Place), 1)
self.assertEqual(storage.count(Amenity), 1)
self.assertEqual(storage.count(Review), 1)