Coverage for tests/test_namespace.py: 95%

103 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-01 22:07 +0000

1import pytest 

2 

3from pepdbagent.exceptions import ProjectAlreadyInFavorites, ProjectNotInFavorites 

4 

5from .utils import PEPDBAgentContextManager 

6 

7 

8@pytest.mark.skipif( 

9 not PEPDBAgentContextManager().db_setup(), 

10 reason="DB is not setup", 

11) 

12class TestNamespace: 

13 """ 

14 Test function within namespace class 

15 """ 

16 

17 def test_annotation(self): 

18 with PEPDBAgentContextManager(add_data=True) as agent: 

19 result = agent.namespace.get() 

20 assert len(result.results) == 3 

21 

22 def test_annotation_private(self): 

23 with PEPDBAgentContextManager(add_data=True) as agent: 

24 result = agent.namespace.get(admin="private_test") 

25 assert len(result.results) == 4 

26 

27 @pytest.mark.skip( 

28 "Skipping test because we are not taking into account the private projects (We are counting all of them)" 

29 ) 

30 def test_namespace_info_private(self): 

31 with PEPDBAgentContextManager(add_data=True) as agent: 

32 agent.project.update( 

33 namespace="private_test", 

34 name="derive", 

35 tag="default", 

36 update_dict={"is_private": False}, 

37 ) 

38 result = agent.namespace.info() 

39 assert len(result.results) == 4 

40 assert result.results[3].number_of_projects == 1 

41 

42 def test_namespace_info_all(self): 

43 with PEPDBAgentContextManager(add_data=True) as agent: 

44 agent.project.update( 

45 namespace="private_test", 

46 name="derive", 

47 tag="default", 

48 update_dict={"is_private": False}, 

49 ) 

50 result = agent.namespace.info() 

51 assert len(result.results) == 4 

52 assert result.results[3].number_of_projects == 6 

53 

54 def test_namespace_stats(self): 

55 with PEPDBAgentContextManager(add_data=True) as agent: 

56 stat_result = agent.namespace.stats(monthly=True) 

57 assert next(iter(stat_result.projects_created.values()), 0) == 30 

58 

59 

60@pytest.mark.skipif( 

61 not PEPDBAgentContextManager().db_setup(), 

62 reason="DB is not setup", 

63) 

64class TestFavorites: 

65 """ 

66 Test method related to favorites 

67 """ 

68 

69 def test_add_projects_to_favorites(self): 

70 with PEPDBAgentContextManager(add_data=True) as agent: 

71 result = agent.annotation.get( 

72 namespace="namespace1", 

73 ) 

74 for project in result.results: 

75 agent.user.add_project_to_favorites( 

76 "random_namespace", project.namespace, project.name, "default" 

77 ) 

78 fav_results = agent.user.get_favorites("random_namespace") 

79 

80 assert fav_results.count == len(result.results) 

81 

82 # This can fail if the order of the results is different 

83 assert fav_results.results[0].namespace == result.results[0].namespace 

84 

85 def test_count_project_none(self): 

86 with PEPDBAgentContextManager(add_data=True) as agent: 

87 result = agent.user.get_favorites("private_test") 

88 assert result.count == 0 

89 

90 @pytest.mark.parametrize( 

91 "namespace, name", 

92 [ 

93 ["namespace1", "amendments1"], 

94 ], 

95 ) 

96 def test_count_project_one(self, namespace, name): 

97 with PEPDBAgentContextManager(add_data=True) as agent: 

98 agent.user.add_project_to_favorites(namespace, namespace, name, "default") 

99 result = agent.user.get_favorites("namespace1") 

100 assert result.count == 1 

101 result1 = agent.user.get_favorites("private_test") 

102 assert result1.count == 0 

103 

104 @pytest.mark.parametrize( 

105 "namespace, name", 

106 [ 

107 ["namespace1", "amendments1"], 

108 ], 

109 ) 

110 def test_remove_from_favorite(self, namespace, name): 

111 with PEPDBAgentContextManager(add_data=True) as agent: 

112 agent.user.add_project_to_favorites( 

113 "namespace1", namespace, name, "default" 

114 ) 

115 agent.user.add_project_to_favorites( 

116 "namespace1", namespace, "amendments2", "default" 

117 ) 

118 result = agent.user.get_favorites("namespace1") 

119 assert result.count == len(result.results) == 2 

120 agent.user.remove_project_from_favorites( 

121 "namespace1", namespace, name, "default" 

122 ) 

123 result = agent.user.get_favorites("namespace1") 

124 assert result.count == len(result.results) == 1 

125 

126 @pytest.mark.parametrize( 

127 "namespace, name", 

128 [ 

129 ["namespace1", "amendments1"], 

130 ], 

131 ) 

132 def test_remove_from_favorite_error(self, namespace, name): 

133 with PEPDBAgentContextManager(add_data=True) as agent: 

134 with pytest.raises(ProjectNotInFavorites): 

135 agent.user.remove_project_from_favorites( 

136 "namespace1", namespace, name, "default" 

137 ) 

138 

139 @pytest.mark.parametrize( 

140 "namespace, name", 

141 [ 

142 ["namespace1", "amendments1"], 

143 ], 

144 ) 

145 def test_favorites_duplication_error(self, namespace, name): 

146 with PEPDBAgentContextManager(add_data=True) as agent: 

147 agent.user.add_project_to_favorites( 

148 "namespace1", namespace, name, "default" 

149 ) 

150 with pytest.raises(ProjectAlreadyInFavorites): 

151 agent.user.add_project_to_favorites( 

152 "namespace1", namespace, name, "default" 

153 ) 

154 

155 @pytest.mark.parametrize( 

156 "namespace, name", 

157 [ 

158 ["namespace1", "amendments1"], 

159 ], 

160 ) 

161 def test_annotation_favorite_number(self, namespace, name): 

162 with PEPDBAgentContextManager(add_data=True) as agent: 

163 agent.user.add_project_to_favorites( 

164 "namespace1", namespace, name, "default" 

165 ) 

166 annotations_in_namespace = agent.annotation.get("namespace1") 

167 

168 for prj_annot in annotations_in_namespace.results: 

169 if prj_annot.name == name: 

170 assert prj_annot.stars_number == 1 

171 else: 

172 assert prj_annot.stars_number == 0 

173 

174 

175@pytest.mark.skipif( 

176 not PEPDBAgentContextManager().db_setup(), 

177 reason="DB is not setup", 

178) 

179class TestUser: 

180 """ 

181 Test methods within user class 

182 """ 

183 

184 def test_create_user(self): 

185 with PEPDBAgentContextManager(add_data=True) as agent: 

186 agent.user.create_user("test_user") 

187 

188 assert agent.user.exists("test_user") 

189 

190 def test_delete_user(self): 

191 with PEPDBAgentContextManager(add_data=True) as agent: 

192 test_user = "test_user" 

193 agent.user.create_user(test_user) 

194 assert agent.user.exists(test_user) 

195 agent.user.delete(test_user) 

196 assert not agent.user.exists(test_user) 

197 

198 def test_delete_user_deletes_projects(self): 

199 with PEPDBAgentContextManager(add_data=True) as agent: 

200 test_user = "namespace1" 

201 

202 assert agent.user.exists(test_user) 

203 agent.user.delete(test_user) 

204 assert not agent.user.exists(test_user) 

205 results = agent.namespace.get(query=test_user) 

206 assert len(results.results) == 0