def test_package_info(self): statuses = session.query(FcPackage.status, func.count(FcPackage.id)). \ group_by(FcPackage.status).all() expected = [] for status in statuses: expected.append(json_from_status(status)) api_r = requests.get(self.url + "info") self.assertEqual(requests.codes.ok, api_r.status_code, api_r.text) self.assertEqual(expected, api_r.json())
def verify_packages(self, api_r, page=1, per_page=25, status="", order_by="", name="", attack_mode="", descending=None): if per_page == 0: per_page = 25 if page == 0: page = 1 db_items = [] q = session.query(FcPackage) if name != "": q = q.filter(FcPackage.name.like("%" + name + "%")) if status != "": q = q.filter(FcPackage.status == PackageStatus[status].value) if attack_mode != "": q = q.filter(FcPackage.attack == attack_mode) if order_by != "": try: order_by = getattr(FcPackage, order_by) except AttributeError: self.assertEqual(400, api_r.status_code, api_r.text) return if descending: order_by = order_by.desc() q = q.order_by(order_by) else: q = q.order_by(FcPackage.id.desc()) if q.count() < (page - 1) * per_page: self.assertEqual(404, api_r.status_code, api_r.text) return packages = q.offset(per_page * (page - 1)).limit(per_page).all() self.assertEqual(requests.codes.ok, api_r.status_code, api_r.text) for p in packages: db_items.append(db_item_from_package(p)) api_json = api_r.json() self.assertEqual(api_json["per_page"], per_page) self.assertEqual(api_json["page"], page) api_items = api_json["items"] self.assertTrue(len(api_items) <= per_page, "Hosts count") print("db items:") for item in db_items: print(item) print("api items:") for item in api_items: print(item) self.assertEqual( api_json["total"], len(api_items), "total count should be same as length " "of item list") self.assertEqual( db_items, api_items, "db items:\n" + str([i for i in db_items]) + "\napi items:" + str([i for i in api_items]))
def tearDownClass(cls): hosts = session.query(Host) for host in hosts: session.delete(host) session.commit()
def verify_hosts(self, api_r, page=1, status="", order_by="", per_page=25, name="", descending=None): if per_page == 0: per_page = 25 if page == 0: page = 1 db_items = [] q = session.query(Host) if name != "": q = q.filter(Host.domain_name.like("%" + name + "%")) if status == "active": q = q.filter( Host.id.in_(session.query(FcHostActivity.boinc_host_id))) elif status == "inactive": q = q.filter( ~Host.id.in_(session.query(FcHostActivity.boinc_host_id))) if order_by != "": try: order_by = getattr(Host, order_by) except AttributeError: self.assertEqual(400, api_r.status_code, api_r.text) return if descending: order_by.desc() q = q.order_by(order_by) else: q = q.order_by(Host.id.desc()) if q.count() < (page - 1) * per_page: self.assertEqual(404, api_r.status_code, api_r.text) return hosts = q.offset(per_page * (page - 1)).limit(per_page).all() self.assertEqual(requests.codes.ok, api_r.status_code, api_r.text) for host in hosts: db_items.append(db_item_from_boinc_host(host)) api_json = api_r.json() self.assertEqual(api_json["per_page"], per_page) self.assertEqual(api_json["page"], page) api_items = api_json["items"] self.assertTrue(len(api_items) <= per_page, "Hosts count") self.assertEqual( api_json["total"], len(api_items), "total count should be same as length " "of item list") self.assertEqual( db_items, api_items, "db items:\n" + str([i for i in db_items]) + "\napi items:" + str([i for i in api_items]))