Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import datetime 

2import sys 

3import os 

4import json 

5 

6sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../../") 

7from sdc_etl_libs.api_helpers.apis.TimeControl.TimeControl import TimeControl 

8 

9 

10def test_no_data(mocker): 

11 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', 

12 return_value={"apikey": "baz"}) 

13 

14 class ReqMock(): 

15 status_code = 200 

16 content = "[]" 

17 

18 mocker.patch('requests.get', return_value=ReqMock) 

19 

20 timecontrol = TimeControl() 

21 

22 df = timecontrol.get_data('employees', 'employees', filter_=None, limit_=1000) 

23 assert (df == None) 

24 

25def test_endpoint_processing(mocker): 

26 

27 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', 

28 return_value={"apikey": "blah"}) 

29 

30 class ReqMock(): 

31 def __init__(self, status_, data_): 

32 self.status_code = status_ 

33 self.content = data_ 

34 

35 mocker.patch( 

36 'sdc_etl_libs.api_helpers.apis.TimeControl.TimeControl.TimeControl.process_endpoint', 

37 return_value= 

38 [ 

39 { 

40 "_metadata": { 

41 "links": [ 

42 { 

43 "id": "9", 

44 "rel": "self", 

45 "href": "/api/v1/languages/9", 

46 "code": "Ceština" 

47 } 

48 ] 

49 }, 

50 "Key": 9, 

51 "Name": "Ceština", 

52 "Description": "Czech", 

53 "Culture": "cs" 

54 }, 

55 { 

56 "_metadata": { 

57 "links": [ 

58 { 

59 "id": "5", 

60 "rel": "self", 

61 "href": "/api/v1/languages/5", 

62 "code": "Dansk" 

63 } 

64 ] 

65 }, 

66 "Key": 5, 

67 "Name": "Dansk", 

68 "Description": "Danish", 

69 "Culture": "da" 

70 }, 

71 { 

72 "_metadata": { 

73 "links": [ 

74 { 

75 "id": "9", 

76 "rel": "self", 

77 "href": "/api/v1/languages/9", 

78 "code": "Ceština" 

79 } 

80 ] 

81 }, 

82 "Key": 9, 

83 "Name": "Ceština", 

84 "Description": "Czech", 

85 "Culture": "cs" 

86 }, 

87 { 

88 "_metadata": { 

89 "links": [ 

90 { 

91 "id": "5", 

92 "rel": "self", 

93 "href": "/api/v1/languages/5", 

94 "code": "Dansk" 

95 } 

96 ] 

97 }, 

98 "Key": 5, 

99 "Name": "Dansk", 

100 "Description": "Danish", 

101 "Culture": "da" 

102 } 

103 ] 

104 ) 

105 

106 timecontrol = TimeControl() 

107 

108 df = timecontrol.get_data('languages', 'languages', filter_=None, 

109 limit_=1000) 

110 assert len(df.df) == 4 

111 

112 

113def test_pagination_of_data(mocker): 

114 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', 

115 return_value={"apikey": "blah"}) 

116 

117 class ReqMock(): 

118 def __init__(self, status_, data_): 

119 self.status_code = status_ 

120 self.content = data_ 

121 

122 def get_mock(url_, **kwargs): 

123 print(url_) 

124 

125 page_1 = [ 

126 { 

127 "_metadata": { 

128 "links": [ 

129 { 

130 "id": "9", 

131 "rel": "self", 

132 "href": "/api/v1/languages/9", 

133 "code": "Ceština" 

134 } 

135 ] 

136 }, 

137 "Key": 9, 

138 "Name": "Ceština", 

139 "Description": "Czech", 

140 "Culture": "cs" 

141 }, 

142 { 

143 "_metadata": { 

144 "links": [ 

145 { 

146 "id": "5", 

147 "rel": "self", 

148 "href": "/api/v1/languages/5", 

149 "code": "Dansk" 

150 } 

151 ] 

152 }, 

153 "Key": 5, 

154 "Name": "Dansk", 

155 "Description": "Danish", 

156 "Culture": "da" 

157 } 

158 ] 

159 page_2 = [ 

160 { 

161 "_metadata": { 

162 "links": [ 

163 { 

164 "id": "9", 

165 "rel": "self", 

166 "href": "/api/v1/languages/9", 

167 "code": "Ceština" 

168 } 

169 ] 

170 }, 

171 "Key": 9, 

172 "Name": "Ceština", 

173 "Description": "Czech", 

174 "Culture": "cs" 

175 }, 

176 { 

177 "_metadata": { 

178 "links": [ 

179 { 

180 "id": "5", 

181 "rel": "self", 

182 "href": "/api/v1/languages/5", 

183 "code": "Dansk" 

184 } 

185 ] 

186 }, 

187 "Key": 5, 

188 "Name": "Dansk", 

189 "Description": "Danish", 

190 "Culture": "da" 

191 } 

192 ] 

193 if url_ == 'https://smiledirectclub.timecontrol.net/api/v1/languages?$skip=0&$top=2': 

194 return ReqMock(200, json.dumps(page_1)) 

195 elif url_ == 'https://smiledirectclub.timecontrol.net/api/v1/languages?$skip=2&$top=2': 

196 return ReqMock(200, json.dumps(page_2)) 

197 else: 

198 return ReqMock(200, "[]") 

199 

200 mock_get = mocker.patch('requests.get', new_callable=lambda: get_mock) 

201 

202 timecontrol = TimeControl() 

203 

204 df = timecontrol.get_data('languages', 'languages', filter_=None, 

205 limit_=2) 

206 assert len(df.df) == 4 

207 

208def test_key_filter(mocker): 

209 

210 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', 

211 return_value={"apikey": "baz"}) 

212 

213 timecontrol = TimeControl() 

214 

215 filter = timecontrol.get_key_filter(start_key_=25, json_key_path_="Key") 

216 

217 assert filter == 'Key ge 25' 

218 

219def test_key_filter_with_path(mocker): 

220 

221 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', 

222 return_value={"apikey": "baz"}) 

223 

224 timecontrol = TimeControl() 

225 

226 filter = timecontrol.get_key_filter(start_key_=25, json_key_path_="Employee/Key") 

227 

228 assert filter == 'Employee/Key ge 25' 

229 

230def test_daily_filter(mocker): 

231 

232 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', 

233 return_value={"apikey": "baz"}) 

234 

235 datetime_ = datetime.datetime(2019, 10, 14, 10, 55, 5, 990098) 

236 

237 timecontrol = TimeControl() 

238 

239 filter = timecontrol.get_daily_filter(datetime_=datetime_, days_=3, 

240 filter_field_list_=['LastModifiedAt']) 

241 

242 assert filter == "(LastModifiedAt ge DateTime'2019-10-11T00:00:00' and LastModifiedAt lt DateTime'2019-10-14T00:00:00')" 

243 

244 

245def test_daily_filter_with_multiple_fields(mocker): 

246 

247 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', 

248 return_value={"apikey": "baz"}) 

249 

250 datetime_ = datetime.datetime(2019, 10, 14, 10, 55, 5, 990098) 

251 

252 timecontrol = TimeControl() 

253 

254 filter = timecontrol.get_daily_filter(datetime_=datetime_, days_=3, 

255 filter_field_list_=['LastModifiedAt','CreatedDate']) 

256 

257 assert filter == "(LastModifiedAt ge DateTime'2019-10-11T00:00:00' and LastModifiedAt lt DateTime'2019-10-14T00:00:00') " \ 

258 "or (CreatedDate ge DateTime'2019-10-11T00:00:00' and CreatedDate lt DateTime'2019-10-14T00:00:00')"