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'

[((1883, 1922), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""animage"""'], {}), "('animage')\n", (1911, 1922), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((2108, 2160), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""hailgenetics/animage"""'], {}), "('hailgenetics/animage')\n", (2136, 2160), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((2382, 2436), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""localhost:5000/animage"""'], {}), "('localhost:5000/animage')\n", (2410, 2436), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((2664, 2719), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""localhost:5000/a/b/name"""'], {}), "('localhost:5000/a/b/name')\n", (2692, 2719), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((2950, 3009), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""localhost:5000/a/b/name:tag"""'], {}), "('localhost:5000/a/b/name:tag')\n", (2978, 3009), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((3245, 3318), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""localhost:5000/a/b/name:tag@sha256:abc123"""'], {}), "('localhost:5000/a/b/name:tag@sha256:abc123')\n", (3273, 3318), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((3579, 3648), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""localhost:5000/a/b/name@sha256:abc123"""'], {}), "('localhost:5000/a/b/name@sha256:abc123')\n", (3607, 3648), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((3904, 3954), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""name@sha256:abc123"""'], {}), "('name@sha256:abc123')\n", (3932, 3954), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((4156, 4226), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""gcr.io/hail-vdc/batch-worker:123fds312"""'], {}), "('gcr.io/hail-vdc/batch-worker:123fds312')\n", (4184, 4226), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((4489, 4568), 'hailtop.utils.parse_docker_image_reference', 'parse_docker_image_reference', (['"""us-docker.pkg.dev/my-project/my-repo/test-image"""'], {}), "('us-docker.pkg.dev/my-project/my-repo/test-image')\n", (4517, 4568), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((796, 825), 'hailtop.utils.url_basename', 'url_basename', (['"""/path/to/file"""'], {}), "('/path/to/file')\n", (808, 825), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((847, 891), 'hailtop.utils.url_basename', 'url_basename', (['"""https://hail.is/path/to/file"""'], {}), "('https://hail.is/path/to/file')\n", (859, 891), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((936, 964), 'hailtop.utils.url_join', 'url_join', (['"""/path/to"""', '"""file"""'], {}), "('/path/to', 'file')\n", (944, 964), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((995, 1024), 'hailtop.utils.url_join', 'url_join', (['"""/path/to/"""', '"""file"""'], {}), "('/path/to/', 'file')\n", (1003, 1024), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((1055, 1094), 'hailtop.utils.url_join', 'url_join', (['"""/path/to/"""', '"""/absolute/file"""'], {}), "('/path/to/', '/absolute/file')\n", (1063, 1094), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((1126, 1169), 'hailtop.utils.url_join', 'url_join', (['"""https://hail.is/path/to"""', '"""file"""'], {}), "('https://hail.is/path/to', 'file')\n", (1134, 1169), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((1215, 1259), 'hailtop.utils.url_join', 'url_join', (['"""https://hail.is/path/to/"""', '"""file"""'], {}), "('https://hail.is/path/to/', 'file')\n", (1223, 1259), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((1305, 1359), 'hailtop.utils.url_join', 'url_join', (['"""https://hail.is/path/to/"""', '"""/absolute/file"""'], {}), "('https://hail.is/path/to/', '/absolute/file')\n", (1313, 1359), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((1431, 1468), 'hailtop.utils.url_scheme', 'url_scheme', (['"""https://hail.is/path/to"""'], {}), "('https://hail.is/path/to')\n", (1441, 1468), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((1491, 1513), 'hailtop.utils.url_scheme', 'url_scheme', (['"""/path/to"""'], {}), "('/path/to')\n", (1501, 1513), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((1559, 1597), 'hailtop.utils.url_and_params', 'url_and_params', (['"""https://example.com/"""'], {}), "('https://example.com/')\n", (1573, 1597), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((1641, 1683), 'hailtop.utils.url_and_params', 'url_and_params', (['"""https://example.com/foo?"""'], {}), "('https://example.com/foo?')\n", (1655, 1683), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((1730, 1779), 'hailtop.utils.url_and_params', 'url_and_params', (['"""https://example.com/foo?a=b&c=d"""'], {}), "('https://example.com/foo?a=b&c=d')\n", (1744, 1779), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n'), ((198, 214), 'hailtop.utils.partition', 'partition', (['(0)', '[]'], {}), '(0, [])\n', (207, 214), False, 'from hailtop.utils import partition, url_basename, url_join, url_scheme, url_and_params, parse_docker_image_reference\n')]