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

Add test for save method #99

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
20 changes: 20 additions & 0 deletions AirBnB_clone/tests/test_models/test_engine/test_file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from models.review import Review
from models.state import State
from models.user import User
import json
import os
import pep8
import unittest
FileStorage = file_storage.FileStorage
Expand Down Expand Up @@ -87,3 +89,21 @@ def test_new(self):
test_dict[instance_key] = instance
self.assertEqual(test_dict, storage._FileStorage__objects)
storage._FileStorage__objects = {}

def test_save(self):
"""Test that save properly saves objects to file.json"""
os.remove("file.json")
storage = FileStorage()
new_dict = {}
for key, value in classes.items():
instance = value()
instance_key = instance.__class__.__name__ + "." + instance.id
new_dict[instance_key] = instance
storage._FileStorage__objects = new_dict
storage.save()
for key, value in new_dict.items():
new_dict[key] = value.to_dict()
string = json.dumps(new_dict)
with open("file.json", "r") as f:
js = f.read()
self.assertEqual(json.loads(string), json.loads(js))