from hailtop.utils import (partition, url_basename, url_join, url_scheme,
                           url_and_params, parse_docker_image_reference)


def test_partition_zero_empty():
    assert list(partition(0, [])) == []


def test_partition_even_small():
    assert list(partition(3, range(3))) == [range(0, 1), range(1, 2), range(2, 3)]


def test_partition_even_big():
    assert list(partition(3, range(9))) == [range(0, 3), range(3, 6), range(6, 9)]


def test_partition_uneven_big():
    assert list(partition(2, range(9))) == [range(0, 5), range(5, 9)]


def test_partition_toofew():
    assert list(partition(6, range(3))) == [range(0, 1), range(1, 2), range(2, 3),
                                            range(3, 3), range(3, 3), range(3, 3)]


def test_url_basename():
    assert url_basename('/path/to/file') == 'file'
    assert url_basename('https://hail.is/path/to/file') == 'file'


def test_url_join():
    assert url_join('/path/to', 'file') == '/path/to/file'
    assert url_join('/path/to/', 'file') == '/path/to/file'
    assert url_join('/path/to/', '/absolute/file') == '/absolute/file'
    assert url_join('https://hail.is/path/to', 'file') == 'https://hail.is/path/to/file'
    assert url_join('https://hail.is/path/to/', 'file') == 'https://hail.is/path/to/file'
    assert url_join('https://hail.is/path/to/', '/absolute/file') == 'https://hail.is/absolute/file'


def test_url_scheme():
    assert url_scheme('https://hail.is/path/to') == 'https'
    assert url_scheme('/path/to') == ''

def test_url_and_params():
    assert url_and_params('https://example.com/') == ('https://example.com/', {})
    assert url_and_params('https://example.com/foo?') == ('https://example.com/foo', {})
    assert url_and_params('https://example.com/foo?a=b&c=d') == ('https://example.com/foo', {'a': 'b', 'c': 'd'})

def test_parse_docker_image_reference():
    x = parse_docker_image_reference('animage')
    assert x.domain is None
    assert x.path == 'animage'
    assert x.tag is None
    assert x.digest is None
    assert x.name() == 'animage'
    assert str(x) == 'animage'

    x = parse_docker_image_reference('hailgenetics/animage')
    assert x.domain == 'hailgenetics'
    assert x.path == 'animage'
    assert x.tag is None
    assert x.digest is None
    assert x.name() == 'hailgenetics/animage'
    assert str(x) == 'hailgenetics/animage'

    x = parse_docker_image_reference('localhost:5000/animage')
    assert x.domain == 'localhost:5000'
    assert x.path == 'animage'
    assert x.tag is None
    assert x.digest is None
    assert x.name() == 'localhost:5000/animage'
    assert str(x) == 'localhost:5000/animage'

    x = parse_docker_image_reference('localhost:5000/a/b/name')
    assert x.domain == 'localhost:5000'
    assert x.path == 'a/b/name'
    assert x.tag is None
    assert x.digest is None
    assert x.name() == 'localhost:5000/a/b/name'
    assert str(x) == 'localhost:5000/a/b/name'

    x = parse_docker_image_reference('localhost:5000/a/b/name:tag')
    assert x.domain == 'localhost:5000'
    assert x.path == 'a/b/name'
    assert x.tag == 'tag'
    assert x.digest is None
    assert x.name() == 'localhost:5000/a/b/name'
    assert str(x) == 'localhost:5000/a/b/name:tag'

    x = parse_docker_image_reference('localhost:5000/a/b/name:tag@sha256:abc123')
    assert x.domain == 'localhost:5000'
    assert x.path == 'a/b/name'
    assert x.tag == 'tag'
    assert x.digest == 'sha256:abc123'
    assert x.name() == 'localhost:5000/a/b/name'
    assert str(x) == 'localhost:5000/a/b/name:tag@sha256:abc123'

    x = parse_docker_image_reference('localhost:5000/a/b/name@sha256:abc123')
    assert x.domain == 'localhost:5000'
    assert x.path == 'a/b/name'
    assert x.tag is None
    assert x.digest == 'sha256:abc123'
    assert x.name() == 'localhost:5000/a/b/name'
    assert str(x) == 'localhost:5000/a/b/name@sha256:abc123'

    x = parse_docker_image_reference('name@sha256:abc123')
    assert x.domain is None
    assert x.path == 'name'
    assert x.tag is None
    assert x.digest == 'sha256:abc123'
    assert x.name() == 'name'
    assert str(x) == 'name@sha256:abc123'

    x = parse_docker_image_reference('gcr.io/hail-vdc/batch-worker:123fds312')
    assert x.domain == 'gcr.io'
    assert x.path == 'hail-vdc/batch-worker'
    assert x.tag == '123fds312'
    assert x.digest is None
    assert x.name() == 'gcr.io/hail-vdc/batch-worker'
    assert str(x) == 'gcr.io/hail-vdc/batch-worker:123fds312'

    x = parse_docker_image_reference('us-docker.pkg.dev/my-project/my-repo/test-image')
    assert x.domain == 'us-docker.pkg.dev'
    assert x.path == 'my-project/my-repo/test-image'
    assert x.tag is None
    assert x.digest is None
    assert x.name() == 'us-docker.pkg.dev/my-project/my-repo/test-image'
    assert str(x) == 'us-docker.pkg.dev/my-project/my-repo/test-image'

