def test_update_name(self): """Testing method / function update_name.""" # Add an entry to the database name = data.hashstring(str(random())) # Make sure it does not exist result = pair_xlate_group.exists(name) self.assertFalse(bool(result)) # Add row to database pair_xlate_group.insert_row(name) # Make sure it exists idx = pair_xlate_group.exists(name) # Get current name with db.db_query(20093) as session: result = session.query(PairXlateGroup.name).filter( PairXlateGroup.idx_pair_xlate_group == idx).one() # Test self.assertEqual(name, result.name.decode()) # Update the name new_name = data.hashstring(str(random())) pair_xlate_group.update_name(idx, new_name) # Get current name with db.db_query(20094) as session: result = session.query(PairXlateGroup.name).filter( PairXlateGroup.idx_pair_xlate_group == idx).one() # Test self.assertEqual(new_name, result.name.decode())
def test_assign(self): """Testing method / function assign.""" # Create a new PairXlateGroup entry name = data.hashstring(str(random())) pair_xlate_group.insert_row(name) idx_pair_xlate_group = pair_xlate_group.exists(name) # Add an entry to the database name = data.hashstring(str(random())) agent_group.insert_row(name) # Make sure it exists idx_agent_group = agent_group.exists(name) # Get current idx_pair_xlate_group for the agent group with db.db_query(20095) as session: result = session.query(AgentGroup.idx_pair_xlate_group).filter( AgentGroup.idx_agent_group == idx_agent_group).one() idx_original = result.idx_pair_xlate_group self.assertNotEqual(idx_pair_xlate_group, idx_original) # Assign agent_group.assign(idx_agent_group, idx_pair_xlate_group) # Get current idx_pair_xlate_group for the agent group with db.db_query(20098) as session: result = session.query(AgentGroup.idx_pair_xlate_group).filter( AgentGroup.idx_agent_group == idx_agent_group).one() idx_new = result.idx_pair_xlate_group self.assertEqual(idx_pair_xlate_group, idx_new)
def test_update_name(self): """Testing method / function update_name.""" # Add an entry to the database code = data.hashstring(str(random())) name = data.hashstring(str(random())) language.insert_row(code, name) # Get current name with db.db_query(20003) as session: result = session.query( Language.name).filter(Language.code == code.encode()).one() # Test self.assertEqual(name, result.name.decode()) # Update the name new_name = data.hashstring(str(random())) language.update_name(code, new_name) # Get current name with db.db_query(20045) as session: result = session.query( Language.name).filter(Language.code == code.encode()).one() # Test self.assertEqual(new_name, result.name.decode())
def set_fn(self, name, expected, cmd_args, target_table, callback, process): """Testing proper setting/updating of entries into a given table associated with the _process_language and _process_pair_xlate_group operations Args: name: current_name of a given element within target_table expected: testcase values cmd_args: command line arguments to be parsed to be passed to target_table: database table to be queried callback: specific cli_set function to be ran process: Boolean to indicate whether process is used to run either _process_language or _process_pair_xlate_group Return: """ # Instantiation of commandline arguments args = self.parser.parse_args(cmd_args) _ts_modified = None # Retrieves the current ts_modified before updates are made with db.db_query(31000) as session: query = session.query(target_table) queried_result = query.filter_by(name = name.encode()).first() _ts_modified = queried_result.ts_modified # Determine how to run callback based on value of process if process == True: with self.assertRaises(SystemExit): callback(args) else: callback(args) result = None # Retrieves updated result with db.db_query(31002) as session: query = session.query(target_table) result = query.filter_by(name = expected['name'].encode()).first() # Asserts that changes made usin the 'callback' function was reflected # in target_table for key, value in expected.items(): result_value = result.__dict__[key] if type(result_value) == int: self.assertEqual(result_value, int(value)) else: self.assertEqual(result_value, value.encode()) # Asserts that result ts_modified time was updated self.assertGreaterEqual(result.ts_modified, _ts_modified)
def test_update(self): """Testing method / function update.""" # Add a language and pair_xlate_group entry to the database code = data.hashstring(str(random())) _translation = data.hashstring(str(random())) language.insert_row(code, _translation) idx_language = language.exists(code) pair_xlate_group.insert_row(_translation) idx_pair_xlate_group = pair_xlate_group.exists(_translation) # Create data _data = [] for key in range(0, 10): _data.append( [code, str(key), '_{}_'.format(key), '0{}0'.format(key)]) _df0 = pd.DataFrame( _data, columns=['language', 'key', 'translation', 'units']) pair_xlate.update(_df0, idx_pair_xlate_group) # Update data _data = [] for key in range(0, 10): _data.append( [code, str(key), '|{}|'.format(key), '1{}1'.format(key)]) _df = pd.DataFrame(_data, columns=['language', 'key', 'translation', 'units']) pair_xlate.update(_df, idx_pair_xlate_group) # Test updated data for key in range(0, 10): with db.db_query(20125) as session: row = session.query(PairXlate).filter( and_( PairXlate.idx_pair_xlate_group == idx_pair_xlate_group, PairXlate.key == str(key).encode(), PairXlate.idx_language == idx_language)).one() self.assertEqual(row.translation.decode(), _df['translation'][key]) self.assertEqual(row.units.decode(), _df['units'][key]) # Old translations should not exist for translation in _df0['translation']: with db.db_query(20126) as session: row = session.query(PairXlate).filter( and_( PairXlate.idx_pair_xlate_group == idx_pair_xlate_group, PairXlate.translation == translation.encode(), PairXlate.idx_language == idx_language)) self.assertEqual(row.count(), 0)
def pair_exists(key, value): """Get the db Pair table for key-value pair. Args: key: Key-value pair key value: Key-value pair value Returns: result: Pair.idx_pair value """ # Initialize key variables result = False rows = [] # Ignore certain restricted keys with db.db_query(20006) as session: rows = session.query(Pair.idx_pair).filter(and_( Pair.key == key.encode(), Pair.value == value.encode() )) # Return for row in rows: result = row.idx_pair break return result
def test_update_row(self): """Testing method / function update_row.""" # Add a language entry to the database code = data.hashstring(str(random())) _translation = data.hashstring(str(random())) language.insert_row(code, _translation) idx_language = language.exists(code) # Make sure row does not exist translation = data.hashstring(str(random())) key = data.hashstring(str(random())) result = agent_xlate.agent_xlate_exists(idx_language, key) self.assertFalse(result) # Add an entry to the database agent_xlate.insert_row(key, translation, idx_language) # Test existence result = agent_xlate.agent_xlate_exists(idx_language, key) self.assertTrue(result) # Test update new_translation = data.hashstring(str(random())) agent_xlate.update_row(key, new_translation, idx_language) with db.db_query(20134) as session: row = session.query(AgentXlate).filter( and_(AgentXlate.agent_program == key.encode(), AgentXlate.idx_language == idx_language)).one() self.assertEqual(row.translation.decode(), new_translation)
def exists(username): """Determine whether name exists in the User table. Args: username: user name Returns: result: User.idx_user value """ # Initialize key variables result = False rows = [] # Lowercase the name username = username.lower().strip() # Get name from database with db.db_query(20031) as session: rows = session.query(_User.idx_user).filter( _User.username == username.encode()) # Return for row in rows: result = row.idx_user break return result
def idx_pairs(_idx_datapoints): """Get all the checksum values for a specific source. Args: _idx_datapoints: List idx_datapoint values Returns: result: Dict of idx_datapoint values keyed by DataPoint.checksum """ # Initialize key variables result = [] rows = [] # Create a list if it doesn't exist if isinstance(_idx_datapoints, list) is False: _idx_datapoints = [_idx_datapoints] # Get the data from the database with db.db_query(20014) as session: rows = session.query( Glue.idx_pair).filter(Glue.idx_datapoint.in_(_idx_datapoints)) # Return for row in rows: result.append(row.idx_pair) return sorted(result)
def exists(agent_id, agent_target): """Get the db Agent.idx_agent value for specific Agent. Args: agent_id: Agent ID agent_target: Agent polled target Returns: result: Agent.idx_agent value """ # Initialize key variables result = False rows = [] # Get the result with db.db_query(20039) as session: rows = session.query(Agent.idx_agent).filter( and_( Agent.agent_id == agent_id.encode(), Agent.agent_polled_target == agent_target.encode(), )) # Return for row in rows: result = row.idx_agent break return result
def cli_show_dump(): """Get entire content of the table. Args: None Returns: result: List of NamedTuples """ # Initialize key variables result = [] # Get the result with db.db_query(20049) as session: rows = session.query(Language) # Process for row in rows: Record = namedtuple('Record', 'idx_language code name') result.append( Record(idx_language=row.idx_language, name=row.name.decode(), code=row.code.decode())) return result
def setUpClass(self): """Setup tables in pattoo_unittest database""" # Setting up parsing for cli_set module subparser = self.parser.add_subparsers(dest='action') _Set(subparser) # Logger self.log_obj = log._GetLog() # Skips class setup if using travis-ci if not self.travis_ci: # Create test tables for Import test self.tables = [PairXlateGroup.__table__, Language.__table__] # Returns engine object self.engine = create_tables(self.tables) # Creating test data in Language and PairXlateGroup tables language.insert_row('en', 'English') pair_xlate_group.insert_row('Pattoo Default') # Getting number of entries in PairXlateGroup table self.idx_pair_xlate_group_count = 1 with db.db_query(30004) as session: result = session.query(PairXlateGroup) self.idx_pair_xlate_group_count += result.count()
def exists(idx_chart, idx_datapoint): """Determine whether name exists in the ChartDataPoint table. Args: idx_chart: Chart table index idx_datapoint: DataPoint table index Returns: result: ChartDataPoint.idx_chart_datapoint value """ # Initialize key variables result = False rows = [] # Get name from database with db.db_query(20037) as session: rows = session.query(ChartDataPoint.idx_chart_datapoint).filter( and_(ChartDataPoint.idx_chart == idx_chart, ChartDataPoint.idx_datapoint == idx_datapoint)) # Return for row in rows: result = row.idx_chart_datapoint break return result
def agent_xlate_exists(idx_language, key): """Get the db AgentXlate.idx_agent_xlate value for specific agent. Args: idx_language: Language table primary key key: Key for translation Returns: result: True if exists """ # Initialize key variables result = False rows = [] # Get the result with db.db_query(20128) as session: rows = session.query(AgentXlate.translation).filter(and_( AgentXlate.idx_language == idx_language, AgentXlate.agent_program == key.encode() )) # Return for _ in rows: result = True break return result
def idx_pairs(_items): """Get the db Pair table indices based on key, value pairs. Args: _items: List of (key, value) tuples Returns: result: list of Pair.idx_pair values """ # Initialize key variables result = [] # Encode the items items = [(key.encode(), value.encode()) for key, value in _items] # Get the data from the database with db.db_query(20011) as session: rows = session.query( Pair.idx_pair).filter(tuple_(Pair.key, Pair.value).in_(items)) # Return for row in rows: result.append(row.idx_pair) return sorted(result)
def idx_pair_xlate_group(agent_id): """Get the idx_pair_xlate_group of an agent. Args: agent_id: Agent ID Returns: result: idx_pair_xlate_group for the agent """ # Initialize key variables result = False rows = [] # Get the result with db.db_query(20079) as session: rows = session.query(PXG.idx_pair_xlate_group).filter( and_(Agent.agent_id == str(agent_id).encode(), PXG.idx_pair_xlate_group == AgentGroup.idx_pair_xlate_group, Agent.idx_agent_group == AgentGroup.idx_agent_group)) # Return for row in rows: result = row.idx_pair_xlate_group break return result
def cli_show_dump(): """Get entire content of the table. Args: None Returns: result: List of NamedTuples """ # Initialize key variables result = [] # Get the result with db.db_query(20042) as session: rows = session.query(Agent) # Process for row in rows: Record = namedtuple('Record', 'idx_agent agent_program agent_target enabled') result.append( Record(idx_agent=row.idx_agent, enabled=row.enabled, agent_program=row.agent_program.decode(), agent_target=row.agent_polled_target.decode())) return result
def exists(code): """Determine whether code exists in the Language table. Args: code: language code Returns: result: Language.idx_language value """ # Initialize key variables result = False rows = [] # Lowercase the code code = code.lower().strip() # Get code from database with db.db_query(20031) as session: rows = session.query( Language.idx_language).filter(Language.code == code.encode()) # Return for row in rows: result = row.idx_language break return result
def glue_exists(_idx_datapoint, idx_pair): """Determine existence of idx_datapoint, idx_pair in the Glue db table. Args: _idx_datapoint: DataPoint.idx_datapoint table index idx_pair: Pair.idx_pair table index Returns: result: True if it exists """ # Initialize idx_datapoint variables result = False rows = [] # Ignore certain restricted idx_datapoints with db.db_query(20008) as session: rows = session.query(Glue.idx_pair).filter(and_( Glue.idx_datapoint == _idx_datapoint, Glue.idx_pair == idx_pair, Pair.idx_pair == Glue.idx_pair, DataPoint.idx_datapoint == Glue.idx_datapoint )) # Return for _ in rows: result = True break return result
def agent_checksums(agent_id): """Get all the checksum values for a specific agent_id. Args: agent_id: PattooDBrecord object agent_id Returns: result: Dict of idx_datapoint values keyed by DataPoint.checksum """ # Result result = {} rows = [] # Get the data from the database with db.db_query(20013) as session: rows = session.query(DataPoint.checksum, DataPoint.last_timestamp, DataPoint.polling_interval, DataPoint.idx_datapoint).filter( and_(Agent.agent_id == agent_id.encode(), DataPoint.idx_agent == Agent.idx_agent)) # Return for row in rows: result[row.checksum.decode()] = ChecksumLookup( idx_datapoint=row.idx_datapoint, polling_interval=row.polling_interval, last_timestamp=row.last_timestamp) return result
def test_assign(self): """Testing method / function assign.""" # Add an entry to the database name = data.hashstring(str(random())) pair_xlate_group.insert_row(name) # Make sure it exists idx_pair_xlate_group = pair_xlate_group.exists(name) # Verify the index exists result = pair_xlate_group.idx_exists(idx_pair_xlate_group) self.assertTrue(result) # Prepare for adding an entry to the database agent_id = data.hashstring(str(random())) agent_target = data.hashstring(str(random())) agent_program = data.hashstring(str(random())) # Make sure it does not exist result = agent.exists(agent_id, agent_target) self.assertFalse(bool(result)) # Add an entry to the database agent.insert_row(agent_id, agent_target, agent_program) idx_agent = agent.exists(agent_id, agent_target) # Assign agent.assign(idx_agent, idx_pair_xlate_group) # Get assigned idx_pair_xlate_group for the agent group with db.db_query(20099) as session: result = session.query(Agent.idx_pair_xlate_group).filter( Agent.idx_agent == idx_agent).one() idx_new = result.idx_pair_xlate_group self.assertEqual(idx_pair_xlate_group, idx_new)
def exists(idx_chart, idx_user): """Determine whether name exists in the Favorite table. Args: idx_chart: Chart table index idx_user: User table index Returns: result: Favorite.idx_favorite value """ # Initialize key variables result = False rows = [] # Get name from database with db.db_query(20010) as session: rows = session.query(Favorite.idx_favorite).filter( and_(Favorite.idx_chart == idx_chart, Favorite.idx_user == idx_user)) # Return for row in rows: result = row.idx_favorite break return result
def pair_xlate_exists(idx_pair_xlate_group, idx_language, key): """Get the db PairXlate.idx_pair_xlate value for specific agent. Args: idx_pair_xlate_group: PairXlateGroup table primary key idx_language: Language table primary key key: Key for translation Returns: result: True if exists """ # Initialize key variables result = False rows = [] # Get the result with db.db_query(20081) as session: rows = session.query(PairXlate.translation).filter( and_(PairXlate.idx_pair_xlate_group == idx_pair_xlate_group, PairXlate.idx_language == idx_language, PairXlate.key == key.encode())) # Return for _ in rows: result = True break return result
def valid_password(self, value): """Get. Args: value: New value to apply Returns: result: True if valid """ # Initialize key variables result = False if bool(self.exists) is True: # Get password from database with db.db_query(20141) as session: password = session.query(_User.password).filter( _User.username == self._username.encode()).one() # Determine state of password found = password[0].decode() salt = found.split('$')[2] expected = crypt.crypt(value, '$6${}'.format(salt)) result = bool(found == expected) return result
def __init__(self, username): """Initialize the class. Args: username: Name of user Returns: None """ # Initialize key variables row = None self.first_name = None self.last_name = None self.role = None self.password_expired = None self.enabled = None self.exists = False self._username = username # Get data with db.db_query(20162) as session: row = session.query(_User).filter( _User.username == username.encode()).first() # Assign variables if not (row is None): self.first_name = row.first_name.decode() self.last_name = row.last_name.decode() self.role = row.role self.password_expired = bool(row.password_expired) self.enabled = bool(row.enabled) self.username = username self.exists = True
def data(self, ts_start, ts_stop): """Create list of dicts of counter values retrieved from database. Args: ts_start: Start time for query ts_stop: Stop time for query Returns: result: List of key-value pair dicts """ # Initialize key variables data_type = self.data_type() _pi = self.polling_interval() places = 10 result = [] # Return nothing if the DataPoint does not exist if self.exists() is False: return result # Normalize timestamp to match the start of the nones array. If not, # we could get the starting timestamp of the result to have a "None" # value ts_start = times.normalized_timestamp(_pi, timestamp=ts_start) # Make sure we have entries for entire time range timestamps = times.timestamps(ts_start, ts_stop, _pi) nones = {_key: None for _key in timestamps} # Get data from database with db.db_query(20092) as session: rows = session.query(Data.timestamp, Data.value).filter( and_(Data.timestamp <= ts_stop, Data.timestamp >= ts_start, Data.idx_datapoint == self._idx_datapoint)).order_by( Data.timestamp).all() # Put values into a dict for ease of processing for row in rows: # Find the first timestamp in the sorted list that is greater than # that found in the database timestamp = times.normalized_timestamp(_pi, row.timestamp) rounded_value = round(float(row.value), places) nones[timestamp] = rounded_value if data_type in [DATA_INT, DATA_FLOAT]: # Process non-counter values result = _response(nones) elif data_type in [DATA_COUNT64, DATA_COUNT] and len(rows) > 1: # Process counter values by calculating the difference between # successive values result = _counters(nones, _pi, places) return result
def test_polling_interval(self): """Testing method / function polling_interval.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) # Get the result with db.db_query(20106) as session: result = session.query(_DataPoint.polling_interval).filter( _DataPoint.idx_datapoint == idx_datapoint).one() self.assertEqual(result.polling_interval, obj.polling_interval())
def test_last_timestamp(self): """Testing method / function last_timestamp.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) # Get the result with db.db_query(20101) as session: result = session.query(_DataPoint.last_timestamp).filter( _DataPoint.idx_datapoint == idx_datapoint).one() self.assertEqual(result.last_timestamp, obj.last_timestamp())
def test_data_type(self): """Testing method / function data_type.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) # Get the result with db.db_query(20102) as session: result = session.query(_DataPoint.data_type).filter( _DataPoint.idx_datapoint == idx_datapoint).one() self.assertEqual(result.data_type, obj.data_type())
def test_checksum(self): """Testing method / function checksum.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) # Get the result with db.db_query(20103) as session: result = session.query(_DataPoint.checksum).filter( _DataPoint.idx_datapoint == idx_datapoint).one() self.assertEqual(result.checksum.decode(), obj.checksum())