Exemplo n.º 1
0
 def test_attributes(self):
     """ Test if attributes are equal to default type """
     attrs = storage.attributes()["User"]
     b = User()
     for k, v in attrs.items():
         self.assertTrue(hasattr(b, k))
         self.assertEqual(type(getattr(b, k, None)), v)
Exemplo n.º 2
0
 def test_8_attributes(self):
     """Tests the attributes of Amenity class."""
     attributes = storage.attributes()["Amenity"]
     o = Amenity()
     for k, v in attributes.items():
         self.assertTrue(hasattr(o, k))
         self.assertEqual(type(getattr(o, k, None)), v)
Exemplo n.º 3
0
 def test_attributes_v2_BaseModel(self):
     """Test the attributes from the v2"""
     attributes = storage.attributes()["BaseModel"]
     o = BaseModel()
     for k, v in attributes.items():
         self.assertTrue(hasattr(o, k))
         self.assertEqual(type(getattr(o, k, None)), v)
Exemplo n.º 4
0
 def test_attributes(self):
     """ Tests attributes of Place class """
     attributes = storage.attributes()["Place"]
     b = Place()
     for k, v in attributes.items():
         self.assertTrue(hasattr(b, k))
         self.assertEqual(type(getattr(b, k, None)), v)
Exemplo n.º 5
0
    def test_3_attributes(self):
        """Tests attributes value for instance of a BaseModel class."""

        attributes = storage.attributes()["BaseModel"]
        o = BaseModel()
        for k, v in attributes.items():
            self.assertTrue(hasattr(o, k))
            self.assertEqual(type(getattr(o, k, None)), v)
Exemplo n.º 6
0
    def do_update(self, line):
        """Updates an instance by adding or updating attribute.
        """
        if line == "" or line is None:
            print("** class name missing **")
            return

        rex = r'^(\S+)(?:\s(\S+)(?:\s(\S+)(?:\s((?:"[^"]*")|(?:(\S)+)))?)?)?'
        match = re.search(rex, line)
        classname = match.group(1)
        uid = match.group(2)
        attribute = match.group(3)
        value = match.group(4)
        if not match:
            print("** class name missing **")
        elif classname not in storage.classes():
            print("** class doesn't exist **")
        elif uid is None:
            print("** instance id missing **")
        else:
            key = "{}.{}".format(classname, uid)
            if key not in storage.all():
                print("** no instance found **")
            elif not attribute:
                print("** attribute name missing **")
            elif not value:
                print("** value missing **")
            else:
                cast = None
                if not re.search('^".*"$', value):
                    if '.' in value:
                        cast = float
                    else:
                        cast = int
                else:
                    value = value.replace('"', '')
                attributes = storage.attributes()[classname]
                if attribute in attributes:
                    value = attributes[attribute](value)
                elif cast:
                    try:
                        value = cast(value)
                    except ValueError:
                        pass  # fine, stay a string then
                setattr(storage.all()[key], attribute, value)
                storage.all()[key].save()
Exemplo n.º 7
0
 def update_dict(self, classname, uid, s_dict):
     """Helper method for update() with a dictionary."""
     s = s_dict.replace("'", '"')
     d = json.loads(s)
     if not classname:
         print("** class name missing **")
     elif classname not in storage.classes():
         print("** class doesn't exist **")
     elif uid is None:
         print("** instance id missing **")
     else:
         key = "{}.{}".format(classname, uid)
         if key not in storage.all():
             print("** no instance found **")
         else:
             attributes = storage.attributes()[classname]
             for attribute, value in d.items():
                 if attribute in attributes:
                     value = attributes[attribute](value)
                 setattr(storage.all()[key], attribute, value)
             storage.all()[key].save()