-
Notifications
You must be signed in to change notification settings - Fork 1
Testcases and outcomes
from django.test import TestCase, Client from django.urls import reverse from django.http import HttpRequest from django.contrib.auth.models import User from myapp.forms import ProfileForm from .models import * from .models import Listing, Watchlist from datetime import datetime from myapp.models import Listing, User
class ProfileFormTests(TestCase): def test_valid_form(self): form = ProfileForm({ 'username': 'testuser', 'email': '[email protected]', 'password1': 'testpass', 'password2': 'testpass', 'account_type': 'B', 'location': 'New York' })
def test_missing_required_field(self):
form = ProfileForm({
'username': 'testuser',
'email': '',
'password1': 'testpass',
'password2': 'testpass',
'account_type': 'B',
'location': 'New York'
})
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['email'], ['This field is required.'])
def test_invalid_email_format(self):
form = ProfileForm({
'username': 'testuser',
'email': 'test',
'password1': 'testpass',
'password2': 'testpass',
'account_type': 'B',
'location': 'New York'
})
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['email'], ['Enter a valid email address.'])
def test_passwords_do_not_match(self):
form = ProfileForm({
'username': 'testuser',
'email': '[email protected]',
'password1': 'testpass',
'password2': 'wrongpass',
'account_type': 'B',
'location': 'New York'
})
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['password2'], ['The two password fields didn’t match.'])
def test_invalid_account_type_choice(self):
form = ProfileForm({
'username': 'testuser',
'email': '[email protected]',
'password1': 'testpass',
'password2': 'testpass',
'account_type': 'X',
'location': 'New York'
})
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['account_type'], ['Select a valid choice. X is not one of the available choices.'])
These test cases cover some common scenarios such as valid form submission, missing required fields, invalid email format, passwords that do not match, and an invalid choice for the account type field. You can customize them as needed to fit your specific requirements.
class ProfileFormTestCase(TestCase): def setUp(self): self.user_data = { 'username': 'testuser', 'password1': 'testpassword123', 'password2': 'testpassword123', 'account_type': 'B', 'email': '[email protected]', 'location': 'New York', }
def test_valid_form(self):
form = ProfileForm(data=self.user_data)
self.assertTrue(form.is_valid())
def test_required_fields(self):
required_fields = ['username', 'password1', 'password2', 'account_type', 'email', 'location']
form = ProfileForm()
for field in required_fields:
self.assertTrue(form.fields[field].required)
def test_username_field(self):
# Test that username field has correct max length
form = ProfileForm()
self.assertEqual(form.fields['username'].max_length, 150)
# Test that username field is unique
User.objects.create_user(username='testuser', password='testpassword123')
form = ProfileForm(data=self.user_data)
self.assertFalse(form.is_valid())
self.assertIn('username', form.errors)
def test_email_field(self):
# Test that email field has correct max length
form = ProfileForm()
self.assertEqual(form.fields['email'].max_length, 30)
# Test that email field is required
self.assertTrue(form.fields['email'].required)
# Test that email field accepts valid emails
self.user_data['email'] = '[email protected]'
form = ProfileForm(data=self.user_data)
self.assertTrue(form.is_valid())
def test_location_field(self):
# Test that location field has correct max length
form = ProfileForm()
self.assertEqual(form.fields['location'].max_length, 30)
# Test that location field is required
self.assertTrue(form.fields['location'].required)
def test_account_type_field(self):
# Test that account type field is required
form = ProfileForm()
self.assertTrue(form.fields['account_type'].required)
# Test that account type field accepts valid choices
self.user_data['account_type'] = 'B'
form = ProfileForm(data=self.user_data)
self.assertTrue(form.is_valid())
# Test that account type field rejects invalid choices
self.user_data['account_type'] = 'invalid_choice'
form = ProfileForm(data=self.user_data)
self.assertFalse(form.is_valid())
self.assertIn('account_type', form.errors)
class ProfileModelTest(TestCase): def setUp(self): self.user = User.objects.create_user( username='testuser', email='[email protected]', password='testpass') self.profile = Profile.objects.create( user=self.user, email='[email protected]', account_type='Buyer', location='New York')
class ListingModelTest(TestCase): def setUp(self): self.user = User.objects.create_user(username='seller1', password='password') self.listing = Listing.objects.create( productName='iPhone 13', sellerID=self.user, dateAdded=datetime.now(), price=1200.00, desc='Brand new iPhone 13', image=None )
def test_listing_creation(self):
self.assertEqual(self.listing.productName, 'iPhone 13')
self.assertEqual(self.listing.sellerID, self.user)
self.assertIsNotNone(self.listing.dateAdded)
self.assertEqual(self.listing.price, 1200.00)
self.assertEqual(self.listing.desc, 'Brand new iPhone 13')
def test_listing_str(self):
self.assertEqual(str(self.listing), 'iPhone 13')
class WatchlistTestCase(TestCase): def setUp(self): user = User.objects.create_user(username="testuser", email="[email protected]", password="testpassword") listing = Listing.objects.create(productName="Test Product", productID=1234, sellerID=5678, listingID=9012) watchlist = Watchlist.objects.create(Owner=user) watchlist.item.add(listing)
The first set of test cases, ProfileFormTestCase, tests the functionality of a form called ProfileForm. This form is used to allow users to update their profile information. The test cases ensure that the form is correctly validated, required fields are properly identified, and that various field-specific constraints are enforced (e.g. email addresses must be valid, usernames must be unique, etc.).
The second set of test cases, ProfileModelTest and ListingModelTest, test the functionality of two models in the application, Profile and Listing. These test cases ensure that the models correctly store and retrieve data.
The third set of test cases, ListingTestCase and WatchlistTestCase, test various other components of the application. These test cases ensure that a listing can be created and retrieved, that messages can be sent between two users, and that a listing can be added to a user's watchlist.
class WatchlistTestCase(TestCase): def setUp(self): self.user1 = User.objects.create_user(username="user1", email="[email protected]", password="password123") self.user2 = User.objects.create_user(username="user2", email="[email protected]", password="password123") self.listing1 = Listing.objects.create(productName="Product1", sellerID=self.user1, price=50.00) self.listing2 = Listing.objects.create(productName="Product2", sellerID=self.user1, price=100.00) self.listing3 = Listing.objects.create(productName="Product3", sellerID=self.user2, price=75.00) self.watchlist1 = Watchlist.objects.create(Owner=self.user1) self.watchlist2 = Watchlist.objects.create(Owner=self.user2)
def test_watchlist_creation(self):
self.assertEqual(self.watchlist1.Owner.username, "user1")
self.assertEqual(self.watchlist2.Owner.username, "user2")
def test_watchlist_add_listing(self):
self.watchlist1.item.add(self.listing1)
self.watchlist1.item.add(self.listing2)
self.assertEqual(self.watchlist1.item.count(), 2)
self.assertEqual(self.watchlist2.item.count(), 0)
self.watchlist2.item.add(self.listing3)
self.assertEqual(self.watchlist2.item.count(), 1)
self.assertEqual(self.watchlist2.item.first().productName, "Product3")