Skip to content

Commit 63a3aaa

Browse files
django-mongo sample app (#31)
* sample django mongo app Signed-off-by: Aryan Bakliwal <aryanbakliwal12345@gmail.com> * add keploy tests Signed-off-by: Aryan Bakliwal <106430579+AryanBakliwal@users.noreply.github.com> --------- Signed-off-by: Aryan Bakliwal <aryanbakliwal12345@gmail.com> Signed-off-by: Aryan Bakliwal <106430579+AryanBakliwal@users.noreply.github.com>
1 parent 0eab33e commit 63a3aaa

File tree

25 files changed

+1228
-0
lines changed

25 files changed

+1228
-0
lines changed

django-mongo/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Inventory Management Application
2+
3+
## Overview
4+
5+
A simple Django + MongoDB inventory management application using mongoengine and Django REST Framework to test Keploy integration capabilities using Django and MongoDB.
6+
The endpoints available will be:
7+
8+
1. `GET /api/items/` - List all items
9+
2. `POST /api/items/` - Create a new item
10+
3. `GET /api/items/<id>/` - Retrieve an item by ID
11+
4. `PUT /api/items/<id>/` - Update an item by ID
12+
5. `DELETE /api/items/<id>/` - Delete an item by ID
13+
14+
## Requirements
15+
16+
- Python 3.x
17+
- Django
18+
- mongoengine
19+
- djangorestframework
20+
21+
## Setup Instructions
22+
23+
1. Clone the repository and navigate to project directory.
24+
```bash
25+
git clone https://github.com/keploy/samples-python.git
26+
cd samples-python/django-mongo/django_mongo
27+
```
28+
2. Install Keploy.
29+
```bash
30+
curl --silent -O -L https://keploy.io/install.sh && source install.sh
31+
```
32+
3. Start MongoDB instance.
33+
```bash
34+
docker pull mongo
35+
docker run --name mongodb -d -p 27017:27017 -v mongo_data:/data/db -e MONGO_INITDB_ROOT_USERNAME=<username> -e MONGO_INITDB_ROOT_PASSWORD=<password> mongo
36+
```
37+
4. Set up Django appllication.
38+
```bash
39+
python3 -m virtualenv venv
40+
source venv/bin/activate
41+
pip3 install -r requirements.txt
42+
```
43+
5. Capture the testcases.
44+
```bash
45+
keploy record -c "python3 manage.py runserver"
46+
```
47+
6. Generate testcases by making API calls.
48+
```bash
49+
# List items
50+
# GET /api/items/
51+
curl -X GET http://localhost:8000/api/items/
52+
```
53+
```bash
54+
# Create Item
55+
# POST /api/items/
56+
curl -X POST http://localhost:8000/api/items/ \
57+
-H "Content-Type: application/json" \
58+
-d '{
59+
"name": "Gadget C",
60+
"quantity": 200,
61+
"description": "A versatile gadget with numerous features."
62+
}'
63+
```
64+
```bash
65+
# Retrieve Item
66+
# GET /api/items/<id>/
67+
curl -X GET http://localhost:8000/api/items/<id>/
68+
```
69+
```bash
70+
# Update Item
71+
# PUT /api/items/<id>/
72+
curl -X PUT http://localhost:8000/api/items/6142d21e122bda15f6f87b1d/ \
73+
-H "Content-Type: application/json" \
74+
-d '{
75+
"name": "Updated Widget A",
76+
"quantity": 120,
77+
"description": "An updated description for Widget A."
78+
}'
79+
```
80+
```bash
81+
# Delete Item
82+
# DELETE /api/items/<id>/
83+
curl -X DELETE http://localhost:8000/api/items/<id>/
84+
```
85+
Replace `<id>` with the actual ID of the item you want to retrieve, update, or delete.
86+
87+
## Run the testcases
88+
89+
Shut down MongoDB, Keploy doesn't need it during tests.
90+
```bash
91+
keploy test -c "python3 manage.py runserver" --delay 10
92+
```

django-mongo/django_mongo/django_mongo/__init__.py

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for django_mongo project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_mongo.settings')
15+
16+
application = get_asgi_application()
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
"""
2+
Django settings for django_mongo project.
3+
4+
Generated by 'django-admin startproject' using Django 5.1.2.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/5.1/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/5.1/ref/settings/
11+
"""
12+
13+
from pathlib import Path
14+
import mongoengine
15+
16+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
17+
BASE_DIR = Path(__file__).resolve().parent.parent
18+
19+
20+
# Quick-start development settings - unsuitable for production
21+
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
22+
23+
# SECURITY WARNING: keep the secret key used in production secret!
24+
SECRET_KEY = 'django-insecure-3g@-x5(3t(i(p^qnu9lz3hk1u1yobrvwz($5^ucvnn0a6b$%ob'
25+
26+
# SECURITY WARNING: don't run with debug turned on in production!
27+
DEBUG = True
28+
29+
ALLOWED_HOSTS = []
30+
31+
32+
# Application definition
33+
34+
INSTALLED_APPS = [
35+
'django.contrib.admin',
36+
'django.contrib.auth',
37+
'django.contrib.contenttypes',
38+
'django.contrib.sessions',
39+
'django.contrib.messages',
40+
'django.contrib.staticfiles',
41+
'rest_framework',
42+
'inventory',
43+
]
44+
45+
REST_FRAMEWORK = {
46+
'DEFAULT_PERMISSION_CLASSES': [
47+
'rest_framework.permissions.AllowAny',
48+
],
49+
}
50+
51+
MIDDLEWARE = [
52+
'django.middleware.security.SecurityMiddleware',
53+
'django.contrib.sessions.middleware.SessionMiddleware',
54+
'django.middleware.common.CommonMiddleware',
55+
'django.middleware.csrf.CsrfViewMiddleware',
56+
'django.contrib.auth.middleware.AuthenticationMiddleware',
57+
'django.contrib.messages.middleware.MessageMiddleware',
58+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
59+
]
60+
61+
ROOT_URLCONF = 'django_mongo.urls'
62+
63+
TEMPLATES = [
64+
{
65+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
66+
'DIRS': [],
67+
'APP_DIRS': True,
68+
'OPTIONS': {
69+
'context_processors': [
70+
'django.template.context_processors.debug',
71+
'django.template.context_processors.request',
72+
'django.contrib.auth.context_processors.auth',
73+
'django.contrib.messages.context_processors.messages',
74+
],
75+
},
76+
},
77+
]
78+
79+
WSGI_APPLICATION = 'django_mongo.wsgi.application'
80+
81+
82+
# Database
83+
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
84+
85+
# DATABASES = {
86+
# 'default': {
87+
# 'ENGINE': 'django.db.backends.sqlite3',
88+
# 'NAME': BASE_DIR / 'db.sqlite3',
89+
# }
90+
# }
91+
92+
93+
# Password validation
94+
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
95+
96+
AUTH_PASSWORD_VALIDATORS = [
97+
{
98+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
99+
},
100+
{
101+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
102+
},
103+
{
104+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
105+
},
106+
{
107+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
108+
},
109+
]
110+
111+
112+
# Internationalization
113+
# https://docs.djangoproject.com/en/5.1/topics/i18n/
114+
115+
LANGUAGE_CODE = 'en-us'
116+
117+
TIME_ZONE = 'UTC'
118+
119+
USE_I18N = True
120+
121+
USE_TZ = True
122+
123+
124+
# Static files (CSS, JavaScript, Images)
125+
# https://docs.djangoproject.com/en/5.1/howto/static-files/
126+
127+
STATIC_URL = 'static/'
128+
129+
# Default primary key field type
130+
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
131+
132+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
133+
134+
# MongoDB configuration
135+
MONGO_DB_NAME = 'inventory_db'
136+
MONGO_USER = 'admin'
137+
MONGO_PASSWORD = 'admin'
138+
MONGO_HOST = 'localhost'
139+
MONGO_PORT = 27017
140+
141+
# Connect to MongoDB
142+
mongoengine.connect(
143+
db=MONGO_DB_NAME,
144+
username=MONGO_USER,
145+
password=MONGO_PASSWORD,
146+
host=MONGO_HOST,
147+
port=MONGO_PORT
148+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
URL configuration for django_mongo project.
3+
4+
The `urlpatterns` list routes URLs to views. For more information please see:
5+
https://docs.djangoproject.com/en/5.1/topics/http/urls/
6+
Examples:
7+
Function views
8+
1. Add an import: from my_app import views
9+
2. Add a URL to urlpatterns: path('', views.home, name='home')
10+
Class-based views
11+
1. Add an import: from other_app.views import Home
12+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
13+
Including another URLconf
14+
1. Import the include() function: from django.urls import include, path
15+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
16+
"""
17+
from django.contrib import admin
18+
from django.urls import path, include
19+
20+
urlpatterns = [
21+
path('admin/', admin.site.urls),
22+
path('api/', include('inventory.urls')),
23+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for django_mongo project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_mongo.settings')
15+
16+
application = get_wsgi_application()

django-mongo/django_mongo/inventory/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class InventoryConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'inventory'

django-mongo/django_mongo/inventory/migrations/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)