Skip to content

Commit 447d7ca

Browse files
authored
Merge pull request #771 from ceydaduzgec/update-trimming-test
Update trimming test
2 parents 07218fd + 8af2219 commit 447d7ca

File tree

4 files changed

+189
-37
lines changed

4 files changed

+189
-37
lines changed

docs/local_linting_guide.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Local Linting Guide
2+
3+
This guide explains how to run the same linting checks locally that are used in the GitHub Actions CI pipeline.
4+
5+
## Prerequisites
6+
7+
Make sure you have Python installed and create a virtual environment:
8+
9+
```bash
10+
# Create a virtual environment (if not already created)
11+
python -m venv venv
12+
13+
# Activate the environment
14+
source venv/bin/activate # On Unix/macOS
15+
# or
16+
venv\Scripts\activate # On Windows
17+
```
18+
19+
## Install Required Tools
20+
21+
Install all the linting tools used in the CI pipeline:
22+
23+
```bash
24+
# Install Python linting tools
25+
pip install black isort
26+
27+
# Install Node.js tools (requires Node.js to be installed)
28+
npm install -g jsonlint
29+
```
30+
31+
## Linting Commands
32+
33+
### 1. Python Code Formatting (Black)
34+
35+
Check if Python files need formatting:
36+
```bash
37+
black --check .
38+
```
39+
40+
Auto-format Python files:
41+
```bash
42+
black .
43+
```
44+
45+
Check specific files:
46+
```bash
47+
black --check tests/test_trimming.py filter.py
48+
```
49+
50+
### 2. Python Import Sorting (isort)
51+
52+
Check import sorting:
53+
```bash
54+
isort --check-only .
55+
```
56+
57+
Auto-fix import sorting:
58+
```bash
59+
isort .
60+
```
61+
62+
### 3. JSON Validation
63+
64+
Validate JSON files:
65+
```bash
66+
jsonlint world_universities_and_domains.json
67+
```
68+
69+
## GitHub Actions Compatibility
70+
71+
The local commands above mirror exactly what GitHub Actions runs:
72+
- `black --check .` (same as GitHub Actions)
73+
- `isort --check-only .` (same as GitHub Actions)
74+
- `jsonlint world_universities_and_domains.json` (same as GitHub Actions)

docs/local_testing_guide.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Local Testing Guide
2+
3+
To run the project tests:
4+
5+
1. **Set up a virtual environment** (recommended)
6+
7+
```bash
8+
# Create a virtual environment (if not already created)
9+
python -m venv venv
10+
11+
# Activate the environment
12+
source venv/bin/activate # On Unix/macOS
13+
# or
14+
venv\Scripts\activate # On Windows
15+
```
16+
17+
2. **Install test dependencies**
18+
19+
```bash
20+
pip install -r tests/requirements.txt
21+
```
22+
23+
3. **Run tests using unittest**
24+
25+
```bash
26+
# From the tests directory
27+
python -m unittest discover
28+
29+
# Or from the project root
30+
python -m unittest discover tests
31+
```
32+
33+
## Test Coverage
34+
35+
- **Structure validation**: Validates JSON schema and field types
36+
- **Domain validation**: Checks domain format using validators library
37+
- **URL validation**: Validates web page URLs
38+
- **Field trimming**: Ensures no leading/trailing whitespace
39+
- **Country code validation**: Verifies 2-character alpha codes
40+
41+
## Adding Custom Tests
42+
43+
Create new test files in `tests/` directory following the existing pattern:
44+
45+
```python
46+
import json
47+
import unittest
48+
import os
49+
50+
class CustomTests(unittest.TestCase):
51+
def setUp(self):
52+
script_dir = os.path.dirname(os.path.abspath(__file__))
53+
json_path = os.path.join(os.path.dirname(script_dir), "world_universities_and_domains.json")
54+
with open(json_path, encoding="utf-8") as json_file:
55+
self.data = json.load(json_file)
56+
57+
def test_custom_validation(self):
58+
# Your test logic here
59+
pass
60+
```

tests/test_trimming.py

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,60 +20,78 @@ def setUp(self):
2020

2121
def test_trimmed_fields(self):
2222
"""Test that all string fields in each university entry are properly trimmed"""
23-
for university in self.valid_json:
23+
errors = []
24+
25+
for i, university in enumerate(self.valid_json):
2426
# Check university name
2527
if isinstance(university["name"], str):
26-
self.assertEqual(
27-
university["name"],
28-
university["name"].strip(),
29-
msg=f"University Name contains leading/trailing whitespace: '{university['name']}'",
30-
)
28+
original_name = university["name"]
29+
trimmed_name = original_name.strip()
30+
if original_name != trimmed_name:
31+
errors.append(
32+
f"Entry {i}: University Name contains leading/trailing whitespace: '{original_name}' -> should be '{trimmed_name}'"
33+
)
3134

