示例#1
0
    def test_main_app(self, client_mock):
        collection = 'cfht'
        observation_id = '7000000o'
        ifile = '/tmp/inputobs'

        obs = SimpleObservation(collection, observation_id)

        # test create
        with open(ifile, 'wb') as infile:
            ObservationWriter().write(obs, infile)
        sys.argv = [
            "caom2tools", "create", '--resource-id',
            'ivo://ca.nrc.ca/resource', ifile
        ]
        core.main_app()
        client_mock.return_value.put_observation.assert_called_with(obs)

        # test update
        sys.argv = [
            "caom2tools", "update", '--resource-id',
            'ivo://ca.nrc.ca/resource', ifile
        ]
        core.main_app()
        client_mock.return_value.post_observation.assert_called_with(obs)

        # test read
        sys.argv = [
            "caom2tools", "read", '--resource-id', 'ivo://ca.nrc.ca/resource',
            collection, observation_id
        ]
        client_mock.return_value.get_observation.return_value = obs
        client_mock.return_value.namespace = obs_reader_writer.CAOM24_NAMESPACE
        core.main_app()
        client_mock.return_value.get_observation.\
            assert_called_with(collection, observation_id)
        # repeat with output argument
        sys.argv = [
            "caom2tools", "read", '--resource-id', 'ivo://ca.nrc.ca/resource',
            "--output", ifile, collection, observation_id
        ]
        client_mock.return_value.get_observation.return_value = obs
        core.main_app()
        client_mock.return_value.get_observation.\
            assert_called_with(collection, observation_id)
        os.remove(ifile)

        # test delete
        sys.argv = [
            "caom2tools", "delete", '--resource-id',
            'ivo://ca.nrc.ca/resource', collection, observation_id
        ]
        core.main_app()
        client_mock.return_value.delete_observation.assert_called_with(
            collection=collection, observation_id=observation_id)

        # test visit
        # get the absolute path to be able to run the tests with the
        # astropy frameworks
        plugin_file = THIS_DIR + "/passplugin.py"
        sys.argv = [
            "caom2tools", "visit", '--resource-id', 'ivo://ca.nrc.ca/resource',
            "--plugin", plugin_file, "--start", "2012-01-01T11:22:33", "--end",
            "2013-01-01T11:33:22", collection
        ]
        client_mock.return_value.visit.return_value = ['1'], ['1'], [], []
        with open(plugin_file, 'r') as infile:
            core.main_app()
            client_mock.return_value.visit.assert_called_with(
                ANY,
                collection,
                halt_on_error=False,
                nthreads=None,
                obs_file=None,
                start=core.str2date("2012-01-01T11:22:33"),
                end=core.str2date("2013-01-01T11:33:22"))

        # repeat visit test with halt-on-error
        sys.argv = [
            "caom2tools", "visit", '--resource-id', 'ivo://ca.nrc.ca/resource',
            "--plugin", plugin_file, '--halt-on-error', "--start",
            "2012-01-01T11:22:33", "--end", "2013-01-01T11:33:22", collection
        ]
        client_mock.return_value.visit.return_value = ['1'], ['1'], [], []
        with open(plugin_file, 'r') as infile:
            core.main_app()
            client_mock.return_value.visit.assert_called_with(
                ANY,
                collection,
                halt_on_error=True,
                nthreads=None,
                obs_file=None,
                start=core.str2date("2012-01-01T11:22:33"),
                end=core.str2date("2013-01-01T11:33:22"))
