Source

audrid / tests / test_validators.py

Full commit
# coding: utf-8

from voluptuous import InvalidList
from audrid.validators import (
    cloze,
    text,
    single,
    multiple_choice,
    mapping,
    pool,
)
import unittest
import json
import os

def openrel(filename):
    """ Return a handle on a filename, relative to this test file and don't care
        where this is called from.
    """
    return open(os.path.join(os.path.abspath(
        os.path.dirname(__file__)), filename))

class TaskValidatorTests(unittest.TestCase):

    def should_pass_on(self, filenames, schema=None):
        for fn in filenames:
            with openrel(fn) as handle:
                content = json.load(handle)
                self.assertEqual(content, schema(content))

    def should_fail_on(self, filename_exc_tuples, schema=None):
        for fn, exc in filename_exc_tuples:
            with self.assertRaises(exc):
                with openrel(fn) as handle:
                    schema(json.load(handle))

    def test_cloze(self):
        valid_files = [
            "documents/cloze0.json",
        ]
        invalid_files = [
            ("documents/cloze1.json", InvalidList),
            ("documents/cloze2.json", InvalidList),
            ("documents/cloze3.json", InvalidList),
            ("documents/cloze4.json", InvalidList),
            ("documents/empty.json", ValueError),
        ]

        self.should_pass_on(valid_files, schema=cloze)
        self.should_fail_on(invalid_files, schema=cloze)

    def test_text(self):
        valid_files = [
            "documents/text0.json",
        ]
        invalid_files = [
            ("documents/text1.json", InvalidList),
        ]

        self.should_pass_on(valid_files, schema=text)
        self.should_fail_on(invalid_files, schema=text)

    def test_mapping(self):
        valid_files = [
            "documents/mapping0.json",
        ]
        invalid_files = [
            ("documents/mapping1.json", InvalidList),
            ("documents/empty.json", ValueError),
        ]

        self.should_pass_on(valid_files, schema=mapping)
        self.should_fail_on(invalid_files, schema=mapping)

    def test_single(self):
        valid_files = [
            "documents/single0.json",
        ]
        invalid_files = [
            ("documents/single1.json", InvalidList),
            ("documents/empty.json", ValueError),
        ]

        self.should_pass_on(valid_files, schema=single)
        self.should_fail_on(invalid_files, schema=single)

    def test_multiple_choice(self):
        self.maxDiff = None
        valid_files = [
            "documents/mc0.json",
        ]
        invalid_files = [
            ("documents/mc1.json", InvalidList),
            ("documents/empty.json", ValueError),
        ]

        self.should_pass_on(valid_files, schema=multiple_choice)
        self.should_fail_on(invalid_files, schema=multiple_choice)

class PoolValidatorTests(unittest.TestCase):

    def should_pass_on(self, filenames, schema=None):
        for fn in filenames:
            with openrel(fn) as handle:
                content = json.load(handle)
                self.assertEqual(content, schema(content))

    def should_fail_on(self, filename_exc_tuples, schema=None):
        for fn, exc in filename_exc_tuples:
            with self.assertRaises(exc):
                with openrel(fn) as handle:
                    schema(json.load(handle))

    def test_pool(self):
        valid_files = [
            "documents/pool0.json",
            "documents/pool2.json",
            "documents/pool4.json",
        ]
        invalid_files = [
            ("documents/pool1.json", InvalidList),
            ("documents/pool3.json", InvalidList),
            ("documents/empty.json", ValueError),
        ]

        self.should_pass_on(valid_files, schema=pool)
        self.should_fail_on(invalid_files, schema=pool)