Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage get count and Test cases #3674

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
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
16 changes: 8 additions & 8 deletions 0-setup_web_static.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env bash
# sets up the web servers for the deployment of web_static

sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y install nginx
sudo mkdir -p /data/web_static/releases/test /data/web_static/shared
echo "This is a test" | sudo tee /data/web_static/releases/test/index.html
sudo ln -sf /data/web_static/releases/test/ /data/web_static/current
sudo chown -hR ubuntu:ubuntu /data/
sudo sed -i '38i\\tlocation /hbnb_static/ {\n\t\talias /data/web_static/current/;\n\t}\n' /etc/nginx/sites-available/default
sudo apt - get - y update
sudo apt - get - y upgrade
sudo apt - get - y install nginx
sudo mkdir - p / data / web_static / releases / test / data / web_static / shared
echo "This is a test" | sudo tee / data / web_static / releases / test / index.html
sudo ln - sf / data / web_static / releases / test / /data / web_static / current
sudo chown - hR ubuntu: ubuntu / data/
sudo sed - i '38i\\tlocation /hbnb_static/ {\n\t\talias /data/web_static/current/;\n\t}\n' / etc / nginx / sites - available / default
sudo service nginx start
2 changes: 1 addition & 1 deletion 1-pack_web_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ def do_pack():
file_name = "versions/web_static_{}.tgz".format(date)
local("tar -cvzf {} web_static".format(file_name))
return file_name
except:
except BaseException:
return None
2 changes: 1 addition & 1 deletion 2-do_deploy_web_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ def do_deploy(archive_path):
run('rm -rf /data/web_static/current')
run('ln -s {}{}/ /data/web_static/current'.format(path, no_ext))
return True
except:
except BaseException:
return False
4 changes: 2 additions & 2 deletions 3-deploy_web_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def do_pack():
file_name = "versions/web_static_{}.tgz".format(date)
local("tar -cvzf {} web_static".format(file_name))
return file_name
except:
except BaseException:
return None


Expand All @@ -40,7 +40,7 @@ def do_deploy(archive_path):
run('rm -rf /data/web_static/current')
run('ln -s {}{}/ /data/web_static/current'.format(path, no_ext))
return True
except:
except BaseException:
return False


