class TestEmptyCompetition(DebugPrintingTestCase):
    def setUp(self):
        self.competition = Competition(name="Empty competition")

    @skip
    def test_create_competition(self):
        self.assertEqual(self.competition.name, "empty_competition")
        self.assertEqual(self.competition.caption, "Empty Competition")
        descs = self.competition.descendants
        self.assertEqual(descs("anyone"), set())
        self.assertEqual(descs("anything"), set())
        self.assertEqual(descs("action"), set())
        self.assertEqual(descs("permissions"), {LATCH_DENY_ALL})

    @skip
    def test_adding_rider(self):
        rider = Rider(name="jim")
        self.competition.add(rider)
        self.assertEqual(self.competition.nodes["jim"], rider)

    @skip
    def test_removing_rider(self):
        rider = Rider(name="jane")
        self.competition.add(rider)
        self.competition.remove(rider)
        self.debug_print(self.competition)
        self.assertItemsEqual(self.competition.nodes.keys(), NODE_GROUPS | {LATCH_DENY_ALL})

    @skip
    def test_removing_non_existent_rider(self):
        rider = Rider(name="jim")
        with self.assertRaises(CompetitionError) as ctxmgr:
            self.competition.remove(rider)
        self.assertIn("Unknown node key", ctxmgr.exception.message)

    @skip
    def test_removing_duplicate_rider(self):
        rider1 = Rider(name="jane")
        rider2 = Rider(name="jane")
        self.competition.add(rider1)
        with self.assertRaises(DuplicateNodeError) as ctxmgr:
            self.competition.remove(rider2)
        self.assertIn("Duplicate node", ctxmgr.exception.message)

    @skip
    def test_adding_pony(self):
        pony = Pony(name="salaries")
        self.competition.add(pony)
        self.assertEqual(self.competition.nodes["salaries"], pony)

    @skip
    def test_removing_pony(self):
        pony = Pony(name="finance")
        self.competition.add(pony)
        self.competition.remove(pony)
        self.assertItemsEqual(self.competition.nodes.keys(), NODE_GROUPS | {LATCH_DENY_ALL})

    @skip
    def test_adding_latch(self):
        latch_data = "allow admin to view accounts wt 60"
        latch = Latch(latch_data)
        self.competition.add(latch)
        self.assertItemsEqual(self.competition.nodes.keys(), NODE_GROUPS | {latch_data, LATCH_DENY_ALL})

    @skip
    def test_removing_latch(self):
        latch_data = "allow admin to view accounts wt 60"
        latch = Latch(latch_data)
        self.competition.add(latch)
        self.competition.remove(latch)
        self.assertItemsEqual(self.competition.nodes.keys(), NODE_GROUPS | {LATCH_DENY_ALL})

    @skip
    def test_adding_unknown_class(self):
        class Jumper(LatchPonyNode):
            collection_name = "_jumpers"

            def __init__(self, name, **kwargs):
                super(Jumper, self).__init__(**kwargs)
                self.name = name

        jumper = Jumper("oops")
        self.competition.add(jumper)
        with self.assertRaises(CompetitionValidationError):
            self.competition.validate()

    def test_load_with_unknown_format(self):
        with self.assertRaises(CompetitionFormatError):
            competition = Competition.load({}, data_format="xml")
        with self.assertRaises(CompetitionFormatError):
            competition = Competition.load({}, data_format="soap")