def test_prune(self): with get_session(bind=get_engine("sqlite:///:memory:")) as session: session.add(CpuPercent(core_id=0, mean=2)) session.add(CpuPercent(core_id=0, mean=3)) session.add(CpuPercent(core_id=1, mean=4)) session.add(CpuPercent(core_id=1, mean=4)) session.add(CpuPercent(core_id=1, mean=5)) with get_session(bind=get_engine("sqlite:///:memory:")) as session: metrics = list(session.query(CpuPercent)) core0, core1 = CpuPercent.prune(metrics) self.assertAlmostEqual(core0.mean, statistics.mean((2, 3)), places=10) self.assertAlmostEqual(core0.variance, statistics.pvariance((2, 3)), places=10) self.assertEqual(core0.sample_count, 2) self.assertAlmostEqual(core1.mean, statistics.mean((4, 4, 5)), places=10) self.assertAlmostEqual(core1.variance, statistics.pvariance((4, 4, 5)), places=10) self.assertEqual(core1.sample_count, 3)
def test_Polymorphism(self): # Put some data into the db with get_session(bind=get_engine("sqlite:///:memory:")) as session: cpu = CPU( manufacturer="AMD", model="Ryzen 3800X", isa="x86-64", tdp=65, core_count=12, threads_per_core=2, nominal_frequency=3.0, maximum_frequency=4.2, ) session.add(cpu) with get_session(bind=get_engine("sqlite:///:memory:")) as session: cpus = session.query(CPU) cpu = cpus[0] self.assertEqual(cpu.manufacturer, "AMD") self.assertEqual(cpu.model, "Ryzen 3800X") self.assertEqual(cpu.isa, "x86-64") self.assertEqual(cpu.tdp, 65) self.assertEqual(cpu.core_count, 12) self.assertEqual(cpu.threads_per_core, 2) self.assertEqual(cpu.nominal_frequency, 3.0) self.assertEqual(cpu.maximum_frequency, 4.2)
def __init__(self, database_uri, data_generator): # Initialize the databse self.database_uri = database_uri self.data_generator = data_generator self.engine = get_engine(self.database_uri) self.session = sessionmaker(bind=self.engine)() ORMBase.metadata.create_all(self.session.bind)
def create_database_engine(args): if args.database_uri: return get_engine(args.database_uri) if 'DATABASE_URI' in os.environ: print(f" * Connecting to database {os.environ.get('DATABASE_URI')}") return get_engine(os.environ.get('DATABASE_URI')) # If the fully resolved database URI is not provided, use the credentials and connect to localhost database_username = args.database_username if args.database_username else os.environ.get('POSTGRES_USER', None) database_password = args.database_password if args.database_password else os.environ.get('POSTGRES_PASSWORD', None) if (database_password is None) or (database_username is None): logging.error("Error when connecting to the database, database user or password not found.") sys.exit(1) else: database_uri = f"postgresql://{database_username}:{database_password}@127.0.0.1:5432/taskobra" print(f" * Connecting to database {database_uri}") return get_engine(database_uri)
def test_query_UserRole(self): # Put some data into the db with get_session(bind=get_engine("sqlite:///:memory:")) as session: user = User(name="Fred") administrator = Role(name="Administrator") reporter = Role(name="Reporter") observer = Role(name="Observer") user.roles.append(administrator) user.roles.append(reporter) user.roles.append(observer) session.add(user) # Query the db and make sure everything lines up with get_session(bind=get_engine("sqlite:///:memory:")) as session: users = session.query(User) roles = session.query(Role) # Make sure there are 3 roles in the roles table self.assertEqual(len(list(roles)), 3) # Make sure there are 3 roles in the user's roles list self.assertEqual(len(users[0].roles), 3) # Make sure they're the same 3 roles for role in users[0].roles: self.assertIn(role, roles) for role in roles: self.assertIn(role, users[0].roles)
def test_add_component(self): # Put some data into the db with get_session(bind=get_engine("sqlite:///:memory:")) as session: cpu = CPU( manufacturer="AMD", model="Ryzen 3800X", isa="x86-64", tdp=105, core_count=8, threads_per_core=2, nominal_frequency=3.9, maximum_frequency=4.5, ) gpu = GPU( manufacturer="NVIDIA", model="1070", architecture="CUDA", tdp=105, core_count=1920, memory=8.0, ) memory = Memory( manufacturer="G-Skill", model="Trident", standard="DDR4", capacity=16.0, frequency=3600, cas_latency=16, t_rcd=19, t_rp=19, t_ras=39, ) storage = Storage( manufacturer="Sabrent", model="Rocket", standard="NVMe PCIe 4.0", capacity=500.0, max_read=5000, max_write=2500, ) os = OperatingSystem( developer="Microsoft", name="Windows 10", ) system = System(name="Some System") system.add_component(cpu) system.add_component(gpu) system.add_component(memory) system.add_component(memory) system.add_component(storage) system.add_component(os) session.add(system) with get_session(bind=get_engine("sqlite:///:memory:")) as session: systems = session.query(System) self.assertEqual(len(list(systems)), 1) system = systems[0] cpu = session.query(CPU).first() gpu = session.query(GPU).first() memory = session.query(Memory).first() storage = session.query(Storage).first() os = session.query(OperatingSystem).first() # Check that our system object is composed correctly self.assertEqual(system.name, "Some System") self.assertEqual(len(list(system.components)), 6) self.assertIn((1, cpu), system.components) self.assertIn((1, gpu), system.components) self.assertIn((2, memory), system.components) self.assertIn((1, storage), system.components) self.assertIn((1, os), system.components)
def tearDown(self): with get_session(bind=get_engine("sqlite:///:memory:")) as session: for table in session.execute( "SELECT * FROM sqlite_master WHERE type='table'"): session.execute(f"DELETE FROM '{table[1]}'")