# -*- coding: utf-8 -*-
'''
    This module implements unit tests of the submission validation.
    
    Usage:
        python3 -m unittest discover -s test_suite_validation/
'''

import os, sys, glob
import unittest
import OpenASR_validate_submission

class TestValidateSubmission(unittest.TestCase):
    """
        Test submissions
    """

    def setUp(self):
        current_path = os.path.abspath(__file__)
        test_dir_path = os.path.dirname(current_path)

        self.submissions_path = os.path.join(test_dir_path, 'submissions')
        self.reference_path = os.path.join(test_dir_path, 'reference')
        self.language = "pashto"


    def test_valid_submissions(self):
        subdirs = glob.glob(os.path.join(self.submissions_path, 'PASS*'))

        assert len(subdirs) > 0

        for subdir in subdirs:
            sys.argv[1:] = ["-ref", os.path.join(self.reference_path, '-'.join(os.path.basename(subdir).split('-')[1:-1]) ),
                                "-s", subdir, "-l", self.language]
            try:
                OpenASR_validate_submission.main()
            except Exception:
                self.fail("Validator failed on valid submission {}".format(subdir))


    def test_invalid_submissions(self):
        subdirs = glob.glob(os.path.join(self.submissions_path, 'FAIL*'))

        assert len(subdirs) > 0

        for subdir in subdirs:
            sys.argv[1:] = ["-ref", os.path.join(self.reference_path, '-'.join(os.path.basename(subdir).split('-')[1:-1]) ),
                                "-s", subdir, "-l", self.language]
            with self.assertRaises(SystemExit):
                OpenASR_validate_submission.main()
                self.fail("Validator succedded on invalid submission {}".format(subdir))


if __name__ == '__main__':
    unittest.main()
