コード例 #1
0
    def test_console_do_create(self):
        """test_console_do_count

        test secure instance creation
        """
        HBNBCommand.do_create(
            self, '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')
        storage.save()
        self.assertTrue(path.exists("file.json"))
        with open("file.json", "r") as file:
            res = json.load(file)
            self.assertTrue(res, dict())
            for key, value in res.items():
                dictionary = value
                self.assertTrue(dictionary, dict())
                self.assertEqual("0001", dictionary["city_id"])
                self.assertEqual("0001", dictionary["user_id"])
                self.assertEqual("My little house", dictionary["name"])
                self.assertEqual(4, dictionary["number_rooms"])
                self.assertEqual(2, dictionary["number_bathrooms"])
                self.assertEqual(10, dictionary["max_guest"])
                self.assertEqual(300, dictionary["price_by_night"])
                self.assertEqual(37.773972, dictionary["latitude"])
                self.assertEqual(-122.431297, dictionary["longitude"])
        os.remove("file.json")
コード例 #2
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': '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 **")
コード例 #3
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)
コード例 #4
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)
コード例 #5
0
ファイル: test_console.py プロジェクト: jmgindi/AirBnB_clone
class testsForConsole(unittest.TestCase):
    """Tests for comsole"""
    @classmethod
    def setUpClass(cls):
        """Prints that unit testing for console
        will commence!
        """
        print("\n...Testing for console...")

    def setUp(self):
        """setUp method creates console instance
        """
        self.hbnb = HBNBCommand()

    def tearDown(self):
        """tearDown method quits console instance if it's open"""
        try:
            self.hbnb.do_quit()
        except TypeError:
            pass

    def test_doc_string(self):
        """Tests doc string of console"""
        doc = ('console').__doc__
        self.assertIsNotNone(doc)

    def test_prompt(self):
        """Tests if the command prompt is correct"""
        self.assertEqual(self.hbnb.prompt, "(hbnb) ")

    def test_prompt_stdout(self):
        """Tests prompt prints to stdout correctly"""
        self.hbnb.do_quit()
        f = io.StringIO()
        with redirect_stdout(f):
            hbnb = HBNBCommand()
            self.assertEqual(f, "(hbnb) ")

    def test_do_quit_return_true(self):
        """Tests if quit returns correct value"""
        self.assertEqual(self.hbnb.do_quit(), True)

    def test_quit_too_many_args(self):
        """Tests quit with too many args"""
        self.assertEqual(self.hbnb.do_quit("arg"), True)

    def test_do_EOF_return_true(self):
        """Tests EOF returning true"""
        self.assertEqual(self.hbnb.do_EOF(), True)

    def test_do_create(self):
        """Tests creation of a new object"""
        x = self.hbnb.do_create("BaseModel")
        self.assertEqual(type(x), type(BaseModel()))

    def test_do_create_no_args(self):
        """Tests create with no class name"""
        strio = io.StringIO()
        with redirect_stdout(strio):
            self.hbnb.do_create()
            self.assertEqual(strio, "** class name missing **)")

    def test_do_create_too_many_args(self):
        """Tests create with too many arguments"""
        strio = io.StringIO()
        with redirect_stdout(strio):
            self.hbnb.do_create("BaseModel", "arg")
            self.assertEqual(strio, "** too many args **")

    def test_create_arg_in_wrong_place(self):
        """Tests args in the wrong place"""
        pass

    def test_create_too_many_wrong_args(self):
        """Tests create with too many args all incorrect"""
        pass

    def test_load_file(self):
        """Tests that a created file is loaded when a new console
        is started"""
        pass

    def test_show_no_args(self):
        """Tests show with no args"""
        strio = io.StringIO()
        with redirected_stdout(strio):
            self.hbnb.do_show()
            self.assertEqual(strio, "** instance id missing **")

    def test_show_one(self):
        """Tests show with one correct arg"""
        strio = io.StringIO()
        self.hbnb.do_show(BaseModel)
        with redirected_stdout(strio):
            self.assertEqual(strio, "")

    def test_show_one_bad(self):
        """Tests show with one bad arg"""
        pass

    def test_show_two_ok(self):
        """Tests standard usage of show"""
        pass

    def test_show_args_wrong_place(self):
        """Tests show with args in the wrong place"""
        pass

    def test_show_bad_id(self):
        """Tests show with a bad id"""
        pass

    def test_show_bad_model_good_id(self):
        """Tests show with only the first argument incorrect"""
        pass

    def test_show_too_many_args(self):
        """Tests show with too many args"""
        pass

    def test_show_bad_model_too_many(self):
        """Tests show with too many arguments and a bad model"""
        pass

    def test_show_bad_id_too_many(self):
        """Tests show with too many args and a bad id"""
        pass

    def test_show_bad_both_too_many(self):
        """Tests show with too many args all bad"""
        pass

    def test_all_no_args(self):
        """Tests all with no args"""
        pass

    def test_all_BaseModel(self):
        """Tests all with the BaseModel class"""
        pass

    def test_all_User(self):
        """Tests all with the User class"""
        pass

    def test_all_FileStorage(self):
        """Tests all with the FileStorage class"""
        pass

    def test_all_bad(self):
        """Tests all with a bad argument"""
        pass

    def test_all_too_many_ok(self):
        """Tests all with too many arguments"""
        pass

    def test_all_args_wrong_place(self):
        """Tests all with args in the wrong place"""
        pass

    def test_all_too_many_bad(self):
        """Tests all with bad args and too many"""
        pass

    def test_destroy_BaseModel(self):
        """Tests destruction of created BaseModel"""
        pass

    def test_destroy_User(self):
        """Tests destruction of a User"""
        pass

    def test_destroy_FileStorage(self):
        """Tests destruction of a FileStorage instance"""
        pass

    def test_destroy_too_few_args(self):
        """Tests destroy with too few args"""
        pass

    def test_destroy_bad_model(self):
        """Tests destroy with a bad model"""
        pass

    def test_destroy_bad_id(self):
        """Tests destroy with a bad id"""
        pass

    def test_destroy_too_many_args(self):
        """Tests destroy with too many args"""
        pass

    def test_destroy_args_wrong_place(self):
        """Tests destroy with misplaced args"""
        pass

    def test_update_no_args(self):
        """Tests update with no args"""
        pass

    def test_update_1_only_model(self):
        """Tests update with only a model"""
        pass

    def test_update_1_bad_model(self):
        """Tests update with only a bad model"""
        pass

    def test_update_2_ok_args(self):
        """Tests update with only 2 correct args"""
        pass

    def test_update_2_bad_model(self):
        """Tests update with 2 args, bad model"""
        pass

    def test_update_2_bad_id(self):
        """Tests update with 2 args, bad id"""
        pass

    def test_update_2_both_bad(self):
        """Tests update with 2 bad args"""
        pass

    def test_update_3_bad_model(self):
        """Tests update with 3 args, bad model"""
        pass

    def test_update_3_bad_id(self):
        """Tests update with 3 args, bad id"""
        pass

    def test_update_3_bad_attr(self):
        """Tests update with 3 args, bad attribute"""
        pass

    def test_update_3_bad_model_id(self):
        """Tests update with 3 args, bad model and id"""
        pass

    def test_update_3_bad_id_attr(self):
        """Tests update with 3 args, bad id and attribute"""
        pass

    def test_update_3_bad_model_attr(self):
        """Tests update with 3 args, bad model and attribute"""
        pass

    def test_update_3_all_bad(self):
        """Tests update with 3 args, all bad"""
        pass

    def test_update_4_ok(self):
        """Tests update with standard usage"""
        pass

    def test_update_4_bad_model(self):
        """Tests update with 4 args, bad model"""
        pass

    def test_update_4_bad_model_id(self):
        """Tests update with 4 args, bad model and id"""
        pass

    def test_update_4_bad_model_id_attr(self):
        """Tests update with 4 args, bad model, id, attribute"""
        pass

    def test_update_4_all_bad(self):
        """Tests update with 4 args, all bad"""
        pass

    def test_update_4_bad_id(self):
        """Tests update with 4 args, bad id only"""
        pass

    def test_update_4_bad_id_attr(self):
        """Tests update with 4 args, bad id and attribute"""
        pass

    def test_update_4_bad_id_attr_value(self):
        """Tests update with 4 args, bad id, attribute, value"""
        pass

    def test_update_4_bad_id_value(self):
        """Tests update with 4 args, bad id and value"""
        pass

    def test_update_4_bad_attr(self):
        """Tests update with 4 args, bad attribute only"""
        pass

    def test_update_4_bad_attr_value(self):
        """Tests update with 4 args, bad attribute and value"""
        pass

    def test_update_4_bad_value(self):
        """Tests update with 4 args, bad value"""
        pass

    def test_update_4_all_bad(self):
        """Tests update with 4 bad args"""

    def test_update_4_args_wrong_place(self):
        """Tests update with 4 args in the wrong place"""
        pass

    def test_update_too_many_args_ok(self):
        """Tests update with too many args"""
        pass

    def test_update_too_many_bad(self):
        """Tests update with too many args, all bad"""
        pass
コード例 #6
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 **")