def setUp(self):
        # Find the schema directory
        dir = os.path.join(".")
        while SCHEMA_FOLDER not in os.listdir(dir):
            dir = os.path.join(dir, "..")

        self.fileIO = MockSynopticFileIO()
        self.bs = MockBlockServer()
        self.sm = SynopticManager(self.bs, os.path.join(dir, SCHEMA_FOLDER), MockVersionControl(), None, self.fileIO)
class TestSynopticManagerSequence(unittest.TestCase):
    def setUp(self):
        # Find the schema directory
        dir = os.path.join(".")
        while SCHEMA_FOLDER not in os.listdir(dir):
            dir = os.path.join(dir, "..")

        self.fileIO = MockSynopticFileIO()
        self.bs = MockBlockServer()
        self.sm = SynopticManager(self.bs, os.path.join(dir, SCHEMA_FOLDER), MockVersionControl(), None, self.fileIO)

    def tearDown(self):
        pass

    def _create_a_synoptic(self, name):
        self.sm.save_synoptic_xml(EXAMPLE_SYNOPTIC % name)

    def test_get_synoptic_names_returns_names_alphabetically(self):
        # Arrange
        self._create_a_synoptic(SYNOPTIC_1)
        self._create_a_synoptic(SYNOPTIC_2)

        # Act
        s = self.sm.get_synoptic_list()

        # Assert
        self.assertTrue(len(s) > 0)
        n = [x['name'] for x in s]
        print n
        self.assertEqual("-- NONE --", n[0])
        self.assertEqual(SYNOPTIC_1, n[1])
        self.assertEqual(SYNOPTIC_2, n[2])

    def test_create_pvs_is_okay(self):
        # Arrange
        self._create_a_synoptic(SYNOPTIC_1)
        # Act
        self.sm._load_initial()

        # Assert
        self.assertTrue(self.bs.does_pv_exist("%sSYNOP1%s" % (SYNOPTIC_PRE, SYNOPTIC_GET)))

    def test_get_default_synoptic_xml_returns_nothing(self):
        # Arrange
        # Act
        xml = self.sm.get_default_synoptic_xml()

        # Assert
        self.assertEqual(xml, "")

    def test_set_default_synoptic_xml_sets_something(self):
        # Arrange
        self._create_a_synoptic(SYNOPTIC_1)
        # Act
        self.sm.save_synoptic_xml(EXAMPLE_SYNOPTIC % "synoptic0")
        self.sm.set_default_synoptic("synoptic0")

        # Assert
        xml = self.sm.get_default_synoptic_xml()

        self.assertTrue(len(xml) > 0)
        # Check the correct name appears in the xml
        self.assertTrue("synoptic0" in xml)

    def test_set_current_synoptic_xml_creates_pv(self):
        # Arrange
        syn_name = "synopt"

        # Act
        self.sm.save_synoptic_xml(EXAMPLE_SYNOPTIC % syn_name)

        # Assert
        self.assertTrue(self.bs.does_pv_exist("%sSYNOPT%s" % (SYNOPTIC_PRE, SYNOPTIC_GET)))

    def test_delete_synoptics_empty(self):
        # Arrange
        self._create_a_synoptic(SYNOPTIC_1)
        self._create_a_synoptic(SYNOPTIC_2)
        initial_len = len(self.sm.get_synoptic_list())
        # Act
        self.sm.delete([])

        # Assert
        synoptic_names = [c["name"] for c in self.sm.get_synoptic_list()]
        self.assertEqual(len(synoptic_names), initial_len)
        self.assertTrue(SYNOPTIC_1 in synoptic_names)
        self.assertTrue(SYNOPTIC_2 in synoptic_names)
        self.assertTrue(self.bs.does_pv_exist(construct_pv_name(SYNOPTIC_1.upper())))
        self.assertTrue(self.bs.does_pv_exist(construct_pv_name(SYNOPTIC_2.upper())))

    def test_delete_one_synoptic(self):
        # Arrange
        self._create_a_synoptic(SYNOPTIC_1)
        self._create_a_synoptic(SYNOPTIC_2)
        initial_len = len(self.sm.get_synoptic_list())

        # Act
        self.sm.delete([SYNOPTIC_1])

        # Assert
        synoptic_names = [c["name"] for c in self.sm.get_synoptic_list()]
        self.assertEqual(len(synoptic_names), initial_len - 1)
        self.assertFalse(SYNOPTIC_1 in synoptic_names)
        self.assertTrue(SYNOPTIC_2 in synoptic_names)
        self.assertFalse(self.bs.does_pv_exist(construct_pv_name(SYNOPTIC_1.upper())))
        self.assertTrue(self.bs.does_pv_exist(construct_pv_name(SYNOPTIC_2.upper())))

    def test_delete_many_synoptics(self):
        # Arrange
        self._create_a_synoptic(SYNOPTIC_1)
        self._create_a_synoptic(SYNOPTIC_2)
        initial_len = len(self.sm.get_synoptic_list())
        # Act
        self.sm.delete([SYNOPTIC_1, SYNOPTIC_2])

        # Assert
        synoptic_names = [c["name"] for c in self.sm.get_synoptic_list()]
        self.assertEqual(len(synoptic_names), initial_len - 2)
        self.assertFalse(SYNOPTIC_1 in synoptic_names)
        self.assertFalse(SYNOPTIC_2 in synoptic_names)
        self.assertFalse(self.bs.does_pv_exist(construct_pv_name(SYNOPTIC_1.upper())))
        self.assertFalse(self.bs.does_pv_exist(construct_pv_name(SYNOPTIC_2.upper())))

    def test_cannot_delete_non_existent_synoptic(self):
        # Arrange
        self._create_a_synoptic(SYNOPTIC_1)
        self._create_a_synoptic(SYNOPTIC_2)
        initial_len = len(self.sm.get_synoptic_list())
        # Act
        self.assertRaises(InvalidDeleteException, self.sm.delete, ["invalid"])

        # Assert
        synoptic_names = [c["name"] for c in self.sm.get_synoptic_list()]
        self.assertEqual(len(synoptic_names), initial_len)
        self.assertTrue(self.bs.does_pv_exist(construct_pv_name(SYNOPTIC_1.upper())))
        self.assertTrue(self.bs.does_pv_exist(construct_pv_name(SYNOPTIC_2.upper())))