From 6dac8b872abc71a5b2c4e2d13b0b8fcb702d839a Mon Sep 17 00:00:00 2001 From: Cassius0924 <2670226747@qq.com> Date: Sat, 3 Feb 2024 13:11:02 +0800 Subject: [PATCH] =?UTF-8?q?[Test]=20=E5=88=9B=E5=BB=BA=20GitHub=20Action?= =?UTF-8?q?=20=E7=9A=84=20test.yml=20(#38)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: 创建 GitHub Action 的 test.yml * test: 修复天气命令测试错误问题 --- .github/workflows/test.yml | 33 +++++++++++++++++++++ tests/commands/test_weather/test_weather.py | 8 ++++- wechatter/commands/_commands/weather.py | 12 ++++---- 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..0e79d02 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,33 @@ +name: Wechatter + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.8 + uses: actions/setup-python@v3 + with: + python-version: "3.8" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: Copy config file + run: | + cp config.ini.example config.ini + - name: Test code + run: | + export LOG_LEVEL="CRITICAL" + python -m unittest discover -s tests diff --git a/tests/commands/test_weather/test_weather.py b/tests/commands/test_weather/test_weather.py index ea5902a..f586617 100644 --- a/tests/commands/test_weather/test_weather.py +++ b/tests/commands/test_weather/test_weather.py @@ -74,8 +74,14 @@ def test_generate_weather_message_success(self): future_weather_list = weather._get_future_weather( self.hourly_data["weather"], "2024020216", 5 ) + sun_time = { + "sun_set_name": "今日日落", + "sun_set": "18:14", + "sun_rise_name": "明日日出", + "sun_rise": "07:06", + } result = weather._generate_weather_message( - self.c_data, self.hourly_data, future_weather_list + self.c_data, self.hourly_data, future_weather_list, sun_time ) true_result1 = "🏙️ 广州 📅 02月02日 星期五\n🌡️ 温度: 19°C ~ 26°C\n🌤️ 天气: 多云(🕓当前26.4°C)\n📈 逐时: 阴25° 阴24° 阴23° 阴21° 阴20° \n☀️ 明日日出: 07:06 今日日落: 18:14\n💨 1级 😷较差 💧60% 🌞最弱\n" true_result2 = "🏙️ 广州 📅 02月02日 星期五\n🌡️ 温度: 19°C ~ 26°C\n🌤️ 天气: 多云(🕓当前26.4°C)\n📈 逐时: 阴25° 阴24° 阴23° 阴21° 阴20° \n☀️ 明日日出: 07:06 明日日落: 18:15\n💨 1级 😷较差 💧60% 🌞最弱\n" diff --git a/wechatter/commands/_commands/weather.py b/wechatter/commands/_commands/weather.py index 39e88c4..8480fc7 100644 --- a/wechatter/commands/_commands/weather.py +++ b/wechatter/commands/_commands/weather.py @@ -106,7 +106,10 @@ def get_weather_str(city: str) -> str: c_data = _parse_c_weather(response2.text) now_ymdh = get_current_ymdh() future_weather_list = _get_future_weather(hourly_data["weather"], now_ymdh, 5) - return _generate_weather_message(c_data, hourly_data, future_weather_list) + sun_time = _get_sun_time( + hourly_data["sun_time"]["sun_set"], hourly_data["sun_time"]["sun_rise"] + ) + return _generate_weather_message(c_data, hourly_data, future_weather_list, sun_time) def _get_city_id(city_name: str) -> int: @@ -228,14 +231,11 @@ def _get_future_weather(h_data: List, now_ymdh: str, hours: int) -> List: def _generate_weather_message( - c_data: Dict, hourly_data: Dict, future_weather_list: List + c_data: Dict, hourly_data: Dict, future_weather_list: List, sun_time: Dict ) -> str: h = int(c_data["time"].split(":")[0]) temp = hourly_data["temp"] date = c_data["date"].replace("(", " ")[:-1] - sun_time = _get_sun_time( - hourly_data["sun_time"]["sun_set"], hourly_data["sun_time"]["sun_rise"] - ) future_str = "" for index, hour in enumerate(future_weather_list): future_str += f"{WEATHER_CONDITIONS[int(hour['ja'])]}{hour['jb']}° " @@ -249,4 +249,4 @@ def _generate_weather_message( f"💨 {c_data['WS']} 😷{hourly_data['air']} 💧{c_data['SD']} 🌞{hourly_data['uv']}\n" # f"💡 会下雨,记得带伞!💡\n" ) - return message \ No newline at end of file + return message