3235
# Check domains
3336
if university["domains"] is not None:
34-
for domain in university["domains"]:
37+
for j, domain in enumerate(university["domains"]):
3538
if isinstance(domain, str):
36-
self.assertEqual(
37-
domain,
38-
domain.strip(),
39-
msg=f"Domain contains leading/trailing whitespace: '{domain}'",
40-
)
39+
original_domain = domain
40+
trimmed_domain = original_domain.strip()
41+
if original_domain != trimmed_domain:
42+
errors.append(
43+
f"Entry {i}: Domain {j} contains leading/trailing whitespace: '{original_domain}' -> should be '{trimmed_domain}'"
44+
)
4145

4246
# Check web_pages
4347
if university["web_pages"] is not None:
44-
for web_page in university["web_pages"]:
48+
for j, web_page in enumerate(university["web_pages"]):
4549
if isinstance(web_page, str):
46-
self.assertEqual(
47-
web_page,
48-
web_page.strip(),
49-
msg=f"Web page contains leading/trailing whitespace: '{web_page}'",
50-
)
50+
original_web_page = web_page
51+
trimmed_web_page = original_web_page.strip()
52+
if original_web_page != trimmed_web_page:
53+
errors.append(
54+
f"Entry {i}: Web page {j} contains leading/trailing whitespace: '{original_web_page}' -> should be '{trimmed_web_page}'"
55+
)
5156

5257
# Check country
5358
if isinstance(university["country"], str):
54-
self.assertEqual(
55-
university["country"],
56-
university["country"].strip(),
57-
msg=f"Country contains leading/trailing whitespace: '{university['country']}'",
58-
)
59+
original_country = university["country"]
60+
trimmed_country = original_country.strip()
61+
if original_country != trimmed_country:
62+
errors.append(
63+
f"Entry {i}: Country contains leading/trailing whitespace: '{original_country}' -> should be '{trimmed_country}'"
64+
)
5965

6066
# Check alpha_two_code
6167
if isinstance(university["alpha_two_code"], str):
62-
self.assertEqual(
63-
university["alpha_two_code"],
64-
university["alpha_two_code"].strip(),
65-
msg=f"Alpha two code contains leading/trailing whitespace: '{university['alpha_two_code']}'",
66-
)
68+
original_alpha = university["alpha_two_code"]
69+
trimmed_alpha = original_alpha.strip()
70+
if original_alpha != trimmed_alpha:
71+
errors.append(
72+
f"Entry {i}: Alpha two code contains leading/trailing whitespace: '{original_alpha}' -> should be '{trimmed_alpha}'"
73+
)
6774

6875
# Check state-province
6976
if isinstance(university["state-province"], str):
70-
self.assertEqual(
71-
university["state-province"],
72-
university["state-province"].strip(),
73-
msg=f"State/Province contains leading/trailing whitespace: '{university['state-province']}'",
74-
)
77+
original_state = university["state-province"]
78+
trimmed_state = original_state.strip()
79+
if original_state != trimmed_state:
80+
errors.append(
81+
f"Entry {i}: State/Province contains leading/trailing whitespace: '{original_state}' -> should be '{trimmed_state}'"
82+
)
83+
84+
# If there are any errors, fail the test with all error messages
85+
if errors:
86+
error_message = (
87+
f"Found {len(errors)} fields with whitespace issues:\n"
88+
+ "\n".join(errors[:10])
89+
) # Show first 10 errors
90+
if len(errors) > 10:
91+
error_message += f"\n... and {len(errors) - 10} more errors"
92+
self.fail(error_message)
7593

7694

7795
# Run tests locally
78-
# if __name__ == "__main__":
79-
# unittest.main()
96+
if __name__ == "__main__":
97+
unittest.main()

world_universities_and_domains.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37568,7 +37568,7 @@
3756837568
"country": "Ireland"
3756937569
},
3757037570
{
37571-
"web_pages": ["https://www.tus.ie/"],
37571+
"web_pages": ["https://tus.ie/"],
3757237572
"name": "Technological University of the Shannon",
3757337573
"alpha_two_code": "IE",
3757437574
"state-province": "Limerick",
@@ -70215,7 +70215,7 @@
7021570215
"web_pages": ["https://www.minerva.edu/"],
7021670216
"name": "Minerva University",
7021770217
"alpha_two_code": "US",
70218-
"state-province": "CA",
70218+
"state-province": "California",
7021970219
"domains": ["minerva.edu"],
7022070220
"country": "United States"
7022170221
},

0 commit comments

Comments
 (0)