<commit_before>import pytest
import io
import json
from .. import config
from .. import vtt

def test_from_srt(ASSETS):
  f = io.BytesIO()
  vtt.from_srt(ASSETS + 'subs.srt', f)
  compare = open(ASSETS + 'subs.vtt', 'r')
  assert f.getvalue() == compare.read()

def test_from_srt_file(ASSETS):
  i = open(ASSETS + 'subs.srt')
  o = io.BytesIO()
  vtt.from_srt(i, o)
  compare = open(ASSETS + 'subs.vtt', 'r')
  assert o.getvalue() == compare.read()

def test_from_bad_srt(ASSETS):
  i = open(ASSETS + 'fake.srt')
  o = io.BytesIO()
  with pytest.raises(Exception):
    vtt.from_srt(i, o)

def test_iso_8859_srt(ASSETS):
  i = open(ASSETS + 'ISO-8859.srt')
  o = io.BytesIO()
  vtt.from_srt(i, o)
  compare = open(ASSETS + 'utf8.vtt', 'r')
  assert o.getvalue() == compare.read()

def test_shift_time(ASSETS):
  f = io.BytesIO()
  vtt.shift_time(ASSETS + 'subs.vtt', f, 10)
  compare = open(ASSETS + 'subs+10.vtt', 'r')
  assert f.getvalue() == compare.read()

def test_shift_time_file(ASSETS):
  i = open(ASSETS + 'subs.vtt', 'r')
  o = io.BytesIO()
  vtt.shift_time(i, o, 10)
  i.close()
  compare = open(ASSETS + 'subs+10.vtt', 'r')
  assert o.getvalue() == compare.read()

def test_validate_vtt(ASSETS):
  assert vtt.is_valid(ASSETS + 'subs.vtt') is True

def test_invalid_vtt(ASSETS):
  assert vtt.is_valid(ASSETS + 'invalid.vtt') is False

def test_upload_srt(ASSETS, ACCOUNTS, app):
  app.login(ACCOUNTS["SUPERUSER"])
  response = None

  with open(ASSETS + 'subs.srt') as f:
    data = {"subtitle": (f, 'subs.srt')}
    response = app.post('/video', data=data)
  
  assert response.status_code == 200
  data = json.loads(response.data)
  file = data['ma:hasRelatedResource'][0]['@id']
  assert file.split('.')[-1] == 'vtt'
  filename = file.split('/')[-1]
  
  file = open(config.SUBTITLE_DIRECTORY + filename, 'r')
  orig = open(ASSETS + 'subs.vtt', 'r')
  assert orig.read() == file.read()

def test_upload_vtt(ASSETS, ACCOUNTS, app):
  app.login(ACCOUNTS["SUPERUSER"])
  response = None

  with open(ASSETS + 'subs.vtt') as f:
    data = {
        'subtitle': (f, 'subs.vtt'),
        'name': "The One True Subtitle",
        'lang': 'en'
    }
    response = app.post('/video', data=data)
  
  assert response.status_code == 200
  data = json.loads(response.data)
  file = data['ma:hasRelatedResource'][0]['@id']
  assert data['ma:hasRelatedResource'][0]['name'] == 'The One True Subtitle'
  assert data['ma:hasRelatedResource'][0]['language'] == 'en'
  assert file.split('.')[-1] == 'vtt'
  filename = file.split('/')[-1]
  
  file = open(config.SUBTITLE_DIRECTORY + filename, 'r')
  orig = open(ASSETS + 'subs.vtt', 'r')
  assert orig.read() == file.read()

def test_upload_multi_period_vtt(ASSETS, ACCOUNTS, app):
  app.login(ACCOUNTS["SUPERUSER"])
  response = None

  with open(ASSETS + 'subs.vtt') as f:
    data = {
        'subtitle': (f, 'has.lots.of.periods.vtt')
    }
    response = app.post('/video', data=data)
  
  assert response.status_code == 200<commit_msg>Test for 400 status code when submitting an invalid file<commit_after>import pytest
import io
import json
from .. import config
from .. import vtt

def test_from_srt(ASSETS):
  f = io.BytesIO()
  vtt.from_srt(ASSETS + 'subs.srt', f)
  compare = open(ASSETS + 'subs.vtt', 'r')
  assert f.getvalue() == compare.read()