Expand Down
6 changes: 3 additions & 3 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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>
Jennifer Huang < 133 @ holbertonschool.com >
Alexa Orrico < 210 @ holbertonschool.com >
Joann Vuong < 130 @ holbertonschool.com >
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ 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)
Joseph Ogbah - [Github](https://github.com/joseph50079) / [Twitter](https://x.com/joseph50079)

Second part of Airbnb: Joann Vuong
## License
Expand Down
Binary file added __pycache__/console.cpython-310.pyc
Binary file not shown.
Empty file added api/__init__.py
Empty file.
Binary file added api/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added api/v1/__pycache__/app.cpython-310.pyc
Binary file not shown.
25 changes: 25 additions & 0 deletions api/v1/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/python3

"""flask app file module"""

import os
from flask import Flask
from models import storage
from api.v1.views import app_views


app = Flask(__name__)

app.register_blueprint(app_views, url_prefix='/api/v1')


@app.teardown_appcontext
def close_storage(exception):
"""method handles close storage"""
storage.close()


if __name__ == "__main__":
host = os.getenv('HBNB_API_HOST', '0.0.0.0')
port = os.getenv('HBNB_API_PORT', '5000')
app.run(host=host, port=port, debug=True, 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 api.v1.views.index import *
from flask import Blueprint

app_views = Blueprint('app_views', __name__)
Binary file added api/v1/views/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/index.cpython-310.pyc
Binary file not shown.
37 changes: 37 additions & 0 deletions api/v1/views/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from models.review import Review
from models.amenity import Amenity
from models.city import City
from models.state import State
from models.place import Place
from models.user import User
from models.base_model import BaseModel
from . import app_views
from flask import Flask, jsonify
from models import storage


j_dict = {
"status": "OK"
}


classes = {
'users': User, 'places': Place,
'states': State, 'cities': City, 'amenities': Amenity,
'reviews': Review
}


@app_views.route('/status')
def status():
"""returns status"""
return (jsonify(j_dict))


@app_views.route('/stats')
def stats():
"""Get stats of objects of Airbnb"""
stats_json = {}
for key, value in classes.items():
stats_json[key] = storage.count(value)
return stats_json
9 changes: 5 additions & 4 deletions console.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def _key_value_parser(self, args):
else:
try:
value = int(value)
except:
except BaseException:
try:
value = float(value)
except:
except BaseException:
continue
new_dict[key] = value
return new_dict
Expand Down Expand Up @@ -140,12 +140,12 @@ def do_update(self, arg):
if args[2] in integers:
try:
args[3] = int(args[3])
except:
except BaseException:
args[3] = 0
elif args[2] in floats:
try:
args[3] = float(args[3])
except:
except BaseException:
args[3] = 0.0
setattr(models.storage.all()[k], args[2], args[3])
models.storage.all()[k].save()
Expand All @@ -160,5 +160,6 @@ def do_update(self, arg):
else:
print("** class doesn't exist **")


if __name__ == '__main__':
HBNBCommand().cmdloop()
1 change: 1 addition & 0 deletions file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"State.996ef645-e958-4c5f-90f1-eb45af2f5730": {"id": "996ef645-e958-4c5f-90f1-eb45af2f5730", "created_at": "2024-08-04T03:56:59.177112", "updated_at": "2024-08-04T03:56:59.177235", "__class__": "State"}, "State.1a322f2d-7319-4f93-9392-062c99a5ce08": {"id": "1a322f2d-7319-4f93-9392-062c99a5ce08", "created_at": "2024-08-04T04:11:57.833175", "updated_at": "2024-08-04T04:11:57.833195", "__class__": "State"}, "State.6fdac736-83fa-4baa-a976-580a1edcb15b": {"id": "6fdac736-83fa-4baa-a976-580a1edcb15b", "created_at": "2024-08-04T04:11:57.840512", "updated_at": "2024-08-04T04:11:57.840530", "__class__": "State"}, "Place.14817679-277c-42f7-9ff8-1cbcfca030d4": {"id": "14817679-277c-42f7-9ff8-1cbcfca030d4", "created_at": "2024-08-04T04:11:57.875332", "updated_at": "2024-08-04T04:11:57.875346", "__class__": "Place"}, "State.9d80c1b3-83f9-4354-aa3a-531c6932a604": {"id": "9d80c1b3-83f9-4354-aa3a-531c6932a604", "created_at": "2024-08-04T04:35:03.014478", "updated_at": "2024-08-04T04:35:03.014497", "__class__": "State"}, "State.7095c4c2-91b7-48bd-bf30-72786ccfce93": {"id": "7095c4c2-91b7-48bd-bf30-72786ccfce93", "created_at": "2024-08-04T04:40:27.834196", "updated_at": "2024-08-04T04:40:27.834217", "__class__": "State"}, "State.d743e00b-cdf9-4e0d-9798-51cfb5fb0b9b": {"id": "d743e00b-cdf9-4e0d-9798-51cfb5fb0b9b", "created_at": "2024-08-04T04:50:38.295484", "updated_at": "2024-08-04T04:50:38.295543", "__class__": "State"}, "State.e63e7987-f895-4ea9-8d05-750c0c8e96ac": {"id": "e63e7987-f895-4ea9-8d05-750c0c8e96ac", "created_at": "2024-08-04T04:50:38.298376", "updated_at": "2024-08-04T04:50:38.298395", "__class__": "State"}, "Place.79b32ef9-34b6-4ff2-bfb9-39dca51b60e1": {"id": "79b32ef9-34b6-4ff2-bfb9-39dca51b60e1", "created_at": "2024-08-04T04:50:38.300770", "updated_at": "2024-08-04T04:50:38.300781", "__class__": "Place"}, "State.fb658501-a669-4453-ae35-9c14e73c8b0f": {"id": "fb658501-a669-4453-ae35-9c14e73c8b0f", "created_at": "2024-08-04T04:57:25.841595", "updated_at": "2024-08-04T04:57:25.841638", "__class__": "State"}, "State.15c8aa98-51c8-44a3-b2d5-9aa3a6c9fca1": {"id": "15c8aa98-51c8-44a3-b2d5-9aa3a6c9fca1", "created_at": "2024-08-04T04:57:25.843198", "updated_at": "2024-08-04T04:57:25.843216", "name": "\"California\"", "__class__": "State"}, "State.9b04197e-7271-4fd4-8a96-b8f0de7ac902": {"id": "9b04197e-7271-4fd4-8a96-b8f0de7ac902", "created_at": "2024-08-04T04:57:25.848963", "updated_at": "2024-08-04T04:57:25.848987", "__class__": "State"}, "State.e471dab2-f3d8-4e90-96cb-3204e3e24df1": {"id": "e471dab2-f3d8-4e90-96cb-3204e3e24df1", "created_at": "2024-08-04T04:57:25.851639", "updated_at": "2024-08-04T04:57:25.851658", "name": "\"Arizona\"", "__class__": "State"}, "Place.78b964f1-b822-45ed-8eb9-70a95fe9a8f3": {"id": "78b964f1-b822-45ed-8eb9-70a95fe9a8f3", "created_at": "2024-08-04T04:57:25.862470", "updated_at": "2024-08-04T04:57:25.862487", "__class__": "Place"}, "Place.5d62f943-225f-425b-8998-240556a32d44": {"id": "5d62f943-225f-425b-8998-240556a32d44", "created_at": "2024-08-04T04:57:25.866797", "updated_at": "2024-08-04T04:57:25.866812", "city_id": "\"0001\"", "user_id": "\"0001\"", "name": "\"My little house\"", "number_rooms": "4", "number_bathrooms": "2", "max_guest": "10", "price_by_night": "300", "latitude": "37.773972", "longitude": "-122.431297", "__class__": "Place"}, "State.658bc516-1fe7-460b-a984-9e8ae379e399": {"id": "658bc516-1fe7-460b-a984-9e8ae379e399", "created_at": "2024-08-04T05:15:43.996810", "updated_at": "2024-08-04T05:15:43.996832", "__class__": "State"}, "State.13b323d1-b614-413f-a5d6-cd5651b7c783": {"id": "13b323d1-b614-413f-a5d6-cd5651b7c783", "created_at": "2024-08-04T05:18:51.657749", "updated_at": "2024-08-04T05:18:51.657788", "__class__": "State"}, "State.eba96ca5-04c0-42d8-b976-86d5ed0e1b54": {"id": "eba96ca5-04c0-42d8-b976-86d5ed0e1b54", "created_at": "2024-08-04T05:18:51.659673", "updated_at": "2024-08-04T05:18:51.659688", "name": "\"California\"", "__class__": "State"}, "State.f58aade7-9f47-4774-994f-c13bd63b0cc7": {"id": "f58aade7-9f47-4774-994f-c13bd63b0cc7", "created_at": "2024-08-04T05:18:51.662674", "updated_at": "2024-08-04T05:18:51.662686", "__class__": "State"}, "State.a4d811d7-c2d7-4fd2-90ab-e31115a5f7ab": {"id": "a4d811d7-c2d7-4fd2-90ab-e31115a5f7ab", "created_at": "2024-08-04T05:18:51.686766", "updated_at": "2024-08-04T05:18:51.686812", "name": "\"Arizona\"", "__class__": "State"}, "Place.1cc4e718-6db8-43b6-840c-7f2540fb3108": {"id": "1cc4e718-6db8-43b6-840c-7f2540fb3108", "created_at": "2024-08-04T05:18:51.704738", "updated_at": "2024-08-04T05:18:51.704777", "__class__": "Place"}, "Place.cdf6da06-4ede-4e97-b414-e2b5b2e46b42": {"id": "cdf6da06-4ede-4e97-b414-e2b5b2e46b42", "created_at": "2024-08-04T05:18:51.728904", "updated_at": "2024-08-04T05:18:51.728945", "city_id": "\"0001\"", "user_id": "\"0001\"", "name": "\"My little house\"", "__class__": "Place"}, "State.4483525d-51bd-4087-9a2e-92d2e5977f89": {"id": "4483525d-51bd-4087-9a2e-92d2e5977f89", "created_at": "2024-08-04T05:49:59.911165", "updated_at": "2024-08-04T05:49:59.911186", "name": "California", "__class__": "State"}, "State.80516885-50f4-48fc-9e3b-146358008410": {"id": "80516885-50f4-48fc-9e3b-146358008410", "created_at": "2024-08-04T05:49:59.914648", "updated_at": "2024-08-04T05:49:59.914664", "name": "Arizona", "__class__": "State"}, "Place.f07cb5ae-c100-4fb9-9303-b4747bd21bc5": {"id": "f07cb5ae-c100-4fb9-9303-b4747bd21bc5", "created_at": "2024-08-04T05:49:59.921669", "updated_at": "2024-08-04T05:49:59.921695", "city_id": 1, "user_id": 1, "name": "My little house", "number_rooms": 4, "number_bathrooms": 2, "max_guest": 10, "price_by_night": 300, "latitude": 37.773972, "longitude": -122.431297, "__class__": "Place"}, "State.ec11758a-774d-4f72-9e98-649bbb22c5cf": {"id": "ec11758a-774d-4f72-9e98-649bbb22c5cf", "created_at": "2024-08-04T20:13:49.599430", "updated_at": "2024-08-04T20:13:49.599458", "name": "California", "__class__": "State"}, "State.99610c96-359c-4378-8ae8-a71e1c0232e3": {"id": "99610c96-359c-4378-8ae8-a71e1c0232e3", "created_at": "2024-08-04T20:13:49.607489", "updated_at": "2024-08-04T20:13:49.607501", "name": "Nevada", "__class__": "State"}, "State.623730e1-5b94-400e-8f07-a02063a96ac4": {"id": "623730e1-5b94-400e-8f07-a02063a96ac4", "created_at": "2024-08-04T20:15:24.038858", "updated_at": "2024-08-04T20:15:24.038875", "name": "California", "__class__": "State"}, "State.f125ed16-65e7-405b-936e-874071fa82a1": {"id": "f125ed16-65e7-405b-936e-874071fa82a1", "created_at": "2024-08-04T20:15:24.041639", "updated_at": "2024-08-04T20:15:24.041655", "name": "Nevada", "__class__": "State"}, "State.1da84a33-68b6-41e4-82b1-f157253a8f8a": {"id": "1da84a33-68b6-41e4-82b1-f157253a8f8a", "created_at": "2024-08-04T21:01:10.121766", "updated_at": "2024-08-04T21:01:10.121788", "name": "Nevada", "__class__": "State"}, "State.1606c649-e0fd-4694-af52-b74f9d6a5b44": {"id": "1606c649-e0fd-4694-af52-b74f9d6a5b44", "created_at": "2024-08-04T21:05:01.099571", "updated_at": "2024-08-04T21:05:01.099587", "name": "Nevada", "__class__": "State"}, "State.c418c6c7-1618-4042-ac8c-a78e8530dcbe": {"id": "c418c6c7-1618-4042-ac8c-a78e8530dcbe", "created_at": "2024-08-05T02:45:04.143045", "updated_at": "2024-08-05T02:45:04.143056", "name": "Nevada", "__class__": "State"}, "State.4e422fc4-83c6-41dc-a863-4f2b7a5f7628": {"id": "4e422fc4-83c6-41dc-a863-4f2b7a5f7628", "created_at": "2024-08-05T02:47:14.393207", "updated_at": "2024-08-05T02:47:14.393218", "name": "Nevada", "__class__": "State"}}
Binary file added models/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/amenity.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/base_model.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/city.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/place.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/review.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/state.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/user.cpython-310.pyc
Binary file not shown.
Binary file added models/engine/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions models/engine/db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,23 @@ def reload(self):
def close(self):
"""call remove() method on the private session attribute"""
self.__session.remove()

def get(self, cls, id):
"""get cls objects from id else None"""
try:
if cls:
obj = self.__session.query(cls).get(id)
return obj
else:
return None
except Exception:
return None

def count(self, cls=None):
"""
Get count of all class or cls if specifyied all not None
"""
if cls:
return len(self.all(cls))
else:
return len(self.all())
24 changes: 23 additions & 1 deletion models/engine/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def reload(self):
jo = json.load(f)
for key in jo:
self.__objects[key] = classes[jo[key]["__class__"]](**jo[key])
except:
except BaseException:
pass

def delete(self, obj=None):
Expand All @@ -68,3 +68,25 @@ 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):
"""get the objects of cls and id"""

