Skip to content

Commit b99d02b

Browse files
Add create and wait job and batch
1 parent dba9c44 commit b99d02b

File tree

2 files changed

+423
-0
lines changed

2 files changed

+423
-0
lines changed

tests/test_functions.py

+317
Original file line numberDiff line numberDiff line change
@@ -690,3 +690,320 @@ def test_wait_for_job_done_timeout():
690690
unittest.TestCase().assertRaises(
691691
TimeoutException, c.wait_for_job_done, "123", "234"
692692
)
693+
694+
695+
@responses.activate
696+
def test_create_and_wait_batch():
697+
responses.add(
698+
responses.POST,
699+
f"{BASE_URL}/ocr/batch/rg",
700+
json={
701+
"urls": {"document": "https://test2.com"},
702+
"id": "123",
703+
"status_url": "https://test.com",
704+
},
705+
status=200,
706+
)
707+
708+
responses.add(
709+
responses.PUT,
710+
"https://test2.com",
711+
status=200,
712+
)
713+
714+
responses.add(
715+
responses.GET,
716+
f"{BASE_URL}/ocr/batch/status/123",
717+
json={
718+
"batch_ksuid": "123",
719+
"jobs": [],
720+
"service": "rg",
721+
"status": "done",
722+
},
723+
status=200,
724+
)
725+
726+
c = Client()
727+
res = c.create_and_wait_batch("rg", "./requirements.txt")
728+
729+
assert res.get("batch_ksuid")
730+
assert res.get("service")
731+
assert res.get("status")
732+
733+
assert res.get("batch_ksuid") == "123"
734+
assert res.get("service") == "rg"
735+
assert res.get("status") == "done"
736+
737+
738+
@responses.activate
739+
def test_create_and_wait_batch_with_jobs():
740+
responses.add(
741+
responses.POST,
742+
f"{BASE_URL}/ocr/batch/rg",
743+
json={
744+
"urls": {"document": "https://test2.com"},
745+
"id": "123",
746+
"status_url": "https://test.com",
747+
},
748+
status=200,
749+
)
750+
751+
responses.add(
752+
responses.PUT,
753+
"https://test2.com",
754+
status=200,
755+
)
756+
757+
responses.add(
758+
responses.GET,
759+
f"{BASE_URL}/ocr/batch/status/123",
760+
json={
761+
"batch_ksuid": "123",
762+
"jobs": [{"job_ksuid": "234"}],
763+
"service": "rg",
764+
"status": "done",
765+
},
766+
status=200,
767+
)
768+
769+
responses.add(
770+
responses.GET,
771+
f"{BASE_URL}/ocr/job/result/123/234",
772+
json={
773+
"status": "done",
774+
},
775+
status=200,
776+
)
777+
778+
c = Client()
779+
res = c.create_and_wait_batch("rg", "./requirements.txt")
780+
781+
assert res.get("batch_ksuid")
782+
assert res.get("service")
783+
assert res.get("status")
784+
785+
assert res.get("batch_ksuid") == "123"
786+
assert res.get("service") == "rg"
787+
assert res.get("status") == "done"
788+
789+
790+
@responses.activate
791+
def test_create_and_wait_batch_unauthorized():
792+
responses.add(
793+
responses.POST,
794+
f"{BASE_URL}/ocr/batch/rg",
795+
json={
796+
"urls": {"document": "https://test2.com"},
797+
"id": "123",
798+
"status_url": "https://test.com",
799+
},
800+
status=200,
801+
)
802+
803+
responses.add(
804+
responses.PUT,
805+
"https://test2.com",
806+
status=200,
807+
)
808+
809+
responses.add(
810+
responses.GET,
811+
f"{BASE_URL}/ocr/batch/status/123",
812+
status=401,
813+
)
814+
815+
c = Client()
816+
unittest.TestCase().assertRaises(
817+
InvalidStatusCodeException, c.create_and_wait_batch, "rg", "./requirements.txt"
818+
)
819+
820+
821+
@responses.activate
822+
def test_create_and_wait_batch_timeout():
823+
responses.add(
824+
responses.POST,
825+
f"{BASE_URL}/ocr/batch/rg",
826+
json={
827+
"urls": {"document": "https://test2.com"},
828+
"id": "123",
829+
"status_url": "https://test.com",
830+
},
831+
status=200,
832+
)
833+
834+
responses.add(
835+
responses.PUT,
836+
"https://test2.com",
837+
status=200,
838+
)
839+
840+
responses.add(
841+
responses.GET,
842+
f"{BASE_URL}/ocr/batch/status/123",
843+
json={
844+
"batch_ksuid": "123",
845+
"service": "rg",
846+
"status": "processing",
847+
},
848+
status=200,
849+
)
850+
851+
c = Client(timeout=1)
852+
unittest.TestCase().assertRaises(
853+
TimeoutException, c.create_and_wait_batch, "rg", "./requirements.txt"
854+
)
855+
856+
857+
@responses.activate
858+
def test_create_and_wait_batch_timeout_job():
859+
responses.add(
860+
responses.POST,
861+
f"{BASE_URL}/ocr/batch/rg",
862+
json={
863+
"urls": {"document": "https://test2.com"},
864+
"id": "123",
865+
"status_url": "https://test.com",
866+
},
867+
status=200,
868+
)
869+
870+
responses.add(
871+
responses.PUT,
872+
"https://test2.com",
873+
status=200,
874+
)
875+
876+
responses.add(
877+
responses.GET,
878+
f"{BASE_URL}/ocr/batch/status/123",
879+
json={
880+
"batch_ksuid": "123",
881+
"jobs": [{"job_ksuid": "234"}],
882+
"service": "rg",
883+
"status": "done",
884+
},
885+
status=200,
886+
)
887+
888+
responses.add(
889+
responses.GET,
890+
f"{BASE_URL}/ocr/job/result/123/234",
891+
json={
892+
"status": "processing",
893+
},
894+
status=200,
895+
)
896+
897+
c = Client(timeout=1)
898+
unittest.TestCase().assertRaises(
899+
TimeoutException, c.create_and_wait_batch, "rg", "./requirements.txt"
900+
)
901+
902+
903+
@responses.activate
904+
def test_create_and_wait_job():
905+
responses.add(
906+
responses.POST,
907+
f"{BASE_URL}/ocr/job/rg",
908+
json={
909+
"urls": {"document": "https://test2.com"},
910+
"id": "123",
911+
"status_url": "https://test.com",
912+
},
913+
status=200,
914+
)
915+
916+
responses.add(
917+
responses.GET,
918+
f"{BASE_URL}/ocr/job/result/123/123",
919+
json={
920+
"job_ksuid": "123",
921+
"status": "done",
922+
"service": "rg",
923+
},
924+
status=200,
925+
)
926+
927+
responses.add(
928+
responses.PUT,
929+
"https://test2.com",
930+
status=200,
931+
)
932+
933+
c = Client()
934+
res = c.create_and_wait_job("rg", "./requirements.txt")
935+
936+
assert res.get("job_ksuid")
937+
assert res.get("service")
938+
assert res.get("status")
939+
940+
assert res.get("job_ksuid") == "123"
941+
assert res.get("service") == "rg"
942+
assert res.get("status") == "done"
943+
944+
945+
@responses.activate
946+
def test_create_and_wait_job_unauthorized():
947+
responses.add(
948+
responses.POST,
949+
f"{BASE_URL}/ocr/job/rg",
950+
json={
951+
"urls": {"document": "https://test2.com"},
952+
"id": "123",
953+
"status_url": "https://test.com",
954+
},
955+
status=200,
956+
)
957+
958+
responses.add(
959+
responses.PUT,
960+
"https://test2.com",
961+
status=200,
962+
)
963+
964+
responses.add(
965+
responses.GET,
966+
f"{BASE_URL}/ocr/job/result/123/123",
967+
status=401,
968+
)
969+
970+
c = Client()
971+
unittest.TestCase().assertRaises(
972+
InvalidStatusCodeException, c.create_and_wait_job, "rg", "./requirements.txt"
973+
)
974+
975+
976+
@responses.activate
977+
def test_create_and_wait_job_timeout():
978+
responses.add(
979+
responses.POST,
980+
f"{BASE_URL}/ocr/job/rg",
981+
json={
982+
"urls": {"document": "https://test2.com"},
983+
"id": "123",
984+
"status_url": "https://test.com",
985+
},
986+
status=200,
987+
)
988+
989+
responses.add(
990+
responses.PUT,
991+
"https://test2.com",
992+
status=200,
993+
)
994+
995+
responses.add(
996+
responses.GET,
997+
f"{BASE_URL}/ocr/job/result/123/123",
998+
json={
999+
"job_ksuid": "123",
1000+
"service": "rg",
1001+
"status": "processing",
1002+
},
1003+
status=200,
1004+
)
1005+
1006+
c = Client(timeout=1)
1007+
unittest.TestCase().assertRaises(
1008+
TimeoutException, c.create_and_wait_job, "rg", "./requirements.txt"
1009+
)

0 commit comments

Comments
 (0)