0

Here is the main code for my program which includes three functions:

import unittest

    def main():
#initial roster#
      brave_roster = {"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273",
        "Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266",
  }

#my code:

def lookup_player(brave_roster,name):
  name = input("Enter the name of the player you want to lookup:\n")
if name in brave_roster:
  print("Here are the", [name], "stats:", brave_roster[name])
else:
  print("That player does not exist.")

def add_player_to_dict(brave_roster,name,stats):
 name = input("Enter the name of the player you want to add:\n")
 if name not in brave_roster:
  stats = input("Please add stats:\n")
  brave_roster[name] = stats
  brave_roster.update([(name, stats)])
  print("Here's", [name],"'s stats:\n")
  for name, stats in brave_roster.items():
   print(name, ':', stats)
 else:
  print("That player is already on our roster.")

def delete_in_dict(brave_roster, name):
  name = input("Enter the name of the player you want to remove:\n")
  if name in brave_roster:
    del brave_roster[name]
    return brave_roster
  else:
    print("\n uh typo?", [name], "does not play for us")

if __name__ == '__main__':
  print("\t ***  Braves Stats!  ***\n")
  print("Welcome to My Braves Stats!")
  
  brave_roster = {
    "Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273",
    "Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266",
  }
  name = main()
  choice = input("Please type your choice number:")
if (choice == "1"):
    lookup_player(brave_roster,name)
elif (choice == "2"):
      add_player_to_dict(brave_roster,name)
else: 
    (choice == "3")
    delete_in_dict(brave_roster,name)
        


print("\nThanks for using my Braves Stats.")

Here are the test cases I am checking my code with:

class TestDictFunctions(unittest.TestCase):

  def test_search_player_success(self):
    test_dict = {
        "Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"
    }
    actual = test_dict["Austin Riley"]
    expected = lookup_player(test_dict, "Austin Riley")
    self.assertEqual(actual, expected)

  def test_search_player_no_result(self):
    test_dict = {
        "Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"
    }
    actual = "N/A"
    expected = lookup_player(test_dict, "Ronald Acuna")
    self.assertEqual(actual, expected)

  def test_add_player_sucess(self):
    test_dict = {
        "Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"
    }
    actual = {
        "Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273",
        "Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266"
    }
    expected = add_player_to_dict(test_dict, "Ronald Acuna", "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266")

    self.assertEqual(actual, expected)

  def test_add_player_duplicate(self):
    test_dict = {"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"}
    actual = {"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273", "Austin Riley(2)": "AB: 350, R: 20, H: 120, HR: 5, AVG: 0.214"}
    expected = add_player_to_dict(test_dict, "Austin Riley", "AB: 350, R: 20, H: 120, HR: 5, AVG: 0.214")
    self.assertEqual(actual, expected)

  def test_delete_player_sucess(self):
    test_dict = {"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"}
    expected = {}
    actual = delete_in_dict(test_dict, "Austin Riley")
    self.assertEqual(actual, expected)

  def test_delete_word_no_result(self):
    test_dict = {"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"}
    expected = test_dict
    actual = delete_in_dict(test_dict, "Shohei Ohtani")
    self.assertEqual(actual, expected)


#uncomment to run tests
unittest.main()

Once I execute the code I get messages such as this:

FAIL: test_add_player_sucess (main.TestDictFunctions.test_add_player_sucess)

Traceback (most recent call last): File "/Users/ray/Documents/DSCI HW/Homework 1/test_roster.py", line 100, in test_add_player_sucess self.assertEqual(actual, expected) AssertionError: {'Austin Riley': 'AB: 615, R: 90, H: 168,[78 chars]266'} != None

What could be the cause of this?

Pislibsco
  • 27
  • 5
  • 1
    can u reformat the post such that the code is in code format, placing ``` ``` around your code else its unreadable – TheRavenSpectre Apr 07 '23 at 00:51
  • @TheRavenSpectre I have formatted differently in my program I just separated my code from the code in the test case class for the sake of the question. I'll edit the question in the correct formation. – Pislibsco Apr 07 '23 at 00:55
  • 1
    have a look closely at what `add_player_to_dict()` returns – bn_ln Apr 07 '23 at 01:19
  • @Pislibsco the formatting is still not correct. Every line has a `>` in front of it and the `if`/`elif`/`else` block indentation is mostly wrong. – Ryan Haining Apr 07 '23 at 01:51
  • @bn_ln I've been looking but I'm still a bit confused. – Pislibsco Apr 07 '23 at 02:20
  • @RyanHaining I was finally able to get the formatting correct. Besides that issue, is there anything in the code that's problematic? – Pislibsco Apr 07 '23 at 02:21

1 Answers1

1
def test_add_player_sucess(self):
    test_dict = {
        "Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"
    }
    actual = {
        "Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273",
        "Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266"
    }
    expected = add_player_to_dict(test_dict, "Ronald Acuna", "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266")

    self.assertEqual(actual, expected)

The function add_player_to_dict() does not have any return statement. Therefore it returns None by default.

Therefore expected is None.

Therefore the assert fails.

John Gordon
  • 29,573
  • 7
  • 33
  • 58