Example #1
0
    def test_get_online_labstats_data(self):
        test_spaces = self.load_json_file('online_labstats_spaces.json')
        mock = Mock()
        mock.status_code = 200
        mock.json = Mock(return_value=test_spaces)
        customers = online_labstats.get_customers(test_spaces)
        customer = customers['749b5ac3-597f-4316-957c-abe939800634']
        page = customer[1002]
        # test a successful get request & assert the returned spaces match
        with patch.object(requests, 'get', return_value=mock):
            spaces = online_labstats.get_online_labstats_data(customer, page)
            self.assertEqual(test_spaces, spaces)

        with patch.object(requests, 'get', return_value=None) as mock_get:
            with patch('spacescout_labstats.endpoints.' +
                       'online_labstats.logger') as mock_log:
                mock_get.side_effect = Exception()
                ret = online_labstats.get_online_labstats_data(customer, page)
                mock_log.error.assert_called_with('Retrieving labstats' +
                                                  ' page failed!', exc_info=1)
                self.assertIs(ret, None)

        bad_resp = Response()
        bad_resp.status_code = 200
        bad_resp._content = b'{ "key" : "a", \\x}'
        with patch.object(requests, 'get', return_value=bad_resp) as mock_get:
            with patch('spacescout_labstats.endpoints.' +
                       'online_labstats.logger') as mock_log:
                ret = online_labstats.get_online_labstats_data(customer, page)
                mock_log.error.assert_called_with('Invalid json received' +
                                                  ' from online labstats '
                                                  'service!Body is { "key" ' +
                                                  ': "a", \\x}', exc_info=1)
                self.assertIs(ret, None)
Example #2
0
    def test_get_labstats_customers(self):
        """
        Loads sample JSON data and compares the output of get_customers to the
        format intended.
        """
        # load the content and garbage collect the file object
        test_json = self.load_json_file('bothell_labstats_spaces.json')

        individual_page_dict = {
                                    "UW2 Lower Level Kiosks": 257,
                                    "UW2 First Floor Kiosks": 258,
                                    "UW1 Second Floor Kiosks": 259,
                                    "Open Learning Lab (UW2-140) Mac": 296,
                                    "Open Learning Lab (UW2-140) Pc": 297,
                                    "UW Husky Hall Kiosks": 273,
                                    "UW Husky Village Kiosks": 274,
                                    "UW1 First Floor Kiosks": 250,
                                    "UW2 Second Floor Kiosks": 252,
                                    "UW1 Third Floor Kiosks": 254
                                }

        pages_dict = {1002: individual_page_dict}
        customers_dict = {"749b5ac3-597f-4316-957c-abe939800634": pages_dict}
        customers = online_labstats.get_customers(test_json)

        self.assertEqual(customers, customers_dict)
Example #3
0
    def test_load_labstats_data(self):
        # TODO : test this with bad data somehow
        """
        This tests the utility method that performs the loading of the online
        labstats data into the spaces.
        """
        labstats_spaces = self.load_json_file('bothell_labstats_spaces.json')

        loaded_labstats_spaces = self.load_json_file(
            'loaded_bothell_labstats_spaces.json')

        online_labstats_data = self.load_json_file(
            'online_labstats_data.json')

        customers = online_labstats.get_customers(labstats_spaces)

        customer = customers["749b5ac3-597f-4316-957c-abe939800634"]

        page = customer[1002]

        online_labstats.load_labstats_data(labstats_spaces,
                                           online_labstats_data,
                                           page)

        self.assertEqual(labstats_spaces, loaded_labstats_spaces)
Example #4
0
    def test_load_labstats_data(self):
        # TODO : test this with bad data somehow
        """
        This tests the utility method that performs the loading of the online
        labstats data into the spaces.
        """
        labstats_spaces = self.load_json_file('bothell_labstats_spaces.json')

        loaded_labstats_spaces = self.load_json_file(
            'loaded_bothell_labstats_spaces.json')

        online_labstats_data = self.load_json_file(
            'online_labstats_data.json')

        customers = online_labstats.get_customers(labstats_spaces)

        customer = customers["749b5ac3-597f-4316-957c-abe939800634"]

        page = customer[1002]

        online_labstats.load_labstats_data(labstats_spaces,
                                           online_labstats_data,
                                           page)

        self.assertEqual(labstats_spaces, loaded_labstats_spaces)

        # Test with bad data
        page = {
            'UW Husky Hall Kiosks': 273
        }
        online_labstats_data["Groups"] = online_labstats_data["Groups"][:1]
        test_spaces = self.load_json_file('online_labstats_spaces.json')[:1]
        with patch('spacescout_labstats.endpoints' +
                   '.online_labstats.logger') as mock_log:
            online_labstats.load_labstats_data(test_spaces,
                                               online_labstats_data,
                                               page)
            mock_log.warning.assert_called_with('space 273 missing' +
                                                ' from spaces!')
            mock_log.reset_mock()

            page = {
                'UW2 Lower Level Kiosks': 257
            }
            online_labstats.load_labstats_data(test_spaces,
                                               online_labstats_data,
                                               page)
            mock_log.warning.assert_called_with('Labstat entry not found for' +
                                                ' label %s and space #257',
                                                u'UW2 Lower Level Kiosks')
Example #5
0
    def test_get_labstats_customers(self):
        """
        Loads sample JSON data and compares the output of get_customers to the
        format intended.
        """
        # load the content and garbage collect the file object
        test_json = self.load_json_file('bothell_labstats_spaces.json')

        individual_page_dict = {
                                    "UW2 Lower Level Kiosks": 257,
                                    "UW2 First Floor Kiosks": 258,
                                    "UW1 Second Floor Kiosks": 259,
                                    "Open Learning Lab (UW2-140) Mac": 296,
                                    "Open Learning Lab (UW2-140) Pc": 297,
                                    "UW Husky Hall Kiosks": 273,
                                    "UW Husky Village Kiosks": 274,
                                    "UW1 First Floor Kiosks": 250,
                                    "UW2 Second Floor Kiosks": 252,
                                    "UW1 Third Floor Kiosks": 254
                                }

        pages_dict = {1002: individual_page_dict}
        customers_dict = {"749b5ac3-597f-4316-957c-abe939800634": pages_dict}
        customers = online_labstats.get_customers(test_json)
        self.assertEqual(customers, customers_dict)

        # Get customers from spaces that have duplicate labstats lables
        test_spaces = self.load_json_file('online_labstats_spaces.json')
        # Mimic duplicate lables by setting the lables to be the same
        test_label = test_spaces[0]["extended_info"]["labstats_label"]
        test_spaces[1]["extended_info"]["labstats_label"] = test_label

        with patch('spacescout_labstats.endpoints' +
                   '.online_labstats.logger') as mock_log:
            online_labstats.get_customers(test_spaces)
            mock_log.warning.assert_called_once()