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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os
  2. import json
  3. import zipfile
  4. import unittest
  5. import tempfile
  6. import argparse
  7. from learnware.client import LearnwareClient
  8. from learnware.specification import generate_semantic_spec
  9. from learnware.market import BaseUserInfo
  10. class TestAllLearnware(unittest.TestCase):
  11. client = LearnwareClient()
  12. @classmethod
  13. def setUpClass(cls) -> None:
  14. config_path = os.path.join(os.path.dirname(__file__), "config.json")
  15. if not os.path.exists(config_path):
  16. data = {"email": None, "token": None}
  17. with open(config_path, "w") as file:
  18. json.dump(data, file)
  19. with open(config_path, "r") as file:
  20. data = json.load(file)
  21. email = data.get("email")
  22. token = data.get("token")
  23. if email is None or token is None:
  24. print("Please set email and token in config.json.")
  25. else:
  26. cls.client.login(email, token)
  27. @unittest.skipIf(not client.is_login(), "Client doest not login!")
  28. def test_all_learnware(self):
  29. max_learnware_num = 2000
  30. semantic_spec = generate_semantic_spec()
  31. user_info = BaseUserInfo(semantic_spec=semantic_spec, stat_info={})
  32. result = self.client.search_learnware(user_info, page_size=max_learnware_num)
  33. learnware_ids = result["single"]["learnware_ids"]
  34. keys = [key for key in result["single"]["semantic_specifications"][0]]
  35. print(f"result size: {len(learnware_ids)}")
  36. print(f"key in result: {keys}")
  37. failed_ids = []
  38. with tempfile.TemporaryDirectory(prefix="learnware_") as tempdir:
  39. for idx in learnware_ids:
  40. zip_path = os.path.join(tempdir, f"test_{idx}.zip")
  41. self.client.download_learnware(idx, zip_path)
  42. with zipfile.ZipFile(zip_path, "r") as zip_file:
  43. with zip_file.open("semantic_specification.json") as json_file:
  44. semantic_spec = json.load(json_file)
  45. try:
  46. LearnwareClient.check_learnware(zip_path, semantic_spec)
  47. print(f"check learnware {idx} succeed")
  48. except:
  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())