@@ -0,0 +1,2862 @@
+import os%0Aimport random%0Aimport string%0Aimport tarfile%0Afrom collections import defaultdict%0Afrom unittest.mock import patch%0A%0Afrom parameterized import parameterized%0Afrom torchtext.datasets.amazonreviewfull import AmazonReviewFull%0A%0Afrom ..common.case_utils import TempDirMixin, zip_equal%0Afrom ..common.torchtext_test_case import TorchtextTestCase%0A%0A%0Adef _get_mock_dataset(root_dir):%0A    %22%22%22%0A    root_dir: directory to the mocked dataset%0A    %22%22%22%0A    base_dir = os.path.join(root_dir, %22AmazonReviewFull%22)%0A    temp_dataset_dir = os.path.join(base_dir, %22temp_dataset_dir%22)%0A    os.makedirs(temp_dataset_dir, exist_ok=True)%0A%0A    seed = 1%0A    mocked_data = defaultdict(list)%0A    for file_name in (%22train.csv%22, %22test.csv%22):%0A        txt_file = os.path.join(temp_dataset_dir, file_name)%0A        with open(txt_file, %22w%22) as f:%0A            for i in range(5):%0A                label = seed %25 5 + 1%0A                rand_string = %22 %22.join(%0A                    random.choice(string.ascii_letters) for i in range(seed)%0A                )%0A                dataset_line = (label, f%22%7Brand_string%7D %7Brand_string%7D%22)%0A                # append line to correct dataset split%0A                mocked_data%5Bos.path.splitext(file_name)%5B0%5D%5D.append(dataset_line)%0A                f.write(f'%22%7Blabel%7D%22,%22%7Brand_string%7D%22,%22%7Brand_string%7D%22%5Cn')%0A                seed += 1%0A%0A    compressed_dataset_path = os.path.join(%0A        base_dir, %22amazon_review_full_csv.tar.gz%22%0A    )%0A    # create tar file from dataset folder%0A    with tarfile.open(compressed_dataset_path, %22w:gz%22) as tar:%0A        tar.add(temp_dataset_dir, arcname=%22amazon_review_full_csv%22)%0A%0A    return mocked_data%0A%0A%0Aclass TestAmazonReviewFull(TempDirMixin, TorchtextTestCase):%0A    root_dir = None%0A    samples = %5B%5D%0A%0A    @classmethod%0A    def setUpClass(cls):%0A        super().setUpClass()%0A        cls.root_dir = cls.get_base_temp_dir()%0A        cls.samples = _get_mock_dataset(cls.root_dir)%0A        cls.patcher = patch(%0A            %22torchdata.datapipes.iter.util.cacheholder._hash_check%22, return_value=True%0A        )%0A        cls.patcher.start()%0A%0A    @classmethod%0A    def tearDownClass(cls):%0A        cls.patcher.stop()%0A        super().tearDownClass()%0A%0A    @parameterized.expand(%5B%22train%22, %22test%22%5D)%0A    def test_amazon_review_full(self, split):%0A        dataset = AmazonReviewFull(root=self.root_dir, split=split)%0A%0A        samples = list(dataset)%0A        expected_samples = self.samples%5Bsplit%5D%0A        for sample, expected_sample in zip_equal(samples, expected_samples):%0A            self.assertEqual(sample, expected_sample)%0A%0A    @parameterized.expand(%5B%22train%22, %22test%22%5D)%0A    def test_amazon_review_full_split_argument(self, split):%0A        dataset1 = AmazonReviewFull(root=self.root_dir, split=split)%0A        (dataset2,) = AmazonReviewFull(root=self.root_dir, split=(split,))%0A%0A        for d1, d2 in zip_equal(dataset1, dataset2):%0A            self.assertEqual(d1, d2)%0A

Add AmazonReviewFull Mocked Unit Test (#1561)