def test_update_no_id(self): """test update no id""" answer = "** instance id missing **" with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("update BaseModel") self.assertEqual(answer, f.getvalue().strip())
def test_empty(self): """ test emplty """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("") self.assertEqual(f.getvalue(), "")
def test_quit(self): """ Test for quit """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("quit") self.assertTrue(bool(f))
def test_do_EOF(self): """ test if command EOF exit the program """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("EOF") self.assertTrue(f.getvalue() == "\n")
def test_do_update(self): """ test command 'update <class name> <id> <attribute name> <attribute value>' and '<class name>.update(<id>, <attribute name>, <attribute value>)' and <class name>.update(<id>, <dictionary representation>) """ for className, Cls in self.classes().items(): u = Cls() """ 'update <class name> <id> <attr name> <attr value>' """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd('update {} {} string "text"' .format(className, u.id)) self.assertTrue(u.string == "text") self.assertIsInstance(u.string, str) """ '<class name>.update(<id>, <attr name>, <attr value>)' """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd('{}.update("{}", string, "text")' .format(className, u.id)) self.assertTrue(u.string == "text") self.assertIsInstance(u.string, str) """ <class name>.update(<id>, <dictionary representation>) """ dic = {'first_name': "John", "age": 89} with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd('{}.update("{}", {})' .format(className, u.id, dic)) self.assertTrue(u.first_name == "John") self.assertTrue(u.age == 89) """ test if the value of type int is correctly stored """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd('update {} {} int 100' .format(className, u.id)) self.assertTrue(u.int == 100) self.assertIsInstance(u.int, int) """ test if the value of type float is correctly stored """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd('update {} {} float 98.5' .format(className, u.id)) self.assertTrue(u.float == 98.5) self.assertIsInstance(u.float, float) """ test if the value of type str is correctly stored (the value starts whith integers. ex: "98.5caraji")""" with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd('update {} {} fake 98.5caraji' .format(className, u.id)) self.assertTrue(u.fake == "98.5caraji") self.assertIsInstance(u.fake, str) """ test if the values sended with kwargs have the correct type """ with patch('sys.stdout', new=StringIO()) as f: dic = {'first_name': "John", "age": 89} HBNBCommand().onecmd("{}.update({}, {})" .format(className, u.id, dic)) self.assertTrue(u.first_name == "John") self.assertTrue(u.age == 89) """ test error message when <class name> is missing """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("update") self.assertEqual(f.getvalue(), "** class name missing **\n") """ test error message when <class name> doesn't exist """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("update NotExistClass") self.assertEqual(f.getvalue(), "** class doesn't exist **\n") """ test error message when <id> is missing """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("update User") self.assertEqual(f.getvalue(), "** instance id missing **\n") """ test error message when no instance found """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("update User 000") self.assertEqual(f.getvalue(), "** no instance found **\n") """ test error message when <attribute name> is missing """ user = self.classes()['User']() with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("update User {}".format(user.id)) self.assertEqual(f.getvalue(), "** attribute name missing **\n") """ test error message when <value> is missing """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("update User {} attribute".format(user.id)) self.assertEqual(f.getvalue(), "** value missing **\n")
def test_help(self): with patch("sys.stdout", new=StringIO()) as filer: self.assertFalse(HBNBCommand().onecmd("help"))
def setUp(cls): """Set up testing environment""" cls.consol = HBNBCommand()
def test_prompt(self): """test prompt""" self.assertEqual('(hbnb) ', HBNBCommand().prompt)
def create(self): ''' Create HBNBCommand() ''' return HBNBCommand()
def test_create(self): """test create""" answer = "** class name missing **" with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("create") self.assertEqual(answer, f.getvalue().strip())
def test_create_fake_class(self): """test create fakeClass""" answer = "** class doesn't exist **" with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("create fakeClass") self.assertEqual(answer, f.getvalue().strip())
def test_help_create(self): """test help create""" answer = "USAGE: create [class], creates an instance of given class" with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("help create") self.assertEqual(answer, f.getvalue().strip())
def test_cmdPrompt(self): """idk""" with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("help show")
def test_update_wrong_id(self): """test update wrong_id""" answer = "** attribute name missing **" with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("update BaseModel 1234") self.assertEqual(answer, f.getvalue().strip())
def test_quit(self): with patch("sys.stdout", new=StringIO()) as filer: self.assertTrue(HBNBCommand().onecmd("quit"))
def create(self, server=None): return HBNBCommand(stdin=self.mock_stdin, stdout=self.mock_stdout)
def test_EOF_exits(self): with patch("sys.stdout", new=StringIO()) as filer: self.assertTrue(HBNBCommand().onecmd("EOF"))
def test_quit(self): """ test quit function""" with patch("sys.stdout", new=StringIO) as out: self.assertTrue(HBNBCommand().onecmd("quit"))
def test_create(self): with patch("sys.stdout", new=StringIO()) as output: self.assertFalse(HBNBCommand().onecmd("create BaseModel")) self.assertLess(0, len(output.getvalue().strip())) testKey = "BaseModel.{}".format(output.getvalue().strip()) self.assertIn(testKey, storage.all().keys())
def test_all(self): """ test all function""" string = "** class doesn't exist **" with patch("sys.stdout", new=StringIO()) as out: self.assertFalse(HBNBCommand().onecmd("all Model")) self.assertEqual(string, out.getvalue().strip())
def test_emptyline(self): """ empty line + ENTER shouldn’t execute anything """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("\n") self.assertTrue(f.getvalue() == "")
def test_help_quit(self): """ test quit from help function""" string = "Quit command to exit the console" with patch("sys.stdout", new=StringIO()) as out: self.assertFalse(HBNBCommand().onecmd("help quit")) self.assertEqual(string, out.getvalue().strip())
def test_do_quit(self): """ test if command 'quit' exit the program """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("quit") print(f.getvalue()) self.assertTrue(f.getvalue() == "")
def test_help_EOF(self): """ test EOF from help""" string = "EOF command to exit the console" with patch("sys.stdout", new=StringIO()) as out: self.assertFalse(HBNBCommand().onecmd("help EOF")) self.assertEqual(string, out.getvalue().strip())
def test_unknown_sintax(self): """ test invalid commands """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("helppp") self.assertEqual(f.getvalue(), "*** Unknown syntax: helppp\n")
def test_create(self): """test create from console""" string = "** class name missing **" with patch("sys.stdout", new=StringIO()) as out: self.assertFalse(HBNBCommand().onecmd("create")) self.assertEqual(string, out.getvalue().strip())
def test_tab(self): """ test tab """ with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("\t") self.assertEqual(f.getvalue(), "")
def setUpClass(cls): '''set up before every test method''' cls.console1 = HBNBCommand()
def setUpClass(cls): """setup console class for test""" cls.consol = HBNBCommand()
def test_destroy_wrong_id(self): """test destroy wrong_id""" answer = "** no instance found **" with patch('sys.stdout', new=StringIO()) as f: HBNBCommand().onecmd("destroy BaseModel 1234") self.assertEqual(answer, f.getvalue().strip())