Esempio n. 1
0
class Test_PlaceModel(unittest.TestCase):
    """
    Test the place model class
    """

    def setUp(self):
        self.cli = HBNBCommand()
        self.model = Place()
        self.model.save()

    def test_var_initialization(self):
        self.assertTrue(hasattr(self.model, "city_id"))
        self.assertTrue(hasattr(self.model, "user_id"))
        self.assertTrue(hasattr(self.model, "name"))
        self.assertTrue(hasattr(self.model, "description"))
        self.assertTrue(hasattr(self.model, "number_rooms"))
        self.assertTrue(hasattr(self.model, "number_bathrooms"))
        self.assertTrue(hasattr(self.model, "max_guest"))
        self.assertTrue(hasattr(self.model, "price_by_night"))
        self.assertTrue(hasattr(self.model, "latitude"))
        self.assertTrue(hasattr(self.model, "longitude"))
        self.assertTrue(hasattr(self.model, "amenities"))
        self.assertEqual(self.model.city_id, "")
        self.assertEqual(self.model.user_id, "")
        self.assertEqual(self.model.name, "")
        self.assertEqual(self.model.description, "")
        self.assertEqual(self.model.number_rooms, 0)
        self.assertEqual(self.model.number_bathrooms, 0)
        self.assertEqual(self.model.max_guest, 0)
        self.assertEqual(self.model.price_by_night, 0)
        self.assertEqual(self.model.latitude, 0.0)
        self.assertEqual(self.model.longitude, 0.0)
        self.assertEqual(self.model.amenities, [''])
        self.cli.do_destroy("Place " + self.model.id)
class Test_AmenityModel(unittest.TestCase):
    """
    Test the amenity model class
    """
    def setUp(self):
        self.cli = HBNBCommand()
        self.model = Amenity()
        self.model.save()

    def test_var_initialization(self):
        self.assertTrue(hasattr(self.model, "name"))
        self.assertEqual(self.model.name, "")
        self.cli.do_destroy("Amenity " + self.model.id)
class Test_BaseModel(unittest.TestCase):
    """
    Test the base model class
    """
    def setUp(self):
        self.cli = HBNBCommand()
        self.model1 = BaseModel()

        test_args = {
            'created_at': datetime(2017, 2, 10, 2, 6, 55, 258849),
            'updated_at': datetime(2017, 2, 10, 2, 6, 55, 258966),
            'id': '46458416-e5d5-4985-aa48-a2b369d03d2a',
            'name': 'model1'
        }
        self.model2 = BaseModel(test_args)
        self.model2.save()

    def tearDown(self):
        self.cli.do_destroy("BaseModel " + self.model1.id)
        self.cli.do_destroy("BaseModel " + self.model2.id)

    def test_instantiation(self):
        self.assertIsInstance(self.model1, BaseModel)
        self.assertTrue(hasattr(self.model1, "created_at"))
        self.assertTrue(hasattr(self.model1, "id"))
        self.assertFalse(hasattr(self.model1, "updated_at"))

    def test_reinstantiation(self):
        self.assertIsInstance(self.model2, BaseModel)
        self.assertEqual(self.model2.id,
                         '46458416-e5d5-4985-aa48-a2b369d03d2a')
        self.assertEqual(self.model2.created_at,
                         datetime(2017, 2, 10, 2, 6, 55, 258849))

    def test_save(self):
        self.assertFalse(hasattr(self.model1, "updated_at"))
        self.model1.save()
        self.assertTrue(hasattr(self.model1, "updated_at"))
        old_time = self.model2.updated_at
        self.model2.save()
        self.assertNotEqual(old_time, self.model2.updated_at)

    def test_to_json(self):
        jsonified = self.model2.to_json()
        self.assertNotEqual(self.model2.__dict__, jsonified)
        self.assertNotIsInstance(jsonified["created_at"], datetime)
        self.assertNotIsInstance(jsonified["updated_at"], datetime)
        self.assertEqual(jsonified["created_at"], '2017-02-10 02:06:55.258849')
        self.assertTrue(hasattr(jsonified, "__class__"))
        self.assertEqual(jsonified["__class__"], "BaseModel")
