def setUpClass(cls) -> None: super().setUpClass() cls.paths = { "guide": reverse("guides-list"), "movie": reverse("movies-list"), "video": reverse("videos-list"), "book": reverse("books-list"), "article": reverse("articles-list"), "video_tags": reverse("videos-tags-list"), "movie_tags": reverse("movies-tags-list"), "book_tags": reverse("books-tags-list"), } BookTagFactory.create_batch(2) MovieTagFactory.create_batch(2) VideoTagFactory.create_batch(2) BookFactory.create_batch(2) ArticleFactory.create_batch(2) GuideFactory.create_batch(2) Movie.objects.create( title="test", description="test", producer="prod", year=1994, link="https://rutube.ru", ) Video.objects.create(title="test", author="prod", link="https://rutube.ru")
def test_a_article_save_twice(self): """Test save method for impossibility to change is_main flag""" ArticleFactory.create_batch(10) article_main = Article.objects.get(id=1) article_main.is_main = True article_main.save() self.assertEqual(article_main.is_main, True) article_main.save() self.assertEqual(article_main.is_main, True)
def test_article_clean(self): ArticleFactory.create_batch(10) article_main = Article.objects.get(id=1) article_test = Article.objects.get(id=2) article_main.is_main = True article_main.save() article_test.is_main = True with self.assertRaises(ValidationError, msg="Основная статья может быть только одна!"): article_test.full_clean()
def test_article_detail_fields(self): client = APIClient() ArticleFactory.create_batch(10) response = client.get(ViewArticlesTests.path_articles + "1/").data fields = [ "id", "is_main", "title", "author", "profession", "text", "color", "image_url", ] for field in fields: with self.subTest(field=field): self.assertTrue(field in response, msg=f"Нет поля {field}")
def test_article_save(self): """ Test save method for opportunity to change is_main flag in False if it set by another article """ ArticleFactory.create_batch(10) article_main = Article.objects.get(id=1) article_main.is_main = True article_main.save() article_test = Article.objects.get(id=2) article_test.is_main = True article_test.save() self.assertEqual(article_main.is_main, True) self.assertEqual( article_test.is_main, False, msg="Если уже есть основная, нельзя установить еще одну", )
def test_article_detail_context(self): client = APIClient() ArticleFactory.create_batch(10) response = client.get(ViewArticlesTests.path_articles + "1/").data obj = Article.objects.get(pk=1) # serialize_text_obj = self.get_text(obj) self.assertEqual(response["id"], obj.pk) self.assertEqual(response["is_main"], obj.is_main) self.assertEqual(response["title"], obj.title) self.assertEqual(response["author"], obj.author) self.assertEqual(response["profession"], obj.profession) self.assertEqual(response["text"], obj.text) self.assertEqual(response["color"], obj.color) self.assertEqual( response["image_url"], "http://testserver/media/" + str(obj.image_url), )
def test_articles_list_correct_fields(self): client = APIClient() ArticleFactory.create_batch(10) response = client.get(ViewArticlesTests.path_articles).data self.assertTrue("count" in response) self.assertTrue("next" in response) self.assertTrue("previous" in response) self.assertTrue("results" in response) fields = [ "id", "is_main", "title", "author", "profession", "text", "color", "image_url", ] results = response.get("results")[0] for field in fields: with self.subTest(field=field): self.assertTrue(field in results, msg=f"Нет поля {field}")
def create_article(self, arg): ArticleFactory.create_batch(arg)
def handle(self, *args, **options): # noqa optional_arguments = 0 for item in list(OPTIONS_AND_FINCTIONS): if options[item]: optional_arguments += 1 with factory.Faker.override_default_locale("ru_RU"): OPTIONS_AND_FINCTIONS[item](options[item][0]) self.stdout.write( self.style.SUCCESS( f"{options[item][0]} {item} created successfully" ) ) if optional_arguments == 0: try: if City.objects.count() > len(CITIES): raise MyException() with factory.Faker.override_default_locale("ru_RU"): for city_name in CITIES: CityFactory(name=city_name) CityFactory.create_batch(10) EventFactory.create_batch(200) CuratorFactory.create_batch(15) RightTagFactory.create_batch(10) for _ in range(70): num_tags = random.randint(1, 5) RightFactory(tags__num=num_tags) for _ in range(70): num_events = random.randint(0, 5) UserFactory(num_events=num_events) QuestionTagFactory.create_batch(15) for _ in range(70): num_tags = random.randint(1, 5) QuestionFactory.create(tags=num_tags) QuestionFactory.create_batch(5) QuestionFactoryWithoutAnswer.create_batch(5) PlacesTagFactory.create_batch(15) for _ in range(70): num_tags = random.randint(1, 5) PlaceFactory.create(tags__num=num_tags) GuideFactory.create_batch(70) MovieTagFactory.create_batch(10) for link in link_movie_list: num_tags = random.randint(1, 5) MovieFactory.create(link=link, tags__num=num_tags) MeetingFactory.create_batch(50) ArticleFactory.create_batch(70) BookTagFactory.create( name="Художественные", slug="hudozhestvennye", color="#C8D1FF", ) BookTagFactory.create( name="Научные", slug="nauchnye", color="#FC8585" ) BookFactory.create_batch(50) VideoTagFactory.create_batch(15) for link in link_video_list: num_tags = random.randint(1, 5) VideoFactory.create(link=link, tags__num=num_tags) StoryFactory.create_batch(30) StoryImageFactory.create_batch(100) MainFactory.create() self.stdout.write( self.style.SUCCESS("The database is filled with test data") ) except MyException: self.stdout.write( self.style.ERROR( "The database is already filled with standard test " "data. To top up individual tables, use the arguments." ) )