import unittest
from fractions import Fraction
from matrix_cases import contribution,rate,delivery_comparison,RequestFixture,journey
class MatrixFixtures(unittest.TestCase):
 def test_segment_tie(self):self.assertEqual(contribution(10,200),contribution(8,250))
 def test_loss_preserved(self):self.assertEqual(contribution(2,-10),-20)
 def test_decimal_exact(self):self.assertEqual(contribution(3,'0.1'),Fraction(3,10))
 def test_negative_count(self):
  with self.assertRaises(ValueError):contribution(-1,20)
 def test_boolean_count(self):
  with self.assertRaises(ValueError):rate(True,2)
 def test_nonfinite(self):
  with self.assertRaises(ValueError):contribution(2,'NaN')
 def test_delivery(self):self.assertEqual(delivery_comparison((90,100),(96,120)),dict(before=Fraction(9,10),after=Fraction(4,5),percentage_points=-10,relative_change=Fraction(-1,9)))
 def test_denominator(self):
  for args in [(0,0),(3,2)]:
   with self.assertRaises(ValueError):rate(*args)
 def test_relative_zero(self):
  with self.assertRaises(ValueError):delivery_comparison((0,10),(1,10))
 def test_retry_and_conflict(self):
  x=RequestFixture();self.assertEqual(x.record('R1','O1'),'accepted');self.assertEqual(x.record('R1','O1'),'already recorded');self.assertEqual(x.record('R2','O2'),'accepted')
  with self.assertRaises(ValueError):x.record('R1','O2')
  self.assertEqual(x.accepted,{'R1':'O1','R2':'O2'})
 def test_missing_identifier(self):
  with self.assertRaises(ValueError):RequestFixture().record(' ','O1')
 def test_restart_boundary(self):
  first=RequestFixture();first.record('R1','O1');second=RequestFixture();self.assertEqual(second.record('R1','O1'),'accepted')
 def test_journey(self):self.assertEqual(journey(9,2,1)['completion_rate'],Fraction(3,4))
 def test_no_participants(self):
  with self.assertRaises(ValueError):journey(0,0,0)
