Skip to content

Instantly share code, notes, and snippets.

@devotdev
devotdev / verify-api-response-time.spec.ts
Created March 21, 2025 11:08
Playwright API test snippet that verifies the API response time for retrieving user data. It ensures the API returns a successful (200) status and asserts the response time is within an acceptable threshold (less than 500 ms).
test('Verify API response time', async ({ request }) => {
const response = await request.get('/users/1');
expect(response.status()).toBe(200);
expect(response.timing().responseEnd - response.timing().requestStart).toBeLessThan(500);
});
@devotdev
devotdev / validate-user-data-api-test.spec.ts
Created March 21, 2025 11:07
Playwright API test illustrating how to validate specific properties in a user's data returned by a GET request. It asserts successful retrieval (HTTP 200) and verifies that the response contains correct and expected values.
test('Validate user data', async ({ request }) => {
const response = await request.get('/users/1');
expect(response.status()).toBe(200);
const responseBody = await response.json();
expect(responseBody).toHaveProperty('id');
expect(responseBody).toHaveProperty('name');
expect(responseBody.name).toBe('John Updated');
});
@devotdev
devotdev / delete-user-api-test.spec.ts
Created March 21, 2025 11:06
Playwright API test demonstrating how to delete an existing user using a DELETE request. It validates successful deletion by checking for the appropriate HTTP status code (204 No Content).
test('Delete a user', async ({ request }) => {
const response = await request.delete('/users/1');
expect(response.status()).toBe(204);
});
@devotdev
devotdev / partial-update-user-api-test.spec.ts
Created March 21, 2025 11:05
Playwright API test showcasing how to partially update user details using the PATCH request method. The test verifies that the update is successful by checking for a status code of 200 and logs the updated user information.
test('Partially update user details', async ({ request }) => {
const response = await request.patch('/users/1', {
data: {
job: 'Lead Engineer'
}
});
expect(response.status()).toBe(200);
const responseBody = await response.json();
console.log(responseBody);
@devotdev
devotdev / update-user-api-test.spec.ts
Created March 21, 2025 11:02
Playwright API test demonstrating how to update existing user details via a PUT request. It checks for a successful HTTP 200 response and logs the updated user's data for verification
test('Update user details', async ({ request }) => {
const response = await request.put('/users/1', {
data: {
name: 'John Updated',
job: 'Senior Engineer'
}
});
expect(response.status()).toBe(200);
const responseBody = await response.json();
@devotdev
devotdev / fetch-user-details-api-test.spec.ts
Created March 21, 2025 10:57
A simple Playwright API test example showing how to fetch details of a specific user using a GET request. The test checks for a successful response (HTTP 200) and logs the response body for inspection.
test('Fetch user details', async ({ request }) => {
const response = await request.get('/users/1');
expect(response.status()).toBe(200);
const responseBody = await response.json();
console.log(responseBody);
});
@devotdev
devotdev / create-user-api-test.spec.ts
Created March 21, 2025 10:49
Basic Playwright API test example demonstrating how to send a POST request to create a new user and verify that the user was created successfully (HTTP status 201).
import { test, expect } from '@playwright/test';
test('Create a new user', async ({ request }) => {
const response = await request.post('/users', {
data: {
name: 'John Doe',
job: 'Software Engineer'
}
});
import logging
logging.basicConfig(level=logging.INFO)
def safe_run(agent, query):
try:
return agent.run(query)
except Exception as e:
logging.error(f"Error while processing query '{query}': {e}")
return "An error occurred. Please try again."
from langchain.memory import ConversationBufferMemory
# Create a memory instance to store the conversation context.
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# Initialize an agent with memory integration.
agent_with_memory = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
from langchain.tools import BaseTool
import math
class CalculatorTool(BaseTool):
"""A tool for performing basic arithmetic operations."""
name = "calculator"
description = "Performs arithmetic calculations given a mathematical expression."
def _run(self, expression: str):