You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_all_learnware.py 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import json
  2. import os
  3. import tempfile
  4. import unittest
  5. from learnware.client import LearnwareClient
  6. from learnware.market import BaseUserInfo
  7. from learnware.specification import generate_semantic_spec
  8. class TestAllLearnware(unittest.TestCase):
  9. client = LearnwareClient()
  10. @classmethod
  11. def setUpClass(cls) -> None:
  12. config_path = os.path.join(os.path.dirname(__file__), "config.json")
  13. if not os.path.exists(config_path):
  14. data = {"email": None, "token": None}
  15. with open(config_path, "w") as file:
  16. json.dump(data, file)
  17. with open(config_path, "r") as file:
  18. data = json.load(file)
  19. email = data.get("email")
  20. token = data.get("token")
  21. if email is None or token is None:
  22. print("Please set email and token in config.json.")
  23. else:
  24. cls.client.login(email, token)
  25. def _skip_test(self):
  26. if not self.client.is_login():
  27. print("Client does not login!")
  28. return True
  29. return False
  30. def test_all_learnware(self):
  31. if not self._skip_test():
  32. semantic_spec = generate_semantic_spec()
  33. user_info = BaseUserInfo(semantic_spec=semantic_spec, stat_info={})
  34. result = self.client.search_learnware(user_info, page_size=None)
  35. learnware_ids = result["single"]["learnware_ids"]
  36. keys = [key for key in result["single"]["semantic_specifications"][0]]
  37. print(f"result size: {len(learnware_ids)}")
  38. print(f"key in result: {keys}")
  39. failed_ids = []
  40. with tempfile.TemporaryDirectory(prefix="learnware_") as tempdir:
  41. for idx in learnware_ids:
  42. zip_path = os.path.join(tempdir, f"test_{idx}.zip")
  43. self.client.download_learnware(idx, zip_path)
  44. try:
  45. semantic_spec = self.client.get_semantic_specification(idx)
  46. LearnwareClient.check_learnware(zip_path, semantic_spec)
  47. print(f"check learnware {idx} succeed")
  48. except Exception:
  49. failed_ids.append(idx)
  50. print(f"check learnware {idx} failed!!!")
  51. print(f"The currently failed learnware ids: {failed_ids}")
  52. def suite():
  53. _suite = unittest.TestSuite()
  54. _suite.addTest(TestAllLearnware("test_all_learnware"))
  55. return _suite
  56. if __name__ == "__main__":
  57. runner = unittest.TextTestRunner()
  58. runner.run(suite())