def setUp(self): self.ca = MockChannelAccess() self.exp_data = ExpData("TEST_PREFIX", self.ca)
class TestExpData(unittest.TestCase): def setUp(self): self.ca = MockChannelAccess() self.exp_data = ExpData("TEST_PREFIX", self.ca) def decodepv(self, pv): return json.loads(dehex_and_decompress(self.ca.caget(pv))) def test_single_surname_returns_surname(self): # Arrange fullname = "Tom Jones" # Act surname = self.exp_data._get_surname_from_fullname(fullname) # Assert self.assertEquals(surname, "Jones") def test_double_barrelled_surname_returns_last_name(self): # Arrange fullname = "Tom de Jones" # Act surname = self.exp_data._get_surname_from_fullname(fullname) # Assert self.assertEquals(surname, "Jones") def test_single_name_returns_fullname(self): # Arrange fullname = "TomJones" # Act surname = self.exp_data._get_surname_from_fullname(fullname) # Assert self.assertEquals(surname, "TomJones") def test_update_username_for_single_user(self): # Arrange users = '[{"name":"Tom Jones","institute":"STFC","role":"user"}]' # Act self.exp_data.updateUsername(users) # Assert simnames = self.decodepv(self.exp_data._simnames) surnames = self.decodepv(self.exp_data._surnamepv) orgs = self.decodepv(self.exp_data._orgspv) self.assertEqual(simnames[0]["name"], "Tom Jones") self.assertTrue("Jones" in surnames) self.assertTrue("STFC" in orgs) def test_update_username_for_multiple_users_same_institute(self): # Arrange users = '[' users += '{"name":"Tom Jones","institute":"STFC","role":"user"},' users += '{"name":"David James","institute":"STFC","role":"user"}' users += ']' # Act self.exp_data.updateUsername(users) # Assert simnames = self.decodepv(self.exp_data._simnames) surnames = self.decodepv(self.exp_data._surnamepv) orgs = self.decodepv(self.exp_data._orgspv) self.assertEqual(len(simnames), 2) self.assertEqual(len(surnames), 2) self.assertEqual(len(orgs), 1) def test_update_username_for_blank_users(self): # Arrange users = '[{"name":"Tom Jones","institute":"STFC","role":"user"}]' self.exp_data.updateUsername(users) # Act self.exp_data.updateUsername("") # Assert simnames = self.decodepv(self.exp_data._simnames) surnames = self.decodepv(self.exp_data._surnamepv) orgs = self.decodepv(self.exp_data._orgspv) self.assertEqual(len(simnames), 0) self.assertEqual(len(surnames), 0) self.assertEqual(len(orgs), 0) def test_remove_accents_from_name(self): # Arrange # list of names in unicode code points, which is the same as ISO-8859-1 encoding names_uni = [ u'Somebody', u'S\xf8rina', u'\xe9\xe5\xf5\xf6\xc6' ] # best ascii equivalents of names names_ascii = [ 'Somebody', 'Sorina', 'eaooAE' ] # Act conv_names = ExpData.make_name_list_ascii(names_uni).split(',') # Assert self.assertTrue(conv_names == names_ascii)