def idx_agent(self): """Insert new agent into database if necessary. Args: None Returns: idx_agent: IDX value of agent from database """ # Initialize key variables agent_name = self.agent_data['agent_name'] id_agent = self.agent_data['id_agent'] # Get information on agent from database agent_data = db_agent.GetIDAgent(id_agent) # Return if agent already exists in the table if agent_data.exists() is True: idx_agent = agent_data.idx_agent() return idx_agent # Get information on agent from database name_data = db_agentname.GetAgentName(agent_name) # Insert data into table if required # Get idx_agentname if name_data.exists() is False: record = AgentName( name=general.encode(agent_name)) database = db.Database() try: database.add(record, 1145) except pymysql.IntegrityError: # There may be a duplicate agent name if this is a brand # new database and there is a flurry of updates from multiple # agents. This is OK, pass. # # We are expecting a 'pymysql.err.IntegrityError' but for some # reason it could not be caught. pass new_name_data = db_agentname.GetAgentName(agent_name) idx_agentname = new_name_data.idx_agentname() else: idx_agentname = name_data.idx_agentname() # Add record to the database new_record = Agent( id_agent=general.encode(id_agent), idx_agentname=idx_agentname) database = db.Database() database.add(new_record, 1081) # Get idx_agent value from database new_agent_data = db_agent.GetIDAgent(id_agent) idx_agent = new_agent_data.idx_agent() return idx_agent
def idx_agent(self): """Insert new agent into database if necessary. Args: None Returns: idx_agent: IDX value of agent from database """ # Initialize key variables agent_name = self.agent_data['agent_name'] id_agent = self.agent_data['id_agent'] # Get information on agent from database agent_data = db_agent.GetIDAgent(id_agent) # Return if agent already exists in the table if agent_data.exists() is True: idx_agent = agent_data.idx_agent() return idx_agent # Get information on agent from database name_data = db_agentname.GetAgentName(agent_name) # Insert data into table if required # Get idx_agentname if name_data.exists() is False: record = AgentName(name=general.encode(agent_name)) database = db.Database() database.add(record, 1081) new_name_data = db_agentname.GetAgentName(agent_name) idx_agentname = new_name_data.idx_agentname() else: idx_agentname = name_data.idx_agentname() # Add record to the database new_record = Agent(id_agent=general.encode(id_agent), idx_agentname=idx_agentname) database = db.Database() database.add(new_record, 1081) # Get idx_agent value from database new_agent_data = db_agent.GetIDAgent(id_agent) idx_agent = new_agent_data.idx_agent() return idx_agent
def test_init(self): """Testing method __init__.""" # Test with non existent AgentNameID record = db_agentname.GetAgentName('bogus') self.assertEqual(record.exists(), False) self.assertEqual(record.name(), None) self.assertEqual(record.idx_agentname(), None) self.assertEqual(record.enabled(), None)
class Other(unittest.TestCase): """Checks all functions and methods.""" # Setup database based on the config database = unittest_setup_db.TestData() # Define expected values expected = {} expected['idx_agentname'] = database.idx_agentname() expected['name'] = database.agent() expected['enabled'] = True expected['exists'] = True # Retrieve data good_agent = db_agentname.GetAgentName(expected['name']) def test_name_exists(self): """Testing function name_exists.""" # Testing with known good value expected = True result = db_agentname.name_exists(self.expected['name']) self.assertEqual(result, expected) # Testing with known bad value expected = False result = db_agentname.name_exists('bogus') self.assertEqual(result, expected) def test_idx_agentname_exists(self): """Testing function idx_agentname_exists.""" # Testing with known good value expected = True result = db_agentname.idx_agentname_exists( self.expected['idx_agentname']) self.assertEqual(result, expected) # Testing with known bad value expected = False result = db_agentname.idx_agentname_exists(None) self.assertEqual(result, expected) def test_get_all_names(self): """Testing function get_all_names.""" # Testing with known good value result = db_agentname.get_all_names() self.assertEqual(isinstance(result, list), True) self.assertEqual(result[0]['name'], self.expected['name']) self.assertEqual(result[0]['exists'], self.expected['exists']) self.assertEqual(result[0]['enabled'], self.expected['enabled']) self.assertEqual(result[0]['idx_agentname'], self.expected['idx_agentname'])
class TestGetIdentifier(unittest.TestCase): """Checks all functions and methods.""" # Setup database based on the config database = unittest_setup_db.TestData() # Define expected values expected = {} expected['idx_agentname'] = database.idx_agentname() expected['name'] = database.agent() expected['enabled'] = True expected['exists'] = True # Retrieve data good_agent = db_agentname.GetAgentName(expected['name']) def test_init(self): """Testing method __init__.""" # Test with non existent AgentNameID record = db_agentname.GetAgentName('bogus') self.assertEqual(record.exists(), False) self.assertEqual(record.name(), None) self.assertEqual(record.idx_agentname(), None) self.assertEqual(record.enabled(), None) def test_exists(self): """Testing method exists.""" # Testing with known good value result = self.good_agent.exists() self.assertEqual(result, True) def test_name(self): """Testing method name.""" # Testing with known good value result = self.good_agent.name() self.assertEqual(result, self.expected['name']) # Testing with known bad value expected = ('bogus') result = self.good_agent.name() self.assertNotEqual(result, expected) def test_idx_agentname(self): """Testing method idx.""" # Testing with known good value result = self.good_agent.idx_agentname() self.assertEqual(result, self.expected['idx_agentname']) # Testing with known bad value expected = ('bogus') result = self.good_agent.idx_agentname() self.assertNotEqual(result, expected) def test_enabled(self): """Testing method enabled.""" # Testing with known good value result = self.good_agent.enabled() self.assertEqual(result, self.expected['enabled']) # Testing with known bad value expected = ('bogus') result = self.good_agent.enabled() self.assertNotEqual(result, expected) def test_everything(self): """Testing method everything.""" # Testing with known good value result = self.good_agent.everything() for key, _ in self.expected.items(): self.assertEqual(result[key], self.expected[key]) # Test the number and names of keys keys = ['idx_agentname', 'name', 'enabled', 'exists'] self.assertEqual(len(result), len(keys)) for key in keys: self.assertEqual(key in result, True)