import os import random import unittest class Test(unittest.TestCase): def test_01_bar(self): b = Bar() b.naroci("Bill", "whisky") self.assertEqual({"whisky": 1}, b.bilanca("Bill")) b.naroci("Bill", "whisky") b.naroci("Bill", "gin") b.naroci("Joe", "gin") b.naroci("Worf", "blood wine") b.naroci("Worf", "plum juice") b.naroci("Worf", "water") b.naroci("Worf", "plum juice") self.assertEqual({"whisky": 2, "gin": 1}, b.bilanca("Bill")) self.assertEqual("whisky", b.najpogostejse("Bill")) b.naroci("Bill", None) self.assertEqual({"whisky": 3, "gin": 1}, b.bilanca("Bill")) self.assertEqual({"gin": 1}, b.bilanca("Joe")) self.assertEqual("gin", b.najpogostejse("Joe")) b.naroci("Joe", None) self.assertEqual({"gin": 2}, b.bilanca("Joe")) self.assertEqual({"plum juice": 2, "blood wine": 1, "water": 1}, b.bilanca("Worf")) self.assertEqual("plum juice", b.najpogostejse("Worf")) b.naroci("Worf", "blood wine") b.naroci("Worf", "blood wine") self.assertEqual("blood wine", b.najpogostejse("Worf")) ime = "".join(random.choice("asdfghjkl") for _ in range(10)) pijaca = "".join(random.choice("asdfghjkl") for _ in range(10)) b.naroci(ime, pijaca) self.assertEqual({pijaca: 1}, b.bilanca(ime)) def test_02_je_hitrejsi_od(self): relacije = [("Joe", "Bill"), ("Joe", "Walter"), ("Joe", "Shane"), ("Bill", "Scott"), ("Bill", "Luke"), ("Bill", "Danny"), ("Walter", "Danny"), ("Shane", "John"), ("Scott", "Dylan"), ("Scott", "Kit"), ("Danny", "Bo"), ("Danny", "Luke"), ("Danny", "John"), ("Dylan", "Mack"), ("Dylan", "Bo"), ("Kit", "Luke"), ("John", "Luke"), ] random.shuffle(relacije) # Vemo, da so hitrejši self.assertTrue(je_hitrejsi_od(relacije, "Joe", "Bill")) self.assertTrue(je_hitrejsi_od(relacije, "Joe", "Scott")) self.assertTrue(je_hitrejsi_od(relacije, "Joe", "Kit")) self.assertTrue(je_hitrejsi_od(relacije, "Joe", "Luke")) self.assertTrue(je_hitrejsi_od(relacije, "Danny", "Bo")) self.assertTrue(je_hitrejsi_od(relacije, "Shane", "Luke")) # Vemo, da niso hitrejši self.assertFalse(je_hitrejsi_od(relacije, "Bill", "Joe")) self.assertFalse(je_hitrejsi_od(relacije, "Scott", "Joe")) self.assertFalse(je_hitrejsi_od(relacije, "Kit", "Joe")) self.assertFalse(je_hitrejsi_od(relacije, "Luke", "Joe")) self.assertFalse(je_hitrejsi_od(relacije, "Bo", "Danny")) self.assertFalse(je_hitrejsi_od(relacije, "Luke", "Shane")) # Ne vemo, kdo od teh dveh je hitrejši self.assertFalse(je_hitrejsi_od(relacije, "Shane", "Bill")) self.assertFalse(je_hitrejsi_od(relacije, "Shane", "Danny")) self.assertFalse(je_hitrejsi_od(relacije, "Shane", "Kit")) self.assertFalse(je_hitrejsi_od(relacije, "Walter", "Dylan")) def test_03_soplesalka(self): zelje = (("Joe", ("Ana", "Cilka", "Berta")), ("Bill", ("Berta", "Cilka", "Dani", "Ema")), ("Shane", ("Ema", "Cilka")), ("Walter", ("Ana", "Cilka", "Ema", "Fanny")), ("Mack", ("Cilka", "Berta", "Greta", "Fanny"))) self.assertEqual("Ana", soplesalka(zelje, "Joe")) self.assertEqual("Berta", soplesalka(zelje, "Bill")) self.assertEqual("Ema", soplesalka(zelje, "Shane")) self.assertEqual("Cilka", soplesalka(zelje, "Walter")) def test_04_streljanje(self): self.assertEqual( 4, # "Danny", "Kit", "Luke", "Dylan" streljanje([("Bill", "Danny"), ("Bill", "Kit"), ("Walter", "Danny"), ("John", "Luke"), ("Kit", "Luke"), ("Kit", "Joe"), ("Joe", "Dylan")]) ) def test_05_uspesnost(self): ime_datoteke = "".join(random.choice("asdfghjkl") for _ in range(10)) + ".txt" try: with open(ime_datoteke, "w") as f: f.write(""" Bill 578 Joe 641 Bill 385 Bill 684 Joe 483 Scott 684""".strip()) self.assertEqual({"Bill": 578 + 385 + 684, "Joe": 641 + 483, "Scott": 684}, uspesnost(ime_datoteke)) finally: os.remove(ime_datoteke) if __name__ == "__main__": unittest.main()