def test_Table(self): tbl = Table(limit=30) bet1 = Bet(10, Outcome("Red", 1)) bet2 = Bet(20, Outcome("Corner", 2)) bet3 = Bet(30, Outcome("Black", 3)) bet4 = Bet(40, Outcome("Street", 4)) self.assertTrue(tbl.is_valid(bet1)) self.assertTrue(tbl.is_valid( bet3)) # a bet of 30 should be acceptable even with a limit of 30 self.assertFalse(tbl.is_valid(bet4)) # 40 should not be valid tbl.place_bet(bet1) tbl.place_bet(bet2) self.assertFalse(tbl.is_valid(bet1)) """ Test that exception will raise if a bet causes bets to exceed the table limit http://www.lengrand.fr/2011/12/pythonunittest-assertraises-raises-error/ """ self.assertRaises( InvalidBet, lambda: tbl.place_bet(bet2)) # this should raise an exception # test that Table object is iterable self.assertTrue(isinstance(tbl, collections.Iterable))
def test_Wheel(self): """ http://www.itmaybeahack.com/book/oodesign-python-2.1/html/roulette/wheel.html#the-random-bin-selection-responsibility Wheel Deliverables A class which performs a unit test of building the Wheel class. The unit test should create several instances of Outcome, two instances of Bin, and an instance of Wheel. The unit test should establish that Bins can be added to the Wheel. A Non-Random Random Number Generator class, to be used for testing. A class which tests the Wheel and NonRandom class by selecting values from a Wheel object. """ outcome_one = Outcome("Red", 1) outcome_two = Outcome("Corner", 2) outcome_three = Outcome("Black", 3) outcome_four = Outcome("Street", 4) nonrandom = NonRandom() nonrandom.set_seed(1) wheel_one = Wheel(nonrandom) wheel_one.add_outcome(1, outcome_one) wheel_one.add_outcome(2, outcome_two) wheel_one.next() self.assertTrue(wheel_one.next(), outcome_one) # test get_outcome self.wheel_two = Wheel(nonrandom) BB = BinBuilder() BB.build_bins(self.wheel_two)
def test_split_bets(): wheel = Wheel() bb = BinBuilder() bb.generate_split_bets(wheel) for r in range(12): # left-right splits n = 3 * r + 1 out = Outcome(f'{n} {n + 1}', 17) assert out in wheel.get(n) and out in wheel.get(n + 1) n = 3 * r + 2 out = Outcome(f'{n} {n + 1}', 17) assert out in wheel.get(n) and out in wheel.get(n + 1) # top-down splits if r == 11: break else: n = 3 * r + 1 out = Outcome(f'{n} {n + 3}', 17) assert out in wheel.get(n) and out in wheel.get(n + 3) n += 1 out = Outcome(f'{n} {n + 3}', 17) assert out in wheel.get(n) and out in wheel.get(n + 3) n += 1 out = Outcome(f'{n} {n + 3}', 17) assert out in wheel.get(n) and out in wheel.get(n + 3)
def test_invalid_bet(self): with pytest.raises(InvalidBet): self.table.place_bet(Bet(105, Outcome("0", 35))) table2 = Table(100, self.table.wheel) table2.place_bet(Bet(60, Outcome("0", 35))) with pytest.raises(InvalidBet): self.table.place_bet(Bet(50, Outcome("0", 35)))
def test_straight_bets(self): ''' Each bin (0-36) should have its own straight bet with odds 35:1. 00 should have a similar straight bet at index 37 of the wheel. ''' for i in range(0, 37): self.assertIn(Outcome("{}".format(i), 35), self.wheel.get(i), "{} not in bin.".format(i)) self.assertIn(Outcome("00", 35), self.wheel.get(37), "00 not in bin.")
def test_add_outcome(): wheel = Wheel() wheel.add_outcome(0, Outcome(f'0', 35)) wheel.add_outcome(0, Outcome(f'0', 35)) assert Outcome(f'0', 35) in wheel.get(0) assert 1 == len(wheel.get(0)) wheel.add_outcome(0, Outcome(f'street', 11)) assert Outcome(f'street', 11) in wheel.get(0) assert 2 == len(wheel.get(0))
def test_outcome(): o1 = Outcome("Red", 1) o2 = Outcome("Red", 1) o3 = Outcome("Black", 2) assert str(o1) == "Red 1:1" assert repr(o2) == "Outcome(name='Red', odds=1)" assert o1 == o2 assert o1.odds == 1 assert o1.name == "Red" assert o1 != o3 assert o2 != o3
def test_next(self): r = random.Random() r.setstate(self.wheel.rng.getstate()) b1 = Bin([Outcome("Red", 5)]) b2 = Bin([Outcome("0", 35), Outcome("Black", 5)]) self.wheel.add_bin(r.randint(0, 38), b1) self.wheel.add_bin(r.randint(0, 38), b2) assert self.wheel.next() == b1 assert self.wheel.next() == b2 assert self.wheel.next() != b2
def test_bin(): five = Outcome("00-0-1-2-3", 6) zero_out = Outcome("0", 35) zerozero_out = Outcome("00", 35) zero_bin = Bin([zero_out, five]) zerozero_bin = Bin([zerozero_out, five]) assert len(zero_bin) == 2 assert five in zero_bin assert zero_out in zero_bin assert len(zerozero_bin) == 2 assert five in zero_bin assert zerozero_out in zerozero_bin
def test_column_bets(): wheel = Wheel() bb = BinBuilder() bb.generate_column_bets(wheel) col_one = Outcome(f'Column 1', 2) col_two = Outcome(f'Column 2', 2) col_thr = Outcome(f'Column 3', 2) outcomes = {1: col_one, 2: col_two, 0: col_thr} for i, bin in enumerate(wheel.bins): if i == 0 or i == 37: assert 0 == len(bin) else: out = outcomes[i % 3] assert out in bin
def check_corner(self, n): corner_bet = Outcome("{}-{}-{}-{}".format(n, n + 1, n + 3, n + 4), 8) self.assertIn(corner_bet, self.wheel.get(n)) self.assertIn(corner_bet, self.wheel.get(n + 1)) self.assertIn(corner_bet, self.wheel.get(n + 3)) self.assertIn(corner_bet, self.wheel.get(n + 4))
def test_Bin(self): """ A class which performs a unit test of the Bin class. The unit test should create several instances of Outcome, two instances of Bin and establish that Bins can be constructed from the Outcomes. """ outcome_three = Outcome("00-0-1-2-3", 6) outcome_four = Outcome("D", 2) outcome_five = Outcome("E", 3) outcome_six = Outcome("F", 4) bin_one = Bin(outcome_three, outcome_four) print 'what is bin one?: ', bin_one bin_two = Bin(outcome_five, outcome_six) print 'what is bin two?: ', bin_two
def test_column_bets(self): '''Column blocks consist of the 12 bins in a single column.''' for c in range(3): column_bet = Outcome("column({})".format(c + 1), 2) for r in range(12): idx = 3 * r + c + 1 self.assertIn(column_bet, self.wheel.get(idx))
def test_dozen_bets(self): '''Dozen blocks consist of 12 bin intervals: 1-12, 13-24, 25-36.''' for d in range(3): dozen_bet = Outcome("dozen({})".format(d + 1), 2) for m in range(12): idx = 12 * d + m + 1 self.assertIn(dozen_bet, self.wheel.get(idx))
def test_Bin(self): outcome_two = Outcome("Corner", 2) outcome_three = Outcome("Black", 3) bet1 = Bet(10, Outcome("Red", 1)) bet2 = Bet(10, outcome_two) bet3 = Bet(10, outcome_three) self.assertTrue(bet1.win_amount() == 20, ) # 10 + 10 self.assertTrue(bet1.lose_amount() == 10) # lose of wager self.assertTrue(bet2.win_amount() == 30) # 10*2 + 10 self.assertTrue(bet2.lose_amount() == 10) # lose of wager self.assertTrue(bet3.win_amount() == 40) # 10*3 + 10 self.assertTrue(bet3.lose_amount() == 10) # lose of wager self.assertEqual(str(bet1), "amount on Red (1:1)")
def test_Outcome(self): """ A class which performs a unit test of the Outcome class. The unit test should create three instances of Outcome, two of which have the same name. It should use a number of individual tests to establish that two Outcome with the same name will test true for equality, have the same hash code, and establish that the win_amount() method works correctly. """ outcome_one = Outcome("A", 1) outcome_two = Outcome("A", 1) outcome_three = Outcome("B", 2) # tests hash equality self.assertTrue(outcome_one == outcome_two) self.assertTrue(outcome_one != outcome_three) self.assertEqual(outcome_one.win_amount(1), 1 ) self.assertEqual(outcome_three.win_amount(1), 2)
def create_outcome(name=None, odds=None): if name is None: name = '1' if odds is None: odds = 35 tmp_outcome = Outcome(name, odds) return name, odds, tmp_outcome
def check_offset(self, n, offset): ''' Checks that an outcome is in a bin with index, n, and its offset, n + offset. ''' m = "Split bet not in bin." split_bet = Outcome("{}-{}".format(n, n + offset), 17) self.assertIn(split_bet, self.wheel.get(n), m) self.assertIn(split_bet, self.wheel.get(n + offset), m)
def test_bin_staight_bets(): wheel = Wheel() bb = BinBuilder() bb.generate_straight_bets(wheel) for i, bin in enumerate(wheel.bins): assert 1 == len(bin) nm = f'{i}' if i < 37 else '00' assert Outcome(nm, 35) in bin
def test_corner_bets(): wheel = Wheel() bb = BinBuilder() bb.generate_corner_bets(wheel) for r in range(0, 11): n = 3 * r + 1 outcome = Outcome(f'{n} {n+1} {n+3} {n+4}', 8) assert outcome in wheel.get(n) assert outcome in wheel.get(n + 1) assert outcome in wheel.get(n + 3) assert outcome in wheel.get(n + 4) n += 1 outcome = Outcome(f'{n} {n + 1} {n + 3} {n + 4}', 8) assert outcome in wheel.get(n) assert outcome in wheel.get(n + 1) assert outcome in wheel.get(n + 3) assert outcome in wheel.get(n + 4)
def test_generate_doze_bets(): wheel = Wheel() bb = BinBuilder() bb.generate_dozen_bets(wheel) for i, bin in enumerate(wheel.bins): if i == 0 or i == 37: assert 0 == len(bin) else: out = Outcome(f'Dozen {math.ceil(i/12)}', 2) assert out in bin
def test_street_bets(self): ''' A street bet consists of a row of 3 bins. Street bets can be found by iterating over each element in the first column and finding its neighbors, (n, n+1, n+2). ''' for row in range(12): n = 3 * row + 1 street_bet = Outcome("{}-{}-{}".format(n, n + 1, n + 2), 11) self.assertIn(street_bet, self.wheel.get(n)) self.assertIn(street_bet, self.wheel.get(n + 1)) self.assertIn(street_bet, self.wheel.get(n + 2))
def test_line_bets(self): ''' Line blocks consist of 6 bins each. Each line is the line between rows on the roulette board. As there are 12 rows, there are 11 lines, and relevant bins can be found iterating over consecutive 6 bin blocks from the first up to the last row. ''' for row in range(10): n = 3 * row + 1 line_bet = Outcome( "{}-{}-{}-{}-{}-{}".format(*(n + i for i in range(6))), 5) for i in range(6): self.assertIn(line_bet, self.wheel.get(n + i))
def test_line_bets(): wheel = Wheel() bb = BinBuilder() bb.generate_line_bets(wheel) for r in range(0, 11): n = 3 * r + 1 line_out = Outcome(f'{n} {n + 1} {n + 2} {n + 3} {n + 4} {n + 5}', 5) assert line_out in wheel.get(n) assert line_out in wheel.get(n + 1) assert line_out in wheel.get(n + 2) assert line_out in wheel.get(n + 3) assert line_out in wheel.get(n + 4) assert line_out in wheel.get(n + 5)
def test_get_outcome(): wheel = Wheel() bb = BinBuilder() bb.generate_even_money_bets(wheel) bb.generate_column_bets(wheel) assert Outcome('High', 1) == wheel.get_outcome('High') assert Outcome('Low', 1) == wheel.get_outcome('Low') assert Outcome('Red', 1) == wheel.get_outcome('Red') assert Outcome('Black', 1) == wheel.get_outcome('Black') assert Outcome('Column 1', 1) == wheel.get_outcome('Column 1') assert Outcome('Column 2', 1) == wheel.get_outcome('Column 2') assert Outcome('Column 3', 1) == wheel.get_outcome('Column 3')
def test_street_bets(): wheel = Wheel() bb = BinBuilder() bb.generate_street_bets(wheel) for i, bin in enumerate(wheel.bins): if i == 0 or i == 37: assert 0 == len(bin) else: assert 1 == len(bin) for i in range(1, 36, 3): out = Outcome(f'{i} {i + 1} {i + 2}', 11) assert out in wheel.get(i) assert out in wheel.get(i + 1) assert out in wheel.get(i + 2)
def test_Outcome(self): """ A class which performs a unit test of the Outcome class. The unit test should create three instances of Outcome, two of which have the same name. It should use a number of individual tests to establish that two Outcome with the same name will test true for equality, have the same hash code, and establish that the win_amount() method works correctly. """ outcome_one = Outcome("A", 1) outcome_two = Outcome("A", 1) outcome_three = Outcome("B", 2) # tests hash equality self.assertTrue(outcome_one == outcome_two) self.assertTrue(outcome_one != outcome_three) self.assertEqual(outcome_one.win_amount(1), 1) self.assertEqual(outcome_three.win_amount(1), 2)
def test_even_money_bets(self): ''' Even money bets consist of 6 types: 1. Red Red bins on the boards. 2. Black Black bins on the board. 3. Even Even bins on the board. 4. Odd Odd bins on the board. 5. High Bins < 19. 6. Low Bins >= 19. ''' red = Outcome("red", 1) black = Outcome("black", 1) even = Outcome("even", 1) odd = Outcome("odd", 1) high = Outcome("high", 1) low = Outcome("low", 1) red_bins = { 1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 } for n in range(1, 37): if n >= 1 and n < 19: self.assertIn(low, self.wheel.get(n)) elif n >= 19 and n < 37: self.assertIn(high, self.wheel.get(n)) if n % 2 == 0: self.assertIn(even, self.wheel.get(n)) else: self.assertIn(odd, self.wheel.get(n)) if n in red_bins: self.assertIn(red, self.wheel.get(n)) else: self.assertIn(black, self.wheel.get(n))
def test_even_money_bets(): wheel = Wheel() bb = BinBuilder() bb.generate_even_money_bets(wheel) # Red-Black reds = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36} blacks = set(range(1, 37)) - reds red_out = Outcome('Red', 1) black_out = Outcome('Black', 1) # Even-Odd even_out = Outcome('Even', 1) odd_out = Outcome('Odd', 1) # Hi - Lo hi_out = Outcome('High', 1) lo_out = Outcome('Low', 1) for i, bin in enumerate(wheel.bins): if i == 0 or i == 37: assert 0 == len(bin) elif i < 19: assert lo_out in bin else: assert hi_out in bin if i == 0 or i == 37: assert 0 == len(bin) elif i % 2 == 0: assert even_out in bin else: assert odd_out in bin if i == 0 or i == 37: assert 0 == len(bin) elif i in reds: assert red_out in bin else: assert black_out in bin
def test_table_clear_bet(self): self.table.place_bet(Bet(60, Outcome("0", 35))) assert len(self.table.bets) == 1 self.table.clear_bets() assert len(self.table.bets) == 0
def test_add_bet(self): bet = Bet(60, Outcome("0", 35)) self.table.place_bet(bet) assert bet == next(iter(self.table))