tests(regex): Improve testing functionality for regex. - Add sample unit tests - Add case insensitivity

This commit is contained in:
santiagosayshey
2024-08-15 16:29:14 +09:30
committed by Sam Chau
parent 3bd7017917
commit 3391ff76b6
4 changed files with 119 additions and 57 deletions

View File

@@ -14,15 +14,37 @@ def regex101_proxy():
logging.debug(f"Received data from frontend: {request.json}")
# Validate the request data before sending
required_fields = ['regex', 'flags', 'delimiter', 'flavor']
required_fields = ['regex', 'delimiter', 'flavor']
for field in required_fields:
if field not in request.json:
logging.error(f"Missing required field: {field}")
return jsonify({"error": f"Missing required field: {field}"}), 400
# Ensure testString is present
if 'testString' not in request.json or not request.json['testString']:
request.json['testString'] = "Sample test string" # Add a default test string
# Set default flags to 'gmi' for global, multiline, and case-insensitive matching
request.json['flags'] = 'gmi'
# Include a separate test string if not provided
if 'testString' not in request.json:
request.json['testString'] = "Sample test string"
# Always include unit tests with every request
request.json['unitTests'] = [
{
"description": "Sample DOES_MATCH test",
"testString": request.json['testString'], # Use the main test string
"criteria": "DOES_MATCH",
"target": "REGEX"
},
{
"description": "Sample DOES_NOT_MATCH test",
"testString": "Non-matching string", # This should not match the regex
"criteria": "DOES_NOT_MATCH",
"target": "REGEX"
}
]
# Log the complete payload before sending
logging.debug(f"Final payload being sent to Regex101 API: {json.dumps(request.json, indent=2)}")
# Construct the data payload for curl
data = json.dumps(request.json)
@@ -45,6 +67,8 @@ def regex101_proxy():
logging.error(f"An unexpected error occurred: {str(e)}")
return jsonify({"error": "An unexpected error occurred"}), 500
@bp.route('', methods=['GET', 'POST'])
def handle_items():
if request.method == 'POST':

View File

@@ -5,6 +5,6 @@ tags:
- Release Group
- HDB Internal
pattern: (?<=^|[\s.-])D-Z0N3\b
regex101Link: ''
regex101Link: https://regex101.com/r/OWv3R9
date_created: '2024-08-15T15:37:02.733456'
date_modified: '2024-08-15T15:38:23.541760'
date_modified: '2024-08-15T16:24:11.809191'

View File

@@ -27,6 +27,7 @@ function App() {
{activeTab === 'regex' ? <RegexManager /> : <CustomFormatManager />}
</div>
</div>
);
}

View File

