Activity: Use context variables @
Now we will use the context variables to help us with some common scenarios.
There are many context variables, just type @
and you will see a full list.
Scenario: Let's work on a sample project
This sample project is a simple Flask application that provides a REST API for managing products. The API allows you to perform CRUD operations on products, including creating, reading, updating, and deleting products. The application uses SQLAlchemy as the ORM for database interactions.
Using this repo, we will explore how to use Copilot to help us context variables to help us out.
-1- Clone the repo
Clone the following repo:
git clone https://github.com/softchris/flask-sample.git
-2- Inspect the repo
Run the following prompt
tell me about this repo
``
You should see an output like this:
```text
Here are some details:
- Default Branch: main
- Visibility: Public
- Issues Enabled: Yes
- Wiki Enabled: Yes
- Forking Allowed: Yes
- Topics: None listed
- Stargazers: 0
- Forks: 0
- Created: 4 minutes ago
- Last Pushed: April 12, 2025 (future timestamp).
Try adding some repo issues or PRs to the repo and run the same prompt again. You should see the new issues or PRs listed in the output similar to the following:
Currently, the repository softchris/flask-sample has the following open issues:
- docs: add README, CONTRIBUTION, license - Label: documentation
- feature: add requirement.txt to make it easy to install - Label: feature
-3- Working with your workspace
There's another interesting context variable that you can use to get information about your workspace namely @workspace
Let's try it out:
@workspace Tell me about this project
You should see an output like this:
This project is a Python Flask-based API for managing an e-commerce system. It provides CRUD (Create, Read, Update, Delete) operations for two main entities: products and carts. Here's an overview of the project:
Files
- api.py:
Implements the Flask API.
Contains static data for products and carts.
Defines routes for CRUD operations on products and carts.
api_test.py:
Next, let's add tests using @workspace /tests
. Run the following prompt:
@workspace /tests
This may try to install a test runner but will also generate test and test file for you. Something similar to:
import unittest
from flask import json
from api import app
# test_api-test.py
class TestAPI(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
self.app.testing = True
# Products Tests
def test_get_products(self):
response = self.app.get('/products')
self.assertEqual(response.status_code, 200)
self.assertIsInstance(json.loads(response.data), list)
def test_get_product_success(self):
response = self.app.get('/products/1')
self.assertEqual(response.status_code, 200)
self.assertIn("name", json.loads(response.data))
def test_get_product_not_found(self):
response = self.app.get('/products/999')
self.assertEqual(response.status_code, 404)
self.assertIn("error", json.loads(response.data))
def test_create_product(self):
new_product = {"name": "Tablet", "price": 300, "stock": 15}
response = self.app.post(
'/products',
data=json.dumps(new_product),
content_type='application/json'
)
self.assertEqual(response.status_code, 201)
self.assertIn("id", json.loads(response.data))
def test_update_product_success(self):
updated_data = {"name": "Updated Laptop", "price": 1200, "stock": 8}
response = self.app.put(
'/products/1',
data=json.dumps(updated_data),
content_type='application/json'
)
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.data)["name"], "Updated Laptop")
def test_update_product_not_found(self):
updated_data = {"name": "Non-existent Product", "price": 100, "stock": 0}
response = self.app.put(
'/products/999',
data=json.dumps(updated_data),
content_type='application/json'
)
self.assertEqual(response.status_code, 404)
self.assertIn("error", json.loads(response.data))
def test_delete_product_success(self):
response = self.app.delete('/products/1')
self.assertEqual(response.status_code, 200)
self.assertIn("message", json.loads(response.data))
def test_delete_product_not_found(self):
response = self.app.delete('/products/999')
self.assertEqual(response.status_code, 200)
self.assertIn("message", json.loads(response.data))
# Carts Tests
def test_get_carts(self):
response = self.app.get('/carts')
self.assertEqual(response.status_code, 200)
self.assertIsInstance(json.loads(response.data), list)
def test_get_cart_success(self):
response = self.app.get('/carts/1')
self.assertEqual(response.status_code, 200)
self.assertIn("items", json.loads(response.data))
def test_get_cart_not_found(self):
response = self.app.get('/carts/999')
self.assertEqual(response.status_code, 404)
self.assertIn("error", json.loads(response.data))
def test_create_cart(self):
response = self.app.post('/carts')
self.assertEqual(response.status_code, 201)
self.assertIn("id", json.loads(response.data))
def test_update_cart_success(self):
updated_data = {"items": [{"product_id": 2, "quantity": 3}]}
response = self.app.put(
'/carts/1',
data=json.dumps(updated_data),
content_type='application/json'
)
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.data)["items"], updated_data["items"])
def test_update_cart_not_found(self):
updated_data = {"items": [{"product_id": 2, "quantity": 3}]}
response = self.app.put(
'/carts/999',
data=json.dumps(updated_data),
content_type='application/json'
)
self.assertEqual(response.status_code, 404)
self.assertIn("error", json.loads(response.data))
def test_delete_cart_success(self):
response = self.app.delete('/carts/1')
self.assertEqual(response.status_code, 200)
self.assertIn("message", json.loads(response.data))
def test_delete_cart_not_found(self):
response = self.app.delete('/carts/999')
self.assertEqual(response.status_code, 200)
self.assertIn("message", json.loads(response.data))
if __name__ == '__main__':
unittest.main()
If you want, try out @workspace /explain
, it will explain what your open file is doing.