def test_add_author(self): authors_count = 0 delta_percent = count_delta(NUMBER_OF_ITERATIONS_AUTHORS) percentage = delta_percent for i in range(1, NUMBER_OF_ITERATIONS_AUTHORS): percentage = write_percentage(percentage, delta_percent) will_be_an_error = random.randint(0, 1) was_error = False if will_be_an_error and random.randint(0, 1): # it will be in this field first_name = random_string(size=(random.randint(46, 100))) was_error = True else: first_name = random_string(size=(random.randint(1, 45))) if will_be_an_error and (random.randint(0, 1) or not was_error): # read "and it will be in this field" last_name = random_string(size=(random.randint(46, 100))) else: last_name = random_string(size=(random.randint(1, 45))) request = self.client.post(reverse("books:add_author"), {"first_name": first_name, "last_name": last_name}) if not will_be_an_error: authors_count += 1 self.assertEqual(request.status_code, 302) self.assertEqual("http://testserver/", request["location"]) new_author = Author.authors.get(pk=authors_count) self.assertEqual(new_author.first_name, first_name) self.assertEqual(new_author.last_name, last_name) else: self.assertTrue(not request.context_data["form"].is_valid())
def get_success_authors_string(): number_of_authors = random.randint(1, MAX_LENGTH_OF_AUTHORS // 3) # 3 cause a shortest author is a_b max_average_length = MAX_LENGTH_OF_AUTHORS // number_of_authors list_of_authors = list() for item in range(0, number_of_authors): random_author_first_name = random_string(random.randint(1, max_average_length - 2), chars=string.letters) random_number_of_spaces = random_string( random.randint(1, max_average_length - len(random_author_first_name)), chars=" " ) random_author_last_name = random_string( random.randint(1, 1 + max_average_length - (len(random_number_of_spaces) + len(random_author_first_name))), chars=string.letters, ) list_of_authors.append(random_author_first_name + random_number_of_spaces + random_author_last_name + ",") return "".join(list_of_authors)
def test_registration(self): users_count = 0 delta_percent = count_delta(NUMBER_OF_ITERATIONS) percentage = delta_percent for i in range(1, NUMBER_OF_ITERATIONS): percentage = write_percentage(percentage, delta_percent) username = random_string(size=(random.randint(1, 26)), chars=string.letters) + str(i) password = random_string(random.randint(8, MAX_PASSWORD_LENGTH)) email = random_string(size=random.randint(2, MAX_EMAIL_LENGTH), chars=string.letters + string.digits, at_least_one_char=True)+"@gmail.com" pass_are_equal = random.randint(0, 1) if pass_are_equal: # randomize passwords equality password2 = password else: password2 = password list_pass = list(password2) list_pass = chr(ord(password2[0]) + 1) password2 = ''.join(list_pass) request = self.client.post(reverse('registration_register'), {'username': username, 'password1': password, 'password2': password2, 'email': email, 'recaptcha_response_field': 'PASSED'}) if pass_are_equal: users_count += 1 add = 1 self.assertEqual(302, request.status_code) self.assertEqual('http://testserver/accounts/register/complete/', request['location']) new_user = User.objects.get(pk=users_count) self.assertEqual(new_user.is_active, False) new_profile = RegistrationProfile.objects.get(pk=users_count) request = self.client.get("http://testserver/accounts/activate/" + new_profile.activation_key +'/') new_user = User.objects.get(pk=users_count) self.assertEqual(new_user.is_active, True) request = self.client.post(reverse('auth_login'), {'username': new_user.username, 'password': password}) new_user = User.objects.get(pk=users_count) self.assertEqual('http://testserver/', request['location']) delta = new_user.last_login - timezone.now() self.assertTrue(delta.total_seconds() < 1) else: self.assertTrue(not request.context_data['form'].is_valid())
def test_search(self): tags_count = 0 delta_percent = count_delta(NUMBER_OF_ITERATIONS_SEARCH) percentage = delta_percent for i in range(1, NUMBER_OF_ITERATIONS_SEARCH): percentage = write_percentage(percentage, delta_percent) book_free = Book.books.create(title="BUGABUGA") book_busy = Book.books.create(title="BUGABUGA") book_busy.busy = True book_busy.save() will_be_an_error = random.randint(0, 1) if will_be_an_error: while True: tags = random_string(size=(random.randint(70, 100))) if " BUGABUGA " in tags: continue was_error = True break else: tags = random_string(size=(random.randint(0, 80 - len(" BUGABUGA ")))) position = random.randint(0, len(tags)) tags = tags[:position] + " BUGABUGA " + tags[position:] request = self.client.get(reverse("books:list"), {"keywords": tags}) if not will_be_an_error: book_busy = Book.books.get(pk=2) book_free = Book.books.get(pk=1) self.assertTrue( book_free in request.context_data["object_list"] and book_busy in request.context_data["object_list"] ) request = self.client.get(reverse("books:list"), {"keywords": tags, "busy": "1"}) book_busy = Book.books.get(pk=2) book_free = Book.books.get(pk=1) self.assertTrue( book_free in request.context_data["object_list"] and book_busy not in request.context_data["object_list"] ) request = self.client.get(reverse("books:list"), {"keywords": tags, "busy": "2"}) book_busy = Book.books.get(pk=2) book_free = Book.books.get(pk=1) self.assertTrue( book_free not in request.context_data["object_list"] and book_busy in request.context_data["object_list"] )
def test_add_tag(self): tags_count = 0 delta_percent = count_delta(NUMBER_OF_ITERATIONS_TAGS) percentage = delta_percent for i in range(1, NUMBER_OF_ITERATIONS_TAGS): percentage = write_percentage(percentage, delta_percent) will_be_an_error = random.randint(0, 1) was_error = False if will_be_an_error: # it will be in this field tag = random_string(size=(random.randint(21, 100))) was_error = True else: tag = random_string(size=(random.randint(1, 20))) request = self.client.post(reverse("books:add_tag"), {"tag": tag}) if not will_be_an_error: tags_count += 1 self.assertEqual(request.status_code, 302) self.assertEqual("http://testserver/", request["location"]) new_tag = Book_Tag.tags.get(pk=tags_count) self.assertEqual(new_tag.tag, tag) else: self.assertTrue(not request.context_data["form"].is_valid())
def test_add_book(self): books_count = 0 delta_percent = count_delta(NUMBER_OF_ITERATIONS_BOOKS) percentage = delta_percent for i in range(1, NUMBER_OF_ITERATIONS_BOOKS): percentage = write_percentage(percentage, delta_percent) there_is_isbn = random.randint(0, 1) will_be_an_error = random.randint(0, 1) was_error = False isbn = None if there_is_isbn: if will_be_an_error and random.randint(0, 1): # it will be in this field while True: isbn = random_string(random.randint(1, 50), chars=string.digits) if len(isbn) == 13 and isbn.isdigit(): continue was_error = True break else: isbn = random_string(13, chars=string.digits) if will_be_an_error and random.randint(0, 1): # it will be in this field title = random_string(size=(random.randint(46, 100))) was_error = True else: title = random_string(size=(random.randint(1, 45))) e_version_exists = 0 paperback_version_exists = random.randint(0, 1) # if e_version_exists ==0: # file = None # else: # file = 'file' description = random_string(size=random.randint(1, MAX_LENGTH_OF_DESCRIPTION), chars=string.printable) if will_be_an_error and (random.randint(0, 1) or not was_error): authors = random_string(size=random.randint(1, 90), chars=string.letters) else: authors = get_success_authors_string() # if will_be_an_error and (random.randint(0, 1) or not was_error): # tags = random_string(size=random.randint(1, 90), chars=string.letters) # else: # tags = random_string(size=random.randint(1, MAX_LENGTH_OF_TAGS), chars=string.printable) form_context = { "title": title, "e_version_exists": e_version_exists, "paperback_version_exists": paperback_version_exists, "description": description, "file": file, "authors_names": authors, } if there_is_isbn: form_context["isbn"] = isbn request = self.client.post(reverse("books:add"), form_context) if not will_be_an_error: books_count += 1 self.assertEqual(request.status_code, 302) self.assertEqual("http://testserver/", request["location"]) new_book = Book.books.get(pk=books_count) self.assertEqual(new_book.title, title) self.assertEqual(new_book.description, description) if there_is_isbn: self.assertEqual(new_book.isbn, isbn) else: self.assertTrue(not request.context_data["form"].is_valid())