# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for bigbench_json_paths."""

from absl.testing import absltest
from bigbench.bbseqio import bigbench_bridge as bb
from bigbench.bbseqio import bigbench_json_paths

TEST_JSON_PATHS = {
    'simple_gen_task': 'paths1',
    'simple_mul_task': 'paths2',
    'composite_gen_task': 'paths3',
    'composite_mul_task': 'paths4',
    'combination_simple_task': 'paths5',
    'combination_composite_task': 'paths6',
    'task_with_alternate_path': 'path7',
}

TEST_METADATA = {
    'simple_gen_task': {
        'description': 'simple_gen_task',
        'subtask_names': [],
        'num_generate_text': 100,
        'num_multiple_choice': 0
    },
    'simple_mul_task': {
        'description': 'simple_mul_task',
        'subtask_names': [],
        'num_generate_text': 0,
        'num_multiple_choice': 100
    },
    'composite_gen_task': {
        'description': 'composite_gen_task',
        'subtask_names': ['gen_subtask_1', 'gen_subtask_2'],
        'num_generate_text': 100,
        'num_multiple_choice': 0
    },
    'composite_mul_task': {
        'description': 'composite_mul_task',
        'subtask_names': ['mul_subtask_1', 'mul_subtask_2'],
        'num_generate_text': 0,
        'num_multiple_choice': 100
    },
    'combination_simple_task': {
        'description': 'combination_simple_task',
        'subtask_names': [],
        'num_generate_text': 100,
        'num_multiple_choice': 100
    },
    'combination_composite_task': {
        'description': 'combination_composite_task',
        'subtask_names': ['combination_subtask_1', 'combination_subtask_2'],
        'num_generate_text': 100,
        'num_multiple_choice': 100
    },
    'gen_subtask_1': {
        'description': 'gen_subtask_1',
        'subtask_names': [],
        'num_generate_text': 50,
        'num_multiple_choice': 0
    },
    'gen_subtask_2': {
        'description': 'gen_subtask_2',
        'subtask_names': [],
        'num_generate_text': 50,
        'num_multiple_choice': 0
    },
    'mul_subtask_1': {
        'description': 'mul_subtask_1',
        'subtask_names': [],
        'num_generate_text': 0,
        'num_multiple_choice': 50
    },
    'mul_subtask_2': {
        'description': 'mul_subtask_2',
        'subtask_names': [],
        'num_generate_text': 0,
        'num_multiple_choice': 50
    },
    'combination_subtask_1': {
        'description': 'combination_subtask_1',
        'subtask_names': [],
        'num_generate_text': 50,
        'num_multiple_choice': 50
    },
    'combination_subtask_2': {
        'description': 'combination_subtask_2',
        'subtask_names': [],
        'num_generate_text': 50,
        'num_multiple_choice': 50
    },
    'task_with_alternate_path': {
        'description': 'task_with_alternate_path',
        'subtask_names': [],
        'num_generate_text': 100,
        'num_multiple_choice': 0,
        'alternate_path': '/alternate/path'
    }
}

TEST_SUBTASK_NAMES = {
    'composite_gen_task': ['gen_subtask_1', 'gen_subtask_2'],
    'composite_mul_task': ['mul_subtask_1', 'mul_subtask_2'],
    'combination_composite_task': [
        'combination_subtask_1', 'combination_subtask_2'
    ]
}


