|
1 | 1 | """
|
2 | 2 | Test Client
|
3 | 3 | """
|
4 |
| -from .hstestcase import HSTestCase |
5 | 4 | from scrapinghub import HubstorageClient
|
6 | 5 | from scrapinghub.hubstorage.utils import apipoll
|
7 | 6 |
|
8 |
| -class ClientTest(HSTestCase): |
9 |
| - |
10 |
| - def test_default_ua(self): |
11 |
| - self.assertEqual(self.hsclient.user_agent, |
12 |
| - HubstorageClient.DEFAULT_USER_AGENT) |
13 |
| - |
14 |
| - def test_custom_ua(self): |
15 |
| - client = HubstorageClient(auth=HSTestCase.auth, |
16 |
| - endpoint=HSTestCase.endpoint, |
17 |
| - user_agent='testUA') |
18 |
| - self.assertEqual(client.user_agent, 'testUA') |
19 |
| - |
20 |
| - def test_push_job(self): |
21 |
| - c = self.hsclient |
22 |
| - c.push_job(self.projectid, self.spidername, |
23 |
| - priority=self.project.jobq.PRIO_LOW, |
24 |
| - foo='baz') |
25 |
| - job = self.start_job() |
26 |
| - m = job.metadata |
27 |
| - self.assertEqual(m.get('state'), u'running', c.auth) |
28 |
| - self.assertEqual(m.get('foo'), u'baz') |
29 |
| - self.project.jobq.finish(job) |
30 |
| - self.project.jobq.delete(job) |
31 |
| - |
32 |
| - # job auth token is valid only while job is running |
33 |
| - m = c.get_job(job.key).metadata |
34 |
| - self.assertEqual(m.get('state'), u'deleted') |
35 |
| - self.assertEqual(m.get('foo'), u'baz') |
36 |
| - |
37 |
| - def test_jobsummaries(self): |
38 |
| - hsc = self.hsclient |
39 |
| - # add at least one running or pending job to ensure summary is returned |
40 |
| - hsc.push_job(self.projectid, self.spidername, state='running') |
41 |
| - |
42 |
| - def _get_summary(): |
43 |
| - jss = hsc.projects.jobsummaries() |
44 |
| - mjss = dict((str(js['project']), js) for js in jss) |
45 |
| - return mjss.get(self.projectid) |
46 |
| - summary = apipoll(_get_summary) |
47 |
| - self.assertIsNotNone(summary) |
48 |
| - |
49 |
| - def test_timestamp(self): |
50 |
| - ts1 = self.hsclient.server_timestamp() |
51 |
| - ts2 = self.hsclient.server_timestamp() |
52 |
| - self.assertGreater(ts1, 0) |
53 |
| - self.assertLessEqual(ts1, ts2) |
| 7 | +from .conftest import TEST_AUTH, TEST_ENDPOINT |
| 8 | +from .conftest import TEST_PROJECT_ID, TEST_SPIDER_NAME |
| 9 | +from .conftest import start_job |
| 10 | + |
| 11 | + |
| 12 | +def test_default_ua(hsclient): |
| 13 | + assert hsclient.user_agent == HubstorageClient.DEFAULT_USER_AGENT |
| 14 | + |
| 15 | + |
| 16 | +def test_custom_ua(): |
| 17 | + client = HubstorageClient(auth=TEST_AUTH, |
| 18 | + endpoint=TEST_ENDPOINT, |
| 19 | + user_agent='testUA') |
| 20 | + assert client.user_agent == 'testUA' |
| 21 | + |
| 22 | + |
| 23 | +def test_push_job(hsclient, hsproject): |
| 24 | + hsclient.push_job( |
| 25 | + TEST_PROJECT_ID, TEST_SPIDER_NAME, |
| 26 | + priority=hsproject.jobq.PRIO_LOW, |
| 27 | + foo='baz', |
| 28 | + ) |
| 29 | + job = start_job(hsproject) |
| 30 | + meta = job.metadata |
| 31 | + assert meta.get('state') == u'running', hsclient.auth |
| 32 | + assert meta.get('foo') == u'baz' |
| 33 | + hsproject.jobq.finish(job) |
| 34 | + hsproject.jobq.delete(job) |
| 35 | + |
| 36 | + # job auth token is valid only while job is running |
| 37 | + meta = hsclient.get_job(job.key).metadata |
| 38 | + assert meta.get('state') == u'deleted' |
| 39 | + assert meta.get('foo') == u'baz' |
| 40 | + |
| 41 | + |
| 42 | +def test_jobsummaries(hsclient): |
| 43 | + # add at least one running or pending job to ensure summary is returned |
| 44 | + hsclient.push_job(TEST_PROJECT_ID, TEST_SPIDER_NAME, state='running') |
| 45 | + |
| 46 | + def _get_summary(): |
| 47 | + jss = hsclient.projects.jobsummaries() |
| 48 | + mjss = dict((str(js['project']), js) for js in jss) |
| 49 | + return mjss.get(TEST_PROJECT_ID) |
| 50 | + |
| 51 | + summary = apipoll(_get_summary) |
| 52 | + assert summary is not None |
| 53 | + |
| 54 | + |
| 55 | +def test_timestamp(hsclient): |
| 56 | + ts1 = hsclient.server_timestamp() |
| 57 | + ts2 = hsclient.server_timestamp() |
| 58 | + assert ts1 > 0 |
| 59 | + assert ts1 <= ts2 |
0 commit comments