Example #1
0
 def test_name(self):
     """
     Check the correct type of attribute name
     """
     new_instance = Amenity()
     new_instance.name = "lavo, plancho, cocino, cuido los guric"
     self.assertIsInstance(new_instance.name, str)
     second_instance = Amenity()
     second_instance.name = new_instance.name
     self.assertEqual(new_instance.name, second_instance.name)
Example #2
0
 def test_3_Amenity(self):
     """ check if the Amenity methods exists """
     Amenity_1 = Amenity()
     Amenity_1.name = "the greenzone"
     Amenity_1.save()
     self.assertTrue(os.path.exists('file.json'))
     self.assertTrue(Amenity_1.name, "the greenzone")
Example #3
0
 def test_amenity(self):
     """
     Test attributes of Class Amenity
     """
     my_amenity = Amenity()
     my_amenity.name = "Wi-Fi"
     self.assertEqual(my_amenity.name, 'Wi-Fi')
 def test_get_amenity(self):
     """Test get method that retrieve one class object"""
     new = Amenity()
     new.name = "ca"
     new.save()
     obj = storage.get("Amenity", new.id)
     self.assertEqual(new, obj)
 def test_name(self):
     """
     test the password class attribute
     """
     bill = Amenity()
     bill.name = "abdlfah"
     self.assertEqual(type(Amenity.name), str)
Example #6
0
 def test_class_attributes(self):
     """ Test User class attributes """
     check = 0
     my_amenity = Amenity()
     """
     Test keys in User dictionary
     """
     self.assertTrue(
         sorted(list(my_amenity.__dict__.keys())) == [
             'created_at', 'id', 'updated_at'
         ], True)
     self.assertEqual(my_amenity.name, "")
     """
     Test for class attributes in User
     """
     my_amenity.name = "Pool"
     my_amenity.save()
     self.assertEqual(my_amenity.name, "Pool")
     """
     Test file.json store the object just created
     """
     if os.path.isfile("file.json"):
         with open("file.json", 'r') as f:
             string = f.read()
             dic = json.loads(string)
             for key, value in dic.items():
                 if key.split('.')[1] == my_amenity.id:
                     check = 1
                     break
     self.assertEqual(check, 1)
Example #7
0
    def test_Amenity(self):
        """Task 9
        Tests `Amenity` class.
        """
        # Normal use: no args
        a1 = Amenity()
        self.assertIsInstance(a1, Amenity)

        # attr `name` defaults to empty string
        self.assertIsInstance(a1.name, str)
        self.assertEqual(a1.name, '')

        # Amenity can be serialized to JSON by FileStorage
        a1.name = 'test'
        self.assertIn(a1, storage._FileStorage__objects.values())
        a1.save()
        with open(storage._FileStorage__file_path, encoding='utf-8') as file:
            content = file.read()
        key = a1.__class__.__name__ + '.' + a1.id
        self.assertIn(key, json.loads(content))

        # Amenity can be deserialized from JSON by FileStorage
        self.assertIn(key, storage._FileStorage__objects.keys())
        storage._FileStorage__objects = dict()
        storage.reload()
        self.assertIn(key, storage._FileStorage__objects.keys())
Example #8
0
 def test_inherited_method_to_dict(self):
     """Test functionality of inherited methods."""
     new_instance = Amenity()
     new_instance.name = "o sea jelouuu"
     new_dict = new_instance.to_dict()
     self.assertIsInstance(new_dict, dict)
     self.assertEqual(new_dict["name"], new_instance.name)
Example #9
0
 def test_amenity(self):
     """ checks amenity """
     a = Amenity()
     self.assertEqual(type(a), Amenity)
     self.assertEqual(a.name, "")
     a.name = "b"
     self.assertEqual(a.name, "b")
 def test_basic(self):
     """Test for BaseModel
     """
     a = Amenity()
     a.name = "Holberton"
     a.number = 89
     self.assertEqual([a.name, a.number], ["Holberton", 89])