def test_from_srt_file(ASSETS):
  i = open(ASSETS + 'subs.srt')
  o = io.BytesIO()
  vtt.from_srt(i, o)
  compare = open(ASSETS + 'subs.vtt', 'r')
  assert o.getvalue() == compare.read()

def test_from_bad_srt(ASSETS):
  i = open(ASSETS + 'fake.srt')
  o = io.BytesIO()
  with pytest.raises(Exception):
    vtt.from_srt(i, o)

def test_iso_8859_srt(ASSETS):
  i = open(ASSETS + 'ISO-8859.srt')
  o = io.BytesIO()
  vtt.from_srt(i, o)
  compare = open(ASSETS + 'utf8.vtt', 'r')
  assert o.getvalue() == compare.read()

def test_shift_time(ASSETS):
  f = io.BytesIO()
  vtt.shift_time(ASSETS + 'subs.vtt', f, 10)
  compare = open(ASSETS + 'subs+10.vtt', 'r')
  assert f.getvalue() == compare.read()

def test_shift_time_file(ASSETS):
  i = open(ASSETS + 'subs.vtt', 'r')
  o = io.BytesIO()
  vtt.shift_time(i, o, 10)
  i.close()
  compare = open(ASSETS + 'subs+10.vtt', 'r')
  assert o.getvalue() == compare.read()

def test_validate_vtt(ASSETS):
  assert vtt.is_valid(ASSETS + 'subs.vtt') is True

def test_invalid_vtt(ASSETS):
  assert vtt.is_valid(ASSETS + 'invalid.vtt') is False

def test_upload_srt(ASSETS, ACCOUNTS, app):
  app.login(ACCOUNTS["SUPERUSER"])
  response = None

  with open(ASSETS + 'subs.srt') as f:
    data = {"subtitle": (f, 'subs.srt')}
    response = app.post('/video', data=data)
  
  assert response.status_code == 200
  data = json.loads(response.data)
  file = data['ma:hasRelatedResource'][0]['@id']
  assert file.split('.')[-1] == 'vtt'
  filename = file.split('/')[-1]
  
  file = open(config.SUBTITLE_DIRECTORY + filename, 'r')
  orig = open(ASSETS + 'subs.vtt', 'r')
  assert orig.read() == file.read()

def test_upload_vtt(ASSETS, ACCOUNTS, app):
  app.login(ACCOUNTS["SUPERUSER"])
  response = None

  with open(ASSETS + 'subs.vtt') as f:
    data = {
        'subtitle': (f, 'subs.vtt'),
        'name': "The One True Subtitle",
        'lang': 'en'
    }
    response = app.post('/video', data=data)
  
  assert response.status_code == 200
  data = json.loads(response.data)
  file = data['ma:hasRelatedResource'][0]['@id']
  assert data['ma:hasRelatedResource'][0]['name'] == 'The One True Subtitle'
  assert data['ma:hasRelatedResource'][0]['language'] == 'en'
  assert file.split('.')[-1] == 'vtt'
  filename = file.split('/')[-1]
  
  file = open(config.SUBTITLE_DIRECTORY + filename, 'r')
  orig = open(ASSETS + 'subs.vtt', 'r')
  assert orig.read() == file.read()

def test_upload_invalid_vtt(ASSETS, ACCOUNTS, app):
  app.login(ACCOUNTS['SUPERUSER'])
  response = None

  with open(ASSETS + 'invalid.vtt') as f:
    data = {
        'subtitle': (f, 'subs.vtt'),
        'name': "The One True Subtitle",
        'lang': 'en'
    }
    response = app.post('/video', data=data)

  assert response.status_code == 400
    

def test_upload_multi_period_vtt(ASSETS, ACCOUNTS, app):
  app.login(ACCOUNTS["SUPERUSER"])
  response = None

  with open(ASSETS + 'subs.vtt') as f:
    data = {
        'subtitle': (f, 'has.lots.of.periods.vtt')
    }
    response = app.post('/video', data=data)
  
  assert response.status_code == 200
{'fraction_non_alphanumeric': 0.17984934086629, 'fraction_numerical': 0.011613308223477715, 'mean_word_length': 3.79894578313253, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5597767', 'n_tokens_mistral': 2662, 'n_tokens_neox': 2538, 'n_words': 515}