Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/se24ncsu/PopcornPicks into …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
Shrimadh committed Nov 1, 2024
2 parents 98c450d + 2338f2c commit 221640e
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 74 deletions.
18 changes: 15 additions & 3 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
# .coveragerc

[run]
include = src/recommenderapp/*,src/prediction_scripts/*
source = .
omit =
*/test/*
*/tests/*
*/venv/*
setup.py

[report]
exclude_lines =
pragma: no cover
def __repr__
raise NotImplementedError
if __name__ == .__main__.:
pass
raise ImportError
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
build
.git
.DS_Store
.env
*.pyc
_pycache_/
39 changes: 11 additions & 28 deletions .github/workflows/codecoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,8 @@ on: [push]
jobs:
upload-coverage:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
# The MySQL docker container requires these environment variables to be set
# so we can create and migrate the test database.
# See: https://hub.docker.com/_/mysql
MYSQL_DATABASE: testdb
MYSQL_ROOT_PASSWORD: testrootpass
DB_HOST: 127.0.0.1
ports:
# Opens port 3306 on service container and host
# https://docs.github.com/en/actions/using-containerized-services/about-service-containers
- 3306:3306
# Before continuing, verify the mysql container is reachable from the ubuntu host
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
env:
MONGO_URI: ${{ secrets.MONGO_URI }}

steps:
- name: Checkout code
Expand All @@ -29,29 +15,26 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.11 # Replace '3.x' with your desired Python version
python-version: 3.9 # Replace '3.x' with your desired Python version

- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Create env file
run: |
cat << EOF > .env
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
DB_PASSWORD=${{ secrets.DB_PASSWORD }}
SALT=${{ secrets.SALT }}
EOF
- name: Set up DB
- name: Create env file
run: |
mysql -h 127.0.0.1 --port 3306 -u root -ptestrootpass < test/test_init.sql
echo "CODECOV_TOKEN=${{ secrets.CODECOV_TOKEN }}" > .env
echo "MONGO_URI=${{ secrets.MONGO_URI }}" >> .env
- name: Test with pytest
run: pytest --tb=line

- name: Generate coverage
run: pytest --tb=line --cov-config=.coveragerc --cov --cov-report=xml
run: |
pytest --tb=line --cov-config=.coveragerc --cov --cov-report=xml
python -c "import os;from pymongo import MongoClient; client = MongoClient(os.getenv('MONGO_URI')); client.testDB.movies.drop(); client.close()"
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
Expand Down
51 changes: 15 additions & 36 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
@@ -1,52 +1,31 @@
name: Unittest
name: Tests

on:
push:
branches: [main, workflows]
schedule:
- cron: '00 23 * * *'
on: [push]

jobs:
build:
upload-coverage:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: ["3.11"]
env:
MONGO_URI: ${{ secrets.MONGO_URI }}

steps:
- uses: actions/checkout@v3
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
python-version: 3.9 # Replace '3.x' with your desired Python version

- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Set up MongoDB Client for Tests
env:
MONGO_URI: "mongodb+srv://svrao3:popcorn1234@popcorn.xujnm.mongodb.net/?retryWrites=true&w=majority&appName=PopCorn"
- name: Create env file
run: |
python -c "
import os
from pymongo import MongoClient
uri = os.getenv('MONGO_URI')
client = MongoClient(uri)
try:
client.admin.command('ping')
print('Successfully connected to MongoDB Atlas!')
client.close()
except Exception as e:
print('MongoDB connection failed:', e)
exit(1)
"
echo "CODECOV_TOKEN=${{ secrets.CODECOV_TOKEN }}" > .env
echo "MONGO_URI=${{ secrets.MONGO_URI }}" >> .env
- name: Run Unit Tests
env:
MONGO_URI: "mongodb+srv://svrao3:popcorn1234@popcorn.xujnm.mongodb.net/?retryWrites=true&w=majority&appName=PopCorn"
run: |
python -m unittest discover -s test -p "*.py"
- name: Test with pytest
run: pytest --tb=line
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
coverage
.coveralls.yml
react-frontend/node_modules
*.tar

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
13 changes: 13 additions & 0 deletions set_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
This script will create a .env file with environment variables.
"""

env_variables = {
"MONGO_URI": "mongodb+srv://svrao3:popcorn1234@popcorn.xujnm.mongodb.net",
}

with open(".env", "w", encoding="utf-8") as env_file:
for key, value in env_variables.items():
env_file.write(f"{key}={value}\n")

print(".env file has been created with your environment variables.")
4 changes: 2 additions & 2 deletions src/recommenderapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ def setup_mongodb_indexes():
Sets up the MongoDB indexes.
"""
try:
db.users.create_index([("username", 1)], unique=True)
db.users.create_index([("email", 1)], unique=True)
db.users.create_index([("username", 1)])
db.users.create_index([("email", 1)])
db.movies.create_index([("imdb_id", 1)], unique=True)
db.movies.create_index([("name", 1)])
db.ratings.create_index([("user_id", 1), ("time", -1)])
Expand Down
15 changes: 10 additions & 5 deletions src/recommenderapp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
@author: PopcornPicks
"""

import os
from dotenv import load_dotenv

from pymongo import MongoClient
from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError
from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError, InvalidURI

load_dotenv()

MONGO_URI = "mongodb+srv://svrao3:popcorn1234@popcorn.xujnm.mongodb.net"
MONGO_URI = os.getenv("MONGO_URI")
MONGO_OPTIONS = "/?retryWrites=true&w=majority&appName=PopCorn"
MONGO_URI += MONGO_OPTIONS

client = MongoClient(MONGO_URI)

try:
client.admin.command("ping")
print("Pinged your deployment. You successfully connected to MongoDB!")
except (ConnectionFailure, ServerSelectionTimeoutError) as mongo_e:
print(f"Could not connect to MongoDB: {mongo_e}")
print("Pinged your deployment. You successfully connected to MongoDB!", flush=True)
except (InvalidURI, ConnectionFailure, ServerSelectionTimeoutError) as mongo_e:
print(f"Could not connect to MongoDB: {mongo_e}", flush=True)

0 comments on commit 221640e

Please sign in to comment.