seed_comment_golden.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. """为商品评价接口造黄金样本数据(本地库 192.168.1.177/shop)。
  3. 本地 PHP 站点(php -S 127.0.0.1:8899)与 Java 共用同一个库,造完即可双端对比。
  4. 用法:python seed_comment_golden.py [--cleanup]
  5. """
  6. import sys
  7. import pymysql
  8. HOST = "192.168.1.177"
  9. USER = "root"
  10. PASSWORD = "@@qinys12346.."
  11. DB = "shop"
  12. NOW = 1792250000 # 固定时间,便于双端逐字段对照
  13. def connect():
  14. return pymysql.connect(host=HOST, user=USER, password=PASSWORD, database=DB,
  15. autocommit=True, charset="utf8mb4")
  16. def cleanup(cur):
  17. cur.execute("delete from yoshop_comment_image where comment_id in (10001,10002,10003,10004)")
  18. cur.execute("delete from yoshop_comment where comment_id in (10001,10002,10003,10004)")
  19. def seed(cur):
  20. cleanup(cur)
  21. rows = [
  22. # comment_id, score, content, is_picture, status, sort, user, order, goods, order_goods,
  23. # is_delete, create, update
  24. (10001, 10, "很好吃,包装也扎实", 1, 1, 0, 10001, 10001, 10004, 10001, 0, NOW - 300, NOW - 300),
  25. (10002, 20, "一般般吧", 0, 1, 0, 10001, 10002, 10003, 10002, 0, NOW - 200, NOW - 200),
  26. (10003, 30, "差评,不推荐", 0, 0, 1, 10001, 10001, 10004, 10001, 0, NOW - 100, NOW - 100),
  27. (10004, 10, "这条应被过滤", 0, 1, 0, 10001, 10001, 10004, 10001, 1, NOW, NOW),
  28. ]
  29. cur.executemany(
  30. "insert into yoshop_comment (comment_id, score, content, is_picture, status, sort, user_id,"
  31. " order_id, goods_id, order_goods_id, store_id, is_delete, create_time, update_time)"
  32. " values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,10001,%s,%s,%s)", rows)
  33. cur.executemany(
  34. "insert into yoshop_comment_image (comment_id, image_id, store_id, create_time)"
  35. " values (%s,%s,10001,%s)",
  36. [(10001, 10001, NOW - 300), (10001, 10002, NOW - 300), (10002, 10005, NOW - 200)])
  37. def main():
  38. conn = connect()
  39. cur = conn.cursor()
  40. if "--cleanup" in sys.argv:
  41. cleanup(cur)
  42. print("已清理评价测试数据")
  43. else:
  44. seed(cur)
  45. print("已写入评价测试数据:comment 10001-10004 / comment_image 3 条")
  46. conn.close()
  47. if __name__ == "__main__":
  48. main()