if cls:
for v in self.__objects.values():
if cls == v.__class__ or cls == v.__class__.__name__:
if id == v.id:
return v

return None
else:
return None

def count(self, cls=None):
"""
Get count of all or cls
"""
if cls:
num = len(self.all(cls))
return num
return len(self.all())
12 changes: 12 additions & 0 deletions test_get_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/python3
""" Test .get() and .count() methods
"""
from models import storage
from models.state import State

print("All objects: {}".format(storage.count()))
print("State objects: {}".format(storage.count(State)))

first_state_id = list(storage.all(State).values())[0].id
print(first_state_id)
print("First state: {}".format(storage.get(State, first_state_id)))
Binary file added tests/__pycache__/test_console.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/test_models/test_engine/test_db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@ def test_new(self):
@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_save(self):
"""Test that save properly saves objects to file.json"""
pass

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_count(self):
"""for count() Test"""
pass
30 changes: 30 additions & 0 deletions tests/test_models/test_engine/test_file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,33 @@ def test_save(self):
with open("file.json", "r") as f:
js = f.read()
self.assertEqual(json.loads(string), json.loads(js))

def test_count(self):
""" Test .count() methods
"""
storage = FileStorage()
new_dict = {}
for key, value in classes.items():
instance = value()
instance_key = instance.__class__.__name__ + "." + instance.id
new_dict[instance_key] = instance
save = FileStorage._FileStorage__objects
FileStorage._FileStorage__objects = new_dict
storage.save()
FileStorage._FileStorage__objects = save

all_count = storage.count()
state_all = storage.count(State)
self.assertLessEqual(state_all, all_count)
self.assertNotEqual(all_count, 0)
self.assertNotEqual(state_all, 0)

def test_get(self):
""" Test the .get() methods in Filestorage
"""
storage = FileStorage()

first_state_id = list(storage.all(State).values())[0].id
self.assertIsNotNone(first_state_id)
val = storage.get(State, first_state_id)
self.assertIsNotNone(val)