Coverage for src/tests/modules/portal/test_create_news_item.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for testing the use case "Create News Item".""" 

2 

3import pytest 

4 

5from kwai_bc_portal.applications.application_db_repository import ( 

6 ApplicationDbRepository, 

7) 

8from kwai_bc_portal.applications.application_repository import ( 

9 ApplicationNotFoundException, 

10) 

11from kwai_bc_portal.create_news_item import CreateNewsItem, CreateNewsItemCommand 

12from kwai_bc_portal.domain.news_item import NewsItemEntity 

13from kwai_bc_portal.news.news_item_db_repository import NewsItemDbRepository 

14from kwai_core.db.database import Database 

15from kwai_core.domain.presenter import EntityPresenter 

16from kwai_core.domain.use_case import TextCommand 

17from kwai_core.domain.value_objects.owner import Owner 

18 

19 

20async def test_create_news_item( 

21 database: Database, make_author_in_db, make_application_in_db 

22): 

23 """Test "Create News Item" use case.""" 

24 application = await make_application_in_db() 

25 author = await make_author_in_db() 

26 

27 command = CreateNewsItemCommand( 

28 enabled=True, 

29 texts=[ 

30 TextCommand( 

31 locale="en", 

32 format="md", 

33 title="Test", 

34 summary="This is a test", 

35 content="This is a test", 

36 ) 

37 ], 

38 application=application.id.value, 

39 publish_datetime="2023-01-01 00:00:00", 

40 end_datetime=None, 

41 promotion=0, 

42 promotion_end_datetime=None, 

43 remark="Created with test_create_news_item", 

44 ) 

45 presenter = EntityPresenter[NewsItemEntity]() 

46 await CreateNewsItem( 

47 NewsItemDbRepository(database), 

48 ApplicationDbRepository(database), 

49 Owner(id=author.id, uuid=author.uuid, name=author.name), 

50 presenter, 

51 ).execute(command) 

52 assert presenter.entity is not None, "There should be a news item." 

53 

54 

55async def test_create_news_item_with_wrong_application( 

56 database: Database, 

57 make_author_in_db, 

58): 

59 """Test "Create News Item" raising ApplicationNotFoundException.""" 

60 author = await make_author_in_db() 

61 

62 command = CreateNewsItemCommand( 

63 enabled=True, 

64 texts=[ 

65 TextCommand( 

66 locale="en", 

67 format="md", 

68 title="Test", 

69 summary="This is a test", 

70 content="This is a test", 

71 ) 

72 ], 

73 application=0, 

74 publish_datetime="2023-01-01 00:00:00", 

75 end_datetime=None, 

76 promotion=0, 

77 promotion_end_datetime=None, 

78 remark="Created with test_create_news_item", 

79 ) 

80 presenter = EntityPresenter[NewsItemEntity]() 

81 with pytest.raises(ApplicationNotFoundException): 

82 await CreateNewsItem( 

83 NewsItemDbRepository(database), 

84 ApplicationDbRepository(database), 

85 Owner(id=author.id, uuid=author.uuid, name=author.name), 

86 presenter, 

87 ).execute(command)