Skip to content

Commit

Permalink
Merge pull request #53 from Cassius0924/refactor-sql
Browse files Browse the repository at this point in the history
[Refactor] 使用SQLAlchemy重构数据库
  • Loading branch information
Cassius0924 authored Feb 19, 2024
2 parents b25f25a + 9478522 commit c15d925
Show file tree
Hide file tree
Showing 75 changed files with 1,337 additions and 898 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__/
.vscode/
.idea/
.ruff_cache/
.DS_Store

# config.ini 配置文件
config.ini
Expand Down
9 changes: 3 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import uvicorn
from loguru import logger

import wechatter.database as db
import wechatter.utils.file_manager as fm
from wechatter.init_logger import init_logger

Expand All @@ -17,19 +18,15 @@ def main():
# 为了让此文件的 config 模块是首次导入,下面这些模块需要放到 config 导入之后
from wechatter.app.app import app
from wechatter.bot.bot_info import BotInfo
from wechatter.sqlite.sqlite_manager import SqliteManager
# isort: on

BotInfo.update_name(config.bot_name)
# 创建文件夹
fm.check_and_create_folder("data/qrcodes")
fm.check_and_create_folder("data/todos")
fm.check_and_create_folder("data/text_image")
# 创建文件
fm.check_and_create_file("data/wechatter.sqlite")
# 创建数据库表
sqlite_manager = SqliteManager("data/wechatter.sqlite")
sqlite_manager.excute_folder("wechatter/sqlite/sqls")

db.create_tables()

logger.info("WeChatter 启动成功!")
# 启动uvicorn
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ docstring-code-format = true
line_length = 88
multi_line_output = 3
include_trailing_comma = true
combine_as_imports = true
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ apscheduler==3.10.4
pre-commit==3.5.0
loguru==0.7.2
tenacity==8.2.3
pydantic==2.5.3
sqlalchemy==2.0.25
15 changes: 7 additions & 8 deletions tests/commands/test_bili_hot/test_bili_hot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import unittest
import json
import unittest

from wechatter.commands._commands import bili_hot


class TestBiliHotCommand(unittest.TestCase):
def setUp(self):
with open('tests/commands/test_bili_hot/bili_hot_response.json') as f:
with open("tests/commands/test_bili_hot/bili_hot_response.json") as f:
self.r_json = json.load(f)
self.bili_hot_list = self.r_json['data']['list']
self.bili_hot_list = self.r_json["data"]["list"]

def test_extract_bili_hot_data_success(self):
result = bili_hot._extract_bili_hot_data(self.r_json)
Expand All @@ -18,12 +19,10 @@ def test_extract_bili_hot_data_failure(self):
bili_hot._extract_bili_hot_data({})

def test_generate_bili_hot_message_success(self):
result = bili_hot._generate_bili_hot_message(self.r_json['data']['list'])
true_result = (
"1. 复旦教师杀害学院书记被判死缓\n2. 免费领取心魔\n3. 扎克伯格道歉\n4. 易烊千玺当选中国影协理事\n5. kei和marin分手\n6. 上海禁止网约车在浦东机场运营\n7. 中国豆浆机在韩国热销\n8. 何同学工作室首次公开\n9. 官方称确定国花时机未成熟\n10. Aimer参演原神新春会\n11. 总台龙年春晚动画宣传片\n12. 只解冻刘德华太保守了\n13. 烟花引发住宅起火致老人去世\n14. 750万人在等尊严死\n15. 坠亡姐弟生母被送医院\n16. 不绵之夜\n17. 清华宣布脑机接口重大突破\n18. Uzi 使用替身攻击\n19. 捡雪吃患病连烧八天\n20. 幻塔EVA联动明日香登场"
)
result = bili_hot._generate_bili_hot_message(self.r_json["data"]["list"])
true_result = "1. 复旦教师杀害学院书记被判死缓\n2. 免费领取心魔\n3. 扎克伯格道歉\n4. 易烊千玺当选中国影协理事\n5. kei和marin分手\n6. 上海禁止网约车在浦东机场运营\n7. 中国豆浆机在韩国热销\n8. 何同学工作室首次公开\n9. 官方称确定国花时机未成熟\n10. Aimer参演原神新春会\n11. 总台龙年春晚动画宣传片\n12. 只解冻刘德华太保守了\n13. 烟花引发住宅起火致老人去世\n14. 750万人在等尊严死\n15. 坠亡姐弟生母被送医院\n16. 不绵之夜\n17. 清华宣布脑机接口重大突破\n18. Uzi 使用替身攻击\n19. 捡雪吃患病连烧八天\n20. 幻塔EVA联动明日香登场"
self.assertIn(true_result, result)

