diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..954a4aa4cf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +task_tests diff --git a/README.md b/README.md index f1d72de6355..19b3f7ce8bf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/api/v1/__init__.py b/api/v1/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/api/v1/app.py b/api/v1/app.py new file mode 100644 index 00000000000..16cfa366a45 --- /dev/null +++ b/api/v1/app.py @@ -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) diff --git a/api/v1/views/__init__.py b/api/v1/views/__init__.py new file mode 100644 index 00000000000..d6bfb87f594 --- /dev/null +++ b/api/v1/views/__init__.py @@ -0,0 +1,4 @@ +from flask import Blueprint +from api.v1.views import * + +app_views = Blueprint("app_views", __name__, url_prefix="/api/v1") diff --git a/api/v1/views/index.py b/api/v1/views/index.py new file mode 100644 index 00000000000..7a82f2062e5 --- /dev/null +++ b/api/v1/views/index.py @@ -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"}) diff --git a/models/engine/db_storage.py b/models/engine/db_storage.py index b8e7d291e6f..c1eef869180 100755 --- a/models/engine/db_storage.py +++ b/models/engine/db_storage.py @@ -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 = {} @@ -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()) diff --git a/models/engine/file_storage.py b/models/engine/file_storage.py index c8cb8c1764d..83684cc91d3 100755 --- a/models/engine/file_storage.py +++ b/models/engine/file_storage.py @@ -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()) diff --git a/tests/test_models/test_engine/test_db_storage.py b/tests/test_models/test_engine/test_db_storage.py index 766e625b5af..f2404352cc9 100755 --- a/tests/test_models/test_engine/test_db_storage.py +++ b/tests/test_models/test_engine/test_db_storage.py @@ -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, @@ -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, @@ -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) diff --git a/tests/test_models/test_engine/test_file_storage.py b/tests/test_models/test_engine/test_file_storage.py index 1474a34fec0..6e95329295b 100755 --- a/tests/test_models/test_engine/test_file_storage.py +++ b/tests/test_models/test_engine/test_file_storage.py @@ -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, @@ -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, @@ -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)