Example #1
0
    def test_SplitQueryFn_without_num_splits(self):
        with patch.object(helper,
                          'get_datastore',
                          return_value=self._mock_datastore):
            # Force SplitQueryFn to compute the number of query splits
            num_splits = 0
            expected_num_splits = 23
            entity_bytes = (expected_num_splits *
                            ReadFromDatastore._DEFAULT_BUNDLE_SIZE_BYTES)
            with patch.object(ReadFromDatastore,
                              'get_estimated_size_bytes',
                              return_value=entity_bytes):

                def fake_get_splits(datastore,
                                    query,
                                    num_splits,
                                    partition=None):
                    return self.split_query(query, num_splits)

                with patch.object(query_splitter,
                                  'get_splits',
                                  side_effect=fake_get_splits):
                    split_query_fn = ReadFromDatastore.SplitQueryFn(
                        self._PROJECT, self._query, None, num_splits)
                    split_query_fn.start_bundle()
                    returned_split_queries = []
                    for split_query in split_query_fn.process(self._query):
                        returned_split_queries.append(split_query)

                    self.assertEqual(len(returned_split_queries),
                                     expected_num_splits)
                    self.assertEqual(
                        0, len(self._mock_datastore.run_query.call_args_list))
                    self.verify_unique_keys(returned_split_queries)
Example #2
0
    def test_SplitQueryFn_with_exception(self):
        """A test that verifies that no split is performed when failures occur."""
        with patch.object(helper,
                          'get_datastore',
                          return_value=self._mock_datastore):
            # Force SplitQueryFn to compute the number of query splits
            num_splits = 0
            expected_num_splits = 1
            entity_bytes = (expected_num_splits *
                            ReadFromDatastore._DEFAULT_BUNDLE_SIZE_BYTES)
            with patch.object(ReadFromDatastore,
                              'get_estimated_size_bytes',
                              return_value=entity_bytes):

                with patch.object(
                        query_splitter,
                        'get_splits',
                        side_effect=ValueError("Testing query split error")):
                    split_query_fn = ReadFromDatastore.SplitQueryFn(
                        self._PROJECT, self._query, None, num_splits)
                    split_query_fn.start_bundle()
                    returned_split_queries = []
                    for split_query in split_query_fn.process(self._query):
                        returned_split_queries.append(split_query)

                    self.assertEqual(len(returned_split_queries),
                                     expected_num_splits)
                    self.assertEqual(returned_split_queries[0][1], self._query)
                    self.assertEqual(
                        0, len(self._mock_datastore.run_query.call_args_list))
                    self.verify_unique_keys(returned_split_queries)
Example #3
0
    def test_SplitQueryFn_with_query_limit(self):
        """A test that verifies no split is performed when the query has a limit."""
        with patch.object(helper,
                          'get_datastore',
                          return_value=self._mock_datastore):
            self._query.limit.value = 3
            split_query_fn = ReadFromDatastore.SplitQueryFn(
                self._PROJECT, self._query, None, 4)
            split_query_fn.start_bundle()
            returned_split_queries = []
            for split_query in split_query_fn.process(self._query):
                returned_split_queries.append(split_query)

            self.assertEqual(1, len(returned_split_queries))
            self.assertEqual(0, len(self._mock_datastore.method_calls))
Example #4
0
  def test_SplitQueryFn_with_num_splits(self):
    with patch.object(helper, 'get_datastore',
                      return_value=self._mock_datastore):
      num_splits = 23

      def fake_get_splits(datastore, query, num_splits, partition=None):
        return self.split_query(query, num_splits)

      with patch.object(query_splitter, 'get_splits',
                        side_effect=fake_get_splits):

        split_query_fn = ReadFromDatastore.SplitQueryFn(
            self._PROJECT, self._query, None, num_splits)
        mock_context = MagicMock()
        mock_context.element = self._query
        split_query_fn.start_bundle(mock_context)
        returned_split_queries = []
        for split_query in split_query_fn.process(mock_context):
          returned_split_queries.append(split_query)

        self.assertEqual(len(returned_split_queries), num_splits)
        self.assertEqual(0, len(self._mock_datastore.run_query.call_args_list))
        self.verify_unique_keys(returned_split_queries)