Coverage for src/tests/test_model_constraints.py: 100%

64 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-11-21 04:21 +0000

1import pytest 

2from sqlalchemy.exc import IntegrityError 

3 

4from couchers.db import session_scope 

5from couchers.models import Cluster, Node, Page, PageType, PageVersion, Thread 

6from couchers.utils import create_polygon_lat_lng, to_multi 

7from tests.test_communities import create_1d_polygon, create_community 

8from tests.test_fixtures import db, generate_user, testconfig # noqa 

9 

10 

11@pytest.fixture(autouse=True) 

12def _(testconfig): 

13 pass 

14 

15 

16def test_node_constraints(db): 

17 # check we can't have two official clusters for a given node 

18 with pytest.raises(IntegrityError) as e: 

19 with session_scope() as session: 

20 node = Node(geom=to_multi(create_1d_polygon(0, 2))) 

21 session.add(node) 

22 cluster1 = Cluster( 

23 name="Testing community, cluster 1", 

24 description="Testing community description", 

25 parent_node=node, 

26 is_official_cluster=True, 

27 ) 

28 session.add(cluster1) 

29 cluster2 = Cluster( 

30 name="Testing community, cluster 2", 

31 description="Testing community description", 

32 parent_node=node, 

33 is_official_cluster=True, 

34 ) 

35 session.add(cluster2) 

36 assert "violates unique constraint" in str(e.value) 

37 assert "ix_clusters_owner_parent_node_id_is_official_cluster" in str(e.value) 

38 

39 

40def test_page_constraints(db): 

41 user, token = generate_user() 

42 

43 with session_scope() as session: 

44 c_id = create_community(session, 0, 2, "Root node", [user], [], None).id 

45 

46 # check we can't create a page without an owner 

47 with pytest.raises(IntegrityError) as e: 

48 with session_scope() as session: 

49 page = Page( 

50 parent_node_id=c_id, 

51 # note no owner 

52 creator_user_id=user.id, 

53 type=PageType.guide, 

54 thread=Thread(), 

55 ) 

56 session.add(page) 

57 session.add( 

58 PageVersion( 

59 page=page, 

60 editor_user_id=user.id, 

61 title="Title", 

62 content="Content", 

63 ) 

64 ) 

65 assert "violates check constraint" in str(e.value) 

66 assert "one_owner" in str(e.value) 

67 

68 with session_scope() as session: 

69 node = Node(geom=to_multi(create_polygon_lat_lng([[0, 0], [0, 2], [2, 2], [2, 0], [0, 0]]))) 

70 session.add(node) 

71 cluster = Cluster( 

72 name="Testing Community", 

73 description="Description for testing community", 

74 parent_node=node, 

75 ) 

76 session.add(cluster) 

77 session.flush() 

78 cluster_parent_id = cluster.parent_node_id 

79 cluster_id = cluster.id 

80 

81 # check we can't create a page with two owners 

82 with pytest.raises(IntegrityError) as e: 

83 with session_scope() as session: 

84 page = Page( 

85 parent_node_id=cluster_parent_id, 

86 creator_user_id=user.id, 

87 owner_cluster_id=cluster_id, 

88 owner_user_id=user.id, 

89 type=PageType.guide, 

90 thread=Thread(), 

91 ) 

92 session.add(page) 

93 session.add( 

94 PageVersion( 

95 page=page, 

96 editor_user_id=user.id, 

97 title="Title", 

98 content="Content", 

99 ) 

100 ) 

101 assert "violates check constraint" in str(e.value) 

102 assert "one_owner" in str(e.value) 

103 

104 # main page must be owned by the right cluster 

105 with pytest.raises(IntegrityError) as e: 

106 with session_scope() as session: 

107 main_page = Page( 

108 parent_node_id=cluster_parent_id, 

109 # note owner is not cluster 

110 creator_user_id=user.id, 

111 owner_user_id=user.id, 

112 type=PageType.main_page, 

113 thread=Thread(), 

114 ) 

115 session.add(main_page) 

116 session.add( 

117 PageVersion( 

118 page=main_page, 

119 editor_user_id=user.id, 

120 title="Main page for the testing community", 

121 content="Empty.", 

122 ) 

123 ) 

124 assert "violates check constraint" in str(e.value) 

125 assert "main_page_owned_by_cluster" in str(e.value) 

126 

127 # can only have one main page 

128 with pytest.raises(IntegrityError) as e: 

129 with session_scope() as session: 

130 main_page1 = Page( 

131 parent_node_id=cluster_parent_id, 

132 creator_user_id=user.id, 

133 owner_cluster_id=cluster_id, 

134 type=PageType.main_page, 

135 thread=Thread(), 

136 ) 

137 session.add(main_page1) 

138 session.add( 

139 PageVersion( 

140 page=main_page1, 

141 editor_user_id=user.id, 

142 title="Main page 1 for the testing community", 

143 content="Empty.", 

144 ) 

145 ) 

146 main_page2 = Page( 

147 parent_node_id=cluster_parent_id, 

148 creator_user_id=user.id, 

149 owner_cluster_id=cluster_id, 

150 type=PageType.main_page, 

151 thread=Thread(), 

152 ) 

153 session.add(main_page2) 

154 session.add( 

155 PageVersion( 

156 page=main_page2, 

157 editor_user_id=user.id, 

158 title="Main page 2 for the testing community", 

159 content="Empty.", 

160 ) 

161 ) 

162 assert "violates unique constraint" in str(e.value) 

163 assert "ix_pages_owner_cluster_id_type" in str(e.value)