class TestCompetition(unittest.TestCase):

    def setUp(self):
        self.competition = Competition(org_id='org-123123')

    def test_hello_world(self):
        self.competition.say_hello() == "Hello, World"

    def test_new_competition_requires_org_id(self):
        with pytest.raises(TypeError):
            Competition()
    def test_build_graph_by_hand(self):
        # Check that this graph is dumped identical with the auto-loaded
        # graph. Then run the tests with auto-loaded. Easier to change data.
        competition = Competition(name="comp_data_01")
        competition.created = "2012-09-18T12:12:12"

        cp_add = competition.add
        nodes = competition.nodes
        # Add riders and groups
        cp_add(Rider("staff", groups=["anyone"]))
        cp_add(Rider("admin", groups=["staff"]))
        cp_add(Rider("accounts-dept"))
        cp_add(Rider("jane", groups=["admin"]))
        cp_add(Rider("jim", groups=["admin", "accounts-dept"]))

        # Add other rider groups
        nodes["accounts-dept"].add_groups(["staff"])

        # Add ponies and groups
        cp_add(Pony("financial", groups=["anything"]))
        cp_add(Pony("share-price"))
        cp_add(Pony("accounts"))
        cp_add(Pony("salaries", groups=["accounts"]))

        # Add other pony groups
        nodes["share-price"].add_groups(["financial"])
        nodes["accounts"].add_groups(["financial"])

        # Add action

        cp_add(Action("view", groups=["action"]))
        cp_add(Action("maintain", groups=["view"]))
        cp_add(Action("market", groups=["view"]))
        cp_add(Action("edit", groups=["view"]))
        cp_add(Action("create"))
        nodes["create"].add_groups(["edit", "maintain"])

        # Add latches
        cp_add(Latch("allow jane market share-price 30"))
        cp_add(Latch("allow anyone view share-price 40"))
        cp_add(Latch("deny admin view accounts 60"))
        cp_add(Latch("allow accounts-dept create accounts 70"))
        cp_add(Latch("allow jane edit salaries 85"))
        cp_add(Latch("deny accounts-dept edit salaries 85"))
        cp_add(Latch("allow jim create salaries 90"))
        cp_add(Latch("deny jane view salaries 125"))

        competition.validate()

        self.assertDictEqual(competition.dump(), TEST_COMPETITION_DICT)
 def setUp(self):
     self.competition = Competition.load(TEST_COMPETITION_TREE)
 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")
 def setUp(self):
     self.competition = Competition(name="Empty competition")
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")
 def setUp(self):
     self.competition = Competition(org_id='org-123123')