def test_generate_bili_hot_message_empty_list(self):
result = bili_hot._generate_bili_hot_message([])
self.assertEqual(result, '暂无Bilibili热搜')
self.assertEqual(result, "暂无Bilibili热搜")
11 changes: 6 additions & 5 deletions tests/commands/test_douyin_hot/test_douyin_hot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import unittest
import json
import unittest

from wechatter.commands._commands import douyin_hot


class TestDouyinHotCommand(unittest.TestCase):
def setUp(self):
with open('tests/commands/test_douyin_hot/douyin_hot_response.json') as f:
with open("tests/commands/test_douyin_hot/douyin_hot_response.json") as f:
self.r_json = json.load(f)
self.douyin_hot_list = self.r_json['word_list']
self.douyin_hot_list = self.r_json["word_list"]

def test_extract_douyin_hot_data_success(self):
result = douyin_hot._extract_douyin_hot_data(self.r_json)
Expand All @@ -18,10 +19,10 @@ def test_extract_douyin_hot_data_failure(self):
douyin_hot._extract_douyin_hot_data({})

def test_generate_douyin_hot_message_success(self):
result = douyin_hot._generate_douyin_hot_message(self.r_json['word_list'])
result = douyin_hot._generate_douyin_hot_message(self.r_json["word_list"])
true_result = "1. 董宇辉对刘德华说想演兵马俑\n2. 今年春联是自己写的\n3. 中国同23国全面互免签证\n4. 暗夜变装挑战\n5. 晒出你的新春红\n6. 相亲相爱接力挑战\n7. 诀别书的正确打开方式\n8. 2月第一天\n9. 北京产权交易所澄清声明\n10. 贵州有多钟爱办酒席\n11. 各地人这么做一定有原因\n12. 韩国瑜当选台立法机构负责人\n13. 一起跳甜妹手势舞\n14. 上海楼房凌晨坍塌 多方回应\n15. 刘德华宁浩红毯先生今晚直播\n16. 没有同桌我可怎么办啊\n17. 河北一保安阻止奔驰加塞被顶撞\n18. 网友过年爱上组养生局了\n19. 召集全抖音晚8找乐子\n20. 春运期间天气预测"
self.assertIn(true_result, result)

def test_generate_douyin_hot_message_empty_list(self):
result = douyin_hot._generate_douyin_hot_message([])
self.assertEqual(result, '暂无抖音热搜')
self.assertEqual(result, "暂无抖音热搜")
6 changes: 5 additions & 1 deletion tests/commands/test_people_daily/test_people_daily.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import unittest

from wechatter.commands._commands import people_daily


class TestPeopleDailyCommand(unittest.TestCase):
def test_get_people_daily_url_success(self):
result = people_daily.get_people_daily_url("2024010901")
self.assertEqual(result, "http://paper.people.com.cn/rmrb/images/2024-01/09/01/rmrb2024010901.pdf")
self.assertEqual(
result,
"http://paper.people.com.cn/rmrb/images/2024-01/09/01/rmrb2024010901.pdf",
)

def test_get_people_daily_value_error(self):
with self.assertRaises(ValueError):
Expand Down
8 changes: 5 additions & 3 deletions tests/commands/test_qrcode/test_qrcode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import unittest
from unittest.mock import patch

import qrcode as qrc

from wechatter.commands._commands import qrcode


Expand All @@ -15,7 +17,7 @@ def tearDown(self):
os.remove(self.path)

def test_generate_qrcode_success(self):
result = qrcode.generate_qrcode('https://www.baidu.com')
result = qrcode.generate_qrcode("https://www.baidu.com")
self.assertIsInstance(result, qrc.image.pil.PilImage)

