from csv_utils import byte_ranges, detect_delimiter


def test_detect_delimiter():
    with open('tests/samples/users.csv') as fp:
        assert detect_delimiter(fp) == ','


def test_byte_ranges():
    csvpath = 'tests/samples/users.csv'
    ranges = byte_ranges(csvpath, 10)
    *_, pair = ranges
    start_byte, end_byte = pair

    assert ranges == [(0, 808), (809, 1655), (1656, 2303)]

    expected = """,RICARDO GALLES BONET,ricardo.bonet@fanucamerica.com,424.430.528-93,NR-10 (RECICLAGEM)
,RULIO SIEFERT SERA,rulio.sera@fanucamerica.com,063.916.859-08,NR-10 (RECICLAGEM)
,MACIEL FERREIRA BOMFIM,maciel.bomfim@fanucamerica.com,334.547.088-85,NR-10 (RECICLAGEM)
,JAIME EDUARDO GALVEZ AVILES,jaime.galvez@fanucamerica.com,280.238.818-50,NR-12
,JAIME EDUARDO GALVEZ AVILES,jaime.galvez@fanucamerica.com,280.238.818-50,NR-35 (RECICLAGEM)
,HIGOR MACHADO SILVA,higor.silva@fanucamerica.com,419.879.878-88,NR-12
,LÁZARO SOUZA DIAS,lazaro.dias@fanucamerica.com,067.179.825-19,NR-12
,JOÃO PEDRO AGUIAR GALASSO,joao.pedro@fanucamerica.com,570.403.588-40,NR-12"""

    with open(csvpath, 'rb') as f:
        f.seek(start_byte)
        data = f.read(end_byte - start_byte + 1)
        assert data.decode('utf-8') == expected
