This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 814
Add DBpedia Mocked Unit Test #1566
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
584069e
add test_yelpreviewpolarity to mock YelpReviewP..
vcm2114 ffea227
add test_dbpedia to mock DBpedia
vcm2114 2996668
remove yelpreview test
vcm2114 be15665
Delete test_yelpreviewpolarity.py
vcm2114 e31856f
Merge branch 'mock_dbpedia' of github.com:VirgileHlav/text into mock_…
vcm2114 146b629
PR comments follow up + add second string
vcm2114 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import os | ||
| import random | ||
| import string | ||
| import tarfile | ||
| from collections import defaultdict | ||
| from unittest.mock import patch | ||
|
|
||
| from parameterized import parameterized | ||
| from torchtext.datasets.dbpedia import DBpedia | ||
|
|
||
| from ..common.case_utils import TempDirMixin, zip_equal | ||
| from ..common.torchtext_test_case import TorchtextTestCase | ||
|
|
||
|
|
||
| def _get_mock_dataset(root_dir): | ||
| """ | ||
| root_dir: directory to the mocked dataset | ||
| """ | ||
| base_dir = os.path.join(root_dir, "DBpedia") | ||
| temp_dataset_dir = os.path.join(base_dir, "temp_dataset_dir") | ||
| os.makedirs(temp_dataset_dir, exist_ok=True) | ||
|
|
||
| seed = 1 | ||
| mocked_data = defaultdict(list) | ||
| for file_name in ("train.csv", "test.csv"): | ||
| csv_file = os.path.join(temp_dataset_dir, file_name) | ||
| mocked_lines = mocked_data[os.path.splitext(file_name)[0]] | ||
| with open(csv_file, "w") as f: | ||
| for i in range(5): | ||
| label = seed % 14 + 1 | ||
| rand_string = " ".join( | ||
| random.choice(string.ascii_letters) for i in range(seed) | ||
| ) | ||
| dataset_line = (label, rand_string + " " + rand_string) | ||
| f.write(f'{label},"{rand_string}","{rand_string}"\n') | ||
|
|
||
| # append line to correct dataset split | ||
| mocked_lines.append(dataset_line) | ||
| seed += 1 | ||
|
|
||
| compressed_dataset_path = os.path.join(base_dir, "dbpedia_csv.tar.gz") | ||
| # create gz file from dataset folder | ||
| with tarfile.open(compressed_dataset_path, "w:gz") as tar: | ||
| tar.add(temp_dataset_dir, arcname="dbpedia_csv") | ||
|
|
||
| return mocked_data | ||
|
|
||
|
|
||
| class TestDBpedia(TempDirMixin, TorchtextTestCase): | ||
| root_dir = None | ||
| samples = [] | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
| cls.root_dir = cls.get_base_temp_dir() | ||
| cls.samples = _get_mock_dataset(cls.root_dir) | ||
| cls.patcher = patch( | ||
| "torchdata.datapipes.iter.util.cacheholder._hash_check", return_value=True | ||
| ) | ||
| cls.patcher.start() | ||
|
|
||
| @classmethod | ||
| def tearDownClass(cls): | ||
| cls.patcher.stop() | ||
| super().tearDownClass() | ||
|
|
||
| @parameterized.expand(["train", "test"]) | ||
| def test_dbpedia(self, split): | ||
| dataset = DBpedia(root=self.root_dir, split=split) | ||
|
|
||
| samples = list(dataset) | ||
| expected_samples = self.samples[split] | ||
| for sample, expected_sample in zip_equal(samples, expected_samples): | ||
| self.assertEqual(sample, expected_sample) | ||
|
|
||
| @parameterized.expand(["train", "test"]) | ||
| def test_dbpedia_split_argument(self, split): | ||
| dataset1 = DBpedia(root=self.root_dir, split=split) | ||
| (dataset2,) = DBpedia(root=self.root_dir, split=(split,)) | ||
|
|
||
| for d1, d2 in zip_equal(dataset1, dataset2): | ||
| self.assertEqual(d1, d2) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.