Example #11
0
 def test_hasattribute(self):
     """test that instance of Base have been correctly made"""
     a2 = Amenity()
     a2.name = "Indoor Fireplace"
     self.assertTrue(hasattr(a2, "created_at"))
     self.assertTrue(hasattr(a2, "updated_at"))
     self.assertTrue(hasattr(a2, "id"))
     self.assertTrue(hasattr(a2, "name"))
 def test_Amenity_attr_types(self):
     """Test Amenity instance attributes"""
     U = Amenity()
     U.name = "Space"
     self.assertEqual(type(U.id), str)
     self.assertTrue(type(U.created_at) is datetime.datetime)
     self.assertTrue(type(U.updated_at) is datetime.datetime)
     self.assertEqual(type(U.name), str)
Example #13
0
 def test_basic_test(self):
     """Basic tests for Amenity class"""
     self.assertTrue(issubclass(Amenity, BaseModel))
     my_model = Amenity()
     my_model.name = "Holberton"
     my_model.my_number = 89
     self.assertEqual([my_model.name, my_model.my_number],
                      ["Holberton", 89])
    def test_Amenity(self):
        """testing Amenity class"""

        a = Amenity()
        self.assertEqual(type(a), Amenity)
        self.assertEqual(a.name, "")
        a.name = "Muy bueno"
        self.assertEqual(a.name, "Muy bueno")
Example #15
0
 def test_assigned_attributes(self):
     """ test assigned attributes """
     brba = Amenity()
     brba.name = "Paradise"
     brba.appartment = 3
     self.assertTrue(hasattr(brba, "name"))
     self.assertTrue(hasattr(brba, "appartment"))
     self.assertEqual(type(brba.name), str)
     self.assertEqual(type(brba.appartment), int)
Example #16
0
    def test_kwargs_instantiation(self):
        """Tests instantiation with **kwargs."""

        my_model = Amenity()
        my_model.name = "Holberton"
        my_model.my_number = 89
        my_model_json = my_model.to_dict()
        my_new_model = Amenity(**my_model_json)
        self.assertEqual(my_new_model.to_dict(), my_model.to_dict())
Example #17
0
 def test_new_user(self):
     '''
     test base case
     '''
     new_a = Amenity()
     self.assertTrue(issubclass(Amenity, BaseModel))
     new_a.name = "betty"
     new_a.my_number = 12
     self.assertEqual([new_a.name, new_a.my_number], ["betty", 12])
 def test_Amenity_basic_instance(self):
     """Test Amenity instance"""
     A = Amenity()
     A.name = "Space"
     self.assertIsInstance(A, Amenity)
     self.assertEqual(Amenity, type(A))
     self.assertTrue(hasattr(A, "id"))
     self.assertTrue(hasattr(A, "created_at"))
     self.assertTrue(hasattr(A, "updated_at"))
     self.assertTrue(hasattr(A, "name"))
Example #19
0
 def test_amenity_dict_to_instance_with_kwargs(self):
     """Amenity can instantiate new object with dictionary"""
     amenity = Amenity()
     amenity.name = "Betty"
     amenity.number = 972
     amenity_dict = amenity.to_dict()
     new_amenity = Amenity(**amenity_dict)
     new_amenity_dict = new_amenity.to_dict()
     self.assertFalse(new_amenity is amenity)
     self.assertDictEqual(new_amenity_dict, amenity_dict)
Example #20
0
 def test_attr(self):
     """ Tests creation of new attributes """
     m1 = Amenity()
     m1.name = "John"
     self.assertAlmostEqual(m1.name, "John")
     m1.number = 123
     self.assertAlmostEqual(m1.number, 123)
     self.assertEqual(type(m1.id), str)
     self.assertEqual(type(m1.created_at), datetime.datetime)
     self.assertEqual(type(m1.updated_at), datetime.datetime)
Example #21
0
def create_amenity():
    """Creates an Amenity"""
    if not request.is_json:
        abort(400, "Not a JSON")
    if 'name' not in request.json:
        abort(400, "Missing name")
    amenity = Amenity()
    amenity.name = request.get_json().get('name')
    storage.new(amenity)
    storage.save()
    return jsonify(amenity.to_dict()), 201