示例#2
0
    def test_help(self):
        """ Tests the helper displays for commands and subcommands in main"""

        # expected helper messages
        with open(os.path.join(TESTDATA_DIR, 'help.txt'), 'r') as myfile:
            usage = myfile.read()
        with open(
                os.path.join(TESTDATA_DIR, 'create_help.txt'), 'r') as myfile:
            create_usage = myfile.read()
        with open(os.path.join(TESTDATA_DIR, 'read_help.txt'), 'r') as myfile:
            read_usage = myfile.read()
        with open(
                os.path.join(TESTDATA_DIR, 'update_help.txt'), 'r') as myfile:
            update_usage = myfile.read()
        with open(
                os.path.join(TESTDATA_DIR, 'delete_help.txt'), 'r') as myfile:
            delete_usage = myfile.read()
        with open(os.path.join(TESTDATA_DIR, 'visit_help.txt'), 'r') as myfile:
            visit_usage = myfile.read()

        self.maxDiff = None  # Display the entire difference
        # --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(usage, stdout_mock.getvalue())

        # create --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "create", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(create_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # read --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "read", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(read_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # update --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "update", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(update_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # delete --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "delete", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(delete_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # visit --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "visit", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(visit_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # visit too few number of threads
        with patch('sys.stderr', new_callable=StringIO) as stderr_mock:
            sys.argv = ["caom2-repo", "visit", "--threads", "1",
                        "--obs_file",
                        os.path.join(THIS_DIR, 'data/obs_id.txt'),
                        "--plugin", os.path.join(THIS_DIR, 'passplugin.py'),
                        "TEST"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertTrue('error: argument --threads: invalid choice' in
                            stderr_mock.getvalue())
        # print(stderr_mock.getvalue())

        # visit too many number of threads
        with patch('sys.stderr', new_callable=StringIO) as stderr_mock:
            sys.argv = ["caom2-repo", "visit", "--threads", "11",
                        "--obs_file",
                        os.path.join(THIS_DIR, 'data/obs_id.txt'),
                        "--plugin",
                        os.path.join(THIS_DIR, 'passplugin.py'), "TEST"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertTrue('error: argument --threads: invalid choice' in
                            stderr_mock.getvalue())
示例#3
0
    def test_help(self):
        """ Tests the helper displays for commands and subcommands in main"""

        # expected helper messages
        with open(os.path.join(TESTDATA_DIR, 'help.txt'), 'r') as myfile:
            usage = myfile.read()
        with open(os.path.join(TESTDATA_DIR, 'create_help.txt'),
                  'r') as myfile:
            create_usage = myfile.read()
        with open(os.path.join(TESTDATA_DIR, 'read_help.txt'), 'r') as myfile:
            read_usage = myfile.read()
        with open(os.path.join(TESTDATA_DIR, 'update_help.txt'),
                  'r') as myfile:
            update_usage = myfile.read()
        with open(os.path.join(TESTDATA_DIR, 'delete_help.txt'),
                  'r') as myfile:
            delete_usage = myfile.read()
        with open(os.path.join(TESTDATA_DIR, 'visit_help.txt'), 'r') as myfile:
            visit_usage = myfile.read()

        self.maxDiff = None  # Display the entire difference
        # --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(usage, stdout_mock.getvalue())

        # create --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "create", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(create_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # read --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "read", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(read_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # update --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "update", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(update_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # delete --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "delete", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(delete_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # visit --help
        with patch('sys.stdout', new_callable=StringIO) as stdout_mock:
            sys.argv = ["caom2-repo", "visit", "--help"]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertEqual(visit_usage, stdout_mock.getvalue())
        # print(stdout_mock.getvalue())

        # visit too few number of threads
        with patch('sys.stderr', new_callable=StringIO) as stderr_mock:
            sys.argv = [
                "caom2-repo", "visit", "--threads", "1", "--obs_file",
                os.path.join(THIS_DIR, 'data/obs_id.txt'), "--plugin",
                os.path.join(THIS_DIR, 'passplugin.py'), "TEST"
            ]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertTrue('error: argument --threads: invalid choice' in
                            stderr_mock.getvalue())
        # print(stderr_mock.getvalue())

        # visit too many number of threads
        with patch('sys.stderr', new_callable=StringIO) as stderr_mock:
            sys.argv = [
                "caom2-repo", "visit", "--threads", "11", "--obs_file",
                os.path.join(THIS_DIR, 'data/obs_id.txt'), "--plugin",
                os.path.join(THIS_DIR, 'passplugin.py'), "TEST"
            ]
            with self.assertRaises(MyExitError):
                core.main_app()
            self.assertTrue('error: argument --threads: invalid choice' in
                            stderr_mock.getvalue())
示例#4
0
    def test_main_app(self, client_mock):
        collection = 'cfht'
        observation_id = '7000000o'
        ifile = '/tmp/inputobs'

        obs = SimpleObservation(collection, observation_id)

        # test create
        with open(ifile, 'wb') as infile:
            ObservationWriter().write(obs, infile)
        sys.argv = ["caom2tools", "create", '--resource-id',
                    'ivo://ca.nrc.ca/resource', ifile]
        core.main_app()
        client_mock.return_value.put_observation.assert_called_with(obs)

        # test update
        sys.argv = ["caom2tools", "update", '--resource-id',
                    'ivo://ca.nrc.ca/resource', ifile]
        core.main_app()
        client_mock.return_value.post_observation.assert_called_with(obs)

        # test read
        sys.argv = ["caom2tools", "read", '--resource-id',
                    'ivo://ca.nrc.ca/resource',
                    collection, observation_id]
        client_mock.return_value.get_observation.return_value = obs
        core.main_app()
        client_mock.return_value.get_observation.\
            assert_called_with(collection, observation_id)
        # repeat with output argument
        sys.argv = ["caom2tools", "read", '--resource-id',
                    'ivo://ca.nrc.ca/resource',
                    "--output", ifile, collection, observation_id]
        client_mock.return_value.get_observation.return_value = obs
        core.main_app()
        client_mock.return_value.get_observation.\
            assert_called_with(collection, observation_id)
        os.remove(ifile)

        # test delete
        sys.argv = ["caom2tools", "delete", '--resource-id',
                    'ivo://ca.nrc.ca/resource',
                    collection, observation_id]
        core.main_app()
        client_mock.return_value.delete_observation.assert_called_with(
            collection=collection,
            observation_id=observation_id)

        # test visit
        # get the absolute path to be able to run the tests with the
        # astropy frameworks
        plugin_file = THIS_DIR + "/passplugin.py"
        sys.argv = ["caom2tools", "visit", '--resource-id',
                    'ivo://ca.nrc.ca/resource',
                    "--plugin", plugin_file, "--start", "2012-01-01T11:22:33",
                    "--end", "2013-01-01T11:33:22", collection]
        client_mock.return_value.visit.return_value = ['1'], ['1'], [], []
        with open(plugin_file, 'r') as infile:
            core.main_app()
            client_mock.return_value.visit.assert_called_with(
                ANY, collection, halt_on_error=False, nthreads=None,
                obs_file=None,
                start=core.str2date("2012-01-01T11:22:33"),
                end=core.str2date("2013-01-01T11:33:22"))

        # repeat visit test with halt-on-error
        sys.argv = ["caom2tools", "visit", '--resource-id',
                    'ivo://ca.nrc.ca/resource',
                    "--plugin", plugin_file, '--halt-on-error',
                    "--start", "2012-01-01T11:22:33",
                    "--end", "2013-01-01T11:33:22", collection]
        client_mock.return_value.visit.return_value = ['1'], ['1'], [], []
        with open(plugin_file, 'r') as infile:
            core.main_app()
            client_mock.return_value.visit.assert_called_with(
                ANY, collection, halt_on_error=True, nthreads=None,
                obs_file=None,
                start=core.str2date("2012-01-01T11:22:33"),
                end=core.str2date("2013-01-01T11:33:22"))