Coverage for tests/store/test_store_routes.py: 100%

63 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-17 22:07 +0000

1import unittest 

2from unittest.mock import patch 

3from webapp.app import app 

4from webapp.store.logic import fetch_rocks, get_rock 

5 

6 

7class StoreRouteTests(unittest.TestCase): 

8 def setUp(self): 

9 self.client = app.test_client() 

10 

11 @patch("webapp.store.views.get_rocks") 

12 def test_get_store_packages_json(self, mock_get_rocks): 

13 mock_get_rocks.return_value = { 

14 "packages": [], 

15 "total_pages": 1, 

16 "total_items": 0, 

17 } 

18 response = self.client.get("/store.json") 

19 self.assertEqual(response.status_code, 200) 

20 self.assertEqual(response.json["total_pages"], 1) 

21 

22 @patch("webapp.store.logic.device_gw.get_item_details") 

23 def test_store_details(self, mock_get_item_details): 

24 mock_get_item_details.return_value = { 

25 "name": "sample-rock", 

26 "metadata": { 

27 "description": "Test rock", 

28 "summary": "A summary", 

29 "license": "Apache-2.0", 

30 "links": {}, 

31 "media": [], 

32 "private": False, 

33 "publisher": { 

34 "display-name": "Test Publisher", 

35 "username": "testuser", 

36 "validation": "verified", 

37 }, 

38 "categories": [], 

39 }, 

40 "channel-map": [ 

41 { 

42 "channel": { 

43 "risk": "stable", 

44 "released-at": "2024-06-01T12:00:00+00:00", 

45 "track": "latest", 

46 }, 

47 "revision": { 

48 "version": "1.0", 

49 "revision": 1, 

50 }, 

51 } 

52 ], 

53 } 

54 

55 response = self.client.get("/sample-rock") 

56 self.assertEqual(response.status_code, 200) 

57 response_text = response.get_data(as_text=True) 

58 self.assertIn("sample-rock", response_text) 

59 self.assertIn("Test rock", response_text) 

60 

61 

62class TestGetRockCaching(unittest.TestCase): 

63 def setUp(self): 

64 self.entity_name = "sample-rock" 

65 self.raw_rock = { 

66 "name": "sample-rock", 

67 "metadata": { 

68 "description": "Test rock", 

69 "summary": "A summary", 

70 "license": "Apache-2.0", 

71 "links": {}, 

72 "media": [], 

73 "private": False, 

74 "publisher": { 

75 "display-name": "Test Publisher", 

76 "username": "testuser", 

77 "validation": "verified", 

78 }, 

79 "categories": [], 

80 }, 

81 "channel-map": [ 

82 { 

83 "channel": { 

84 "risk": "stable", 

85 "released-at": "2024-06-01T12:00:00+00:00", 

86 "track": "latest", 

87 }, 

88 "revision": { 

89 "version": "1.0", 

90 "revision": 1, 

91 }, 

92 } 

93 ], 

94 } 

95 

96 @patch("webapp.store.logic.device_gw.get_item_details") 

97 @patch("webapp.store.logic.cache") 

98 def test_get_rock_cache_miss(self, mock_cache, mock_get_item_details): 

99 """If rock not in cache, call device_gw and set cache.""" 

100 mock_cache.get.return_value = None 

101 mock_get_item_details.return_value = self.raw_rock 

102 

103 rock = get_rock(self.entity_name) 

104 

105 mock_cache.get.assert_called_once_with( 

106 f"get_rock:{self.entity_name}", expected_type=dict 

107 ) 

108 mock_get_item_details.assert_called_once_with( 

109 self.entity_name, 

110 fields=mock_get_item_details.call_args[1]["fields"], 

111 ) 

112 mock_cache.set.assert_called_once() 

113 self.assertEqual(rock["name"], "sample-rock") 

114 

115 @patch("webapp.store.logic.device_gw.get_item_details") 

116 @patch("webapp.store.logic.cache") 

117 def test_get_rock_cache_hit(self, mock_cache, mock_get_item_details): 

118 """If rock is already cached, it should not call device_gw.""" 

119 cached_rock = {"name": "sample-rock", "channels": []} 

120 mock_cache.get.return_value = cached_rock 

121 

122 rock = get_rock(self.entity_name) 

123 

124 mock_cache.get.assert_called_once_with( 

125 f"get_rock:{self.entity_name}", expected_type=dict 

126 ) 

127 mock_get_item_details.assert_not_called() 

128 mock_cache.set.assert_not_called() 

129 self.assertEqual(rock, cached_rock) 

130 

131 @patch("webapp.store.logic.device_gw.find") 

132 @patch("webapp.store.logic.cache") 

133 def test_fetch_rocks_cache_miss(self, mock_cache, mock_find): 

134 mock_cache.get.return_value = None 

135 mock_find.return_value = {"results": [self.raw_rock]} 

136 

137 fetch_rocks("test") 

138 

139 mock_cache.get.assert_called_once_with( 

140 ("fetch_rocks", {"q": "test"}), expected_type=list 

141 ) 

142 mock_find.assert_called_once_with( 

143 "test", fields=mock_find.call_args[1]["fields"] 

144 ) 

145 mock_cache.set.assert_called_once() 

146 

147 @patch("webapp.store.logic.device_gw.find") 

148 @patch("webapp.store.logic.cache") 

149 def test_fetch_rocks_cache_hit(self, mock_cache, mock_find): 

150 cached_list = [{"sample_rock": "cached-rock"}] 

151 mock_cache.get.return_value = cached_list 

152 

153 rocks = fetch_rocks("test") 

154 

155 mock_find.assert_not_called() 

156 mock_cache.set.assert_not_called() 

157 self.assertEqual(rocks, cached_list)