Пример #1
0
    def test_sparql_manager_bindings_to_dataframe(self):
        bindings = [{
            'name': {
                'type': 'literal',
                'value': 'obj1'
            },
            'stuff': {
                'value': '2'
            }
        }, {
            'name': {
                'type': 2,
                'value': 17
            },
            'stuff': {
                'value': 'I am a thing.'
            }
        }]

        expected = pd.DataFrame({
            'name': ['obj1', 17],
            'stuff': ['2', 'I am a thing.']
        })

        # Ensure _check_bindings is called.
        with patch('pyvvo.sparql.SPARQLManager._check_bindings',
                   return_value=None) as mock:
            # Check the return value.
            actual = self.sparql._bindings_to_dataframe(bindings,
                                                        to_numeric=False)

            mock.assert_called_once()

        _df.ensure_frame_equal_except_mrid(actual, expected)
Пример #2
0
 def test_all(self):
     """Loop and subtest."""
     for t in self.a:
         actual = t[0]()
         expected = _df.read_pickle(t[1])
         with self.subTest(msg=t[1]):
             _df.ensure_frame_equal_except_mrid(actual, expected)
Пример #3
0
    def test_sparql_manager_query_simple_query(self):
        """Get first MRID from query_capacitors_9500.csv and look up
        the item.
        """
        df = pd.read_csv(_df.CAPACITORS_9500)
        mrid = df.iloc[0]['mrid']
        name = df.iloc[0]['name']

        query = (self.sparql.PREFIX + "SELECT ?name "
                 "WHERE {{ "
                 '?s c:IdentifiedObject.mRID'
                 '  "{}". '
                 "?s c:IdentifiedObject.name ?name. "
                 "}} "
                 "ORDER BY ?name ").format(mrid)

        actual = self.sparql._query(query, to_numeric=False)
        expected = pd.DataFrame({'name': [name]})
        _df.ensure_frame_equal_except_mrid(actual, expected)
Пример #4
0
    def test_sparql_manager_query_capacitor_measurements_expected(self):
        """The 9500 node system has a weird capacitor setup:

        3 3 phase units, but each phase counted as an individual.
        1 uncontrollable 3 phase unit, counted as a group.
        """
        actual = self.sparql.query_capacitor_measurements()

        # Read expected value.
        expected = _df.read_pickle(_df.CAP_MEAS_9500)

        _df.ensure_frame_equal_except_mrid(actual, expected)

        # Ensure we get measurements associated with 10 capacitors.
        # (3 * 3) + 1, see docstring.
        caps = actual['cap_mrid'].unique()
        self.assertEqual(10, len(caps))

        # Ensure the measurements are unique.
        self.assertEqual(actual['state_meas_mrid'].unique().shape[0],
                         actual.shape[0])
Пример #5
0
    def test_sparql_manager_query_calls_query_platform(self):
        """Ensure _query_named_objects calls _query_platform, as
        _query_platform does the error handling.
        """
        # Mock the silly-deep return from the platform.
        config = \
            {'return_value':
                {'data':
                    {'results':
                        {'bindings':
                            [{'name': {
                                'value': 'object1'}}]}}}}

        with patch('pyvvo.sparql.SPARQLManager._query_platform', **config) \
                as mock:
            actual = self.sparql._query('some query', to_numeric=False)
            mock.assert_called_once()
            mock.assert_called_with('some query')

        # Test return value.
        expected = pd.DataFrame({'name': ['object1']})
        _df.ensure_frame_equal_except_mrid(actual, expected)
Пример #6
0
    def test_sparql_manager_bindings_to_dataframe_mismatched_lengths(self):
        """A warning should be thrown, output should have NaNs."""
        bindings = [{
            'name': {
                'type': 'literal',
                'value': 'obj1'
            },
            'things': {
                'value': '2'
            }
        }, {
            'name': {
                'type': 2,
                'value': 17
            },
            'stuff': {
                'value': 'I am a thing.'
            },
            'things': {
                'value': '11'
            }
        }]

        expected = pd.DataFrame({
            'name': ['obj1', 17],
            'things': ['2', '11'],
            'stuff': [np.nan, 'I am a thing.']
        })

        # Ensure a warning is logged.
        with self.assertLogs(self.sparql.log, level='DEBUG'):
            actual = self.sparql._bindings_to_dataframe(bindings,
                                                        to_numeric=False)

        # Ensure values match.
        _df.ensure_frame_equal_except_mrid(actual, expected)