def setUp(self):
     self.the_database = Database()
     self.the_database.file_name = 'test_db.txt'
class SavingScoreTest(unittest.TestCase):

    def setUp(self):
        self.the_database = Database()
        self.the_database.file_name = 'test_db.txt'

    def tearDown(self):
        try:
            remove('test_db.txt')
        except FileNotFoundError:
            pass

    def test_making_file(self):

        self.assertFalse(exists('test_db.txt'), "The file doesn't exist yet.")
        result = self.the_database.make_database()
        self.assertTrue(exists('test_db.txt'), "The file now exists...")
        self.assertTrue(isfile('test_db.txt'), "And is a file.")
        self.assertTrue(result, "Method returned True")

        self.assertTrue(self.the_database.make_database(),
                        "Will return true again, but not make a new database.")

    def test_read_data_w_exception(self):
        """we will never make the database but try to read from it"""
        self.assertEqual(self.the_database.read_data(), [],
                         "It returns an empty list if it can't read from the file")


    def test_add_player(self):
        self.the_database.make_database()
        self.assertTrue(self.the_database.add_player("Maurice", "password"), "We added Maurice.")
        ##we will try to add Maurice again, it will return true, but there will only be one Maurice in the data
        self.assertTrue(self.the_database.add_player("Maurice", 'anything'),
                        'We try to add maurice with another password')
        self.assertTrue(len(self.the_database.read_data()) == 1, "There is only one player in the data.")


    def test_add_and_read_data(self):
        self.the_database.make_database()
        self.the_database.add_player("Maurice", "password")
        player_list_dicts = self.the_database.read_data()
        self.assertEqual(player_list_dicts[0].get('name'), "Maurice",
                         "The name was written to and read from the file.")
        ##throwing in another make database() doesn't affect the data
        self.the_database.make_database()
        self.assertEqual(self.the_database.read_data()[0].get('name'), "Maurice",
                         "Maurice is still in the file")



    def test_add_multiple_and_read(self):
        self.the_database.make_database()
        self.the_database.add_player("Maurice", "password")
        self.the_database.add_player("Jerry", "password2")
        player_list_dicts = self.the_database.read_data()
        self.assertEqual(player_list_dicts[0].get('name'),
                         "Maurice", "The first name was written to and read from the file.")
        self.assertEqual(player_list_dicts[1].get('name'),
                         "Jerry", "The second name was written to and read from the file")


    def test_add_player_game(self):
        self.the_database.add_player("Maurice", "password")
        self.assertTrue(self.the_database.add_player_game("Maurice", player_wins=True),
                        "We are able to add a player and he won!")
        self.assertFalse(self.the_database.add_player_game("Sam", player_wins=True))
        self.assertFalse(self.the_database.add_player_game("Will", player_wins=False))

        self.assertEqual(self.the_database.read_data()[0].get('wins'), 1,
                         "We have added just one player win for Maurice")
        self.assertEqual(self.the_database.read_data()[0].get('games'),1, "Just one game also.")


    def test_authenticate_user(self):
        self.the_database.make_database()
        self.the_database.add_player("Maurice", "password")
        self.assertTrue(self.the_database.authenticate_user("Maurice", "password"), "Correct password for Maurice")
        self.assertFalse(self.the_database.authenticate_user("Maurice", "not_the_password"),
                         "Incorrect password for Maurice")
        self.assertIsNone(self.the_database.authenticate_user("Sam", "anything"), "No user named Sam")

    def test_getting_scores(self):
        #TODO think about what you want method Database().get_player_scores(name) to do. It seems to cross MVC lines
        self.the_database.make_database()
        self.the_database.add_player("Maurice", "password")
        self.the_database.add_player_game("Maurice", player_wins=True)
        self.assertEqual(self.the_database.get_player_scores("Maurice"),
                         "Maurice has 1 wins in 1 games",
                         "Correctly output string saying how many wins and games for Maurice")