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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. class TestAllLearnware(unittest.TestCase):
  9. def setUp(self):
  10. unittest.TestCase.setUpClass()
  11. dir_path = os.path.dirname(__file__)
  12. config_path = os.path.join(dir_path, "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["email"]
  20. token = data["token"]
  21. if email is None or token is None:
  22. raise ValueError("Please set email and token in config.json.")
  23. self.client = LearnwareClient()
  24. self.client.login(email, token)
  25. def test_all_learnware(self):
  26. max_learnware_num = 1000
  27. semantic_spec = dict()
  28. semantic_spec["Data"] = {"Type": "Class", "Values": []}
  29. semantic_spec["Task"] = {"Type": "Class", "Values": []}
  30. semantic_spec["Library"] = {"Type": "Class", "Values": []}
  31. semantic_spec["Scenario"] = {"Type": "Tag", "Values": []}
  32. semantic_spec["Name"] = {"Type": "String", "Values": ""}
  33. semantic_spec["Description"] = {"Type": "String", "Values": ""}
  34. specification = Specification(semantic_spec=semantic_spec)
  35. result = self.client.search_learnware(specification, page_size=max_learnware_num)
  36. print(f"result size: {len(result)}")
  37. print(f"key in result: {[key for key in result[0]]}")
  38. failed_ids = []
  39. learnware_ids = [res["learnware_id"] for res in result]
  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. with zipfile.ZipFile(zip_path, "r") as zip_file:
  45. with zip_file.open("semantic_specification.json") as json_file:
  46. semantic_spec = json.load(json_file)
  47. try:
  48. LearnwareClient.check_learnware(zip_path, semantic_spec)
  49. print(f"check learnware {idx} succeed")
  50. except:
  51. failed_ids.append(idx)
  52. print(f"check learnware {idx} failed!!!")
  53. print(f"The currently failed learnware ids: {failed_ids}")
  54. if __name__ == "__main__":
  55. unittest.main()