def test_initialisation(self): table = rolltables.Rolltable( { "CL": [ "G0", "H0", "J0", "K0", "M0", "N0", "Q0", "U0", "V0", "X0", "Z0", "F1" ] }, "roll-in") self.assertIsInstance(table, rolltables.Rolltable) self.assertIsInstance(rolltables.BCOM, rolltables.Rolltable) self.assertIsInstance(rolltables.GSCI, rolltables.Rolltable) with self.assertRaises(ValueError): table = rolltables.Rolltable( { "CL": [ "G0", "H0", "J0", "K0", "M0", "N0", "Q0", "U0", "V0", "X0", "Z0", "F1" ] }, "roll") with self.assertRaises(ValueError): table = rolltables.Rolltable("undefined data", tabletype="roll-in") with self.assertRaises(ValueError): #small typo in the roll table in February table = rolltables.Rolltable( { "CL": [ "G0", "P0", "J0", "K0", "M0", "N0", "Q0", "U0", "V0", "X0", "Z0", "F1" ] }, tabletype="roll-in") with self.assertRaises(ValueError): #11 contracts rolltables.Rolltable( { "CL": [ "G0", "H0", "J0", "K0", "M0", "N0", "Q0", "U0", "V0", "X0", "Z0" ] }, "roll-in")
def test_reverse(self): table = rolltables.Rolltable( { "CL": [ "H0", "H0", "K0", "K0", "N0", "N0", "U0", "U0", "X0", "X0", "F1", "F1" ] }, "roll-in") self.assertEqual(table.reverse("CLF2020", 12, 2019), "F0") self.assertEqual(table.reverse("CLH2020", 12, 2019), "F1") self.assertEqual(table.reverse("CLK2020", 12, 2019), "F3") self.assertEqual(table.reverse("CLX2019", 12, 2019), "F-2")
def test_contains(self): table = rolltables.Rolltable( { "CL": [ "H0", "H0", "K0", "K0", "N0", "N0", "U0", "U0", "X0", "X0", "F1", "F1" ] }, "roll-in") self.assertTrue("CLF2020" in table) self.assertFalse("CLG2020" in table) #must be a valid contract name with self.assertRaises(ValueError): "CLXXXXX" in table
def test_polyarg(self): table = rolltables.Rolltable( { "CL": [ "H0", "H0", "K0", "K0", "N0", "N0", "U0", "U0", "X0", "X0", "F1", "F1" ] }, "roll-in") self.assertEqual(table.resolve("CL", datetime.date(2020, 1, 1)), "CLH2020") self.assertEqual(table.resolve("CL", 2020, 1), "CLH2020") self.assertEqual(table.resolve("F2", "CL", datetime.date(2020, 1, 1)), "CLK2020") self.assertEqual( table.resolve("CL", datetime.date(2020, 1, 1), "roll-out"), "CLF2020")
def test_shift(self): table = rolltables.Rolltable( { "CL": [ "H0", "H0", "K0", "K0", "N0", "N0", "U0", "U0", "X0", "X0", "F1", "F1" ] }, "roll-in") self.assertEqual(table.shift(1).table["CL"][0], "H0") self.assertEqual(table.shift(1).table["CL"][-1], "H1") self.assertEqual( table.shift(1).resolve("CL", datetime.date(2020, 1, 1)), "CLH2020") self.assertEqual( table.shift(1).resolve("CL", "F1", datetime.date(2020, 1, 1)), "CLK2020") self.assertEqual(table.shift(-2).table["CL"][0], "F0") self.assertEqual(table.shift(-2).table["CL"][1], "F0") self.assertEqual(table.shift(-2).table["CL"][-1], "X0")
def test_resolver(self): table = rolltables.Rolltable( { "CL": [ "H0", "H0", "K0", "K0", "N0", "N0", "U0", "U0", "X0", "X0", "F1", "F1" ] }, "roll-in") self.assertEqual(table.resolve("CL", "F0", 12, 2019), "CLF2020") self.assertEqual(table.resolve("CL", "F0", 12, 2019, "roll-in"), "CLF2020") self.assertEqual(table.resolve("CL", "F0", 12, 2019, "roll-out"), "CLF2020") #allow the arguments to be determined automatically self.assertEqual(table.resolve("CL", datetime.date(2019, 12, 1)), "CLF2020") self.assertEqual(table.resolve(datetime.date(2019, 12, 1), "CL"), "CLF2020") self.assertEqual( table.resolve("CL", datetime.date(2019, 12, 1), "roll-in"), "CLF2020") self.assertEqual( table.resolve("CL", datetime.date(2019, 12, 1), "roll-out"), "CLF2020") #F contracts self.assertEqual(table.resolve("CL", "F1", 12, 2019), "CLH2020") self.assertEqual(table.resolve("CL", "F2", 12, 2019), "CLH2020") self.assertEqual(table.resolve("CL", "F3", 12, 2019), "CLK2020") self.assertEqual(table.resolve("CL", "F4", 12, 2019), "CLK2020") self.assertEqual(table.resolve("CL", "F5", 12, 2019), "CLN2020") self.assertEqual(table.resolve("CL", "F6", 12, 2019), "CLN2020") #C contracts self.assertEqual(table.resolve("CL", "C0", 12, 2019), "CLF2020") self.assertEqual(table.resolve("CL", "C1", 12, 2019), "CLH2020") self.assertEqual(table.resolve("CL", "C2", 12, 2019), "CLK2020") self.assertEqual(table.resolve("CL", "C3", 12, 2019), "CLN2020") #negative indices self.assertEqual(table.resolve("CL", "F-1", 12, 2019), "CLF2020") self.assertEqual(table.resolve("CL", "F-2", 12, 2019), "CLX2019") self.assertEqual(table.resolve("CL", "C-1", 12, 2019), "CLX2019") self.assertEqual(table.resolve("CL", "C-2", 12, 2019), "CLU2019") #test roll-out table table = rolltables.Rolltable( { "CL": [ "H0", "H0", "K0", "K0", "N0", "N0", "U0", "U0", "X0", "X0", "F1", "F1" ] }, "roll-out") self.assertEqual(table.resolve("CL", "F0", 12, 2019), "CLF2020") self.assertEqual(table.resolve("CL", "F0", 12, 2019, "roll-out"), "CLF2020") self.assertEqual(table.resolve("CL", "F0", 12, 2019, "roll-in"), "CLH2020") table = rolltables.Rolltable( { "CL": [ "H0", "H0", "K0", "K0", "N0", "N0", "U0", "U0", "X0", "X0", "F1", "F1" ] }, "roll-in") self.assertEqual(table.resolve("CL", "F0", 12, 2019, "roll-out"), "CLF2020") self.assertEqual(table.resolve("CL", "F0", 12, 2019, "roll-in"), "CLF2020") table = rolltables.Rolltable( { "CL": [ "H0", "H0", "K0", "K0", "N0", "N0", "U0", "U0", "X0", "X0", "F1", "F1" ] }, "roll-out") self.assertEqual(table.resolve("CL", "F0", 12, 2019, "roll-out"), "CLF2020") self.assertEqual(table.resolve("CL", "F0", 12, 2019, "roll-in"), "CLH2020") #test exceptions with self.assertRaises(ValueError): table.resolve("CL", "X0", 12, 2019) with self.assertRaises(ValueError): table.resolve("GC", "F0", 12, 2020) with self.assertRaises(ValueError): table.resolve("CL", "F0", 0, 2019) with self.assertRaises(ValueError): table.resolve("CL", "F0", 12, 2019, "roll out")