33Skipped by default. Assertions are on response *shape* only, never web content.
44Never include token values in assertion messages.
55"""
6+ import time
7+ import uuid
8+
69import pytest
710
811pytestmark = pytest .mark .live
@@ -20,13 +23,14 @@ def test_live_dql_post_single_read(db):
2023
2124
2225def test_live_dql_post_csv (db ):
23- body = db .dql ('type:Organization name:"Diffbot"' , size = 1 , format = "csv" , raw = True )
24- assert isinstance (body , bytes ) and len (body ) > 0
26+ text = db .dql ('type:Organization name:"Diffbot"' , size = 1 , format = "csv" )
27+ assert isinstance (text , str ) and len (text .splitlines ()) >= 2
28+ assert isinstance (db .dql ('type:Organization name:"Diffbot"' , size = 1 , format = "csv" , raw = True ), bytes )
2529
2630
2731def test_live_dql_post_jsonl (db ):
28- body = db .dql ('type:Organization name:"Diffbot"' , size = 1 , format = "jsonl" , raw = True )
29- assert isinstance ( body , bytes ) and len (body ) > 0
32+ entities = list ( db .dql ('type:Organization name:"Diffbot"' , size = 1 , format = "jsonl" ) )
33+ assert len (entities ) >= 1
3034
3135
3236def test_live_dql_long_query_beyond_url_limit (db ):
@@ -39,3 +43,60 @@ def test_live_dql_long_query_beyond_url_limit(db):
3943def test_live_entities (db ):
4044 result = db .entities ("Diffbot is a company based in Menlo Park." )
4145 assert "entities" in result
46+
47+
48+ def _redacted (account ) -> str :
49+ """Account bodies carry ``token``/``childTokens``: only ever show the other keys."""
50+ return f"keys={ sorted (k for k in account if k not in ('token' , 'childTokens' ))} "
51+
52+
53+ def test_live_account_and_verify_token (db ):
54+ account = db .account (days = 1 )
55+ assert isinstance (account , dict ), _redacted ({})
56+ assert "plan" in account or "name" in account or "usage" in account , _redacted (account )
57+ assert db .verify_token () is True
58+
59+
60+ def test_live_verify_token_rejects_a_bad_token ():
61+ from diffbot import Diffbot
62+
63+ assert Diffbot (token = "not-a-real-token" ).verify_token () is False
64+
65+
66+ def test_live_extract_with_fields_and_timeout (db ):
67+ result = db .extract ("https://example.com" , "article" , fmt = "json" , fields = ["links" ], timeout = 45000 )
68+ assert "objects" in result and len (result ["objects" ]) > 0
69+
70+
71+ CRAWL_PREFIX = "sdk-live-"
72+
73+
74+ def _purge_leaked_crawls (db ) -> None :
75+ cutoff = time .time () - 3600
76+ for job in db .crawl_list_jobs ():
77+ name = job .get ("name" , "" )
78+ if name .startswith (CRAWL_PREFIX ):
79+ try :
80+ created = int (name .rsplit ("-" , 1 )[- 1 ])
81+ except ValueError :
82+ continue
83+ if created < cutoff :
84+ db .crawl_delete_job (name )
85+
86+
87+ def test_live_crawl_start_poll_download (db ):
88+ _purge_leaked_crawls (db )
89+ name = f"{ CRAWL_PREFIX } py-{ uuid .uuid4 ().hex [:8 ]} -{ int (time .time ())} "
90+ try :
91+ for _ in db .crawl ("https://example.com" , job_name = name , hops = 0 , max_to_crawl = 5 , max_to_process = 5 ):
92+ pass
93+ deadline = time .time () + 180
94+ while time .time () < deadline :
95+ if db .crawl_get_job (name ).get ("jobStatus" , {}).get ("status" , 0 ) not in (0 , 7 ):
96+ break
97+ time .sleep (5 )
98+ records = db .crawl_download (name )
99+ assert isinstance (records , list )
100+ assert isinstance (db .crawl_download (name , format = "csv" ), str )
101+ finally :
102+ db .crawl_delete_job (name )
0 commit comments