class Test_ReviewModel(unittest.TestCase):
    """
    Test the review model class
    """

    def setUp(self):
        self.cli = HBNBCommand()
        self.model = Review()
        self.model.save()

    def test_var_initialization(self):
        self.assertTrue(hasattr(self.model, "place_id"))
        self.assertTrue(hasattr(self.model, "user_id"))
        self.assertTrue(hasattr(self.model, "text"))
        self.assertEqual(self.model.place_id, "")
        self.assertEqual(self.model.user_id, "")
        self.assertEqual(self.model.text, "")
        self.cli.do_destroy("Review " + self.model.id)
Esempio n. 5
0
class Test_UserModel(unittest.TestCase):
    """
    Test the user model class
    """
    def setUp(self):
        self.cli = HBNBCommand()
        self.model = User()
        self.model.save()

    def test_var_initialization(self):
        self.assertTrue(hasattr(self.model, "email"))
        self.assertTrue(hasattr(self.model, "password"))
        self.assertTrue(hasattr(self.model, "first_name"))
        self.assertTrue(hasattr(self.model, "last_name"))
        self.assertEqual(self.model.email, "")
        self.assertEqual(self.model.password, "")
        self.assertEqual(self.model.first_name, "")
        self.assertEqual(self.model.last_name, "")
        self.cli.do_destroy("User " + self.model.id)
Esempio n. 6
0
class Test_FileStorage(unittest.TestCase):
    """
    Test the file storage class
    """
    def setUp(self):
        self.arr = []
        self.cli = HBNBCommand()
        self.store = FileStorage()

        test_args = {
            'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
            'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
            'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900)
        }
        self.model = BaseModel(test_args)

        self.test_len = 0
        if os.path.isfile("file.json"):
            self.test_len = len(self.store.all())

    def test_all(self):
        self.assertEqual(len(self.store.all()), self.test_len)

    def test_new(self):
        self.assertEqual(len(self.store.all()), self.test_len)
        self.model.save()
        self.assertEqual(len(self.store.all()), self.test_len + 1)
        a = BaseModel()
        a.save()
        self.assertEqual(len(self.store.all()), self.test_len + 2)
        self.cli.do_destroy("BaseModel f519fb40-1f5c-458b-945c-2ee8eaaf4900")
        self.cli.do_destroy("BaseModel " + a.id)

    def test_save(self):
        self.test_len = len(self.store.all())
        a = BaseModel()
        a.save()
        self.assertEqual(len(self.store.all()), self.test_len + 1)
        b = User()
        self.assertNotEqual(len(self.store.all()), self.test_len + 2)
        b.save()
        self.assertEqual(len(self.store.all()), self.test_len + 2)
        self.cli.do_destroy("BaseModel " + a.id)
        self.cli.do_destroy("User " + b.id)

    def test_reload(self):
        pass
