From 86315f25a0555bf0324bb06b05da35c794503332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=B5=E3=81=81?= Date: Sun, 30 Jul 2023 03:17:47 +0900 Subject: [PATCH] add test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ふぁ --- .github/workflows/test-python.yaml | 99 +++++++++++++++++++++++ .gitignore | 5 +- .vscode/settings.json | 2 +- dist/compatible/paths/profile.yaml | 1 - dist/compatible/paths/tweet.yaml | 4 +- dist/compatible/schemas/content.yaml | 1 - dist/docs/paths/profile.yaml | 1 - dist/docs/paths/tweet.yaml | 4 +- dist/docs/schemas/content.yaml | 1 - src/openapi/paths/profile.yaml | 2 +- src/openapi/paths/tweet.yaml | 4 +- src/openapi/schemas/content.yaml | 2 +- test/local.sh | 6 ++ test/python/login.py | 12 +++ test/python/openapi-generator-config.yaml | 8 ++ test/python/test_serialize.py | 71 ++++++++++++++++ 16 files changed, 209 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/test-python.yaml create mode 100644 test/local.sh create mode 100644 test/python/login.py create mode 100644 test/python/openapi-generator-config.yaml create mode 100644 test/python/test_serialize.py diff --git a/.github/workflows/test-python.yaml b/.github/workflows/test-python.yaml new file mode 100644 index 0000000..2998899 --- /dev/null +++ b/.github/workflows/test-python.yaml @@ -0,0 +1,99 @@ +name: test-python + +on: + # auto review + pull_request: + paths-ignore: + - "test/**" + - ".github/**" + branches: + - master + +env: + TWITTER_SESSION: ${{secrets.TWITTER_SESSION}} + +jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + # Openapi Generator Setup + - name: Get OpenJDK 11 + uses: actions/setup-java@v2 + with: + distribution: "temurin" + java-version: 11 + + - name: Get Java version + run: java -version + + - name: Get Openapi Generator cache file + id: openapi-generator-cache + run: echo "file=openapi-generator-cli.jar" >> $GITHUB_OUTPUT + + - name: Openapi Generator cache + uses: actions/cache@v3 + with: + path: ${{ steps.openapi-generator-cache.outputs.file }} + key: ${{ runner.os }}-openapi-generator-${{ hashFiles('openapi-generator-cli.jar') }} + restore-keys: | + ${{ runner.os }}-openapi-generator- + + - name: Get Openapi Generator + run: | + wget https://github.com/fa0311/openapi-generator/releases/download/twitter_openapi_python_1.2.0/openapi-generator-cli.jar -O openapi-generator-cli.jar --no-verbose --show-progress --no-clobber + + # Python Setup + - name: Set up Python 3.8 + uses: actions/setup-python@v2 + with: + python-version: 3.8 + architecture: "x64" + + - name: Get Python version + run: python -V + + - name: Get pip cache dir + id: pip-cache + run: echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT + + - name: pip cache + uses: actions/cache@v3 + with: + path: ${{ steps.pip-cache.outputs.dir }} + key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + # generate + - name: Build twitter-openapi + run: | + python tools/build.py + + # clean up dependencies + - name: Clean up dependencies + run: | + pip uninstall -y -r requirements.txt + + # generate + - name: Generate Python client + run: | + java -jar openapi-generator-cli.jar generate -c test/python/openapi-generator-config.yaml -g python + + # test + - name: Test Python client + run: | + pip install ./python_generated + + - name: Run Python client test + run: | + python test/python/test_serialize.py + env: + TWITTER_SESSION: ${{secrets.TWITTER_SESSION}} diff --git a/.gitignore b/.gitignore index 7cd6f5d..5e63e49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .venv -__pycache__/ \ No newline at end of file +__pycache__/ +cookie.json +*_generated +*.jar \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index f9771f3..a19d7b4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,6 @@ "yaml.validate": true, "yaml.hover": true, "yaml.schemas": { - "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.0/schema.json": "twitter-openapi/**/*.yaml" + "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.0/schema.json": "src/**/*.yaml" } } diff --git a/dist/compatible/paths/profile.yaml b/dist/compatible/paths/profile.yaml index 329723a..d352fd5 100644 --- a/dist/compatible/paths/profile.yaml +++ b/dist/compatible/paths/profile.yaml @@ -15,7 +15,6 @@ components: UserResultByScreenName: properties: id: - pattern: ^[0-9a-zA-Z]+$ type: string result: $ref: '#/components/schemas/UserResultByScreenNameResult' diff --git a/dist/compatible/paths/tweet.yaml b/dist/compatible/paths/tweet.yaml index c310eb0..13cde7f 100644 --- a/dist/compatible/paths/tweet.yaml +++ b/dist/compatible/paths/tweet.yaml @@ -44,7 +44,7 @@ paths: /graphql/{pathQueryId}/Favoriters: get: description: get tweet favoriters - operationId: getTweetFavoriters + operationId: getFavoriters parameters: - in: path name: pathQueryId @@ -128,7 +128,7 @@ paths: /graphql/{pathQueryId}/Retweeters: get: description: get tweet retweeters - operationId: getTweetRetweeters + operationId: getRetweeters parameters: - in: path name: pathQueryId diff --git a/dist/compatible/schemas/content.yaml b/dist/compatible/schemas/content.yaml index cdcdd3f..f5b4b9a 100644 --- a/dist/compatible/schemas/content.yaml +++ b/dist/compatible/schemas/content.yaml @@ -60,7 +60,6 @@ components: ModuleItem: properties: entryId: - pattern: ^[a-z\-]+[0-9]+$ type: string item: $ref: '#/components/schemas/ModuleEntry' diff --git a/dist/docs/paths/profile.yaml b/dist/docs/paths/profile.yaml index a2236fd..708a2b8 100644 --- a/dist/docs/paths/profile.yaml +++ b/dist/docs/paths/profile.yaml @@ -15,7 +15,6 @@ components: UserResultByScreenName: properties: id: - pattern: ^[0-9a-zA-Z]+$ type: string result: $ref: '#/components/schemas/UserResultByScreenNameResult' diff --git a/dist/docs/paths/tweet.yaml b/dist/docs/paths/tweet.yaml index 51a0214..8fa8209 100644 --- a/dist/docs/paths/tweet.yaml +++ b/dist/docs/paths/tweet.yaml @@ -44,7 +44,7 @@ paths: /graphql/{pathQueryId}/Favoriters: get: description: get tweet favoriters - operationId: getTweetFavoriters + operationId: getFavoriters parameters: - in: path name: pathQueryId @@ -223,7 +223,7 @@ paths: /graphql/{pathQueryId}/Retweeters: get: description: get tweet retweeters - operationId: getTweetRetweeters + operationId: getRetweeters parameters: - in: path name: pathQueryId diff --git a/dist/docs/schemas/content.yaml b/dist/docs/schemas/content.yaml index cdcdd3f..f5b4b9a 100644 --- a/dist/docs/schemas/content.yaml +++ b/dist/docs/schemas/content.yaml @@ -60,7 +60,6 @@ components: ModuleItem: properties: entryId: - pattern: ^[a-z\-]+[0-9]+$ type: string item: $ref: '#/components/schemas/ModuleEntry' diff --git a/src/openapi/paths/profile.yaml b/src/openapi/paths/profile.yaml index 7b1a723..faf9b80 100644 --- a/src/openapi/paths/profile.yaml +++ b/src/openapi/paths/profile.yaml @@ -39,7 +39,7 @@ components: properties: id: type: string - pattern: "^[0-9a-zA-Z]+$" + # pattern: "^[0-9a-zA-Z]+$" result: $ref: "#/components/schemas/UserResultByScreenNameResult" diff --git a/src/openapi/paths/tweet.yaml b/src/openapi/paths/tweet.yaml index 658a499..e6bdef1 100644 --- a/src/openapi/paths/tweet.yaml +++ b/src/openapi/paths/tweet.yaml @@ -20,7 +20,7 @@ paths: /graphql/{pathQueryId}/Favoriters: get: - operationId: getTweetFavoriters + operationId: getFavoriters description: get tweet favoriters responses: "200": @@ -34,7 +34,7 @@ paths: /graphql/{pathQueryId}/Retweeters: get: - operationId: getTweetRetweeters + operationId: getRetweeters description: get tweet retweeters responses: "200": diff --git a/src/openapi/schemas/content.yaml b/src/openapi/schemas/content.yaml index f8f6b5f..0329367 100644 --- a/src/openapi/schemas/content.yaml +++ b/src/openapi/schemas/content.yaml @@ -95,7 +95,7 @@ components: properties: entryId: type: string - pattern: '^[a-z\-]+[0-9]+$' + # pattern: '^[a-z\-]+[0-9]+$' item: $ref: "#/components/schemas/ModuleEntry" diff --git a/test/local.sh b/test/local.sh new file mode 100644 index 0000000..0b32175 --- /dev/null +++ b/test/local.sh @@ -0,0 +1,6 @@ + + +python3 tools/build.py +java -jar openapi-generator-cli.jar generate -c test/python/openapi-generator-config.yaml -g python +python3 -m pip install ./python_generated +python3 test/python/test_serialize.py \ No newline at end of file diff --git a/test/python/login.py b/test/python/login.py new file mode 100644 index 0000000..8b83387 --- /dev/null +++ b/test/python/login.py @@ -0,0 +1,12 @@ +import json +from tweepy_authlib import CookieSessionUserHandler + +auth_handler = CookieSessionUserHandler( + screen_name=input("screen_name: "), + password=input("password: "), +) + + +cookies = auth_handler.get_cookies() + +print(json.dumps(cookies.get_dict())) diff --git a/test/python/openapi-generator-config.yaml b/test/python/openapi-generator-config.yaml new file mode 100644 index 0000000..11a4569 --- /dev/null +++ b/test/python/openapi-generator-config.yaml @@ -0,0 +1,8 @@ +inputSpec: dist/compatible/openapi-3.0.yaml +outputDir: python_generated + +packageName: python_generated +packageVersion: 0.0.1 +projectName: python_generated + +useOneOfDiscriminatorLookup: true diff --git a/test/python/test_serialize.py b/test/python/test_serialize.py new file mode 100644 index 0000000..29457d5 --- /dev/null +++ b/test/python/test_serialize.py @@ -0,0 +1,71 @@ +import json +import os +import logging +import python_generated as pt +from pathlib import Path + + +logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s") +logger = logging.getLogger("test_serialize") + +if Path("cookie.json").exists(): + with open("cookie.json", "r") as f: + cookies = json.load(f) +else: + cookies = json.loads(os.environ["TWITTER_SESSION"]) + +cookies_str = "; ".join([f"{k}={v}" for k, v in cookies.items()]) + + +with open("src/config/placeholder.json", "r") as f: + placeholder = json.load(f) + + +def getKey(snake_str): + components = snake_str.split("_") + return "".join(x.title() for x in components[1:]) + + +api_conf = pt.Configuration( + api_key={ + "ClientLanguage": "en", + "ActiveUser": "yes", + "AuthType": "OAuth2Session", + "CsrfToken": cookies["ct0"], + }, +) + +api_conf.access_token = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA" + +api_client = pt.ApiClient(configuration=api_conf, cookie=cookies_str) +api_client.user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" + +error_count = 0 + +for x in [pt.DefaultApi, pt.TweetApi, pt.UserApi, pt.UserListApi]: + for props, fn in x.__dict__.items(): + if not callable(fn): + continue + if props.startswith("__") or props.endswith("_with_http_info"): + continue + + key = getKey(props) + logger.info(f"Try: {key}") + + kwargs = {"path_query_id": placeholder[key]["queryId"]} + if placeholder[key].get("variables") is not None: + kwargs["variables"] = json.dumps(placeholder[key]["variables"]) + if placeholder[key].get("features") is not None: + kwargs["features"] = json.dumps(placeholder[key]["features"]) + if placeholder[key].get("fieldToggles") is not None: + kwargs["field_toggles"] = json.dumps(placeholder[key]["fieldToggles"]) + + try: + res = getattr(x(api_client), props)(**kwargs) + except Exception as e: + logger.error(f"{key}") + logger.error(e) + error_count += 1 + +if error_count > 0: + exit(1)