Skip to content

Testcases and outcomes

Lance-Ly edited this page Apr 17, 2023 · 17 revisions

Test cases 1 - 5

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.

================================================================

Test cases 6-10

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())

    # Test that email field rejects invalid emails
    self.user_data['email'] = 'invalidemail'
    form = ProfileForm(data=self.user_data)
    self.assertFalse(form.is_valid())
    self.assertIn('email', form.errors)

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)

Profile model test and listing test 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')

def test_profile_creation(self):
    self.assertEqual(self.profile.user.username, 'testuser')
    self.assertEqual(self.profile.email, '[email protected]')
    self.assertEqual(self.profile.account_type, 'Buyer')
    self.assertEqual(self.profile.location, 'New York')

class ListingModelTest(TestCase): def setUp(self): self.listing = Listing.objects.create( productName='iPhone 13', productID=12345, sellerID=54321, listingID=1)

def test_listing_creation(self):
    self.assertEqual(self.listing.productName, 'iPhone 13')
    self.assertEqual(self.listing.productID, 12345)
    self.assertEqual(self.listing.sellerID, 54321)
    self.assertEqual(self.listing.listingID, 1)

Test Case: Create a Listing object from .models import Listing

class ListingTestCase(TestCase): def setUp(self): Listing.objects.create(productName="Test Product", productID=1234, sellerID=5678, listingID=9012)

def test_listing_creation(self):
    product = Listing.objects.get(productName="Test Product")
    self.assertEqual(product.productID, 1234)
    self.assertEqual(product.sellerID, 5678)
    self.assertEqual(product.listingID, 9012)

Test Case: Send a message between two users from .models import Messages

class MessagesTestCase(TestCase): def setUp(self): user1 = User.objects.create_user(username="user1", email="[email protected]", password="testpassword") user2 = User.objects.create_user(username="user2", email="[email protected]", password="testpassword") Messages.objects.create(body="Test message", senderID=user1.id, targetID=user2.id)

def test_message_sending(self):
    user1 = User.objects.get(username="user1")
    user2 = User.objects.get(username="user2")
    message = Messages.objects.get(senderID=user1.id, targetID=user2.id)
    self.assertEqual(message.body, "Test message")

Test Case: Add a Listing object to a user's watchlist from .models import Listing, Watchlist

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)

def test_watchlist_addition(self):
    user = User.objects.get(username="testuser")
    watchlist = Watchlist.objects.get(Owner=user)
    self.assertEqual(watchlist.item.count(), 1)

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, tests 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, MessagesTestCase, and WatchlistTestCase, tests 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.

Clone this wiki locally