class BigbenchJsonPathsTest(absltest.TestCase):

  def test_get_task_names(self):
    self.assertListEqual(
        bigbench_json_paths.get_task_names(bb_json_paths=TEST_JSON_PATHS), [
            'combination_composite_task', 'combination_simple_task',
            'composite_gen_task', 'composite_mul_task', 'simple_gen_task',
            'simple_mul_task', 'task_with_alternate_path'
        ])

  def test_get_task_path(self):
    self.assertEqual(
        bigbench_json_paths.get_task_path(
            'simple_gen_task',
            bb_json_paths=TEST_JSON_PATHS,
            bb_json_metadata=TEST_METADATA)[0], 'paths1')
    self.assertEqual(
        bigbench_json_paths.get_task_path(
            'simple_mul_task',
            bb_json_paths=TEST_JSON_PATHS,
            bb_json_metadata=TEST_METADATA)[0], 'paths2')
    self.assertEqual(
        bigbench_json_paths.get_task_path(
            'composite_gen_task',
            bb_json_paths=TEST_JSON_PATHS,
            bb_json_metadata=TEST_METADATA)[0], 'paths3')
    self.assertEqual(
        bigbench_json_paths.get_task_path(
            'composite_mul_task',
            bb_json_paths=TEST_JSON_PATHS,
            bb_json_metadata=TEST_METADATA)[0], 'paths4')
    self.assertEqual(
        bigbench_json_paths.get_task_path(
            'combination_simple_task',
            bb_json_paths=TEST_JSON_PATHS,
            bb_json_metadata=TEST_METADATA)[0], 'paths5')
    self.assertEqual(
        bigbench_json_paths.get_task_path(
            'combination_composite_task',
            bb_json_paths=TEST_JSON_PATHS,
            bb_json_metadata=TEST_METADATA)[0], 'paths6')
    self.assertEqual(
        bigbench_json_paths.get_task_path(
            'task_with_alternate_path',
            bb_json_paths=TEST_JSON_PATHS,
            bb_json_metadata=TEST_METADATA)[0], '/alternate/path')

  def test_get_num_examples(self):
    for bb_task_name in [
        'combination_composite_task', 'combination_simple_task',
        'composite_gen_task', 'composite_mul_task', 'simple_gen_task',
        'simple_mul_task'
    ]:
      self.assertEqual(
          bigbench_json_paths.get_num_examples(
              bb_task_name,
              bb_json_metadata=TEST_METADATA,
              bb_json_subtask_names=TEST_SUBTASK_NAMES), 100)

  def test_get_task_types(self):
    self.assertListEqual(
        bigbench_json_paths.get_task_types(
            'simple_gen_task', bb_json_metadata=TEST_METADATA),
        [bb.BigBenchTaskType.GENERATIVE, bb.BigBenchTaskType.SCORING])
    self.assertListEqual(
        bigbench_json_paths.get_task_types(
            'simple_mul_task', bb_json_metadata=TEST_METADATA),
        [bb.BigBenchTaskType.MULTIPLE_CHOICE])
    self.assertListEqual(
        bigbench_json_paths.get_task_types(
            'composite_gen_task', bb_json_metadata=TEST_METADATA),
        [bb.BigBenchTaskType.GENERATIVE, bb.BigBenchTaskType.SCORING])
    self.assertListEqual(
        bigbench_json_paths.get_task_types(
            'composite_mul_task', bb_json_metadata=TEST_METADATA),
        [bb.BigBenchTaskType.MULTIPLE_CHOICE])
    self.assertListEqual(
        bigbench_json_paths.get_task_types(
            'combination_simple_task', bb_json_metadata=TEST_METADATA), [
                bb.BigBenchTaskType.GENERATIVE, bb.BigBenchTaskType.SCORING,
                bb.BigBenchTaskType.MULTIPLE_CHOICE
            ])
    self.assertListEqual(
        bigbench_json_paths.get_task_types(
            'combination_composite_task', bb_json_metadata=TEST_METADATA), [
                bb.BigBenchTaskType.GENERATIVE, bb.BigBenchTaskType.SCORING,
                bb.BigBenchTaskType.MULTIPLE_CHOICE
            ])

  def test_has_subtasks(self):
    for task_name in [
        'combination_composite_task', 'composite_gen_task', 'composite_mul_task'
    ]:
      self.assertTrue(
          bigbench_json_paths.has_subtasks(
              task_name, bb_json_subtask_names=TEST_SUBTASK_NAMES))
    for task_name in [
        'combination_simple_task', 'simple_gen_task', 'simple_mul_task'
    ]:
      self.assertFalse(
          bigbench_json_paths.has_subtasks(
              task_name, bb_json_subtask_names=TEST_SUBTASK_NAMES))

  def test_get_subtask_names(self):
    self.assertListEqual(
        bigbench_json_paths.get_subtask_names(
            'composite_gen_task', bb_json_subtask_names=TEST_SUBTASK_NAMES),
        ['gen_subtask_1', 'gen_subtask_2'])
    self.assertListEqual(
        bigbench_json_paths.get_subtask_names(
            'composite_mul_task', bb_json_subtask_names=TEST_SUBTASK_NAMES),
        ['mul_subtask_1', 'mul_subtask_2'])
    self.assertListEqual(
        bigbench_json_paths.get_subtask_names(
            'combination_composite_task',
            bb_json_subtask_names=TEST_SUBTASK_NAMES),
        ['combination_subtask_1', 'combination_subtask_2'])

  def test_get_task_description(self):
    for bb_task_name in [
        'combination_composite_task', 'combination_simple_task',
        'composite_gen_task', 'composite_mul_task', 'simple_gen_task',
        'simple_mul_task'
    ]:
      self.assertEqual(
          bigbench_json_paths.get_task_description(
              bb_task_name, bb_json_metadata=TEST_METADATA), bb_task_name)


if __name__ == '__main__':
  absltest.main()