@@ -11,6 +11,7 @@ function RegexModal({ regex = null, isOpen, onClose, onSave }) {
const [newTag, setNewTag] = useState('');
const [regex101Link, setRegex101Link] = useState('');
const [error, setError] = useState('');
const [isLoading, setIsLoading] = useState(false);
const initialRegexRef = useRef(regex);
useEffect(() => {
@@ -31,41 +32,72 @@ function RegexModal({ regex = null, isOpen, onClose, onSave }) {
}
setError('');
setNewTag('');
setIsLoading(false);
}
}, [regex, isOpen]);
const handleCreateRegex101Link = async () => {
if (!pattern.trim()) {
setError('Please provide a regex pattern before creating tests.');
return;
}
// Define your unit tests here
const unitTests = [
{
description: "Test if 'D-Z0N3' is detected correctly",
testString: "Test D-Z0N3 pattern",
criteria: "DOES_MATCH",
target: "REGEX"
},
{
description: "Test if 'random text' does not match",
testString: "random text",
criteria: "DOES_NOT_MATCH",
target: "REGEX"
}
// Add more unit tests as needed
];
setIsLoading(true);
try {
const response = await createRegex101Link({
regex: pattern,
flavor: 'pcre',
flags: 'gm',
delimiter: '/',
});
const permalinkFragment = response.permalinkFragment;
const response = await createRegex101Link({
regex: pattern,
flavor: 'pcre',
flags: 'gmi',
delimiter: '/',
unitTests: unitTests
});
const permalinkFragment = response.permalinkFragment;
const regex101Link = `https://regex101.com/r/${permalinkFragment}`;
setRegex101Link(regex101Link);
const regex101Link = `https://regex101.com/r/${permalinkFragment}`;
setRegex101Link(regex101Link);
await saveRegex({
id: regex ? regex.id : 0,
name,
pattern,
description,
tags,
regex101Link,
});
await saveRegex({
id: regex ? regex.id : 0,
name,
pattern,
description,
tags,
regex101Link,
});
window.open(regex101Link, '_blank');
onSave(); // Refresh the list after saving
setError('');
window.open(regex101Link, '_blank');
onSave(); // Refresh the list after saving
setError('');
} catch (error) {
console.error('Error creating regex101 link:', error);
setError('Failed to create regex101 link. Please try again.');
console.error('Error creating regex101 link:', error);
setError('Failed to create regex101 link. Please try again.');
} finally {
setIsLoading(false);
}
};
const handleRemoveRegex101Link = async () => {
const confirmRemoval = window.confirm("Are you sure you want to remove this Regex101 link?");
if (!confirmRemoval) return;
setIsLoading(true);
setRegex101Link(''); // Clear the regex101Link in state
try {
@@ -83,12 +115,14 @@ function RegexModal({ regex = null, isOpen, onClose, onSave }) {
} catch (error) {
console.error('Error removing regex101 link:', error);
setError('Failed to remove regex101 link. Please try again.');
} finally {
setIsLoading(false);
}
};
const handleSave = async () => {
if (!name.trim() || !pattern.trim() || !description.trim()) {
setError('Name, pattern, and description are all required.');
if (!name.trim() || !pattern.trim()) {
setError('Name and pattern are required.');
return;
}
try {
@@ -109,15 +143,16 @@ function RegexModal({ regex = null, isOpen, onClose, onSave }) {
};
const handleDelete = async () => {
if (regex && regex.id) {
try {
await deleteRegex(regex.id);
onSave();
onClose();
} catch (error) {
console.error('Error deleting regex:', error);
setError('Failed to delete regex. Please try again.');
}
const confirmDeletion = window.confirm("Are you sure you want to delete this regex?");
if (!confirmDeletion) return;
try {
await deleteRegex(regex.id);
onSave();
onClose();
} catch (error) {
console.error('Error deleting regex:', error);
setError('Failed to delete regex. Please try again.');
}
};
@@ -165,7 +200,7 @@ function RegexModal({ regex = null, isOpen, onClose, onSave }) {
</div>
<div className="mb-4">
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Description
Description (Optional)
</label>
<input
type="text"
@@ -206,29 +241,31 @@ function RegexModal({ regex = null, isOpen, onClose, onSave }) {
<div className="mb-4">
{regex101Link ? (
<>
<p className="mt-2">
<a
href={regex101Link}
target="_blank"
rel="noopener noreferrer"
className="text-blue-500 hover:underline"
>
Open in Regex101
</a>
</p>
<button
onClick={handleRemoveRegex101Link}
className="bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600 transition-colors mt-2"
>
Remove Link
</button>
<div className="flex justify-between">
<div>
<button
onClick={() => window.open(regex101Link, '_blank')}
className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600 transition-colors"
>
Open in Regex101
</button>
<button
onClick={handleRemoveRegex101Link}
className="bg-red-500 text-white px-4 py-2 rounded ml-2 hover:bg-red-600 transition-colors"
disabled={isLoading}
>
{isLoading ? 'Removing...' : 'Remove Link'}
</button>
</div>
</div>
</>
) : (
<button
onClick={handleCreateRegex101Link}
className="bg-green-500 text-white px-4 py-2 rounded hover:bg-green-600 transition-colors"
disabled={isLoading}
>
Create Tests
{isLoading ? 'Creating Tests...' : 'Create Tests'}
</button>
)}
</div>