-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_client.py
More file actions
1042 lines (853 loc) · 38.9 KB
/
test_client.py
File metadata and controls
1042 lines (853 loc) · 38.9 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import time
import types
from encodings.utf_8 import encode
from pathlib import Path
from unittest.mock import MagicMock, patch
import pandas as pd
import pytest
from requests import HTTPError, Response
from requests.exceptions import InvalidJSONError
from tenacity import RetryError
import datareservoirio as drio
from datareservoirio._logging import get_exceptions_logger
from datareservoirio._utils import DataHandler
TEST_PATH = Path(__file__).parent
exceptions_logger = get_exceptions_logger()
def change_logging(self, msg, *args, exc_info=True, **kwargs):
if kwargs["extra"]:
self.was_called = True
else:
raise ValueError("Missing extra parameters")
def drioResponseWithNoFiles():
response = Response()
json_data = {"Files": []}
response._content = json.dumps(json_data).encode("utf-8")
response.headers["Content-Type"] = "application/json"
response.status_code = 200
return response
def failed_connection_error(self, url, timeout):
if hasattr(self, "call_count"):
self.call_count = self.call_count + 1
else:
self.call_count = 1
if self.call_count >= 3:
return drioResponseWithNoFiles()
else:
raise ConnectionError()
def fail_with_invalid_json_error(self, url, timeout):
if hasattr(self, "call_count"):
self.call_count = self.call_count + 1
else:
self.call_count = 1
if self.call_count >= 3:
return drioResponseWithNoFiles()
else:
raise InvalidJSONError()
def _mock_blob_sequence_days(response_json):
return {1: "file1", 2: "file2"}
class Test_Client:
"""
Tests the ``datareservoirio.Client`` class.
"""
@pytest.fixture
def group1_data(self):
data = DataHandler.from_csv(
TEST_PATH / "testdata" / "response_cases" / "group1" / "dataframe.csv"
)
return data
@pytest.fixture
def group2_data(self):
"""Overlapping data"""
data = DataHandler.from_csv(
TEST_PATH / "testdata" / "response_cases" / "group2" / "dataframe.csv"
)
return data
@pytest.fixture
def client(self, auth_session):
return drio.Client(auth_session, cache=False)
@pytest.fixture
def cache_root(self, tmp_path):
return tmp_path / ".cache"
@pytest.fixture
def client_with_cache(self, auth_session, cache_root):
cache_opt = {"max_size": 1024, "cache_root": cache_root}
return drio.Client(auth_session, cache=True, cache_opt=cache_opt)
def test__init__(self, auth_session, tmp_path):
cache_opt = {
"max_size": 1024,
"cache_root": tmp_path / ".cache",
}
drio.Client(auth_session, cache=True, cache_opt=cache_opt)
def client_error_handler(self, client, method):
exceptions_logger.exception = types.MethodType(change_logging, client)
client._auth_session.get = types.MethodType(method, client._auth_session)
return client
@pytest.fixture
def client_with_connection_error(self, client):
return self.client_error_handler(client, failed_connection_error)
@pytest.fixture
def client_with_invalid_json_error(self, client):
return self.client_error_handler(client, fail_with_invalid_json_error)
@pytest.mark.parametrize(
"start, end",
[
(1672358400000000000, 1672703939999999999 + 1),
("2022-12-30T00:00:00+00:00", "2023-01-02T23:59:00+00:00"),
(pd.to_datetime("2022-12-30T00:00"), pd.to_datetime("2023-01-02T23:59:00")),
(None, None),
],
)
def test_get(self, mock_requests, client, start, end, group1_data, response_cases):
response_cases.set("group1")
series_out = client.get(
"2fee7f8a-664a-41c9-9b71-25090517c275",
start=start,
end=end,
convert_date=False,
)
series_expect = group1_data.as_series()
pd.testing.assert_series_equal(series_out, series_expect)
# Check that the correct HTTP request is made
if start is not None and end is not None:
request_url_expect = "https://reservoir-api.4subsea.net/api/timeseries/2fee7f8a-664a-41c9-9b71-25090517c275/data/days?start=1672358400000000000&end=1672703939999999999"
else:
request_url_expect = "https://reservoir-api.4subsea.net/api/timeseries/2fee7f8a-664a-41c9-9b71-25090517c275/data/days?start=0&end=9214646399999999999"
assert mock_requests.call_args_list[0].args[1] == request_url_expect
def test_get_convert_date(self, client, group1_data, response_cases):
response_cases.set("group1")
series_out = client.get(
"2fee7f8a-664a-41c9-9b71-25090517c275",
start=1672358400000000000,
end=1672703939999999999 + 1,
convert_date=True,
)
series_expect = group1_data.as_series()
series_expect.index = pd.to_datetime(series_expect.index, utc=True)
pd.testing.assert_series_equal(series_out, series_expect)
def test_get_raise_empty(self, client):
with pytest.raises(ValueError):
client.get("e3d82cda-4737-4af9-8d17-d9dfda8703d0", raise_empty=True)
def test_get_keyerror(self, client):
series_id = "test_series_id"
start = "2023-01-01"
end = "2023-01-02"
response_mock = MagicMock()
response_mock.status_code = 200
response_mock.json.return_value = {
"Files": [
{"Chunks": "file1"},
{"Chunks": "file2"},
] # mock files with correct structure
}
client._auth_session.get = MagicMock(return_value=response_mock)
def mock_storage_get(blob_sequence_i):
if blob_sequence_i == "file1":
return pd.DataFrame(
{
"index": [1672358410000000000, 1672358400000000000],
"values": [100, 200],
}
)
elif blob_sequence_i == "file2":
return pd.DataFrame(
{
"index": [1672358410000000000, 1672358420000000000],
"values": [200, 400],
}
)
else:
raise ValueError("Unexpected blob_sequence_i value")
client._storage.get = MagicMock(side_effect=mock_storage_get)
with patch(
"datareservoirio.client._blob_sequence_days",
side_effect=_mock_blob_sequence_days,
):
with patch("datareservoirio.logging.warning") as mock_logging_warning:
result = client.get(series_id, start, end)
mock_logging_warning.assert_called_once_with(
"The time series you requested is not properly ordered. The data will be sorted to attempt to resolve the issue. Please note that this operation may take some time."
)
assert isinstance(result, pd.Series)
def test_get_raises_end_not_after_start(self, client):
start = 1672358400000000000
end = start - 1
with pytest.raises(ValueError):
client.get(
"2fee7f8a-664a-41c9-9b71-25090517c275",
start=start,
end=end,
)
def test_get_empty(self, client, response_cases):
response_cases.set("datareservoirio-api")
series_out = client.get(
"e3d82cda-4737-4af9-8d17-d9dfda8703d0",
start=None,
end=None,
convert_date=True,
)
series_expect = pd.Series(name="values", dtype="object")
series_expect.index.name = None
series_expect.index = pd.to_datetime(series_expect.index, utc=True)
pd.testing.assert_series_equal(series_out, series_expect)
def test_get_overlapping(self, client, group2_data, response_cases):
response_cases.set("group2")
series_out = client.get(
"693cb0b2-3599-46d3-b263-ea913a648535",
start=1672358400000000000,
end=1672617600000000000 + 1,
convert_date=False,
)
series_expect = group2_data.as_series()
pd.testing.assert_series_equal(series_out, series_expect)
def test_get_with_cache(
self,
client_with_cache,
cache_root,
STOREFORMATVERSION,
group2_data,
response_cases,
):
response_cases.set("group2")
# Check that the cache folder is empty
cache_path_expect = cache_root / STOREFORMATVERSION
assert cache_path_expect.exists()
assert len(list(cache_path_expect.iterdir())) == 0
# Get data (and store in cache)
series_out = client_with_cache.get(
"693cb0b2-3599-46d3-b263-ea913a648535",
start=1672358400000000000,
end=1672617600000000000 + 1,
convert_date=False,
)
series_expect = group2_data.as_series()
pd.testing.assert_series_equal(series_out, series_expect)
# Check that the cache folder now contains six files
assert len(list(cache_path_expect.iterdir())) == 6
# Get data (from cache)
time_before_get = time.time()
time.sleep(0.1)
series_out = client_with_cache.get(
"693cb0b2-3599-46d3-b263-ea913a648535",
start=1672358400000000000,
end=1672617600000000000 + 1,
convert_date=False,
)
time.sleep(0.1)
time_after_get = time.time()
for cache_file_i in cache_path_expect.iterdir():
time_access_file_i = os.path.getatime(cache_file_i) # last access time
assert time_before_get < time_access_file_i < time_after_get
pd.testing.assert_series_equal(series_out, series_expect)
def test_ping(self, client, response_cases):
response_cases.set("datareservoirio-api")
ping_out = client.ping()
ping_json = (
TEST_PATH
/ "testdata"
/ "response_cases"
/ "datareservoirio_api"
/ "ping.json"
)
with open(ping_json, mode="r") as f:
ping_expect = json.load(f)
assert ping_out == ping_expect
def test_info(self, client, response_cases):
response_cases.set("datareservoirio-api")
info_out = client.info("2fee7f8a-664a-41c9-9b71-25090517c275")
info_json = (
TEST_PATH
/ "testdata"
/ "response_cases"
/ "datareservoirio_timeseries_api"
/ "info.json"
)
with open(info_json, mode="r") as f:
info_expect = json.load(f)
assert info_out == info_expect
def test_search(self, client, response_cases):
response_cases.set("datareservoirio-api")
search_out = client.search("foo.bar")
search_expect = {
"ae3fe5c0-5521-4738-9664-87ef6c112cd8": [
"{'Namespace':'foo.bar','Key':'baz','Value':{'a':1,'b':'something','d':1234}}",
"{'Namespace':'foo.bar','Key':'other','Value':{'a':1,'b':'something','d':1234}}",
],
"34a89adf-90af-40bb-8a99-b9ea710f01fd": [
"{'Namespace':'foo.bar','Key':'baz','Value':{'a':1,'b':'something','d':1234}}"
],
}
assert search_out == search_expect
def test_search_key(self, client, response_cases):
response_cases.set("datareservoirio-api")
search_out = client.search("foo.bar", key="baz")
search_expect = {
"ae3fe5c0-5521-4738-9664-87ef6c112cd8": [
"{'Namespace':'foo.bar','Key':'baz','Value':{'a':1,'b':'something','d':1234}}",
],
"34a89adf-90af-40bb-8a99-b9ea710f01fd": [
"{'Namespace':'foo.bar','Key':'baz','Value':{'a':1,'b':'something','d':1234}}"
],
}
assert search_out == search_expect
def test_search_key_name(self, client, response_cases):
response_cases.set("datareservoirio-api")
search_out = client.search("foo.bar", key="baz", name="a")
search_expect = {
"ae3fe5c0-5521-4738-9664-87ef6c112cd8": [
"{'Namespace':'foo.bar','Key':'baz','Value':{'a':1,'b':'something','d':1234}}",
],
"34a89adf-90af-40bb-8a99-b9ea710f01fd": [
"{'Namespace':'foo.bar','Key':'baz','Value':{'a':1,'b':'something','d':1234}}"
],
}
assert search_out == search_expect
def test_search_key_name_value(self, client, response_cases):
response_cases.set("datareservoirio-api")
search_out = client.search("foo.bar", key="baz", name="a", value="1")
search_expect = [
"ae3fe5c0-5521-4738-9664-87ef6c112cd8",
"34a89adf-90af-40bb-8a99-b9ea710f01fd",
]
assert search_out == search_expect
def test_delete(self, client, mock_requests, response_cases):
response_cases.set("datareservoirio-api")
client.delete("7bd106dd-d87f-4504-a888-6aeaff1ec31f")
# Check that the correct URL is poked
request_url_expect = "https://reservoir-api.4subsea.net/api/timeseries/7bd106dd-d87f-4504-a888-6aeaff1ec31f"
assert mock_requests.call_args.args[1] == request_url_expect
def test_create(self, client, monkeypatch, response_cases):
response_cases.set("datareservoirio-api")
def uuid4_mock():
return "9f74b0b1-54c2-4148-8854-5f78b81bb592"
monkeypatch.setattr("datareservoirio.client.uuid4", uuid4_mock)
create_out = client.create()
create_expect = {"TimeSeriesId": "9f74b0b1-54c2-4148-8854-5f78b81bb592"}
assert create_out == create_expect
def test_create_with_data(
self, client, data_float, mock_requests, bytesio_with_memory, response_cases
):
response_cases.set("group3")
create_out = client.create(
series=data_float.as_series(), wait_on_verification=True
)
create_expect = {
"FileId": "e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2",
"TimeSeriesId": "d30519af-5035-4093-a425-dafd857ad0ef",
"TimeOfFirstSample": 1640995215379000000,
"TimeOfLastSample": 1640995271472000000,
}
assert create_out == create_expect
# Check first HTTP call to /api/files/upload
call_url = mock_requests.call_args_list[0].args[1]
call_url_expect = "https://reservoir-api.4subsea.net/api/files/upload"
assert call_url == call_url_expect
# Check second HTTP call to blob
call_url = mock_requests.call_args_list[1].kwargs["url"]
call_url_expect = "https://reservoirprod.blob.core.windows.net/files/e4fb7a7e07964f6a8c79f39a3af66dd2?sv=2021-10-04&spr=https&se=2023-04-28T10%3A30%3A10Z&sr=b&sp=rw&sig=Clj4cdfu%2FWivUqhnsxShkmG8STLmnzcCLzDEniSQZZg%3D"
assert call_url == call_url_expect
call_data = mock_requests.call_args_list[1].kwargs["data"]
assert call_data.memory == data_float.as_binary_csv()
# Check third HTTP call to /api/files/commit
call_url = mock_requests.call_args_list[2].kwargs["url"]
call_url_expect = "https://reservoir-api.4subsea.net/api/files/commit"
assert call_url == call_url_expect
call_json = mock_requests.call_args_list[2].kwargs["json"]
call_json_expect = {"FileId": "e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2"}
assert call_json == call_json_expect
# Check fourth HTTP call to /api/files/{id}/status
call_url = mock_requests.call_args_list[3].args[1]
call_url_expect = "https://reservoir-api.4subsea.net/api/files/e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2/status"
assert call_url == call_url_expect
# Check fifth HTTP call to /api/timeseries/create
call_url = mock_requests.call_args_list[4].args[1]
call_url_expect = "https://reservoir-api.4subsea.net/api/timeseries/create"
assert call_url == call_url_expect
call_data = mock_requests.call_args_list[4].kwargs["data"]
call_data_expect = {"FileId": "e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2"}
assert call_data == call_data_expect
def test_create_failed(self, client, data_float, response_cases):
response_cases.set("group3-failed")
create_out = client.create(
series=data_float.as_series(), wait_on_verification=True
)
create_expect = "Failed"
assert create_out == create_expect
def test_create_upload_raises(self, client, data_float, response_cases):
response_cases.set("group3-upload-raises")
with pytest.raises(HTTPError):
client.create(series=data_float.as_series(), wait_on_verification=True)
def test_create_raises_valueerror_unsorted_index(self, client):
data = pd.Series(
[1, 2, 3],
index=[
pd.to_datetime("2022-04-04"),
pd.to_datetime("2022-04-03"),
pd.to_datetime("2022-04-05"),
],
)
with pytest.raises(ValueError) as e:
client.create(data)
assert (
str(e.value)
== "Index not sorted. Please sort series on index before creating a timeseries."
)
def test_append(
self, client, data_float, mock_requests, bytesio_with_memory, response_cases
):
response_cases.set("group3")
series_id = "d30519af-5035-4093-a425-dafd857ad0ef"
append_out = client.append(
data_float.as_series(), series_id, wait_on_verification=True
)
append_expect = {
"FileId": "ae7ef55f-6861-44b4-be06-a1f789221c93",
"TimeSeriesId": "d30519af-5035-4093-a425-dafd857ad0ef",
"TimeOfFirstSample": 1640995215379000000,
"TimeOfLastSample": 1640995271472000000,
}
assert append_out == append_expect
# Check first HTTP call to /api/files/upload
call_url = mock_requests.call_args_list[0].args[1]
call_url_expect = "https://reservoir-api.4subsea.net/api/files/upload"
assert call_url == call_url_expect
# Check second HTTP call to blob
call_url = mock_requests.call_args_list[1].kwargs["url"]
call_url_expect = "https://reservoirprod.blob.core.windows.net/files/e4fb7a7e07964f6a8c79f39a3af66dd2?sv=2021-10-04&spr=https&se=2023-04-28T10%3A30%3A10Z&sr=b&sp=rw&sig=Clj4cdfu%2FWivUqhnsxShkmG8STLmnzcCLzDEniSQZZg%3D"
assert call_url == call_url_expect
call_data = mock_requests.call_args_list[1].kwargs["data"]
assert call_data.memory == data_float.as_binary_csv()
# Check third HTTP call to /api/files/commit
call_url = mock_requests.call_args_list[2].kwargs["url"]
call_url_expect = "https://reservoir-api.4subsea.net/api/files/commit"
assert call_url == call_url_expect
call_json = mock_requests.call_args_list[2].kwargs["json"]
call_json_expect = {"FileId": "e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2"}
assert call_json == call_json_expect
# Check fourth HTTP call to /api/files/{id}/status
call_url = mock_requests.call_args_list[3].args[1]
call_url_expect = "https://reservoir-api.4subsea.net/api/files/e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2/status"
assert call_url == call_url_expect
# Check fifth HTTP call to /api/timeseries/create
call_url = mock_requests.call_args_list[4].args[1]
call_url_expect = "https://reservoir-api.4subsea.net/api/timeseries/add"
assert call_url == call_url_expect
call_data = mock_requests.call_args_list[4].kwargs["data"]
call_data_expect = {
"TimeSeriesId": "d30519af-5035-4093-a425-dafd857ad0ef",
"FileId": "e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2",
}
assert call_data == call_data_expect
def test_append_failed(self, client, data_float, response_cases):
response_cases.set("group3-failed")
series_id = "d30519af-5035-4093-a425-dafd857ad0ef"
append_out = client.append(
data_float.as_series(), series_id, wait_on_verification=True
)
append_expect = "Failed"
assert append_out == append_expect
def test_append_upload_raises(self, client, data_float, response_cases):
response_cases.set("group3-upload-raises")
series_id = "d30519af-5035-4093-a425-dafd857ad0ef"
with pytest.raises(HTTPError):
client.append(data_float.as_series(), series_id, wait_on_verification=True)
def test_append_raises_valueerror_unsorted_index(self, client):
series_id = "d30519af-5035-4093-a425-dafd857ad0ef"
data = pd.Series(
[1, 2, 3],
index=[
pd.to_datetime("2022-04-04"),
pd.to_datetime("2022-04-03"),
pd.to_datetime("2022-04-05"),
],
)
with pytest.raises(ValueError) as e:
client.append(data, series_id)
assert (
str(e.value)
== "Index not sorted. Please sort series on index before appending data."
)
@pytest.mark.parametrize("data", ("data_float", "data_string"))
def test__verify_and_prepare_series(self, client, data, request):
data = request.getfixturevalue(data)
df_out = client._verify_and_prepare_series(data.as_series())
df_expect = data.as_dataframe().rename(columns={"index": 0, "values": 1})
pd.testing.assert_frame_equal(df_out, df_expect)
def test__verify_and_prepare_series_raises_not_series(self, client, data_float):
with pytest.raises(ValueError):
client._verify_and_prepare_series(data_float.as_dataframe())
def test__verify_and_prepare_series_raises_index(self, client, data_float):
with pytest.raises(ValueError):
client._verify_and_prepare_series(data_float.as_dataframe())
def test__verify_and_prepare_series_raises_not_unique(self, client, data_float):
series = data_float.as_series()
index = list(series.index)
index[1] = index[0] # not unique index
series.index = index
with pytest.raises(ValueError):
client._verify_and_prepare_series(data_float.as_dataframe())
def test__get_file_status(self, client, response_cases):
response_cases.set("group3")
status_out = client._get_file_status("e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2")
status_expect = "Ready"
assert status_out == status_expect
def test__get_file_status_failed(self, client, response_cases):
response_cases.set("group3-failed")
status_out = client._get_file_status("e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2")
status_expect = "Failed"
assert status_out == status_expect
def test__wait_until_file_ready(self, client, response_cases):
response_cases.set("group3")
out = client._wait_until_file_ready("e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2")
assert out == "Ready"
def test__wait_until_file_ready_failed(self, client, response_cases):
response_cases.set("group3-failed")
out = client._wait_until_file_ready("e4fb7a7e-0796-4f6a-8c79-f39a3af66dd2")
assert out == "Failed"
def test_metadata_set(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
response = client.metadata_set(
"foo.bar", "baz", vendor="Sensor Corp", type_="Ampermeter"
)
id_expect = "dd5945ad-67f5-499c-fea4-08db4d49f13b"
assert response["Id"] == id_expect
# Check that the correct URL is poked
request_url_expect = (
"https://reservoir-api.4subsea.net/api/metadata/foo.bar/baz?overwrite=true"
)
assert mock_requests.call_args.args[1] == request_url_expect
# Check that the correct json is sent
json_expect = {"Value": {"vendor": "Sensor Corp", "type_": "Ampermeter"}}
assert mock_requests.call_args.kwargs["json"] == json_expect
def test_metadata_get(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
response = client.metadata_get(namespace="foo.bar", key="baz")
response_expect = {
"Id": "19b7230b-f88a-4217-b1c9-08daff938054",
"Namespace": "foo.bar",
"Key": "baz",
"Value": {"vendor": "Sensor Corp", "type_": "Ampermeter"},
"LastModifiedByEmail": "user@4subsea.com",
"LastModified": "2023-05-05T09:01:32.6706215+00:00",
"Created": "2023-01-26T11:50:20.4812338+00:00",
"CreatedByEmail": "user@4subsea.com",
}
assert response == response_expect
# Check that the correct URL is poked
request_url_expect = (
"https://reservoir-api.4subsea.net/api/metadata/foo.bar/baz"
)
assert mock_requests.call_args.args[1] == request_url_expect
def test_metadata_get_by_id(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
response = client.metadata_get(
metadata_id="8620c8cf-d9db-4ec6-9393-d45a7df6c115"
)
response_expect = {
"Id": "8620c8cf-d9db-4ec6-9393-d45a7df6c115",
"Namespace": "foo.bar",
"Key": "baz",
"Value": {"vendor": "Sensor Corp", "type_": "Ampermeter"},
"TimeSeries": [
{
"TimeSeriesId": "fc485e50-e641-4410-bf10-8ce4b5d24405",
"TimeOfFirstSample": 0,
"TimeOfLastSample": -1,
"LastModifiedByEmail": "user@4subsea.com",
"Created": "2024-06-14T08:37:47.109Z",
"LastModified": "2024-06-14T08:37:47.109Z",
"CreatedByEmail": "user@4subsea.com",
}
],
"LastModifiedByEmail": "user@4subsea.com",
"LastModified": "2023-05-05T09:01:32.6706215+00:00",
"Created": "2023-01-26T11:50:20.4812338+00:00",
"CreatedByEmail": "user@4subsea.com",
}
assert response == response_expect
# Check that the correct URL is poked
request_url_expect = "https://reservoir-api.4subsea.net/api/metadata/8620c8cf-d9db-4ec6-9393-d45a7df6c115"
assert mock_requests.call_args.args[1] == request_url_expect
def test_metadata_get_raises(self, client, response_cases):
response_cases.set("datareservoirio-api")
with pytest.raises(ValueError):
client.metadata_get(metadata_id=None, namespace="foo.bar", key=None)
def test_metadata_search(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
response = client.metadata_search("foo.bar", "baz")
response_expect = [
{
"Id": "19b7230b-f88a-4217-b1c9-08daff938054",
"Namespace": "foo.bar",
"Key": "baz",
"Value": {"vendor": "Sensor Corp", "type_": "Ampermeter"},
"LastModifiedByEmail": "user@4subsea.com",
"LastModified": "2023-05-05T09:01:32.6706215+00:00",
"Created": "2023-01-26T11:50:20.4812338+00:00",
"CreatedByEmail": "user@4subsea.com",
}
]
assert response == response_expect
# Check that the correct URL is poked
request_url_expect = "https://reservoir-api.4subsea.net/api/metadata/search"
assert mock_requests.call_args.args[1] == request_url_expect
# Check that the correct json is sent
json_expect = {"Namespace": "foo.bar", "Key": "baz", "Value": {}}
assert mock_requests.call_args.kwargs["json"] == json_expect
def test_metadata_browse_namespace(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
response = client.metadata_browse(namespace="foo.bar")
response_expect = ["abcd", "baz"]
assert response == response_expect
# Check that the correct URL is poked
request_url_expect = "https://reservoir-api.4subsea.net/api/metadata/foo.bar"
assert mock_requests.call_args.args[1] == request_url_expect
def test_metadata_browse(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
response = client.metadata_browse()
response_expect = sorted(
[
"TheRig2",
"TheRig3",
"This is a new test",
"This is a test",
"This is a third test",
"Tor_TEST",
"TorEinarTest",
"tress",
"vessel.electrical",
"vessel.electrical.rune",
]
)
assert response == response_expect
# Check that the correct URL is poked
request_url_expect = "https://reservoir-api.4subsea.net/api/metadata/"
assert mock_requests.call_args.args[1] == request_url_expect
def test_metadata_delete(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
client.metadata_delete("19b7230b-f88a-4217-b1c9-08daff938054")
# Check that the correct URL is poked
request_url_expect = "https://reservoir-api.4subsea.net/api/metadata/19b7230b-f88a-4217-b1c9-08daff938054"
assert mock_requests.call_args.args[1] == request_url_expect
def test_set_metadata(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
series_id = "857ca134-5bf7-4c14-b687-ede7d5cbf22f"
metadata_id = "19b7230b-f88a-4217-b1c9-08daff938054"
namevalues = {"vendor": "Sensor Corp", "type_": "Ampermeter"}
response = client.set_metadata(
series_id=series_id,
metadata_id=metadata_id,
overwrite=True,
namevalues=namevalues,
)
response_expect = {
"TimeSeriesId": "857ca134-5bf7-4c14-b687-ede7d5cbf22f",
"TimeOfFirstSample": 0,
"TimeOfLastSample": -1,
"LastModifiedByEmail": "user@4subsea.com",
"Created": "2023-05-03T10:25:44.572Z",
"LastModified": "2023-05-03T10:25:44.572Z",
"CreatedByEmail": "user@4subsea.com",
"Metadata": [
{
"Id": "8dc03b22-5a7c-499b-8861-fed5902e9f91",
"Namespace": "ns",
"Key": "key",
"Value": {"some_name": "tress", "some_value": "emerald"},
"LastModifiedByEmail": "user@4subsea.com",
"LastModified": "2023-05-03T10:25:44.572Z",
"Created": "2023-05-03T10:25:44.572Z",
"CreatedByEmail": "user@4subsea.com",
}
],
}
assert response == response_expect
# Check that the correct URL is poked
request_url_expect = "https://reservoir-api.4subsea.net/api/timeseries/857ca134-5bf7-4c14-b687-ede7d5cbf22f/metadata"
assert mock_requests.call_args.args[1] == request_url_expect
# Check that the correct json with metadata id is sent
json_expect = ["19b7230b-f88a-4217-b1c9-08daff938054"]
assert mock_requests.call_args.kwargs["json"] == json_expect
def test_set_metadata_ns_key(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
series_id = "857ca134-5bf7-4c14-b687-ede7d5cbf22f"
namevalues = {"vendor": "Sensor Corp", "type_": "Ampermeter"}
response = client.set_metadata(
series_id=series_id,
namespace="foo.bar",
key="baz",
overwrite=True,
namevalues=namevalues,
)
response_expect = {
"TimeSeriesId": "857ca134-5bf7-4c14-b687-ede7d5cbf22f",
"TimeOfFirstSample": 0,
"TimeOfLastSample": -1,
"LastModifiedByEmail": "user@4subsea.com",
"Created": "2023-05-03T10:25:44.572Z",
"LastModified": "2023-05-03T10:25:44.572Z",
"CreatedByEmail": "user@4subsea.com",
"Metadata": [
{
"Id": "8dc03b22-5a7c-499b-8861-fed5902e9f91",
"Namespace": "ns",
"Key": "key",
"Value": {"some_name": "tress", "some_value": "emerald"},
"LastModifiedByEmail": "user@4subsea.com",
"LastModified": "2023-05-03T10:25:44.572Z",
"Created": "2023-05-03T10:25:44.572Z",
"CreatedByEmail": "user@4subsea.com",
}
],
}
assert response == response_expect
# Check that the correct URL is poked
request_url_expect = "https://reservoir-api.4subsea.net/api/timeseries/857ca134-5bf7-4c14-b687-ede7d5cbf22f/metadata"
assert mock_requests.call_args.args[1] == request_url_expect
# Check that the correct json with metadata id is sent
json_expect = ["dd5945ad-67f5-499c-fea4-08db4d49f13b"]
assert mock_requests.call_args.kwargs["json"] == json_expect
def test_set_metadata_raises_no_id_and_namespace(self, client, response_cases):
response_cases.set("datareservoirio-api")
series_id = "857ca134-5bf7-4c14-b687-ede7d5cbf22f"
namevalues = {"vendor": "Sensor Corp", "type_": "Ampermeter"}
with pytest.raises(ValueError):
client.set_metadata(
series_id,
metadata_id=None,
namespace=None,
key="baz",
overwrite=True,
namevalues=namevalues,
)
def test_set_metadata_raises_namespace_and_no_key(self, client, response_cases):
response_cases.set("datareservoirio-api")
series_id = "857ca134-5bf7-4c14-b687-ede7d5cbf22f"
namevalues = {"vendor": "Sensor Corp", "type_": "Ampermeter"}
with pytest.raises(ValueError):
client.set_metadata(
series_id,
metadata_id=None,
namespace="foo.bar",
key=None,
overwrite=True,
namevalues=namevalues,
)
def test_remove_metadata(self, client, response_cases, mock_requests):
response_cases.set("datareservoirio-api")
series_id = "857ca134-5bf7-4c14-b687-ede7d5cbf22f"
metadata_id = "19b7230b-f88a-4217-b1c9-08daff938054"
response = client.remove_metadata(series_id, metadata_id)
response_expect = {
"TimeSeriesId": "857ca134-5bf7-4c14-b687-ede7d5cbf22f",
"TimeOfFirstSample": 0,
"TimeOfLastSample": -1,
"LastModifiedByEmail": "user@4subsea.com",
"Created": "2023-05-03T10:25:44.567Z",
"LastModified": "2023-05-03T10:25:44.567Z",
"CreatedByEmail": "user@4subsea.com",
"Metadata": [
{
"Id": "8dc03b22-5a7c-499b-8861-fed5902e9f91",
"Namespace": "ns",
"Key": "key",
"Value": {"some_name": "tress", "some_value": "emerald"},
"LastModifiedByEmail": "user@4subsea.com",
"LastModified": "2023-05-03T10:25:44.567Z",
"Created": "2023-05-03T10:25:44.567Z",
"CreatedByEmail": "user@4subsea.com",
}
],
}
assert response == response_expect
# Check that the correct URL is poked
request_url_expect = "https://reservoir-api.4subsea.net/api/timeseries/857ca134-5bf7-4c14-b687-ede7d5cbf22f/metadata"
assert mock_requests.call_args.args[1] == request_url_expect
# Check that the correct json with metadata id is sent
json_expect = ["19b7230b-f88a-4217-b1c9-08daff938054"]
assert mock_requests.call_args.kwargs["json"] == json_expect
def test_client_get_throws_exception_is_logged(self, client):
client.was_called = False
exceptions_logger.exception = types.MethodType(change_logging, client)
with pytest.raises(ValueError):
client.get("e3d82cda-4737-4af9-8d17-d9dfda8703d0", raise_empty=True)
assert client.was_called == True
def test_client_retries_on_connection_errors(self, client_with_connection_error):
client_with_connection_error.get("e3d82cda-4737-4af9-8d17-d9dfda8703d0")
attempts_from_tenacity = client_with_connection_error.get.retry.statistics[
"attempt_number"
]
call_count_from_fake_auth_counter = (
client_with_connection_error._auth_session.call_count
)
assert attempts_from_tenacity == 3
assert attempts_from_tenacity == call_count_from_fake_auth_counter
assert call_count_from_fake_auth_counter == 3
def test_tries_error_does_not_throw_retry(self, client_with_invalid_json_error):
with pytest.raises(InvalidJSONError) as ex:
client_with_invalid_json_error.get("e3d82cda-4737-4af9-8d17-d9dfda8703d0")
attempts = client_with_invalid_json_error.get.retry.statistics["attempt_number"]
assert attempts == 1
@pytest.mark.response_irrelevant
@pytest.mark.parametrize(
"aggregation_function, expected",
[("mean", "Avg"), ("std", "Stdev"), ("min", "Min"), ("max", "Max")],
)
def test_aggregation_function_gets_translated(
self, client, mock_requests, aggregation_function, expected, response_cases
):
response_cases.set("datareservoirio-api")
client.get_samples_aggregate(
"e3d82cda-4737-4af9-8d17-d9dfda8703d0",
start="2023-12-01",
end="2023-12-02",
aggregation_period="15m",
aggregation_function=aggregation_function,
)
request_url = mock_requests.call_args.args[1]
assert f"aggregationFunction={expected}" in request_url
@pytest.mark.response_irrelevant
@pytest.mark.parametrize(
"aggregation_period, expected",