def test_fake_device(): '''Purely tests whether the helper function works''' # Create a fake device with fakeDevice() as f: assert isinstance(f['index'], int) assert isinstance(f['device'], NetworkDevice)
def test_time_in_sql(): ''' Test whether updating the time works when you manually update it with execute SQL. This was done because, for some reason, the 'set_dependents_as_updated' method was failing while this wasn't. (Ended up that I had forgotten to actually update the devices table among everything else...) ''' with fakeDevice() as f: db= io_sql.device_db() record= db.get_device_record('device_id', f['index']) oldtime= record['updated'] print(oldtime) del(db) sleep(.5) db= io_sql.device_db() db.execute_sql(''' UPDATE devices SET updated = now() where device_id = %s; ''', (f['index'], ), fetch= False) record2= db.get_device_record('device_id', f['index']) print('Index: ', f['index']) print('New Time: ', record2['updated']) assert oldtime != record2['updated']
def test_devicedb_get_record(): '''The SQL database columns should match up with the names of attributes in the base network device_class''' db= device_db() # Create a fake device and add it to the database with fakeDevice() as f: record= db.get_device_record('device_id', f['index']) assert isinstance(record, psycopg2.extras.DictRow) , 'Record is type [{}]'.format(type(record)) print('Database Columns:\n', [k for k in record.keys()]) print('Class Attributes:\n', [item for item in dir(f['device']) if not item.startswith("__")]) # Make sure that the records in the database have a matching class variable # and that the values are the same for k, v in record.items(): # We dont care about 'updated' if k == 'updated': continue assert hasattr(f['device'], k) assert getattr(f['device'], k) == v
def test_fake_device(): db = io_sql.device_db() with fakeDevice() as f: assert db.exists(device_id=f['index']) assert not db.exists(device_id=f['index'])
def test_update_device(): with fakeDevice() as f: db= io_sql.device_db() record= db.get_device_record('device_id', f['index']) assert f['device'].device_name == record['device_name'] # Change the device name f['device'].device_name= 'something' db.update_device_entry(f['device'], device_id= f['index']) record= db.get_device_record('device_id', f['index']) assert f['device'].device_name == 'something'
def test_set_dependents_as_updated(): with fakeDevice() as f: db= io_sql.device_db() record= db.get_device_record('device_id', f['index']) oldtime= record['updated'] print(type(oldtime)) print(oldtime) del(db) sleep(.5) config.cc.debug= True config.cc.verbosity= 6 db= io_sql.device_db() db.set_dependents_as_updated(device_id= f['index']) newrecord= db.get_device_record('device_id', f['index']) print(type(newrecord['updated'])) print(newrecord['updated']) assert oldtime != newrecord['updated']