[((50, 8, 50, 47), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(50, 37, 50, 46): '"""animage"""'}, {}), "('animage')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((58, 8, 58, 60), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(58, 37, 58, 59): '"""hailgenetics/animage"""'}, {}), "('hailgenetics/animage')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((66, 8, 66, 62), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(66, 37, 66, 61): '"""localhost:5000/animage"""'}, {}), "('localhost:5000/animage')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((74, 8, 74, 63), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(74, 37, 74, 62): '"""localhost:5000/a/b/name"""'}, {}), "('localhost:5000/a/b/name')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((82, 8, 82, 67), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(82, 37, 82, 66): '"""localhost:5000/a/b/name:tag"""'}, {}), "('localhost:5000/a/b/name:tag')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((90, 8, 90, 81), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(90, 37, 90, 80): '"""localhost:5000/a/b/name:tag@sha256:abc123"""'}, {}), "('localhost:5000/a/b/name:tag@sha256:abc123')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((98, 8, 98, 77), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(98, 37, 98, 76): '"""localhost:5000/a/b/name@sha256:abc123"""'}, {}), "('localhost:5000/a/b/name@sha256:abc123')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((106, 8, 106, 58), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(106, 37, 106, 57): '"""name@sha256:abc123"""'}, {}), "('name@sha256:abc123')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((114, 8, 114, 78), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(114, 37, 114, 77): '"""gcr.io/hail-vdc/batch-worker:123fds312"""'}, {}), "('gcr.io/hail-vdc/batch-worker:123fds312')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((122, 8, 122, 87), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', ({(122, 37, 122, 86): '"""us-docker.pkg.dev/my-project/my-repo/test-image"""'}, {}), "('us-docker.pkg.dev/my-project/my-repo/test-image')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((27, 11, 27, 40), 'hailtop.utils.url_basename', 'url_basename', ({(27, 24, 27, 39): '"""/path/to/file"""'}, {}), "('/path/to/file')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((28, 11, 28, 55), 'hailtop.utils.url_basename', 'url_basename', ({(28, 24, 28, 54): '"""https://hail.is/path/to/file"""'}, {}), "('https://hail.is/path/to/file')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((32, 11, 32, 39), 'hailtop.utils.url_join', 'url_join', ({(32, 20, 32, 30): '"""/path/to"""', (32, 32, 32, 38): '"""file"""'}, {}), "('/path/to', 'file')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((33, 11, 33, 40), 'hailtop.utils.url_join', 'url_join', ({(33, 20, 33, 31): '"""/path/to/"""', (33, 33, 33, 39): '"""file"""'}, {}), "('/path/to/', 'file')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((34, 11, 34, 50), 'hailtop.utils.url_join', 'url_join', ({(34, 20, 34, 31): '"""/path/to/"""', (34, 33, 34, 49): '"""/absolute/file"""'}, {}), "('/path/to/', '/absolute/file')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((35, 11, 35, 54), 'hailtop.utils.url_join', 'url_join', ({(35, 20, 35, 45): '"""https://hail.is/path/to"""', (35, 47, 35, 53): '"""file"""'}, {}), "('https://hail.is/path/to', 'file')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((36, 11, 36, 55), 'hailtop.utils.url_join', 'url_join', ({(36, 20, 36, 46): '"""https://hail.is/path/to/"""', (36, 48, 36, 54): '"""file"""'}, {}), "('https://hail.is/path/to/', 'file')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((37, 11, 37, 65), 'hailtop.utils.url_join', 'url_join', ({(37, 20, 37, 46): '"""https://hail.is/path/to/"""', (37, 48, 37, 64): '"""/absolute/file"""'}, {}), "('https://hail.is/path/to/', '/absolute/file')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((41, 11, 41, 48), 'hailtop.utils.url_scheme', 'url_scheme', ({(41, 22, 41, 47): '"""https://hail.is/path/to"""'}, {}), "('https://hail.is/path/to')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((42, 11, 42, 33), 'hailtop.utils.url_scheme', 'url_scheme', ({(42, 22, 42, 32): '"""/path/to"""'}, {}), "('/path/to')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((45, 11, 45, 49), 'hailtop.utils.url_and_params', 'url_and_params', ({(45, 26, 45, 48): '"""https://example.com/"""'}, {}), "('https://example.com/')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((46, 11, 46, 53), 'hailtop.utils.url_and_params', 'url_and_params', ({(46, 26, 46, 52): '"""https://example.com/foo?"""'}, {}), "('https://example.com/foo?')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((47, 11, 47, 60), 'hailtop.utils.url_and_params', 'url_and_params', ({(47, 26, 47, 59): '"""https://example.com/foo?a=b&c=d"""'}, {}), "('https://example.com/foo?a=b&c=d')", False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((6, 16, 6, 32), 'hailtop.utils.partition', 'partition', ({(6, 26, 6, 27): '(0)', (6, 29, 6, 31): '[]'}, {}), '(0, [])', False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n')]