示例#1
0
    def test_weather(self):
        options = JCMTWeather.get_available()
        self.assertIsInstance(options, OrderedDict)

        option_names = JCMTWeather.get_options()
        self.assertIsInstance(option_names, OrderedDict)

        weathers = set()
        for weather in (
                JCMTWeather.BAND1,
                JCMTWeather.BAND2,
                JCMTWeather.BAND3,
                JCMTWeather.BAND4,
                JCMTWeather.BAND5,
                ):
            # Identifiers should be unique integers.
            self.assertIsInstance(weather, int)
            self.assertNotIn(weather, weathers)
            weathers.add(weather)

            # is_valid and get_name should work on these values.
            self.assertTrue(JCMTWeather.is_valid(weather))
            weather_name = JCMTWeather.get_name(weather)
            self.assertIsInstance(weather_name, unicode)

            # It should also be in the list of options.
            self.assertIn(weather, options)
            self.assertIsInstance(options[weather].name, unicode)
            self.assertEqual(options[weather].name, weather_name)

            self.assertIn(weather, option_names)
            self.assertIsInstance(option_names[weather], unicode)
            self.assertEqual(option_names[weather], weather_name)

        self.assertFalse(JCMTWeather.is_valid(0))
        self.assertFalse(JCMTWeather.is_valid(999))

        # Check set of weathers considered to be available.
        self.assertEqual(set(options.keys()), weathers)

        self.assertEqual(set(JCMTWeather.get_options().keys()), weathers)

        self.assertEqual(
            set(JCMTWeather.get_options(include_unavailable=True).keys()),
            weathers)
