def testExecuteAllOptions(self):
        """Tests execution of a method with filters, groups and order specified."""
        data_client = test_util.GetDataClient(mocked=True)
        data_client.mock_reply = self.EXPECTED_RESULT.copy()
        method = explorer_method.ExplorerQueryBase(
            data_client=data_client, dataset_name=self.EXPECTED_DATASET)

        method.tables = [self.EXPECTED_TABLE_NAME]
        method.fields = self.EXPECTED_FIELDS
        method.orders = self.EXPECTED_ORDERS
        method.wheres = self.EXPECTED_WHERES
        method.groups = self.EXPECTED_GROUPS

        actual = method.Execute()
        expected = self.EXPECTED_PROCESSED_RESULT

        self.assertEqual(actual, expected)
        self.assertEqual(actual['schema'], expected['schema'])
        self.assertEqual(actual['rows'], expected['rows'])

        expected_sql = (self.EXPECTED_SQL_BASE + self.EXPECTED_SQL_WHERE +
                        self.EXPECTED_SQL_GROUP_BY +
                        self.EXPECTED_SQL_ORDER_BY + self.EXPECTED_SQL_LIMIT)
        actual_sql = method.GetSql()

        self.assertEqual(actual_sql, expected_sql)
    def testExecuteNoFields(self):
        """Tests execution of a method without specifying fields."""
        data_client = test_util.GetDataClient(mocked=True)
        method = explorer_method.ExplorerQueryBase(
            data_client=data_client, dataset_name=self.EXPECTED_DATASET)

        method.tables = [self.EXPECTED_TABLE_NAME]
        self.assertRaisesRegexp(explorer_method.ArgumentError,
                                'The \'fields\' list is required.',
                                method.Execute)
Example #3
0
  def get(self):
    """Request handler for GET operations."""
    urlfetch.set_default_fetch_deadline(URLFETCH_TIMEOUT)
    filters = http_util.GetJsonParam(self.request, 'filters')

    start_date = filters['start_date']
    end_date = filters['end_date']
    product_name = filters['product_name']
    test = filters['test']
    metric = filters['metric']
    field_name = self.request.GET.get('field_name')

    config = explorer_config.ExplorerConfigModel.Get()
    client = DataHandlerUtil.GetDataClient(self.env)
    client.project_id = config.default_project

    query = explorer_method.ExplorerQueryBase(
        data_client=client,
        dataset_name=config.default_dataset)
    query.fields = [field_name + ' AS name']
    query.tables = ['lookup_field_cube']
    query.wheres = []

    if start_date:
      query.wheres.append(
          'day_timestamp >= %s' %
          (explorer_method.ExplorerQueryBase
           .GetTimestampFromFilterExpression(
               start_date)))

    if end_date:
      query.wheres.append(
          'day_timestamp <= %s' %
          (explorer_method.ExplorerQueryBase
           .GetTimestampFromFilterExpression(
               end_date)))

    if product_name and field_name != 'product_name':
      query.wheres.append('product_name = "%s"' % product_name)

    if test and field_name not in ['test', 'product_name']:
      query.wheres.append('test = "%s"' % test)

    if metric and field_name not in ['metric', 'test', 'product_name']:
      query.wheres.append('metric = "%s"' % metric)

    query.groups = ['name']
    query.orders = ['name']

    response = query.Execute()
    data = {'rows': response['rows']}

    self.RenderJson(data)
    def testExecuteRequiredOnly(self):
        """Tests execution of a method specifying only required parameters."""
        data_client = test_util.GetDataClient(mocked=True)
        data_client.mock_reply = self.EXPECTED_RESULT.copy()
        method = explorer_method.ExplorerQueryBase(
            data_client=data_client, dataset_name=self.EXPECTED_DATASET)

        method.tables = [self.EXPECTED_TABLE_NAME]
        method.fields = self.EXPECTED_FIELDS

        expected_sql = (self.EXPECTED_SQL_BASE + self.EXPECTED_SQL_LIMIT)
        actual_sql = method.GetSql()

        self.assertEqual(actual_sql, expected_sql)
    def testExecuteWithResult(self):
        """Verifies that the data_client's result is transformed and returned."""
        self.maxDiff = 1024

        data_client = test_util.GetDataClient(mocked=True)
        data_client.mock_reply = self.EXPECTED_RESULT.copy()
        method = explorer_method.ExplorerQueryBase(
            data_client=data_client, dataset_name=self.EXPECTED_DATASET)

        method.tables = [self.EXPECTED_TABLE_NAME]
        method.fields = self.EXPECTED_FIELDS
        actual = method.Execute()
        expected = self.EXPECTED_PROCESSED_RESULT

        self.assertEqual(actual, expected)