diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..6dcac11 --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +MONGO_URI="mongodb+srv://:@/?retryWrites=true&w=majority" \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..322d6ef --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,37 @@ +name: Backend CI/CD Workflow + +on: + push: + branches: + - main + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + + - name: Install Dependencies + run: pip install -r requirements.txt + + - name: Run Tests (with MockDB) + env: + TESTING: "True" + run: pytest tests/ + + build: + runs-on: ubuntu-latest + needs: test + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Build Docker Image + run: docker build -t suraksha-backend:latest . diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cd29d0a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.10-slim +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +# Optional: if you need port 5000 internally +EXPOSE 5000 +CMD ["python", "app.py"] diff --git a/db/mongo.py b/db/mongo.py index 0bca777..316ec75 100644 --- a/db/mongo.py +++ b/db/mongo.py @@ -8,11 +8,16 @@ try: MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017/") - client = MongoClient(MONGO_URI) + if os.getenv("TESTING") == "True": + import mongomock + client = mongomock.MongoClient() + print("Connected to MongoMock for testing.") + else: + client = MongoClient(MONGO_URI) # Test connection - client.admin.command('ping') - print("Connected successfully to MongoDB") + client.admin.command('ping') + print("Connected successfully to MongoDB") db = client["earnsure"] diff --git a/requirements.txt b/requirements.txt index 482da88..d09d406 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ requests==2.31.0 numpy==1.26.2 pytest==7.4.3 python-dotenv==1.0.0 +mongomock==4.1.2 \ No newline at end of file