示例#2
0
    def test_request_collection(self):
        c = JCMTRequestCollection()
        self.assertIsInstance(c, OrderedDict)

        # An empty table should have a table attribute which is false.
        t = c.to_table()
        self.assertIsInstance(t, ResultTable)
        self.assertFalse(t.table)

        total = c.get_total()
        self.assertIsInstance(total, JCMTRequestTotal)
        self.assertEqual(total.total, 0.0)
        self.assertEqual(total.instrument, {})
        self.assertEqual(total.instrument_grouped, {})
        self.assertEqual(total.weather, {})
        self.assertEqual(total.total_non_free, 0.0)

        # Add some rows.
        c[1] = JCMTRequest(1, 0, instrument=1, ancillary=0,
                           weather=1, time=10.0)
        c[2] = JCMTRequest(2, 0, instrument=1, ancillary=0,
                           weather=1, time=20.0)
        c[3] = JCMTRequest(3, 0, instrument=1, ancillary=0,
                           weather=2, time=100.0)

        # The table's table attribute is now true.
        t = c.to_table()
        self.assertIsInstance(t, ResultTable)
        self.assertTrue(t.table)

        # We should have the instrument, but no totals as there is only one.
        self.assertIn((1, 0), t.table)
        self.assertNotIn(0, t.table)

        # Check the weather band times have been added up as expected.
        self.assertEqual(t.table[(1, 0)], {1: 30.0, 2: 100.0, None: 130.0})

        # And we should have sensible rows and columns dictionaries.
        self.assertIsInstance(t.rows, OrderedDict)
        self.assertEqual(list(t.rows.keys()), [(1, 0)])

        self.assertIsInstance(t.columns, OrderedDict)
        self.assertEqual(list(t.columns.keys()),
                         list(JCMTWeather.get_available().keys()))

        # Add data for more instruments.
        c[4] = JCMTRequest(4, 0, instrument=2, ancillary=0,
                           weather=2, time=200.0)
        c[5] = JCMTRequest(5, 0, instrument=4, ancillary=0,
                           weather=5, time=1000.0)
        t = c.to_table()

        # Check the rows.
        self.assertEqual(
            set(t.table.keys()), set((None, (1, 0), (2, 0), (4, 0))))
        self.assertEqual(
            set(t.rows.keys()), set(((1, 0), (2, 0), (4, 0))))

        # And check the combined times.
        self.assertEqual(
            t.table[(1, 0)], {1: 30.0, 2: 100.0,            None: 130.0})
        self.assertEqual(
            t.table[(2, 0)],          {2: 200.0,            None: 200.0})
        self.assertEqual(
            t.table[(4, 0)],                    {5: 1000.0, None: 1000.0})
        self.assertEqual(
            t.table[None],   {1: 30.0, 2: 300.0, 5: 1000.0, None: 1330.0})

        total = c.get_total()
        self.assertIsInstance(total, JCMTRequestTotal)
        self.assertEqual(total.total, 1330.0)
        self.assertEqual(
            total.instrument, {(1, 0): 130.0, (2, 0): 200.0, (4, 0): 1000.0})
        self.assertEqual(
            total.instrument_total, {1: 130.0, 2: 200.0, 4: 1000.0})
        self.assertEqual(
            total.instrument_grouped,
            {1: {0: 130.0}, 2: {0: 200.0}, 4: {0: 1000.0}})
        self.assertEqual(total.weather, {1: 30.0, 2: 300.0, 5: 1000.0})
        self.assertEqual(total.total_non_free, 330.0)

        # Check conversion to sorted list.
        c[6] = JCMTRequest(6, 0, instrument=2, ancillary=0,
                           weather=1, time=500.0)
        sorted_list = c.to_sorted_list()
        self.assertIsInstance(sorted_list, list)
        self.assertEqual(len(sorted_list), 6)
        self.assertEqual(sorted_list[0].id, 1)
        self.assertEqual(sorted_list[1].id, 2)
        self.assertEqual(sorted_list[2].id, 3)
        self.assertEqual(sorted_list[3].id, 6)
        self.assertEqual(sorted_list[4].id, 4)
        self.assertEqual(sorted_list[5].id, 5)

        self.assertEqual(sorted_list[0].instrument, 'SCUBA-2')
        self.assertEqual(sorted_list[1].instrument, 'SCUBA-2')
        self.assertEqual(sorted_list[2].instrument, 'SCUBA-2')
        self.assertEqual(sorted_list[3].instrument, 'HARP')
        self.assertEqual(sorted_list[4].instrument, 'HARP')
        self.assertEqual(sorted_list[5].instrument, 'RxA3m')

        self.assertEqual(sorted_list[0].weather, 'Band 1')
        self.assertEqual(sorted_list[1].weather, 'Band 1')
        self.assertEqual(sorted_list[2].weather, 'Band 2')
        self.assertEqual(sorted_list[3].weather, 'Band 1')
        self.assertEqual(sorted_list[4].weather, 'Band 2')
        self.assertEqual(sorted_list[5].weather, 'Band 5')

        self.assertEqual(sorted_list[0].time, 10.0)
        self.assertEqual(sorted_list[1].time, 20.0)
        self.assertEqual(sorted_list[2].time, 100.0)
        self.assertEqual(sorted_list[3].time, 500.0)
        self.assertEqual(sorted_list[4].time, 200.0)
        self.assertEqual(sorted_list[5].time, 1000.0)

        # This result collection isn't valid because instrument=1, weather=1
        # is repeated.
        with self.assertRaisesRegexp(UserError, 'multiple entries'):
            c.validate()

        # Validation should succeed if we remove the offending entry.
        del c[1]
        c.validate()

        # Check the other validation constraints.
        c[1] = JCMTRequest(1, 0, instrument=1, ancillary=0,
                           weather=1, time='')
        with self.assertRaisesRegexp(UserError, 'valid number'):
            c.validate()

        c[1] = JCMTRequest(1, 0, instrument=0, ancillary=0,
                           weather=1, time=1.0)
        with self.assertRaisesRegexp(UserError, 'Instrument not recognised'):
            c.validate()

        c[1] = JCMTRequest(1, 0, instrument=1, ancillary=999,
                           weather=1, time=1.0)
        with self.assertRaisesRegexp(
                UserError, 'Ancillary instrument not recognised'):
            c.validate()

        c[1] = JCMTRequest(1, 0, instrument=1,
                           ancillary=1, weather=1, time=1.0)
        with self.assertRaisesRegexp(
                UserError, 'Ancillary not permitted for this instrument'):
            c.validate()

        c[1] = JCMTRequest(1, 0, instrument=1, ancillary=0,
                           weather=0, time=1.0)
        with self.assertRaisesRegexp(UserError, 'Weather band not recognised'):
            c.validate()