def test_contents(self): """Testing function contents.""" # Initialize key variables devicename = general.hashstring(general.randomstring()) id_agent = general.hashstring(general.randomstring()) agent_name = general.hashstring(general.randomstring()) hosthash = general.hashstring(devicename) timestamp = general.normalized_timestamp() # Create a directory and filename for testing directory = tempfile.mkdtemp() filename = ('%s/%s_%s_%s.json') % (directory, timestamp, id_agent, hosthash) # Test with good data data = { 'id_agent': id_agent, 'agent': agent_name, 'devicename': devicename, 'timestamp': timestamp } with open(filename, 'w') as f_handle: json.dump(data, f_handle) result = validate._CheckFile(filename) contents = result.contents() for key in contents.keys(): self.assertEqual(contents[key], data[key]) self.assertEqual(len(contents), len(data))
def test___init__(self): """Testing function __init__.""" # Test non existent file directory = tempfile.mkdtemp() badfile = general.randomstring() result = validate._CheckFile(badfile) self.assertEqual(result.valid(), False) # Test bad filename, existing file (Non hex characters in name) badfile = ('%s/%s_%s_%s.json') % (directory, int( time.time()), general.randomstring(), general.randomstring()) data = general.randomstring() with open(badfile, 'w') as f_handle: f_handle.write(data) result = validate._CheckFile(badfile) self.assertEqual(result.valid(), False) os.remove(badfile) # Test good filename, no data id_agent = general.hashstring(general.randomstring()) hosthash = general.hashstring(general.randomstring()) filename = ('%s/%s_%s_%s.json') % (directory, int( time.time()), id_agent, hosthash) data = general.randomstring() with open(filename, 'w') as f_handle: f_handle.write(data) result = validate._CheckFile(badfile) self.assertEqual(result.valid(), False) os.remove(filename)
def test__valid_filename(self): """Testing function _valid_filename.""" # Test bad filename filename = general.randomstring() result = validate._valid_filename(filename) self.assertEqual(result, False) # Test bad filename filename = str(int(time.time())) result = validate._valid_filename(filename) self.assertEqual(result, False) # Test bad filename filename = ('%s_%s') % (int( time.time()), general.hashstring(general.randomstring())) result = validate._valid_filename(filename) self.assertEqual(result, False) # Test bad filename filename = ('%s_%s_%s') % (int( time.time()), general.hashstring(general.randomstring()), general.hashstring(general.randomstring())) result = validate._valid_filename(filename) self.assertEqual(result, False) # Test bad filename (Non hex characters in name) filename = ('%s_%s') % (int(time.time()), general.randomstring()) result = validate._valid_filename(filename) self.assertEqual(result, False) # Test bad filename (Non hex characters in name) filename = ('%s_%s_%s') % (int( time.time()), general.randomstring(), general.randomstring()) result = validate._valid_filename(filename) self.assertEqual(result, False) # Test bad filename (Non hex characters in name) filename = ('%s_%s_%s.json') % (int( time.time()), general.randomstring(), general.randomstring()) result = validate._valid_filename(filename) self.assertEqual(result, False) # Test good filename filename = ('%s_%s_%s.json') % ( int(time.time()), general.hashstring(general.randomstring()), general.hashstring(general.randomstring())) result = validate._valid_filename(filename) self.assertEqual(result, True)
def __init__(self, config, devicename, test=False): """Method initializing the class. Args: config: ConfigAgent configuration object agent_name: Name of agent devicename: Devicename that the agent applies to test: True if testing functionality Returns: None """ # Initialize key variables self.data = defaultdict(lambda: defaultdict(dict)) agent_name = config.agent_name() id_agent = get_id_agent(agent_name, test=test) # Add timestamp self.data['timestamp'] = general.normalized_timestamp() self.data['id_agent'] = id_agent self.data['agent'] = agent_name self.data['devicename'] = devicename # Create an object for API interaction self._api = ReferenceSampleAPI(config) # Create the cache directory self.cache_dir = config.agent_cache_directory() if os.path.exists(self.cache_dir) is False: os.mkdir(self.cache_dir) # All cache files created by this agent will end with this suffix. devicehash = general.hashstring(self.data['devicename'], sha=1) self.cache_suffix = ('%s_%s.json') % (id_agent, devicehash)
def test_valid(self): """Testing function valid.""" # Initialize key variables devicename = general.hashstring(general.randomstring()) id_agent = general.hashstring(general.randomstring()) agent_name = general.hashstring(general.randomstring()) hosthash = general.hashstring(devicename) timestamp = general.normalized_timestamp() # Create a directory and filename for testing directory = tempfile.mkdtemp() filename = ('%s/%s_%s_%s.json') % ( directory, timestamp, id_agent, hosthash) # Test with good data data = { 'id_agent': id_agent, 'agent': agent_name, 'devicename': devicename, 'timestamp': timestamp } with open(filename, 'w') as f_handle: json.dump(data, f_handle) result = validate._CheckFile(filename) self.assertEqual(result.valid(), True) # Test with bad data (missing id_agent value) data = { 'id_agent': '', 'agent': agent_name, 'devicename': devicename, 'timestamp': timestamp } with open(filename, 'w') as f_handle: json.dump(data, f_handle) result = validate._CheckFile(filename) self.assertEqual(result.valid(), False) # Cleanup os.remove(filename)
def post(self, save=True, data=None): """Post data to central server. Args: save: When True, save data to cache directory if postinf fails data: Data to post. If None, then uses self.data Returns: success: "True: if successful """ # Initialize key variables success = False response = False timestamp = self.data['timestamp'] id_agent = self.data['id_agent'] # Create data to post if data is None: data = self.data # Post data save to cache if this fails try: result = requests.post(self.url, json=data) response = True except: if save is True: # Create a unique very long filename to reduce risk of devicehash = general.hashstring(self.data['devicename'], sha=1) filename = ('%s/%s_%s_%s.json') % (self.cache_dir, timestamp, id_agent, devicehash) # Save data with open(filename, 'w') as f_handle: json.dump(data, f_handle) # Define success if response is True: if result.status_code == 200: success = True # Log message if success is True: log_message = ('Agent "%s" successfully contacted server %s' '') % (self.name(), self.url) log.log2info(1027, log_message) else: log_message = ('Agent "%s" failed to contact server %s' '') % (self.name(), self.url) log.log2warning(1028, log_message) # Return return success
def setup_db_IDXConfiguration(): """Create the database for Configuration table testing. Args: None Returns: None """ # Initialize key variables idx_configuration = 1 # Get Configuration config_key = general.hashstring('_INFOSET_TEST_') config_value = general.hashstring('_INFOSET_TEST_VALUE_') # Create a dict of all the expected values expected = { 'config_key': config_key, 'config_value': config_value, 'idx_configuration': idx_configuration, 'enabled': 1, 'exists': True } # Drop the database and create tables initialize_db() # Insert data into database data = Configuration( config_key=general.encode(expected['config_key']), config_value=general.encode(expected['config_value']), enabled=expected['enabled']) database = db.Database() database.add_all([data], 1045) # Return return expected
def receive(id_agent): """Function for handling /infoset/api/v1.0/receive/<id_agent> route. Args: id_agent: Unique Identifier of an Infoset Agent Returns: Text response of Received """ # Initialize key variables found_count = 0 # Read configuration config = configuration.Config() cache_dir = config.ingest_cache_directory() # Get JSON from incoming agent POST data = request.json # Make sure all the important keys are available keys = ['timestamp', 'id_agent', 'devicename'] for key in keys: if key in data: found_count += 1 # Do processing if found_count == 3: # Extract key values from posting try: timestamp = int(data['timestamp']) except: abort(404) id_agent = data['id_agent'] devicename = data['devicename'] # Create a hash of the devicename device_hash = general.hashstring(devicename, sha=1) json_path = ('%s/%s_%s_%s.json') % (cache_dir, timestamp, id_agent, device_hash) with open(json_path, "w+") as temp_file: json.dump(data, temp_file) # Return return 'OK' else: abort(404)
def _test_get_id_agent(agent_name): """SAMPLE - Create a permanent UID for the agent. Args: config: ConfigAgent configuration object Returns: id_agent: UID for agent """ # Initialize key variables prefix = ('_INFOSET_TEST_%s') % (agent_name) id_agent = general.hashstring(prefix) # Return return id_agent
def receive(id_agent): """Function for handling /infoset/api/v1.0/receive/<id_agent> route. Args: id_agent: Unique Identifier of an Infoset Agent Returns: Text response of Received """ # Initialize key variables found_count = 0 # Read configuration cache_dir = CONFIG.ingest_cache_directory() # Get JSON from incoming agent POST data = request.json # Make sure all the important keys are available keys = ['timestamp', 'id_agent', 'devicename'] for key in keys: if key in data: found_count += 1 # Do processing if found_count == 3: # Extract key values from posting try: timestamp = int(data['timestamp']) except: abort(404) id_agent = data['id_agent'] devicename = data['devicename'] # Create a hash of the devicename device_hash = general.hashstring(devicename, sha=1) redis_key = ('%s-%s-%s') % (timestamp, id_agent, device_hash) REDIS.set(redis_key, data) result = process_cache.delay(redis_key) # Return return 'OK' else: abort(404)
def _generate_id_agent(): """Generate a UID. Args: None Returns: id_agent: the UID """ # Create a UID and save prehash = ('%s%s%s%s%s') % (random(), random(), random(), random(), time.time()) id_agent = general.hashstring(prehash) # Return return id_agent
def test_purge(self): """Testing function purge.""" directory = tempfile.mkdtemp() filepath = ('%s/%s_%s_%s.json') % ( directory, self.data['timestamp'], self.data['id_agent'], general.hashstring(self.data['devicename'])) with open(filepath, 'w') as f_handle: json.dump(self.data, f_handle) # Create a valid Drain object ingest = drain.Drain(filepath) # Test self.assertEqual(os.path.exists(filepath), True) self.assertEqual(os.path.isfile(filepath), True) ingest.purge() self.assertEqual(os.path.exists(filepath), False) self.assertEqual(os.path.isfile(filepath), False)
def generate_id_agent(): """SAMPLE - Generate an id_agent. Args: None Returns: id_agent: the UID """ ######################################################################### ######################################################################### # NOTE: In production you'd need to generate the id_agent value from # a random string ######################################################################### ######################################################################### id_agent = general.hashstring('_INFOSET_TEST_') # Return return id_agent
def _id_datapoint(id_agent, label, index, agent_name, devicename): """Create a unique DID from ingested data. Args: id_agent: Identifier of device that created the cache data file label: Label of the data index: Index of the data agent_name: Name of agent devicename: Devicename Returns: id_datapoint: Datapoint ID """ # Initialize key variables prehash = ('%s%s%s%s%s') % (id_agent, label, index, agent_name, devicename) result = general.hashstring(prehash) id_datapoint = result # Return return id_datapoint
class TestDrain(unittest.TestCase): """Checks all functions and methods.""" # Initialize key variables setup = unittest_setup.TestVariables() data = setup.cache_data() # Create valid file filled with valid data directory = tempfile.mkdtemp() filepath = ('%s/%s_%s_%s.json') % ( directory, data['timestamp'], data['id_agent'], general.hashstring(data['devicename'])) with open(filepath, 'w') as f_handle: json.dump(data, f_handle) # Create a valid Drain object ingest = drain.Drain(filepath) @classmethod def tearDownClass(cls): """Clean up when all over.""" # Delete unnecessary files os.remove(cls.filepath) def test___init__(self): """Testing function __init__.""" pass def test_valid(self): """Testing function valid.""" # Test result = self.ingest.valid() self.assertEqual(result, True) def test_id_agent(self): """Testing function id_agent.""" # Test result = self.ingest.id_agent() self.assertEqual(result, self.data['id_agent']) def test_timestamp(self): """Testing function timestamp.""" # Test result = self.ingest.timestamp() self.assertEqual(result, self.data['timestamp']) def test_agent(self): """Testing function agent.""" # Test result = self.ingest.agent() self.assertEqual(result, self.data['agent']) def test_devicename(self): """Testing function devicename.""" # Test result = self.ingest.devicename() self.assertEqual(result, self.data['devicename']) def test_counter32(self): """Testing function counter32.""" # Initialize key variables datapoints = _expected(self.data, 32) found = 0 # Test results = self.ingest.counter32() for datapoint in datapoints: for result in results: if result['id_datapoint'] == datapoint['id_datapoint']: self.assertEqual( result['timestamp'], datapoint['timestamp']) self.assertEqual( result['value'], datapoint['value']) self.assertEqual( result['id_agent'], datapoint['id_agent']) # Increment found found += 1 # Make sure that all are found self.assertEqual(len(results), len(datapoints)) self.assertEqual(len(results), found) def test_counter64(self): """Testing function counter64.""" # Initialize key variables datapoints = _expected(self.data, 64) found = 0 # Test results = self.ingest.counter64() for datapoint in datapoints: for result in results: if result['id_datapoint'] == datapoint['id_datapoint']: self.assertEqual( result['timestamp'], datapoint['timestamp']) self.assertEqual( result['value'], datapoint['value']) self.assertEqual( result['id_agent'], datapoint['id_agent']) # Increment found found += 1 # Make sure that all are found self.assertEqual(len(results), len(datapoints)) self.assertEqual(len(results), found) def test_floating(self): """Testing function floating.""" # Initialize key variables datapoints = _expected(self.data, 1) found = 0 # Test results = self.ingest.floating() for datapoint in datapoints: for result in results: if result['id_datapoint'] == datapoint['id_datapoint']: self.assertEqual( result['timestamp'], datapoint['timestamp']) self.assertEqual( result['value'], datapoint['value']) self.assertEqual( result['id_agent'], datapoint['id_agent']) # Increment found found += 1 # Make sure that all are found self.assertEqual(len(results), len(datapoints)) self.assertEqual(len(results), found) def test_timeseries(self): """Testing function timeseries.""" # Initialize key variables datapoints = [] found = 0 # Populate datapoints list datapoints.extend(_expected(self.data, 1)) datapoints.extend(_expected(self.data, 32)) datapoints.extend(_expected(self.data, 64)) # Test results = self.ingest.timeseries() for datapoint in datapoints: for result in results: if result['id_datapoint'] == datapoint['id_datapoint']: self.assertEqual( result['timestamp'], datapoint['timestamp']) self.assertEqual( result['value'], datapoint['value']) self.assertEqual( result['id_agent'], datapoint['id_agent']) # Increment found found += 1 # Make sure that all are found self.assertEqual(len(results), len(datapoints)) self.assertEqual(len(results), found) def test_timefixed(self): """Testing function timefixed.""" # Initialize key variables datapoints = _expected(self.data, None) found = 0 # Test results = self.ingest.timefixed() for datapoint in datapoints: for result in results: if result['id_datapoint'] == datapoint['id_datapoint']: self.assertEqual( result['timestamp'], datapoint['timestamp']) self.assertEqual( result['value'], datapoint['value']) self.assertEqual( result['id_agent'], datapoint['id_agent']) # Increment found found += 1 # Make sure that all are found self.assertEqual(len(results), len(datapoints)) self.assertEqual(len(results), found) def test_sources(self): """Testing function sources.""" # Initialize key variables sources = _sources(self.data) found = 0 # Test results = self.ingest.sources() for source in sources: for result in results: if result['id_datapoint'] == source['id_datapoint']: self.assertEqual( result['id_agent'], source['id_agent']) self.assertEqual( result['agent_label'], source['agent_label']) self.assertEqual( result['agent_source'], source['agent_source']) self.assertEqual( result['description'], source['description']) self.assertEqual( result['base_type'], source['base_type']) # Increment found found += 1 # Make sure that all are found self.assertEqual(len(results), len(sources)) self.assertEqual(len(results), found) def test_purge(self): """Testing function purge.""" directory = tempfile.mkdtemp() filepath = ('%s/%s_%s_%s.json') % ( directory, self.data['timestamp'], self.data['id_agent'], general.hashstring(self.data['devicename'])) with open(filepath, 'w') as f_handle: json.dump(self.data, f_handle) # Create a valid Drain object ingest = drain.Drain(filepath) # Test self.assertEqual(os.path.exists(filepath), True) self.assertEqual(os.path.isfile(filepath), True) ingest.purge() self.assertEqual(os.path.exists(filepath), False) self.assertEqual(os.path.isfile(filepath), False)
def test__keys_in_filename(self): """Testing function _keys_in_filename.""" # Initialize key variables id_agent = general.hashstring(general.randomstring()) hosthash = general.hashstring(general.randomstring()) timestamp = general.normalized_timestamp() # Create a directory and filename for testing directory = tempfile.mkdtemp() filename = ('%s/%s_%s_%s.json') % (directory, timestamp, id_agent, hosthash) # Test with good data data = {'id_agent': id_agent, 'timestamp': timestamp} with open(filename, 'w') as f_handle: json.dump(data, f_handle) result = validate._CheckFile(filename) self.assertEqual(result._keys_in_filename(), True) # Test with bad data (missing id_agent value) data = {'id_agent': '', 'timestamp': timestamp} with open(filename, 'w') as f_handle: json.dump(data, f_handle) result = validate._CheckFile(filename) self.assertEqual(result._keys_in_filename(), False) # Test with bad data (mismatched timestamp value) data = {'id_agent': id_agent, 'timestamp': 0} with open(filename, 'w') as f_handle: json.dump(data, f_handle) result = validate._CheckFile(filename) self.assertEqual(result._keys_in_filename(), False) # Test with bad data (string timestamp value) data = {'id_agent': id_agent, 'timestamp': str(timestamp)} with open(filename, 'w') as f_handle: json.dump(data, f_handle) result = validate._CheckFile(filename) self.assertEqual(result._keys_in_filename(), False) # Test with bad data (string timestamp value) data = {'id_agent': id_agent, 'timestamp': str(timestamp)} with open(filename, 'w') as f_handle: json.dump(data, f_handle) result = validate._CheckFile(filename) self.assertEqual(result._keys_in_filename(), False) # Test with bad data # (non normalized timestamp value in filename and data) bad_time = timestamp + 1 bad_filename = ('%s/%s_%s_%s.json') % (directory, bad_time, id_agent, hosthash) data = {'id_agent': id_agent, 'timestamp': str(timestamp)} with open(bad_filename, 'w') as f_handle: json.dump(data, f_handle) result = validate._CheckFile(bad_filename) self.assertEqual(result._keys_in_filename(), False) # Cleanup # Get all files in directory filenames = [ filename for filename in os.listdir(directory) if os.path.isfile(os.path.join(directory, filename)) ] # Get the full filepath for the cache file and post for filename in filenames: filepath = os.path.join(directory, filename) os.remove(filepath)
def test_hashstring(self): """Create a UTF encoded SHA hash string.""" # Initialize key variables test_string = 'banana' test_string_encoded = bytes(test_string.encode()) hasher = hashlib.sha256() hasher.update(test_string_encoded) expected = hasher.hexdigest() result = general.hashstring(test_string) self.assertEqual(result, expected) hasher = hashlib.sha512() hasher.update(test_string_encoded) expected = hasher.hexdigest() result = general.hashstring(test_string, sha=512) self.assertEqual(result, expected) result = general.hashstring(test_string, sha=512, utf8=True) self.assertEqual(result, expected.encode()) hasher = hashlib.sha256() hasher.update(test_string_encoded) expected = hasher.hexdigest() result = general.hashstring(test_string, sha=256) self.assertEqual(result, expected) result = general.hashstring(test_string, sha=256, utf8=True) self.assertEqual(result, expected.encode()) hasher = hashlib.sha224() hasher.update(test_string_encoded) expected = hasher.hexdigest() result = general.hashstring(test_string, sha=224) self.assertEqual(result, expected) result = general.hashstring(test_string, sha=224, utf8=True) self.assertEqual(result, expected.encode()) hasher = hashlib.sha384() hasher.update(test_string_encoded) expected = hasher.hexdigest() result = general.hashstring(test_string, sha=384) self.assertEqual(result, expected) result = general.hashstring(test_string, sha=384, utf8=True) self.assertEqual(result, expected.encode()) hasher = hashlib.sha1() hasher.update(test_string_encoded) expected = hasher.hexdigest() result = general.hashstring(test_string, sha=1) self.assertEqual(result, expected) result = general.hashstring(test_string, sha=1, utf8=True) self.assertEqual(result, expected.encode())
def __init__(self): """Method initializing the class.""" # Initialize key variables self.data = {} self.data['idx_datapoint'] = 1 self.data['idx_agentname'] = 1 self.data['idx_billcode'] = 1 self.data['idx_device'] = 1 self.data['idx_deviceagent'] = 1 self.data['idx_department'] = 1 self.data['timestamp'] = general.normalized_timestamp() self.data['id_datapoint'] = general.hashstring(general.randomstring()) self.data['devicename'] = general.hashstring(general.randomstring()) self.data['id_agent'] = general.hashstring(general.randomstring()) self.data['id_datapoint'] = general.hashstring(general.randomstring()) self.data['devicename'] = general.hashstring(general.randomstring()) self.data['device_description'] = general.hashstring( general.randomstring()) self.data['agent_name'] = general.hashstring(general.randomstring()) self.data['agent_source'] = general.hashstring(general.randomstring()) self.data['agent_label'] = general.hashstring(general.randomstring()) self.data['department_code'] = general.hashstring( general.randomstring()) self.data['department_name'] = general.hashstring( general.randomstring()) self.data['billcode_code'] = general.hashstring(general.randomstring()) self.data['billcode_name'] = general.hashstring(general.randomstring()) # Drop the database and create tables initialize_db() # Initialize agent variables agent_data = {} agent_data['devicename'] = self.data['devicename'] agent_data['device_description'] = self.data['device_description'] agent_data['id_agent'] = self.data['id_agent'] agent_data['agent'] = self.data['agent_name'] agent_data['timestamp'] = self.data['timestamp'] (self.data['idx_device'], self.data['idx_agent']) = _setup_db_deviceagent(agent_data) # Get DeviceAgent index value deviceagent = hagent.GetDeviceAgent(self.data['idx_device'], self.data['idx_agent']) self.data['idx_deviceagent'] = deviceagent.idx_deviceagent() # Insert Department data into database dept_data = Department(name=self.data['department_name'].encode(), code=self.data['department_code'].encode()) database = db.Database() database.add_all([dept_data], 1035) # Insert Billcode data into database bill_data = Billcode(name=self.data['billcode_name'].encode(), code=self.data['billcode_code'].encode()) database = db.Database() database.add_all([bill_data], 1039) # Insert Datapoint data into database new_data = Datapoint(agent_source=self.data['agent_source'].encode(), agent_label=self.data['agent_label'].encode(), last_timestamp=self.data['timestamp'], idx_deviceagent=self.data['idx_deviceagent'], id_datapoint=self.data['id_datapoint'].encode()) database = db.Database() database.add_all([new_data], 1072)
def __init__(self): """Method initializing the class.""" # Initialize key variables self.data = {} self.data['idx_datapoint'] = 1 self.data['idx_agentname'] = 1 self.data['idx_billcode'] = 1 self.data['idx_device'] = 1 self.data['idx_deviceagent'] = 1 self.data['idx_department'] = 1 self.data['timestamp'] = general.normalized_timestamp() self.data['last_timestamp'] = general.normalized_timestamp() self.data['devicename'] = general.hashstring(general.randomstring()) self.data['id_agent'] = general.hashstring(general.randomstring()) self.data['id_datapoint'] = general.hashstring(general.randomstring()) self.data['devicename'] = general.hashstring(general.randomstring()) self.data['device_description'] = general.hashstring( general.randomstring()) self.data['agent'] = general.hashstring(general.randomstring()) self.data['agent_source'] = general.hashstring(general.randomstring()) self.data['agent_label'] = general.hashstring(general.randomstring()) self.data['department_code'] = general.hashstring( general.randomstring()) self.data['department_name'] = general.hashstring( general.randomstring()) self.data['billcode_code'] = general.hashstring( general.randomstring()) self.data['billcode_name'] = general.hashstring( general.randomstring()) # Define data to Insert self.data['values'] = [] for timestamp in _timestamps(): value_dict = { 'idx_datapoint': self.data['idx_datapoint'], 'value': timestamp * (1 + random.uniform(0, 1)), 'timestamp': timestamp} self.data['values'].append(value_dict) # Drop the database and create tables initialize_db() # Initialize agent variables agent_data = {} agent_data['devicename'] = self.data['devicename'] agent_data['device_description'] = self.data['device_description'] agent_data['id_agent'] = self.data['id_agent'] agent_data['agent'] = self.data['agent'] agent_data['timestamp'] = self.data['timestamp'] ( self.data['idx_device'], self.data['idx_agent']) = _setup_db_deviceagent(agent_data) # Get DeviceAgent index value deviceagent = hagent.GetDeviceAgent( self.data['idx_device'], self.data['idx_agent']) self.data['idx_deviceagent'] = deviceagent.idx_deviceagent() # Insert Department data into database dept_data = Department( name=self.data['department_name'].encode(), code=self.data['department_code'].encode() ) database = db.Database() database.add_all([dept_data], 1035) # Insert Billcode data into database bill_data = Billcode( name=self.data['billcode_name'].encode(), code=self.data['billcode_code'].encode() ) database = db.Database() database.add_all([bill_data], 1039) # Insert Datapoint data into database new_data = Datapoint( agent_source=self.data['agent_source'].encode(), agent_label=self.data['agent_label'].encode(), last_timestamp=self.data['last_timestamp'], idx_deviceagent=self.data['idx_deviceagent'], id_datapoint=self.data['id_datapoint'].encode()) database = db.Database() database.add_all([new_data], 1144) # Insert timeseries data into database new_data_list = [] for item in self.data['values']: new_data_list.append( Data( idx_datapoint=item['idx_datapoint'], timestamp=item['timestamp'], value=item['value'])) database = db.Database() database.add_all(new_data_list, 1072)
def test___init__(self): """Testing function __init__.""" # Initialize key variables directory = tempfile.mkdtemp() id_agent = self.data['id_agent'] last_timestamp = self.data['timestamp'] filepath = ('%s/%s_%s_%s.json') % ( directory, last_timestamp, id_agent, general.hashstring(general.randomstring())) # Drop the database and create tables unittest_db.initialize_db() # Test with valid data result = validate.ValidateCache(data=self.data) self.assertEqual(result.valid(), True) # Test with invalid data (string) result = validate.ValidateCache(data='string') self.assertEqual(result.valid(), False) # Test with invalid data (string) data_dict = copy.deepcopy(self.data) data_dict.pop('agent', None) result = validate.ValidateCache(data=data_dict) self.assertEqual(result.valid(), False) # Write good data to file and test with open(filepath, 'w') as f_handle: json.dump(self.data, f_handle) result = validate.ValidateCache(filepath=filepath) self.assertEqual(result.valid(), True) ################################################################# ################################################################# # Add record to DeviceAgent table and test for validity with the # same data, which should be False ################################################################# ################################################################# unittest_db.setup_db_deviceagent(self.data) # Attempting to insert duplicate data should fail with open(filepath, 'w') as f_handle: json.dump(self.data, f_handle) result = validate.ValidateCache(filepath=filepath) self.assertEqual(result.valid(), False) ################################################################# ################################################################# # Test with invalid data in file ################################################################# ################################################################# # Drop the database and create tables unittest_db.initialize_db() # Write bad data to file and test data_dict = copy.deepcopy(self.data) data_dict.pop('agent', None) with open(filepath, 'w') as f_handle: json.dump(data_dict, f_handle) result = validate.ValidateCache(filepath=filepath) self.assertEqual(result.valid(), False) # Cleanup os.remove(filepath) os.removedirs(directory)