def test_item_daily_visits_is_not_negative(self): """Test that the database does not save negative daily visits.""" valid_postcode = 'EC2R 8AH' user_profile = UserProfile(user = User.objects.create_user(username = '******', email = '*****@*****.**', password = '******', first_name = 'test', last_name = 'visits'), postcode = valid_postcode) user_profile.save() category = Category('Test') category.save() section = Section('Test') section.save() size = Size('Test') size.save() valid_price = '10.30' item = Item(title = 'Test', price = valid_price, seller = user_profile, category = category, section = section, size = size, dailyVisits = -1) self.assertRaises(ValidationError, item.save)
def test_item_price_is_decimal_correct(self): '''Test that the database does not save prices with more than two decimal places.''' valid_postcode = 'EC2R 8AH' user_profile = UserProfile(user=User.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='test', last_name='price'), postcode=valid_postcode) user_profile.save() category = Category('Test') category.save() section = Section('Test') section.save() size = Size('Test') size.save() item = Item(title='Test', price='10.301', seller=user_profile, category=category, section=section, size=size) self.assertRaises(ValidationError, item.save)
def test_item_price_is_decimal(self): """Test that the database can handle decimal prices.""" valid_postcode = 'EC2R 8AH' user_profile = UserProfile(user = User.objects.create_user(username = '******', email = '*****@*****.**', password = '******', first_name = 'test', last_name = 'price'), postcode = valid_postcode) user_profile.save() category = Category('Test') category.save() section = Section('Test') section.save() size = Size('Test') size.save() # Needs to be a string for the validator to work valid_price = '10.30' item = Item(title = 'Test', price = valid_price, seller = user_profile, category = category, section = section, size = size) item.save() self.assertEqual(valid_price, str(item.price))
def test_item_price_is_not_negative(self): '''Test that the database does not store a negative item price.''' valid_postcode = 'EC2R 8AH' user_profile = UserProfile(user=User.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='test', last_name='price'), postcode=valid_postcode) user_profile.save() category = Category('Test') category.save() section = Section('Test') section.save() size = Size('Test') size.save() item = Item(title='Test', price=-10, seller=user_profile, category=category, section=section, size=size) self.assertRaises(ValidationError, item.save)
def add_user_profile(username, email, password, first_name, last_name, picture, postcode, phone): '''Adds a new user profile with the given parameters.''' user = User.objects.create_user(username=username, email=email, password=password, first_name=first_name, last_name=last_name) user = RegistrationProfile.objects.create_inactive_user(site=None, new_user=user, send_email=False) RegistrationProfile.objects.activate_user( activation_key=RegistrationProfile.objects.get( user=user).activation_key, site=None) user_profile = UserProfile() user_profile.user = user user_profile.picture = picture user_profile.postcode = postcode user_profile.phone = phone user_profile.rating = 0 user_profile.save() return user_profile
def test_username_is_unique(self): """Test that the username field is unique.""" valid_postcode = 'EC2R 8AH' user = User.objects.create_user(username = '******', email = '*****@*****.**', password = '******', first_name = 'test', last_name = 'username') user_profile1 = UserProfile(user = user, postcode = valid_postcode) user_profile1.save() user_profile2 = UserProfile(user = user, postcode = valid_postcode) self.assertRaises(ValidationError, user_profile2.save)
def test_postcode_is_valid_with_spaces(self): """Test that a correct postcode with spaces can be saved,""" valid_postcode = 'EC2R 8AH' user_profile = UserProfile(user = User.objects.create_user(username = '******', email = '*****@*****.**', password = '******', first_name = 'test', last_name = 'postcode'), postcode = valid_postcode) user_profile.save() self.assertEqual(user_profile.postcode, valid_postcode)
def test_phone_number_is_valid(self): """Test that the database stores a valid number.""" valid_postcode = 'EC2R 8AH' valid_phone_number = '02079304832' user_profile = UserProfile(user = User.objects.create_user(username = '******', email = '*****@*****.**', password = '******', first_name = 'test', last_name = 'phonenumber'), postcode = valid_postcode, phone = valid_phone_number) user_profile.save() self.assertEqual(valid_phone_number, user_profile.phone)
def test_rating_is_decimal(self): """Test that the database can store a decimal rating.""" valid_postcode = 'EC2R 8AH' valid_rating = 2.5 user_profile = UserProfile(user = User.objects.create_user(username = '******', email = '*****@*****.**', password = '******', first_name = 'test', last_name = 'rating'), postcode = valid_postcode, rating = valid_rating) user_profile.save() self.assertEqual(valid_rating, user_profile.rating)
def register(self, form_class): """This function overrides the register function from RegistrationView.""" user = super(MyRegistrationView, self).register(form_class) user.first_name = form_class.cleaned_data['first_name'] user.last_name = form_class.cleaned_data['last_name'] user = RegistrationProfile.objects.create_inactive_user(site=None, new_user=user) # Create our user profile and fill the details. user_profile = UserProfile() user_profile.user = user if not form_class.cleaned_data['picture']: # Handle when the user does not upload a profile picture image = Image.open( path.dirname(path.dirname(path.abspath(__file__))) + static('images/profile.jpg')) if image.mode != "RGB": image = image.convert("RGB") output = BytesIO() image.save(output, 'JPEG', quality=100) output.seek(0) file_system = FileSystemStorage(settings.MEDIA_ROOT + '/profile_images/') filename = file_system.save('profile.jpg', output) file_url = file_system.url(filename) user_profile.picture = file_url.replace('/media', 'profile_images', 1) else: user_profile.picture = form_class.cleaned_data['picture'] # Clean up the postcode input_postcode = form_class.cleaned_data['postcode'].replace( ' ', '').upper() user_profile.postcode = input_postcode[:-3] + ' ' + input_postcode[-3:] user_profile.phone = form_class.cleaned_data['phone'] user_profile.save() return user_profile
def test_review_rating_is_smaller_than_5(self): '''Test that the database does not save ratings that are higher than 5.''' valid_postcode = 'EC2R 8AH' user_profile = UserProfile(user=User.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='test', last_name='rating'), postcode=valid_postcode) user_profile.save() user_profile2 = UserProfile(user=User.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='test', last_name='rating'), postcode=valid_postcode) user_profile2.save() category = Category('Test') category.save() section = Section('Test') section.save() size = Size('Test') size.save() valid_price = '10.30' item = Item(title='Test', price=valid_price, seller=user_profile, category=category, section=section, size=size, sold_to=user_profile2) item.save() review = Review(item=item, rating=6) self.assertRaises(ValidationError, review.save)
def register(self, form_class): # Override the register function from RegistrationView user = super(MyRegistrationView, self).register(form_class) user.first_name = form_class.cleaned_data['first_name'] user.last_name = form_class.cleaned_data['last_name'] user = RegistrationProfile.objects.create_inactive_user(site=None, new_user=user) # Create our user profile and fill the details. user_profile = UserProfile() user_profile.user = user if form_class.cleaned_data['picture'] == None: user_profile.picture = 'profile_images/placeholder.jpg' else: user_profile.picture = form_class.cleaned_data['picture'] input_postcode = form_class.cleaned_data['postcode'].replace( ' ', '').upper() user_profile.postcode = input_postcode[:-3] + ' ' + input_postcode[-3:] user_profile.phone = form_class.cleaned_data['phone'] user_profile.save() return user_profile