class Test_Console(unittest.TestCase):
    """
    Test the console
    """
    def setUp(self):
        self.cli = HBNBCommand()

        test_args = {
            'updated_at': datetime(2017, 2, 11, 23, 48, 34, 339879),
            'id': 'd3da85f2-499c-43cb-b33d-3d7935bc808c',
            'created_at': datetime(2017, 2, 11, 23, 48, 34, 339743),
            'name': 'Ace'
        }
        self.model = BaseModel(test_args)
        self.model.save()

    def tearDown(self):
        self.cli.do_destroy("BaseModel d3da85f2-499c-43cb-b33d-3d7935bc808c")

    def test_quit(self):
        with self.assertRaises(SystemExit):
            self.cli.do_quit(self.cli)

    def test_show_correct(self):
        with captured_output() as (out, err):
            self.cli.do_show("BaseModel d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertFalse("2017, 2, 11, 23, 48, 34, 339879" in output)
        self.assertTrue('2017, 2, 11, 23, 48, 34, 339743' in output)

    def test_show_error_no_args(self):
        with captured_output() as (out, err):
            self.cli.do_show('')
        output = out.getvalue().strip()
        self.assertEqual(output, "** class name missing **")

    def test_show_error_missing_arg(self):
        with captured_output() as (out, err):
            self.cli.do_show("BaseModel")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_show_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_show("Human 1234-5678-9101")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_show_error_class_missing(self):
        with captured_output() as (out, err):
            self.cli.do_show("d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_create(self):
        with captured_output() as (out, err):
            self.cli.do_create('')
        output = out.getvalue().strip()
        self.assertEqual(output, "** clas name missing **")

        with captured_output() as (out, err):
            self.cli.do_create("BaseModel")
        output = out.getvalue().strip()

        with captured_output() as (out, err):
            self.cli.do_show("BaseModel {}".format(output))
        output2 = out.getvalue().strip()
        self.assertTrue(output in output2)

    def test_destroy_correct(self):
        test_args = {
            'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
            'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
            'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900)
        }
        testmodel = BaseModel(test_args)
        testmodel.save()
        self.cli.do_destroy("BaseModel f519fb40-1f5c-458b-945c-2ee8eaaf4900")

        with captured_output() as (out, err):
            self.cli.do_show("BaseModel f519fb40-1f5c-458b-945c-2ee8eaaf4900")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_destroy_error_missing_id(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("BaseModel")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_destroy_error_class_missing(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_destroy_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("Human d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_destroy_error_invalid_id(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("BaseModel " +
                                "f519fb40-1f5c-458b-945c-2ee8eaaf4900")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_all_correct(self):
        test_args = {
            'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
            'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
            'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900)
        }
        testmodel = BaseModel(test_args)
        testmodel.save()
        with captured_output() as (out, err):
            self.cli.do_all("")
        output = out.getvalue().strip()
        self.assertTrue("d3da85f2-499c-43cb-b33d-3d7935bc808c" in output)
        self.assertTrue("f519fb40-1f5c-458b-945c-2ee8eaaf4900" in output)
        self.assertFalse("123-456-abc" in output)
        self.cli.do_destroy("BaseModel " +
                            "f519fb40-1f5c-458b-945c-2ee8eaaf4900")

    def test_all_correct_with_class(self):
        with captured_output() as (out, err):
            self.cli.do_all("BaseModel")
        output = out.getvalue().strip()
        self.assertTrue(len(output) > 0)
        self.assertTrue("d3da85f2-499c-43cb-b33d-3d7935bc808c" in output)

    def test_all_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_all("Human")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_update_correct(self):
        with captured_output() as (out, err):
            self.cli.do_update("BaseModel " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name Bay")
        output = out.getvalue().strip()
        self.assertEqual(output, '')

        with captured_output() as (out, err):
            self.cli.do_show("BaseModel d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertTrue("Bay" in output)
        self.assertFalse("Ace" in output)

    def test_update_error_invalid_id(self):
        with captured_output() as (out, err):
            self.cli.do_update("BaseModel 123-456-abc name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_update_error_no_id(self):
        with captured_output() as (out, err):
            self.cli.do_update("BaseModel name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")

    def test_update_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_update("Human " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_update_error_no_class(self):
        with captured_output() as (out, err):
            self.cli.do_update("d3da85f2-499c-43cb-b33d-3d7935bc808c name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")

    def test_update_error_missing_value(self):
        with captured_output() as (out, err):
            self.cli.do_update("BaseModel " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")
Esempio n. 8
0
class Test_Console(unittest.TestCase):
    """
    Test the console
    """

    def setUp(self):
        self.cli = HBNBCommand()
        test_args = {'updated_at': datetime(2017, 2, 11, 23, 48, 34, 339879),
                     'id': 'd3da85f2-499c-43cb-b33d-3d7935bc808c',
                     'created_at': datetime(2017, 2, 11, 23, 48, 34, 339743),
                     'name': 'SELF.MODEL'}
        self.model = Amenity(**test_args)
        self.model.save()

    def tearDown(self):
        self.cli.do_destroy("Amenity d3da85f2-499c-43cb-b33d-3d7935bc808c")

    def test_quit(self):
        with self.assertRaises(SystemExit):
            self.cli.do_quit(self.cli)

    # one test for db without time zone, one for file
    @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE', 'fs') != 'db', "db")
    def test_show_correct(self):
        with captured_output() as (out, err):
            self.cli.do_show("Amenity d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertFalse("2017, 2, 11, 23, 48, 91" in output)
        self.assertTrue('2017, 2, 11, 23, 48, 34' in output)

    @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE', 'fs') == 'db', "db")
    def test_show_correct(self):
        with captured_output() as (out, err):
            self.cli.do_show("Amenity d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertFalse("2017, 2, 11, 23, 48, 91, 339743" in output)
        self.assertTrue('2017, 2, 11, 23, 48, 34, 339743' in output)

    def test_show_error_no_args(self):
        with captured_output() as (out, err):
            self.cli.do_show('')
        output = out.getvalue().strip()
        self.assertEqual(output, "** class name missing **")

    def test_show_error_missing_arg(self):
        with captured_output() as (out, err):
            self.cli.do_show("Amenity")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_show_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_show("Human 1234-5678-9101")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_show_error_class_missing(self):
        with captured_output() as (out, err):
            self.cli.do_show("d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_create_no_arg(self):
        with captured_output() as (out, err):
            self.cli.do_create('')
        output = out.getvalue().strip()
        self.assertEqual(output, "** class name missing **")

    @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE', 'fs') == 'db', "db")
    def test_create_with_FS(self):
        with captured_output() as (out, err):
            self.cli.do_create("Amenity")
        output = out.getvalue().strip()

        with captured_output() as (out, err):
            self.cli.do_show("Amenity {}".format(output))
        output2 = out.getvalue().strip()
        self.assertTrue(output in output2)

        self.cli.do_destroy("Amenity " + output)

    def test_destroy_correct(self):
        test_args = {'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
                     'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4901',
                     'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900),
                     'name': "TEST_DESTROY"}
        testmodel = Amenity(test_args)
        testmodel.save()
        self.cli.do_destroy("Amenity f519fb40-1f5c-458b-945c-2ee8eaaf4901")

        with captured_output() as (out, err):
            self.cli.do_show("Amenity f519fb40-1f5c-458b-945c-2ee8eaaf4901")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_destroy_error_missing_id(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("Amenity")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_destroy_error_class_missing(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_destroy_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("Human d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_destroy_error_invalid_id(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("Amenity " +
                                "f519fb40-1f5c-AAA-945c-2ee8eaaf4900")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_all_correct(self):
        test_args = {'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
                     'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
                     'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900),
                     'name': "TEST_ALL_CORRECT"}
        testmodel = Amenity(test_args)
        testmodel.save()
        with captured_output() as (out, err):
            self.cli.do_all("")
        output = out.getvalue().strip()
        self.assertTrue("d3da85f2-499c-43cb-b33d-3d7935bc808c" in output)
        self.assertTrue("f519fb40-1f5c-458b-945c-2ee8eaaf4900" in output)
        self.assertFalse("123-456-abc" in output)

        self.cli.do_destroy("Amenity " + test_args['id'])

    def test_all_correct_with_class(self):
        with captured_output() as (out, err):
            self.cli.do_all("Amenity")
        output = out.getvalue().strip()
        self.assertTrue(len(output) > 0)
        self.assertTrue("d3da85f2-499c-43cb-b33d-3d7935bc808c" in output)

    def test_all_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_all("Human")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_update_correct(self):
        with captured_output() as (out, err):
            self.cli.do_update("Amenity " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name Bay")
        output = out.getvalue().strip()
        self.assertEqual(output, '')

        with captured_output() as (out, err):
            self.cli.do_show("Amenity d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertTrue("Bay" in output)
        self.assertFalse("Ace" in output)

    def test_update_error_invalid_id(self):
        with captured_output() as (out, err):
            self.cli.do_update("Amenity 123-456-abc name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_update_error_no_id(self):
        with captured_output() as (out, err):
            self.cli.do_update("Amenity name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")

    def test_update_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_update("Human " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_update_error_no_class(self):
        with captured_output() as (out, err):
            self.cli.do_update("d3da85f2-499c-43cb-b33d-3d7935bc808c name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")

    def test_update_error_missing_value(self):
        with captured_output() as (out, err):
            self.cli.do_update("Amenity " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")

    def test_state_argument(self):
        with captured_output() as (out, err):
            self.cli.do_create('State name="WEIRD"')
        delete_me = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_all("State")
        output = out.getvalue().strip()
        self.assertTrue("WEIRD" in output)

        self.cli.do_destroy("State " + delete_me)

    def test_city_2_arguments(self):
        with captured_output() as (out, err):
            self.cli.do_create('State name="Arizona"')
        state = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('City state_id="{}" name="Fremont"'.format(
                state))
        city = out.getvalue().strip()

        self.cli.do_destroy("State " + state)
        self.cli.do_destroy("City " + city)

    def test_city_2_arguments_space(self):
        with captured_output() as (out, err):
            self.cli.do_create('State name="Arizona"')
        state = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('City state_id="{}" name="Alpha_Beta"'.format(
                state))
        city = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_show('City {}'.format(city))
        output = out.getvalue().strip()
        self.assertTrue("Alpha Beta" in output)

        self.cli.do_destroy("State " + state)
        self.cli.do_destroy("City " + city)

    def test_place(self):
        with captured_output() as (out, err):
            self.cli.do_create('State name="Another_State"')
        state_id = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('City state_id="{}" name="Alpha_Beta"'.format(
                state_id))
        city_id = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('User email="*****@*****.**" password="******" '
                               'first_name="John" last_name="Doe"')
        user_id = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('Place city_id="{}" user_id="{}"'
                               ' name="My_house"'
                               ' description="no_description_yet"'
                               ' number_rooms=4 number_bathrooms=1 max_guest=3'
                               ' price_by_night=100 latitude=120.12'
                               ' longitude=101.4'.format(city_id, user_id))
        place_id = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_show('Place {}'.format(place_id))
        output = out.getvalue().strip()
        self.assertTrue("My house" in output)
        self.assertTrue("100" in output)
        self.assertTrue("120.12" in output)

        self.cli.do_destroy("Place " + place_id)
        self.cli.do_destroy("User " + user_id)
        self.cli.do_destroy("City " + city_id)
        self.cli.do_destroy("State " + state_id)
Esempio n. 9
0
class Test_Console(unittest.TestCase):
    """
    Test the console
    """

    def setUp(self):
        self.cli = HBNBCommand()
        test_args = {'updated_at': datetime(2017, 2, 11, 23, 48, 34, 339879),
                     'id': 'd3da85f2-499c-43cb-b33d-3d7935bc808c',
                     'created_at': datetime(2017, 2, 11, 23, 48, 34, 339743),
                     'name': 'SELF.MODEL'}
        self.model = Amenity(**test_args)
        self.model.save()

    def tearDown(self):
        self.cli.do_destroy("Amenity d3da85f2-499c-43cb-b33d-3d7935bc808c")

    def test_quit(self):
        with self.assertRaises(SystemExit):
            self.cli.do_quit(self.cli)

    # one test for db without time zone, one for file
    @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE', 'fs') != 'db', "db")
    def test_show_correct(self):
        with captured_output() as (out, err):
            self.cli.do_show("Amenity d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertFalse("2017, 2, 11, 23, 48, 91" in output)
        self.assertTrue('2017, 2, 11, 23, 48, 34' in output)

    @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE', 'fs') == 'db', "db")
    def test_show_correct(self):
        with captured_output() as (out, err):
            self.cli.do_show("Amenity d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertFalse("2017, 2, 11, 23, 48, 91, 339743" in output)
        self.assertTrue('2017, 2, 11, 23, 48, 34, 339743' in output)

    def test_show_error_no_args(self):
        with captured_output() as (out, err):
            self.cli.do_show('')
        output = out.getvalue().strip()
        self.assertEqual(output, "** class name missing **")

    def test_show_error_missing_arg(self):
        with captured_output() as (out, err):
            self.cli.do_show("Amenity")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_show_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_show("Human 1234-5678-9101")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_show_error_class_missing(self):
        with captured_output() as (out, err):
            self.cli.do_show("d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_create_no_arg(self):
        with captured_output() as (out, err):
            self.cli.do_create('')
        output = out.getvalue().strip()
        self.assertEqual(output, "** class name missing **")

    @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE', 'fs') == 'db', "db")
    def test_create_with_FS(self):
        with captured_output() as (out, err):
            self.cli.do_create("Amenity")
        output = out.getvalue().strip()

        with captured_output() as (out, err):
            self.cli.do_show("Amenity {}".format(output))
        output2 = out.getvalue().strip()
        self.assertTrue(output in output2)

    def test_destroy_correct(self):
        test_args = {'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
                     'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4901',
                     'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900),
                     'name': "TEST_DESTROY"}
        testmodel = Amenity(test_args)
        testmodel.save()
        self.cli.do_destroy("Amenity f519fb40-1f5c-458b-945c-2ee8eaaf4901")

        with captured_output() as (out, err):
            self.cli.do_show("Amenity f519fb40-1f5c-458b-945c-2ee8eaaf4901")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_destroy_error_missing_id(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("Amenity")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_destroy_error_class_missing(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_destroy_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("Human d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_destroy_error_invalid_id(self):
        with captured_output() as (out, err):
            self.cli.do_destroy("Amenity " +
                                "f519fb40-1f5c-AAA-945c-2ee8eaaf4900")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_all_correct(self):
        test_args = {'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
                     'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
                     'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900),
                     'name': "TEST_ALL_CORRECT"}
        testmodel = Amenity(test_args)
        testmodel.save()
        with captured_output() as (out, err):
            self.cli.do_all("")
        output = out.getvalue().strip()
        self.assertTrue("d3da85f2-499c-43cb-b33d-3d7935bc808c" in output)
        self.assertTrue("f519fb40-1f5c-458b-945c-2ee8eaaf4900" in output)
        self.assertFalse("123-456-abc" in output)

    def test_all_correct_with_class(self):
        with captured_output() as (out, err):
            self.cli.do_all("Amenity")
        output = out.getvalue().strip()
        self.assertTrue(len(output) > 0)
        self.assertTrue("d3da85f2-499c-43cb-b33d-3d7935bc808c" in output)

    def test_all_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_all("Human")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_update_correct(self):
        with captured_output() as (out, err):
            self.cli.do_update("Amenity " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name Bay")
        output = out.getvalue().strip()
        self.assertEqual(output, '')

        with captured_output() as (out, err):
            self.cli.do_show("Amenity d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertTrue("Bay" in output)
        self.assertFalse("Ace" in output)

    def test_update_error_invalid_id(self):
        with captured_output() as (out, err):
            self.cli.do_update("Amenity 123-456-abc name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_update_error_no_id(self):
        with captured_output() as (out, err):
            self.cli.do_update("Amenity name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")

    def test_update_error_invalid_class(self):
        with captured_output() as (out, err):
            self.cli.do_update("Human " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_update_error_no_class(self):
        with captured_output() as (out, err):
            self.cli.do_update("d3da85f2-499c-43cb-b33d-3d7935bc808c name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")

    def test_update_error_missing_value(self):
        with captured_output() as (out, err):
            self.cli.do_update("Amenity " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")

    def test_state_argument(self):
        with captured_output() as (out, err):
            self.cli.do_create('State name="WEIRD"')
        with captured_output() as (out, err):
            self.cli.do_all("State")
        output = out.getvalue().strip()
        self.assertTrue("WEIRD" in output)

    def test_city_2_arguments(self):
        with captured_output() as (out, err):
            self.cli.do_create('State name="Arizona"')
        output = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('City state_id="{}" name="Fremont"'.format(
                output))

    def test_city_2_arguments_space(self):
        with captured_output() as (out, err):
            self.cli.do_create('State name="Arizona"')
        output = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('City state_id="{}" name="Alpha_Beta"'.format(
                output))
        output = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_show('City {}'.format(output))
        output = out.getvalue().strip()
        self.assertTrue("Alpha Beta" in output)

    def test_place(self):
        with captured_output() as (out, err):
            self.cli.do_create('State name="Another_State"')
        state_id = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('City state_id="{}" name="Alpha_Beta"'.format(
                state_id))
        city_id = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('User email="*****@*****.**" password="******" '
                               'first_name="John" last_name="Doe"')
        user_id = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_create('Place city_id="{}" user_id="{}"'
                               ' name="My_house"'
                               ' description="no_description_yet"'
                               ' number_rooms=4 number_bathrooms=1 max_guest=3'
                               ' price_by_night=100 latitude=120.12'
                               ' longitude=101.4'.format(city_id, user_id))
        output = out.getvalue().strip()
        with captured_output() as (out, err):
            self.cli.do_show('Place {}'.format(output))
        output = out.getvalue().strip()
        self.assertTrue("My house" in output)
        self.assertTrue("100" in output)
        self.assertTrue("120.12" in output)
Esempio n. 10
0
class Test_Console(unittest.TestCase):
    """
    Test the console
    """
    def setUp(self):
        '''setup objects for testing
        '''
        self.cli = HBNBCommand()

        test_args = {
            'updated_at': datetime(2017, 2, 11, 23, 48, 34, 339879),
            'id': 'd3da85f2-499c-43cb-b33d-3d7935bc808c',
            'created_at': datetime(2017, 2, 11, 23, 48, 34, 339743),
            'name': 'Ace'
        }
        self.model = BaseModel(test_args)
        self.model.save()

    def tearDown(self):
        '''remove objects after testing
        '''
        self.cli.do_destroy("BaseModel d3da85f2-499c-43cb-b33d-3d7935bc808c")

    def test_quit(self):
        '''test quit method
        '''
        with self.assertRaises(SystemExit):
            self.cli.do_quit(self.cli)

    def test_show_correct(self):
        '''test show method
        '''
        with captured_output() as (out, err):
            self.cli.do_show("BaseModel d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertFalse("2017, 2, 11, 23, 48, 34, 339879" in output)
        self.assertTrue('2017, 2, 11, 23, 48, 34, 339743' in output)

    def test_show_error_no_args(self):
        '''test show method
        '''
        with captured_output() as (out, err):
            self.cli.do_show('')
        output = out.getvalue().strip()
        self.assertEqual(output, "** class name missing **")

    def test_show_error_missing_arg(self):
        '''test show method
        '''
        with captured_output() as (out, err):
            self.cli.do_show("BaseModel")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_show_error_invalid_class(self):
        '''test show method
        '''
        with captured_output() as (out, err):
            self.cli.do_show("Human 1234-5678-9101")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_show_error_class_missing(self):
        '''test show method
        '''
        with captured_output() as (out, err):
            self.cli.do_show("BaseModel d3da85f2-499c-43cb-b33d-3d7935bc809d")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_create(self):
        '''test create method
        '''
        with captured_output() as (out, err):
            self.cli.do_create('')
        output = out.getvalue().strip()
        self.assertEqual(output, "** class name missing **")

        with captured_output() as (out, err):
            self.cli.do_create("BaseModel")
        output = out.getvalue().strip()

        with captured_output() as (out, err):
            self.cli.do_show("BaseModel {}".format(output))
        output2 = out.getvalue().strip()
        self.assertTrue(output in output2)

    def test_create_state(self):
        '''test creation of state
        '''
        with captured_output() as (out, err):
            self.cli.do_create('State name=\"California\"')
        output3 = out.getvalue().strip()

        with captured_output() as (out, err):
            self.cli.do_show("State {}".format(output3))
        output4 = out.getvalue().strip()
        self.assertTrue(output3 in output4)

    def test_update_correct_docreate(self):
        '''test update of created variables in db
        '''
        self.cli.do_create("State name=\"California\"")
        self.cli.do_create("State name=\"Arizona\"")
        with captured_output() as (out, err):
            self.cli.do_all("State")
        output = out.getvalue().strip()
        self.assertTrue("California" in output)
        self.assertTrue("Arizona" in output)
        self.assertFalse("Ohio" in output)

    def test_update_correct_docreate_variables(self):
        '''test update of created variables
        '''
        phrase = "Place 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"
        self.cli.do_create(phrase)
        with captured_output() as (out, err):
            self.cli.do_all("Place")
        output = out.getvalue().strip()
        self.assertTrue("price_by_night" in output)
        self.assertTrue("datetime.datetime" in output)
        self.assertTrue("122.431297" in output)

    def test_db_create_simple(self):
        '''tests creation of db
        '''
        self.cli.do_create("State name=\"California\"")
        with captured_output() as (out, err):
            self.cli.do_all("State")
        output = out.getvalue().strip()
        self.assertTrue("California" in output)

    def test_destroy_correct(self):
        '''tests the destroy method
        '''
        test_args = {
            'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
            'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
            'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900)
        }
        testmodel = BaseModel(test_args)
        testmodel.save()
        self.cli.do_destroy("BaseModel f519fb40-1f5c-458b-945c-2ee8eaaf4900")

        with captured_output() as (out, err):
            self.cli.do_show("BaseModel f519fb40-1f5c-458b-945c-2ee8eaaf4900")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_destroy_error_missing_id(self):
        '''tests the destroy method
        '''
        with captured_output() as (out, err):
            self.cli.do_destroy("BaseModel")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_destroy_error_class_missing(self):
        '''tests the destroy method
        '''
        with captured_output() as (out, err):
            self.cli.do_destroy("d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class name missing **")

    def test_destroy_error_invalid_class(self):
        '''tests the destroy method
        '''
        with captured_output() as (out, err):
            self.cli.do_destroy("Human d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_destroy_error_invalid_id(self):
        '''tests the destroy method
        '''
        with captured_output() as (out, err):
            self.cli.do_destroy("BaseModel " +
                                "f519fb40-1f5c-458b-945c-2ee8eaaf4900")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_all_correct(self):
        '''test the all method
        '''
        test_args = {
            'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
            'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
            'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900)
        }
        testmodel = BaseModel(test_args)
        testmodel.save()
        with captured_output() as (out, err):
            self.cli.do_all("")
        output = out.getvalue().strip()
        self.assertTrue("d3da85f2-499c-43cb-b33d-3d7935bc808c" in output)
        self.assertTrue("f519fb40-1f5c-458b-945c-2ee8eaaf4900" in output)
        self.assertFalse("123-456-abc" in output)

    def test_all_correct_with_class(self):
        '''test the all method
        '''
        with captured_output() as (out, err):
            self.cli.do_all("BaseModel")
        output = out.getvalue().strip()
        self.assertTrue(len(output) > 0)
        self.assertTrue("d3da85f2-499c-43cb-b33d-3d7935bc808c" in output)

    def test_all_error_invalid_class(self):
        '''test the all method
        '''
        with captured_output() as (out, err):
            self.cli.do_all("Human")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_update_correct(self):
        '''test the update method
        '''
        with captured_output() as (out, err):
            self.cli.do_update("BaseModel " +
                               "d3da85f2-499c-43cb-b33d-3d7935bc808c name Bay")
        output = out.getvalue().strip()
        self.assertEqual(output, '')

        with captured_output() as (out, err):
            self.cli.do_show("BaseModel d3da85f2-499c-43cb-b33d-3d7935bc808c")
        output = out.getvalue().strip()
        self.assertTrue("Bay" in output)
        self.assertFalse("Ace" in output)

    def test_update_error_invalid_id(self):
        '''test the update method
        '''
        with captured_output() as (out, err):
            self.cli.do_update("BaseModel 123-456-abc name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** no instance found **")

    def test_update_error_no_id(self):
        '''test the update method
        '''
        with captured_output() as (out, err):
            self.cli.do_update("BaseModel")
        output = out.getvalue().strip()
        self.assertEqual(output, "** instance id missing **")

    def test_update_error_invalid_class(self):
        '''test the update method
        '''
        with captured_output() as (out, err):
            self.cli.do_update(
                "Human d3da85f2-499c-43cb-b33d-3d7935bc808c name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class doesn't exist **")

    def test_update_error_no_class(self):
        '''test the update method
        '''
        with captured_output() as (out, err):
            self.cli.do_update("d3da85f2-499c-43cb-b33d-3d7935bc808c name Cat")
        output = out.getvalue().strip()
        self.assertEqual(output, "** class name missing **")

    def test_update_error_missing_value(self):
        '''test the update method
        '''
        with captured_output() as (out, err):
            self.cli.do_update(
                "BaseModel d3da85f2-499c-43cb-b33d-3d7935bc808c name")
        output = out.getvalue().strip()
        self.assertEqual(output, "** value missing **")