def test_save_qrcode_attribute_error(self):
Expand All @@ -26,7 +28,7 @@ def test_save_qrcode_successfully(self):
qrcode.save_qrcode(self.img, self.path)
self.assertTrue(os.path.exists(self.path))

@patch('os.access', return_value=False)
@patch("os.access", return_value=False)
def test_save_qrcode_permission_error(self, mock_access):
with self.assertRaises(PermissionError):
qrcode.save_qrcode(self.img, self.path)
qrcode.save_qrcode(self.img, self.path)
8 changes: 5 additions & 3 deletions tests/commands/test_today_in_history/test_today_in_history.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import json
import unittest

from wechatter.commands._commands import today_in_history


class TestTodayInHistoryCommand(unittest.TestCase):

def setUp(self):
with open('tests/commands/test_today_in_history/today_in_history_response.json') as f:
with open(
"tests/commands/test_today_in_history/today_in_history_response.json"
) as f:
self.tih_response = json.load(f)
self.tih_list = self.tih_response['data']
self.tih_list = self.tih_response["data"]

def test_extract_today_in_history_data_success(self):
result = today_in_history._extract_today_in_history_data(self.tih_response)
Expand Down
7 changes: 4 additions & 3 deletions tests/commands/test_trivia/test_trivia.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import unittest
import json
import unittest

from requests import Response

from wechatter.commands._commands import trivia
from wechatter.exceptions import Bs4ParsingError


class TestTriviaCommand(unittest.TestCase):

def setUp(self):
with open("tests/commands/test_trivia/trivia_response.html.test") as f:
r_html = f.read()
self.response = Response()
self.response._content = r_html.encode('utf-8')
self.response._content = r_html.encode("utf-8")
with open("tests/commands/test_trivia/trivia_data.json") as f:
self.trivia_list = json.load(f)

Expand Down
8 changes: 4 additions & 4 deletions tests/commands/test_weibo_hot/test_weibo_hot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
import json
import unittest

from wechatter.commands._commands import weibo_hot


class TestWeiboHotCommand(unittest.TestCase):

def setUp(self):
with open('tests/commands/test_weibo_hot/weibo_hot_response.json') as f:
with open("tests/commands/test_weibo_hot/weibo_hot_response.json") as f:
self.r_json = json.load(f)
self.weibo_hot_list = self.r_json["data"]["cards"][0]["card_group"][:20]

Expand All @@ -25,4 +25,4 @@ def test_generate_weibo_hot_message_success(self):

def test_generate_weibo_hot_message_empty_list(self):
result = weibo_hot._generate_weibo_hot_message([])
self.assertEqual(result, '微博热搜列表为空')
self.assertEqual(result, "微博热搜列表为空")
13 changes: 6 additions & 7 deletions tests/commands/test_zhihu_hot/test_zhihu_hot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import unittest
import json
import unittest

from wechatter.commands._commands import zhihu_hot


class TestZhihuHotCommand(unittest.TestCase):
def setUp(self):
with open('tests/commands/test_zhihu_hot/zhihu_hot_response.json') as f:
with open("tests/commands/test_zhihu_hot/zhihu_hot_response.json") as f:
self.r_json = json.load(f)
self.zhihu_hot_list = self.r_json['data']
self.zhihu_hot_list = self.r_json["data"]

def test_extract_zhihu_hot_data_success(self):
result = zhihu_hot._extract_zhihu_hot_data(self.r_json)
Expand All @@ -19,11 +20,9 @@ def test_extract_zhihu_hot_data_failure(self):

