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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import os
  2. import json
  3. import zipfile
  4. import unittest
  5. import tempfile
  6. from learnware.client import LearnwareClient
  7. from learnware.specification import Specification
  8. from learnware.market import BaseUserInfo
  9. class TestAllLearnware(unittest.TestCase):
  10. def setUp(self):
  11. unittest.TestCase.setUpClass()
  12. dir_path = os.path.dirname(__file__)
  13. config_path = os.path.join(dir_path, "config.json")
  14. if not os.path.exists(config_path):
  15. data = {"email": None, "token": None}
  16. with open(config_path, "w") as file:
  17. json.dump(data, file)
  18. with open(config_path, "r") as file:
  19. data = json.load(file)
  20. email = data["email"]
  21. token = data["token"]
  22. if email is None or token is None:
  23. raise ValueError("Please set email and token in config.json.")
  24. self.client = LearnwareClient()
  25. self.client.login(email, token)
  26. def test_all_learnware(self):
  27. max_learnware_num = 1000
  28. semantic_spec = self.client.create_semantic_specification()
  29. user_info = BaseUserInfo(semantic_spec=semantic_spec, stat_info={})
  30. result = self.client.search_learnware(user_info, page_size=max_learnware_num)
  31. print(f"result size: {len(result)}")
  32. print(f"key in result: {[key for key in result[0]]}")
  33. failed_ids = []
  34. learnware_ids = [res["learnware_id"] for res in result]
  35. with tempfile.TemporaryDirectory(prefix="learnware_") as tempdir:
  36. for idx in learnware_ids:
  37. zip_path = os.path.join(tempdir, f"test_{idx}.zip")
  38. self.client.download_learnware(idx, zip_path)
  39. with zipfile.ZipFile(zip_path, "r") as zip_file:
  40. with zip_file.open("semantic_specification.json") as json_file:
  41. semantic_spec = json.load(json_file)
  42. try:
  43. LearnwareClient.check_learnware(zip_path, semantic_spec)
  44. print(f"check learnware {idx} succeed")
  45. except:
  46. failed_ids.append(idx)
  47. print(f"check learnware {idx} failed!!!")
  48. print(f"The currently failed learnware ids: {failed_ids}")
  49. if __name__ == "__main__":
  50. unittest.main()