You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.1 KiB

from django.test import TestCase
# Create your tests here.
from django.test import TestCase, Client
from django.urls import reverse
from seatsApp.models import Seats
from movieApp.models import AppMovie, AppMovieTheater
from userApp.models import Order, User, VIP
import json
class SeatsViewTestCase(TestCase):
def setUp(self):
self.client = Client()
# 创建测试数据
self.vip = VIP.objects.create(vipname='test_user', money=100)
self.movie = AppMovie.objects.create(id=1, title='Test Movie')
self.theater = AppMovieTheater.objects.create(name='Test Theater')
self.session = self.client.session
self.session['username'] = 'test_user'
self.session.save()
def test_seats_get_request(self):
response = self.client.get(reverse('seats'), {'id': 1})
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'seats.html')
def test_seats_post_request_sufficient_money(self):
data = {
'price': 50,
'seats': 'A1 B2',
'theater': 'Test Theater',
'id': json.dumps(1),
'movietype': json.dumps('2D')
}
response = self.client.post(reverse('seats'), data)
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content)['msg'], 'exist')
# 检查座位是否保存
self.assertEqual(Seats.objects.filter(movieid=1).count(), 2)
# 检查订单是否保存
self.assertEqual(Order.objects.filter(username='test_user').count(), 1)
# 检查 VIP 余额是否更新
vip = VIP.objects.get(vipname='test_user')
self.assertEqual(vip.money, 50)
def test_seats_post_request_insufficient_money(self):
data = {
'price': 150,
'seats': 'A1 B2',
'theater': 'Test Theater',
'id': json.dumps(1),
'movietype': json.dumps('2D')
}
response = self.client.post(reverse('seats'), data)
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content)['msg'], 'error')