Example #22
0
 def test_print_args(self):
     """ Test __str__ with args """
     b1 = Amenity(None, 1, ["A"])
     b1.name = "Holberton"
     b1.code = 123
     s = "[{:s}] ({:s}) {:s}\n"
     s = s.format(b1.__class__.__name__, b1.id, str(b1.__dict__))
     with patch('sys.stdout', new=io.StringIO()) as p:
         print(b1)
         st = p.getvalue()
         self.assertEqual(st, s)
 def test_Amenity_kwargs(self):
     """Test Amenity kwargs"""
     U1 = Amenity()
     U1.name = "Holberton"
     dict = U1.to_dict()
     U2 = Amenity(**dict)
     self.assertEqual(U1.id, U1.id)
     self.assertEqual(U1.created_at, U2.created_at)
     self.assertEqual(U1.updated_at, U2.updated_at)
     self.assertEqual(U1.name, U2.name)
     self.assertNotEqual(U1, U2)
def post_amenity():
    """Create a new Amenity"""
    if not request.get_json():
        return make_response(jsonify({'error': "Not a JSON"}), 400)
    if 'name' not in request.get_json():
        return make_response(jsonify({'error': "Missing name"}), 400)
    content = request.get_json()
    new_ame = Amenity()
    new_ame.name = content['name']
    new_ame.save()
    return make_response(jsonify(new_ame.to_dict()), 201)
Example #25
0
def create_amenity():
    is_json = request.get_json()
    if is_json is None:
        abort(400, description="Not a Json")

    if is_json.get('name') is None:
        abort(400, description="Missing name")

    new_state = Amenity()
    new_state.name = is_json.get('name')
    new_state.save()
    return (jsonify(new_state.to_dict())), 201
Example #26
0
 def test_06a_to_dict_values(self):
     """Test to validate to_dict values are all strings"""
     a = Amenity()
     a.name = "Tu"
     a.number = 1987
     d = a.to_dict()
     self.assertEqual(type(d['name']), str)
     self.assertEqual(type(d['number']), int)
     self.assertEqual(type(d['created_at']), str)
     self.assertEqual(type(d['updated_at']), str)
     self.assertEqual(type(d['id']), str)
     self.assertEqual(type(d['__class__']), str)
Example #27
0
 def test_06_to_dict(self):
     """Test to validate to_dict is outputting correctly"""
     a = Amenity()
     a.name = "Tu"
     a.number = 1987
     d = a.to_dict()
     self.assertTrue('number' in d)
     self.assertTrue('name' in d)
     self.assertTrue('id' in d)
     self.assertTrue('created_at' in d)
     self.assertTrue('updated_at' in d)
     self.assertTrue('__class__' in d)
Example #28
0
 def test_print(self):
     my_Amenity = Amenity()
     my_Amenity.name = "Rollercoaster"
     my_Amenity.save()
     temp_stdout = StringIO()
     with contextlib.redirect_stdout(temp_stdout):
         print(my_Amenity)
     output = temp_stdout.getvalue().strip()
     self.assertIn("[Amenity]", output)
     self.assertIn("'name': 'Rollercoaster'", output)
     self.assertIn("'created_at': datetime.datetime", output)
     self.assertIn("'updated_at': datetime.datetime", output)
Example #29
0
def create_amenity():
    """This function create a new amenity"""
    data = request.get_json()
    if not data:
        abort(400, 'Not a JSON')
    elif 'name' not in data:
        abort(400, 'Missing name')
    amenity = Amenity()
    amenity.name = data['name']
    amenity.save()
    amenity = amenity.to_dict()
    return jsonify(amenity), 201
Example #30
0
def postAmenity():
    """ creates a new Amenity """
    thing = request.get_json()
    if thing is None or not request.json:
        return (jsonify({"error": "Not a JSON"}), 400)
    amenity = thing.get("name")
    if amenity is None or len(thing) == 0:
        return (jsonify({"error": "Missing name"}), 400)
    a = Amenity()
    a.name = amenity
    a.save()
    return (jsonify(a.to_dict()), 201)