1"""
2unittest.TestCase for JSON Schema tests.
3"""
4
5from __future__ import absolute_import
6
7from . import interface
8from ... import config
9from ... import core
10from ... import utils
11
12
13class JSONSchemaTestCase(interface.TestCase):
14    """
15    A JSON Schema test to execute.
16    """
17
18    REGISTERED_NAME = "json_schema_test"
19    TEST_RUNNER_FILE = "jstests/libs/json_schema_test_runner.js"
20
21    def __init__(self,
22                 logger,
23                 json_filename,
24                 shell_executable=None,
25                 shell_options=None):
26        """Initializes the JSONSchemaTestCase with the JSON test file."""
27
28        interface.TestCase.__init__(self, logger, "JSON Schema Test", json_filename)
29
30        # Command line options override the YAML configuration.
31        self.shell_executable = utils.default_if_none(config.MONGO_EXECUTABLE, shell_executable)
32
33        self.json_filename = json_filename
34        self.shell_options = utils.default_if_none(shell_options, {}).copy()
35
36    def configure(self, fixture, *args, **kwargs):
37        interface.TestCase.configure(self, fixture, *args, **kwargs)
38
39        global_vars = self.shell_options.get("global_vars", {}).copy()
40
41        test_data = global_vars.get("TestData", {}).copy()
42        test_data["jsonSchemaTestFile"] = self.json_filename
43
44        global_vars["TestData"] = test_data
45        self.shell_options["global_vars"] = global_vars
46
47    def run_test(self):
48        try:
49            shell = self._make_process()
50            self._execute(shell)
51        except self.failureException:
52            raise
53        except:
54            self.logger.exception("Encountered an error running JSON Schema test %s.",
55                                  self.basename())
56            raise
57
58    def _make_process(self):
59        return core.programs.mongo_shell_program(
60            self.logger,
61            executable=self.shell_executable,
62            connection_string=self.fixture.get_driver_connection_url(),
63            filename=JSONSchemaTestCase.TEST_RUNNER_FILE,
64            **self.shell_options)
65