Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Group15_all_project/yangchen
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
159 lines (134 sloc)
7.06 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from salalchemy import add_user, delete_user, add_favorite, delete_favorite, add_browsing_history, add_order, add_product, delete_product, update_product, increment_click_count, increment_favorite_count, increment_positive_feedback, increment_negative_feedback | |
class TestYourModule(unittest.TestCase): | |
def setUp(self): | |
# 在每个测试方法开始前,创建一个新的会话 | |
self.session_user = SessionUser() | |
self.session_product = SessionProduct() | |
def tearDown(self): | |
# 在每个测试方法结束后,关闭会话 | |
self.session_user.close() | |
self.session_product.close() | |
def test_add_user(self): | |
# 测试添加用户 | |
username = "test_user" | |
password = "test_password" | |
add_user(username, password) | |
user = self.session_user.query(User).filter_by(username=username).first() | |
self.assertEqual(user.username, username) | |
def test_delete_user(self): | |
# 测试删除用户 | |
user_id = add_user("test_user", "test_password").user_id | |
delete_user(user_id) | |
user = self.session_user.query(User).filter_by(user_id=user_id).first() | |
self.assertIsNone(user) | |
def test_add_favorite(self): | |
# 测试添加收藏商品 | |
user_id = add_user("test_user", "test_password").user_id | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
add_favorite(user_id, product_id) | |
favorite = self.session_user.query(UserFavorite).filter_by(user_id=user_id, product_id=product_id).first() | |
self.assertEqual(favorite.user_id, user_id) | |
self.assertEqual(favorite.product_id, product_id) | |
def test_delete_favorite(self): | |
# 测试删除收藏商品 | |
user_id = add_user("test_user", "test_password").user_id | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
add_favorite(user_id, product_id) | |
delete_favorite(user_id, product_id) | |
favorite = self.session_user.query(UserFavorite).filter_by(user_id=user_id, product_id=product_id).first() | |
self.assertIsNone(favorite) | |
def test_add_browsing_history(self): | |
# 测试添加浏览历史 | |
user_id = add_user("test_user", "test_password").user_id | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
add_browsing_history(user_id, product_id) | |
history = self.session_user.query(UserBrowsingHistory).filter_by(user_id=user_id, product_id=product_id).first() | |
self.assertEqual(history.user_id, user_id) | |
self.assertEqual(history.product_id, product_id) | |
def test_add_order(self): | |
# 测试添加订单 | |
user_id = add_user("test_user", "test_password").user_id | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
add_order(user_id, product_id, quantity=2, total_price=20.0) | |
order = self.session_user.query(Order).filter_by(user_id=user_id, product_id=product_id).first() | |
self.assertEqual(order.user_id, user_id) | |
self.assertEqual(order.product_id, product_id) | |
self.assertEqual(order.quantity, 2) | |
self.assertEqual(order.total_price, 20.0) | |
def test_add_product(self): | |
# 测试添加商品 | |
product = add_product("test_product", "", "", "", "", 10.0) | |
self.assertEqual(product.product_name, "test_product") | |
def test_delete_product(self): | |
# 测试删除商品 | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
delete_product(product_id) | |
product = self.session_product.query(Product).filter_by(product_id=product_id).first() | |
self.assertIsNone(product) | |
def test_update_product(self): | |
# 测试更新商品信息 | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
update_product(product_id, name="updated_product") | |
product = self.session_product.query(Product).filter_by(product_id=product_id).first() | |
self.assertEqual(product.product_name, "updated_product") | |
def test_increment_click_count(self): | |
# 测试增加商品点击量 | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
increment_click_count(product_id) | |
product = self.session_product.query(Product).filter_by(product_id=product_id).first() | |
self.assertEqual(product.click_count, 1) | |
def test_increment_favorite_count(self): | |
# 测试增加商品收藏量 | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
increment_favorite_count(product_id) | |
product = self.session_product.query(Product).filter_by(product_id=product_id).first() | |
self.assertEqual(product.favorite_count, 1) | |
def test_increment_positive_feedback(self): | |
# 测试增加商品好评 | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
increment_positive_feedback(product_id, content="great_product") | |
product = self.session_product.query(Product).filter_by(product_id=product_id).first() | |
self.assertEqual(product.positive_feedback, 1) | |
self.assertEqual(product.positive_feedback_content, "great_product") | |
def test_increment_negative_feedback(self): | |
# 测试增加商品差评 | |
product_id = add_product("test_product", "", "", "", "", 10.0).product_id | |
increment_negative_feedback(product_id, content="bad_product") | |
product = self.session_product.query(Product).filter_by(product_id=product_id).first() | |
self.assertEqual(product.negative_feedback, 1) | |
self.assertEqual(product.negative_feedback_content, "bad_product") | |
def test_invalid_user_id(self): | |
# 测试错误的用户 ID | |
with self.assertRaises(ValueError): | |
delete_user(-1) | |
def test_invalid_product_id(self): | |
# 测试错误的商品 ID | |
with self.assertRaises(ValueError): | |
add_favorite(1, -1) | |
def test_invalid_quantity(self): | |
# 测试错误的数量 | |
with self.assertRaises(ValueError): | |
add_order(1, 1, quantity=-1) | |
def test_invalid_total_price(self): | |
# 测试错误的总价 | |
with self.assertRaises(ValueError): | |
add_order(1, 1, total_price=-1.0) | |
def test_invalid_username(self): | |
# 测试错误的用户名 | |
with self.assertRaises(ValueError): | |
add_user("test_user", "test_password", address="test_address", phone="1234567890") | |
def test_invalid_password(self): | |
# 测试错误的密码 | |
with self.assertRaises(ValueError): | |
add_user("test_user", "123", address="test_address", phone="1234567890") | |
def test_invalid_address(self): | |
# 测试错误的地址 | |
with self.assertRaises(ValueError): | |
add_user("test_user", "test_password", address="test_address", phone="1234567890") | |
def test_invalid_phone(self): | |
# 测试错误的手机号 | |
with self.assertRaises(ValueError): | |
add_user("test_user", "test_password", address="test_address", phone="1234567") | |
if __name__ == "__main__": | |
unittest.main() |