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 #3684

Open
wants to merge 3 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
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

Jennifer Huang <133@holbertonschool.com>
Alexa Orrico <210@holbertonschool.com>
Joann Vuong <130@holbertonschool.com>
Joann Vuong <130@holbertonschool.com>
Nwali Ugonna Emmanuel <emmanuelsticx6@gmail.com>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ No known bugs at this time.
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)

Nwali Ugonna Emmanuel - [Github](https://github.com/Tigo-cmd)

Second part of Airbnb: Joann Vuong
## License
Public Domain. No copy write protection.
20 changes: 19 additions & 1 deletion models/engine/db_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3
"""
Contains the class DBStorage
This Module Contains the class DBStorage
"""

import models
Expand Down Expand Up @@ -74,3 +74,21 @@ def reload(self):
def close(self):
"""call remove() method on the private session attribute"""
self.__session.remove()

def get(self, cls, id) -> __class__ | int | None:
"""returns the object based on it's class and ID, of None if not found
Args:
cls: class to be returned
id: string representing the object ID class to be returned and none if not exists
"""
if cls in classes.values():
return self.__session.query(cls).filter(cls.id == id).first()
return None

def count(self, cls=None):
"""Counts the number of objects in storage andReturns the number of objects,
in storage matching the given class. If no class is passed, returns the count of all objects in storage.
Args:
cls: class (optional)
"""
return len(self.all(cls))
20 changes: 19 additions & 1 deletion models/engine/file_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3
"""
Contains the FileStorage class
This Module Contains the FileStorage class for all functionality
"""

import json
Expand Down Expand Up @@ -68,3 +68,21 @@ 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) -> __class__ | int | None:
"""returns the object based on it's class and ID, of None if not found
Args:
cls: class to be returned
id: string representing the object ID class to be returned and none if not exists
"""
if cls in classes.values():
return self.__session.query(cls).filter(cls.id == id).first()
return None

def count(self, cls=None):
"""Counts the number of objects in storage andReturns the number of objects,
in storage matching the given class. If no class is passed, returns the count of all objects in storage.
Args:
cls: class (optional)
"""
return len(self.all(cls))
37 changes: 36 additions & 1 deletion tests/test_models/test_engine/test_db_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3
"""
Contains the TestDBStorageDocs and TestDBStorage classes
This TestModule Contains the TestDBStorageDocs and TestDBStorage classes
"""

from datetime import datetime
Expand Down Expand Up @@ -86,3 +86,38 @@ 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"""

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_get(self, cls, id):
"""tests the get method properly for all functionalities"""

models.storage.reload()
user = User(email="emmanwaliugo@gmail.com", password="123")
models.storage.new(user)
models.storage.save()
user_id = user.id
bad_id = "666"
self.assertTrue(models.storage.get(User, user_id) is user)
self.assertIsNone(models.storage.get(User, bad_id))
models.storage.delete(user)
state = State(name="Abia")
models.storage.new(state)
models.storage.save()
state_id = State.id
bad_id = "sharwarma"
self.assertTrue(models.storage.get(State, state_id) is state)
self.assertIsNone(models.storage.get(State, bad_id))

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_count(self):
"""test the count id it returns accurate number of objects"""

models.storage.reload()
state = State(name="Abakaliki")
user = User(email="ja161612040@gmail.com", password="234")
models.storage.new(state)
models.storage.new(user)
models.storage.save()
self.assertEqual(models.storage.count(), 2)
models.storage.delete(user)
models.storage.delete(state)
35 changes: 34 additions & 1 deletion tests/test_models/test_engine/test_file_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3
"""
Contains the TestFileStorageDocs classes
This testModule Contains the TestFileStorageDocs classes for functionalities
"""

from datetime import datetime
Expand Down Expand Up @@ -113,3 +113,36 @@ 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 db storage")
def test_get(self, cls, id):
"""tests the get method properly fro all functionalities"""
models.storage.reload()
user = User(email="emmanwaliugo@gmail.com", password="123")
models.storage.new(user)
models.storage.save()
user_id = user.id
bad_id = "666"
self.assertTrue(models.storage.get(User, user_id) is user)
self.assertIsNone(models.storage.get(User, bad_id))
models.storage.delete(user)
state = State(name="Abia")
models.storage.new(state)
models.storage.save()
state_id = State.id
bad_id = "sharwarma"
self.assertTrue(models.storage.get(State, state_id) is state)
self.assertIsNone(models.storage.get(State, bad_id))

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_count(self):
"""test the count id it returns accurate number of objects"""
models.storage.reload()
state = State(name="Abakaliki")
user = User(email="ja161612040@gmail.com", password="234")
models.storage.new(state)
models.storage.new(user)
models.storage.save()
self.assertEqual(models.storage.count(), 2)
models.storage.delete(user)
models.storage.delete(state)