Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic Running of Tests On Pull Request #1123

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
37 changes: 37 additions & 0 deletions .github/workflows/run_full_test_suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Django Test Suite on PR

on:
pull_request:
branches:
- dev

jobs:
run-tests:
runs-on: ubuntu-latest

services:
docker:
image: docker:19.03.12
options: --privileged
ports:
- 5432:5432

steps:
- name: Check out merged code
uses: actions/checkout@v2

- name: Set up Docker Compose
run: |
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

- name: Build the Docker environment
run: docker-compose -f local.yml build

- name: Run test suite
env:
DJANGO_ENV: test
run: docker-compose -f local.yml run --rm django bash ./init.sh

- name: Cleanup
run: docker-compose -f local.yml down --volumes
31 changes: 31 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
echo "Running all test cases across the project..."

# Initialize a failure counter
failure_count=0

# Exclude tests in `document_classifier` and `functional_tests` directories
excluded_dirs="document_classifier functional_tests"

# Find all test files except those in excluded directories
test_files=$(find . -type f -name "test_*.py" | grep -Ev "$(echo $excluded_dirs | sed 's/ /|/g')")

# Run each test file
for test_file in $test_files; do
echo "Running $test_file..."
pytest "$test_file"

# Check the exit status of pytest
if [ $? -ne 0 ]; then
echo "Test failed: $test_file"
failure_count=$((failure_count + 1))
fi
done

# Report the results
if [ $failure_count -ne 0 ]; then
echo "$failure_count test(s) failed."
exit 1
else
echo "All tests passed successfully!"
fi
12 changes: 0 additions & 12 deletions sde_collections/tests/test_models_collections.py

This file was deleted.

52 changes: 37 additions & 15 deletions sde_indexing_helper/users/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from sde_indexing_helper.users.forms import UserAdminChangeForm
from sde_indexing_helper.users.models import User
from sde_indexing_helper.users.tests.factories import UserFactory
from sde_indexing_helper.users.views import (
UserRedirectView,
UserUpdateView,
Expand All @@ -22,39 +21,45 @@

class TestUserUpdateView:
"""
TODO:
extracting view initialization code as class-scoped fixture
would be great if only pytest-django supported non-function-scoped
fixture db access -- this is a work-in-progress for now:
https://github.com/pytest-dev/pytest-django/pull/258
Tests for the UserUpdateView.
"""

def dummy_get_response(self, request: HttpRequest):
@staticmethod
def dummy_get_response(request: HttpRequest):
"""Dummy get_response method for middleware testing."""
return None

def test_get_success_url(self, user: User, rf: RequestFactory):
"""
Test that UserUpdateView redirects to the correct success URL.
"""
view = UserUpdateView()
request = rf.get("/fake-url/")
request.user = user

view.request = request

assert view.get_success_url() == f"/users/{user.username}/"
expected_url = f"/users/{user.username}/"
assert view.get_success_url() == expected_url, f"Expected {expected_url}, got {view.get_success_url()}"

def test_get_object(self, user: User, rf: RequestFactory):
"""
Test that UserUpdateView retrieves the correct user object.
"""
view = UserUpdateView()
request = rf.get("/fake-url/")
request.user = user

view.request = request

assert view.get_object() == user

def test_form_valid(self, user: User, rf: RequestFactory):
"""
Test that form submission in UserUpdateView processes correctly.
"""
view = UserUpdateView()
request = rf.get("/fake-url/")

# Add the session/message middleware to the request
# Add session and message middleware
SessionMiddleware(self.dummy_get_response).process_request(request)
MessageMiddleware(self.dummy_get_response).process_request(request)
request.user = user
Expand All @@ -72,26 +77,43 @@ def test_form_valid(self, user: User, rf: RequestFactory):


class TestUserRedirectView:
"""
Tests for the UserRedirectView.
"""

def test_get_redirect_url(self, user: User, rf: RequestFactory):
"""
Test that UserRedirectView redirects to the "sde_collections:list" URL.
"""
view = UserRedirectView()
request = rf.get("/fake-url")
request = rf.get("/fake-url/")
request.user = user

view.request = request

assert view.get_redirect_url() == f"/users/{user.username}/"
expected_url = reverse("sde_collections:list")
assert view.get_redirect_url() == expected_url, f"Expected {expected_url}, got {view.get_redirect_url()}"


class TestUserDetailView:
"""
Tests for the user_detail_view function.
"""

def test_authenticated(self, user: User, rf: RequestFactory):
"""
Test that an authenticated user can access their detail view.
"""
request = rf.get("/fake-url/")
request.user = UserFactory()
request.user = user

response = user_detail_view(request, username=user.username)

assert response.status_code == 200

def test_not_authenticated(self, user: User, rf: RequestFactory):
"""
Test that an unauthenticated user is redirected to the login page.
"""
request = rf.get("/fake-url/")
request.user = AnonymousUser()

Expand Down
Loading