#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Tests for the `PpsClient` class of the `python_pachyderm` package."""

import pytest

import python_pachyderm


@pytest.fixture(scope='function')
def pps_client():
    """Connect to Pachyderm before tests and reset to initial state after tests."""
    client = python_pachyderm.PpsClient()
    client.delete_all()
    yield client 
    client.delete_all()


@pytest.fixture(scope='function')
def pps_client_with_sandbox():
    """Connect to Pachyderm before tests and reset to initial state after tests."""

    pfs_client = python_pachyderm.PfsClient()
    pps_client = python_pachyderm.PpsClient()

    pps_client.delete_all()
    pfs_client.delete_all()

    pfs_client.create_repo('test-pps-input', 'This is a test repository for PPS functionality')

    pps_client.create_pipeline(
        "test-pps-copy",
        transform=python_pachyderm.Transform(cmd=["sh"], image="alpine", stdin=["ls /pfs; cp /pfs/*.dat /pfs/out/"]),
        input=python_pachyderm.Input(pfs=python_pachyderm.PFSInput(glob="/*", repo="test-pps-input")),
    )

    yield pps_client, pfs_client

    pps_client.delete_all()
    pfs_client.delete_all()

def test_list_job(pps_client_with_sandbox):
    pps_client, pfs_client = pps_client_with_sandbox

    jobs = pps_client.list_job()
    assert len(jobs.job_info) == 0

    with pfs_client.commit('test-pps-input', 'master') as c:
        pfs_client.put_file_bytes(c, 'file.dat', b'DATA')

    # wait for the job to run
    pfs_client.inspect_commit(f"test-pps-input/{c.id}")

    jobs = pps_client.list_job()
    assert len(jobs.job_info) == 1

    jobs = pps_client.list_job(pipeline_name='test-pps-copy')
    assert len(jobs.job_info) == 1

    jobs = pps_client.list_job(input_commit=f"test-pps-input/{c.id}")
    assert len(jobs.job_info) == 1

@@ -940,24 +940,30 @@
 n=%5B%22
-ls
+cp
  /pfs
-; cp /pfs
+/test-pps-input
 /*.d
@@ -1189,19 +1189,18 @@
 ef test_
-lis
+ge
 t_job(pp
@@ -1223,16 +1223,158 @@
 ndbox):%0A
+    %22%22%22%0A    Tests both listing and inspecting jobs. Tests are combined because waiting%0A    for commits to flush is a slow operation.%0A    %22%22%22%0A%0A
     pps_
@@ -1638,24 +1638,29 @@
  to run%0A    
+list(
 pfs_client.i
@@ -1662,23 +1662,21 @@
 ent.
-inspect
+flush
 _commit(
 f%22te
@@ -1671,16 +1671,17 @@
 _commit(
+%5B
 f%22test-p
@@ -1696,16 +1696,18 @@
 /%7Bc.id%7D%22
+%5D)
 )%0A%0A    j
@@ -1938,16 +1938,16 @@
 c.id%7D%22)%0A
-
     asse
@@ -1953,28 +1953,192 @@
 ert len(jobs.job_info) == 1%0A
+%0A    job_id = jobs.job_info%5B0%5D.job.id%0A    job = pps_client.inspect_job(job_id)%0A    assert job.job.id == job_id%0A    assert job.state == python_pachyderm.JOB_SUCCESS%0A

Test inspect_job