def test_generate_zhihu_hot_message_success(self):
result = zhihu_hot._generate_zhihu_hot_message(self.zhihu_hot_list)
true_result = (
"1. 得州州长警告拜登称,若「联邦化」得州国民警卫队将是政治错误,如何解读?该事件将会如何演变?\n2. 贵州一村民办酒席被挂工作证人员往食物撒盐,当地通报「已致歉」,如何评价此事?\n3. 美国联合包裹运送服务公司宣布将裁员 1.2 万人,将节省约 10 亿美元成本,哪些信息值得关注?\n4. 汞比金多一个质子,理论上汞比金更难形成,为什么汞比金便宜?\n5. 沙特伊朗等五国已正式成为金砖成员国,34 个国家提出书面申请,对金砖扩容做何展望?哪些信息值得关注?\n6. 23-24 赛季 NBA快船 125:109 奇才,如何评价这场比赛?\n7. 戴尔服务中国公司拟注销,哪些信息值得关注?\n8. 如何看待 2 月 1 日 A 股市场行情?\n9. 如何评价“祸不及家人的前提是惠不及家人”这个观点?\n10. 明明知道熬夜对身体不好,可就是戒不掉,我该怎么办?\n11. 「复旦研究员刺杀书记案」一审宣判被告死缓,犯故意杀人罪,哪些信息值得关注?如何看待此案?\n12. 白所成等10名缅北重大犯罪嫌疑人被押解回国,缅北四大家族尽数覆灭,哪些信息值得关注?\n13. 韩国瑜当选台新任立法机构负责人,释放了哪些信息?这对台湾当局执政有何影响?\n14. 财政部表示基本养老保险基金已累计结余近 6 万亿元,养老金按时足额发放有保证,哪些信息值得关注?\n15. 河南暴雪来临,洛阳等地已开始下雪,目前情况如何?会对春运出行带来哪些影响?\n16. 同事听到我用“免贵姓…”回答“您贵姓?”这个问题的时候全都笑了,是哪里出了问题?\n17. 如何评价《崩坏:星穹铁道》千星纪游PV:「旧梦重温」?\n18. 23-24 赛季英超利物浦 4:1 切尔西,如何评价这场比赛?\n19. 孩子应该做家务、做饭吗?\n20. 你在跑步路上遇到的最大的阻碍是什么?"
)
true_result = "1. 得州州长警告拜登称,若「联邦化」得州国民警卫队将是政治错误,如何解读?该事件将会如何演变?\n2. 贵州一村民办酒席被挂工作证人员往食物撒盐,当地通报「已致歉」,如何评价此事?\n3. 美国联合包裹运送服务公司宣布将裁员 1.2 万人,将节省约 10 亿美元成本,哪些信息值得关注?\n4. 汞比金多一个质子,理论上汞比金更难形成,为什么汞比金便宜?\n5. 沙特伊朗等五国已正式成为金砖成员国,34 个国家提出书面申请,对金砖扩容做何展望?哪些信息值得关注?\n6. 23-24 赛季 NBA快船 125:109 奇才,如何评价这场比赛?\n7. 戴尔服务中国公司拟注销,哪些信息值得关注?\n8. 如何看待 2 月 1 日 A 股市场行情?\n9. 如何评价“祸不及家人的前提是惠不及家人”这个观点?\n10. 明明知道熬夜对身体不好,可就是戒不掉,我该怎么办?\n11. 「复旦研究员刺杀书记案」一审宣判被告死缓,犯故意杀人罪,哪些信息值得关注?如何看待此案?\n12. 白所成等10名缅北重大犯罪嫌疑人被押解回国,缅北四大家族尽数覆灭,哪些信息值得关注?\n13. 韩国瑜当选台新任立法机构负责人,释放了哪些信息?这对台湾当局执政有何影响?\n14. 财政部表示基本养老保险基金已累计结余近 6 万亿元,养老金按时足额发放有保证,哪些信息值得关注?\n15. 河南暴雪来临,洛阳等地已开始下雪,目前情况如何?会对春运出行带来哪些影响?\n16. 同事听到我用“免贵姓…”回答“您贵姓?”这个问题的时候全都笑了,是哪里出了问题?\n17. 如何评价《崩坏:星穹铁道》千星纪游PV:「旧梦重温」?\n18. 23-24 赛季英超利物浦 4:1 切尔西,如何评价这场比赛?\n19. 孩子应该做家务、做饭吗?\n20. 你在跑步路上遇到的最大的阻碍是什么?"
self.assertIn(true_result, result)

def test_generate_zhihu_hot_message_empty_list(self):
result = zhihu_hot._generate_zhihu_hot_message([])
self.assertEqual(result, '暂无知乎热搜')
self.assertEqual(result, "暂无知乎热搜")
Loading

0 comments on commit c15d925

Please sign in to comment.