Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

api_mocks.py 7.0 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  1. import pytest
  2. import os
  3. from six import binary_type
  4. import logging
  5. def _files():
  6. return {
  7. 'edges': [
  8. {'node': {
  9. 'name': 'weights.h5',
  10. 'url': 'https://weights.url',
  11. 'md5': 'fakemd5',
  12. 'updatedAt': None
  13. }},
  14. {'node': {
  15. 'name': 'model.json',
  16. 'url': 'https://model.url',
  17. 'md5': 'mZFLkyvTelC5g8XnyQrpOw==',
  18. 'updatedAt': None
  19. }},
  20. ]
  21. }
  22. def _download_urls(name='test', empty=False, files=None):
  23. files = {'edges': []} if empty else (files or _files())
  24. return {
  25. 'name': name,
  26. 'description': 'Test model',
  27. 'bucket': {
  28. 'id': 'test1234',
  29. 'framework': 'keras',
  30. 'files': files
  31. }
  32. }
  33. def _run_resume_status(name='test', empty=False, files=None):
  34. return {
  35. 'bucket': {
  36. 'name': name,
  37. 'id': name,
  38. 'logLineCount': 14,
  39. 'historyLineCount': 5,
  40. 'eventsLineCount': 2,
  41. 'historyTail': '[]',
  42. 'eventsTail': '[]'
  43. }
  44. }
  45. def _bucket(name='test'):
  46. return {
  47. 'name': name,
  48. 'description': "Description of the bucket",
  49. 'framework': 'keras',
  50. 'id': 'a1b2c3d4e5',
  51. 'files': _files()
  52. }
  53. def _run(name='test'):
  54. return {
  55. 'id': 'test',
  56. 'name': name,
  57. 'state': "running",
  58. 'config': '{"epochs": {"value": 10}}',
  59. 'systemMetrics': '{"cpu": 100}',
  60. 'summaryMetrics': '{"acc": 100, "loss": 0}',
  61. 'history': [
  62. '{"acc": 10, "loss": 90}',
  63. '{"acc": 20, "loss": 80}',
  64. '{"acc": 30, "loss": 70}'
  65. ],
  66. 'events': [
  67. '{"cpu": 10}',
  68. '{"cpu": 20}',
  69. '{"cpu": 30}'
  70. ]
  71. }
  72. def _bucket_config():
  73. return {
  74. 'patch': '''
  75. diff --git a/patch.txt b/patch.txt
  76. index 30d74d2..9a2c773 100644
  77. --- a/patch.txt
  78. +++ b/patch.txt
  79. @@ -1 +1 @@
  80. -test
  81. \ No newline at end of file
  82. +testing
  83. \ No newline at end of file
  84. ''',
  85. 'commit': 'HEAD',
  86. 'config': '{"foo":{"value":"bar"}}'
  87. }
  88. def success_or_failure(payload=None, body_match="query"):
  89. def wrapper(mocker, status_code=200, error=None):
  90. if error:
  91. body = {'errors': error}
  92. else:
  93. body = {'data': payload}
  94. def match_body(request):
  95. return body_match in (request.text or '')
  96. return mocker.register_uri('POST', 'https://api.wandb.ai/graphql',
  97. [{'json': body, 'status_code': status_code}], additional_matcher=match_body)
  98. return wrapper
  99. def _query(key, json, body_match="query"):
  100. payload = {}
  101. if type(json) == list:
  102. json = {'edges': [{'node': item} for item in json]}
  103. payload[key] = json
  104. return success_or_failure(payload=payload, body_match=body_match)
  105. def _mutate(key, json):
  106. payload = {}
  107. payload[key] = json
  108. return success_or_failure(payload=payload, body_match="mutation")
  109. @pytest.fixture
  110. def upsert_run(request):
  111. return _mutate('upsertBucket', {'bucket': _bucket("default")})
  112. @pytest.fixture
  113. def query_project():
  114. # this should really be called query_download_urls
  115. return _query('model', _download_urls(), body_match="updatedAt")
  116. @pytest.fixture
  117. def query_run_resume_status(request):
  118. return _query('model', _run_resume_status(), body_match="historyTail")
  119. @pytest.fixture
  120. def query_no_run_resume_status():
  121. return _query('model', {'bucket': None}, body_match="historyTail")
  122. @pytest.fixture
  123. def query_download_h5():
  124. def wrapper(mocker, status_code=200, error=None, content=None):
  125. mocker.register_uri('GET', 'https://h5py.url',
  126. content=content, status_code=status_code)
  127. return _query('model', _download_urls(files={'edges': [{'node': {
  128. 'name': 'wandb.h5',
  129. 'url': 'https://h5py.url',
  130. 'md5': 'fakemd5',
  131. 'updatedAt': 'now',
  132. }}]}), body_match="files(names: [")(mocker, status_code, error)
  133. return wrapper
  134. @pytest.fixture
  135. def query_upload_h5(mocker):
  136. def wrapper(mocker, status_code=200, error=None, content=None):
  137. mocker.register_uri('PUT', "https://h5py.url")
  138. return _query('model', _download_urls(files={'edges': [{'node': {
  139. 'name': 'wandb.h5',
  140. 'url': 'https://h5py.url',
  141. 'md5': 'fakemd5'
  142. }}]}), body_match='files(names: ')(mocker, status_code, error)
  143. return wrapper
  144. @pytest.fixture
  145. def query_empty_project():
  146. return _query('model', _download_urls(empty=True))
  147. @pytest.fixture
  148. def query_projects():
  149. return _query('models', [_download_urls("test_1"), _download_urls("test_2"), _download_urls("test_3")], body_match="query Models")
  150. @pytest.fixture
  151. def query_runs():
  152. return _query('buckets', [_bucket("default"), _bucket("test_1")])
  153. @pytest.fixture
  154. def query_run():
  155. return _query('model', {'bucket': _bucket_config()})
  156. @pytest.fixture
  157. def query_run_v2():
  158. return _query('project', {'run': _run()})
  159. @pytest.fixture
  160. def query_runs_v2():
  161. return _query('project', {
  162. 'runCount': 4,
  163. 'runs': {
  164. 'pageInfo': {
  165. 'hasNextPage': True,
  166. 'endCursor': 'end'
  167. },
  168. 'edges': [{'node': _run(),
  169. 'cursor': 'cursor'}, {'node': _run(),
  170. 'cursor': 'cursor'}]
  171. }
  172. })
  173. @pytest.fixture
  174. def query_viewer(request):
  175. marker = request.node.get_marker('teams')
  176. if marker:
  177. teams = marker.args
  178. else:
  179. teams = ['foo']
  180. return success_or_failure(payload={'viewer': {
  181. 'entity': 'foo',
  182. 'teams': {
  183. 'edges': [{'node': {'name': team}} for team in teams]
  184. }
  185. }}, body_match="query Viewer")
  186. @pytest.fixture
  187. def upload_url():
  188. def wrapper(mocker, status_code=200, headers={}):
  189. mocker.register_uri('PUT', 'https://weights.url',
  190. status_code=status_code, headers=headers)
  191. mocker.register_uri('PUT', 'https://model.url',
  192. status_code=status_code, headers=headers)
  193. return wrapper
  194. @pytest.fixture
  195. def download_url():
  196. def wrapper(mocker, status_code=200, error=None, size=5000):
  197. mocker.register_uri('GET', 'https://weights.url',
  198. content=os.urandom(size), status_code=status_code)
  199. mocker.register_uri('GET', 'https://model.url',
  200. content=os.urandom(size), status_code=status_code)
  201. return wrapper
  202. @pytest.fixture
  203. def upload_logs():
  204. def wrapper(mocker, run, status_code=200, body_match='', error=None):
  205. from wandb.cli import api
  206. def match_body(request):
  207. return body_match in (request.text or '')
  208. api._current_run_id = run
  209. url = api.get_file_stream_api()._endpoint
  210. return mocker.register_uri("POST", url,
  211. status_code=status_code, additional_matcher=match_body)
  212. return wrapper
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...