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

Github actions added, tests modified #129

Merged
merged 15 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Django CI

on:
push:
branches: [ master, develop ]
pull_request:
branches: [ master, develop ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.8]

services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
# options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
#

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install --upgrade pytest pytest-django pytest-cov codecov mixer selenium

- name: Run Unit Tests
env:
# Main dir is /home/runner/work/certifier
DJANGO_DEBUG: ${{ secrets.DJANGO_DEBUG }}
DJANGO_DATABASE_ENGINE: django.db.backends.postgresql
DJANGO_DATABASE_NAME: postgres
DJANGO_DATABASE_USER: postgres
DJANGO_DATABASE_PORT: 5432
DJANGO_DATABASE_PASSWORD: postgres
DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET_KEY }}
OMS_CLIENT_ID: ${{ secrets.OMS_CLIENT_ID }}
OMS_CLIENT_SECRET: ${{ secrets.OMS_CLIENT_SECRET }}

run: |
PYTHONWARNINGS=all pytest --ds=dqmhelper.test_ci_settings --cov=./ --ignore addrefrun/tests/test_addrefrun_views.py --ignore certifier/tests/test_certifier_views.py --ignore oms/tests/test_oms_utils.py
codecov

# - name: Run Functional Tests with Selenium on Chrome
# env:
# DIR_PATH_EOS_CMSML4DC: /home/runner/work/MLplayground/MLplayground/histograms/test_files/
# DJANGO_DATABASE_ENGINE: ${{ secrets.DJANGO_DATABASE_ENGINE }}
# DJANGO_DATABASE_NAME: ${{ secrets.DJANGO_DATABASE_NAME }}
# DJANGO_DEBUG: ${{ secrets.DJANGO_DEBUG }}
# DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET_KEY }}
# FILE_PATH_EOS_CMSML4DC: ${{ secrets.FILE_PATH_EOS_CMSML4DC }}
# WEB_BROWSER: Chrome
# run: |
# python manage.py test functional_tests
# - name: Run Functional Tests with Selenium on Firefox
# env:
# DIR_PATH_EOS_CMSML4DC: /home/runner/work/MLplayground/MLplayground/histograms/test_files/
# DJANGO_DATABASE_ENGINE: ${{ secrets.DJANGO_DATABASE_ENGINE }}
# DJANGO_DATABASE_NAME: ${{ secrets.DJANGO_DATABASE_NAME }}
# DJANGO_DEBUG: ${{ secrets.DJANGO_DEBUG }}
# DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET_KEY }}
# FILE_PATH_EOS_CMSML4DC: ${{ secrets.FILE_PATH_EOS_CMSML4DC }}
# WEB_BROWSER: Firefox
# run: |
# python manage.py test functional_tests
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Build Status](https://travis-ci.com/CMSTrackerDPG/certifier.svg?branch=master)](https://travis-ci.com/CMSTrackerDPG/certifier)
<!-- [![Build Status](https://travis-ci.com/CMSTrackerDPG/certifier.svg?branch=develop)](https://travis-ci.com/CMSTrackerDPG/certifier) -->
[![codecov](https://codecov.io/gh/CMSTrackerDPG/certifier/branch/master/graph/badge.svg)](https://codecov.io/gh/CMSTrackerDPG/certifier)
[![Django CI](https://github.com/CMSTrackerDPG/certifier/actions/workflows/django.yml/badge.svg)](https://github.com/CMSTrackerDPG/certifier/actions/workflows/django.yml)
[![Django CI](https://github.com/CMSTrackerDPG/certifier/actions/workflows/django.yml/badge.svg?branch=develop)](https://github.com/CMSTrackerDPG/certifier/actions/workflows/django.yml)

<!-- [![codecov](https://codecov.io/gh/CMSTrackerDPG/certifier/branch/master/graph/badge.svg)](https://codecov.io/gh/CMSTrackerDPG/certifier) -->

# Tracker Data Certification Helper

Expand Down
22 changes: 14 additions & 8 deletions addrefrun/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
from django.shortcuts import render
from tables.tables import SimpleRunReconstructionTable
from certifier.models import RunReconstruction, TrackerCertification
from certifier.exceptions import RunReconstructionIsAlreadyReference, RunReconstructionNotYetCertified
from certifier.exceptions import (
RunReconstructionIsAlreadyReference,
RunReconstructionNotYetCertified,
)
from oms.utils import retrieve_run
from django.contrib import messages

logger = logging.getLogger(__name__)


@user_passes_test(lambda user: hasattr(user, 'has_shift_leader_rights') and
user.has_shift_leader_rights,
redirect_field_name=None)
def addreference(request):
@user_passes_test(
lambda user: hasattr(user, "has_shift_leader_rights")
and user.has_shift_leader_rights,
redirect_field_name=None,
)
def addreference(request): # pragma: no cover
"""
Main view for listing Reference Run Reconstructions
and adding new ones as reference or promoting existing ones.
Expand All @@ -33,11 +38,12 @@ def addreference(request):
run = retrieve_run(run_number)

run_reconstruction, created = RunReconstruction.objects.get_or_create(
run_id=run_number, reconstruction=reco)
run_id=run_number, reconstruction=reco
)
if created:
messages.info(
request,
f"{run_reconstruction} created, must be certified first")
request, f"{run_reconstruction} created, must be certified first"
)

try:
run_reconstruction.promote_to_reference()
Expand Down
71 changes: 64 additions & 7 deletions certifier/tests/test_certifier_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
from users.models import User
from django.urls import reverse


pytestmark = pytest.mark.django_db


class TestCertify:
def test_certify_get(self):
run_number = 321123
arguments={'run_number': run_number }
arguments = {"run_number": run_number}

req = RequestFactory().get(reverse("certify", kwargs=arguments))

Expand All @@ -30,7 +32,7 @@ def test_certify_valid(self):
ref_runReconstruction = mixer.blend(RunReconstruction, is_reference=True)
bad_reason = mixer.blend(BadReason)
dataset = mixer.blend(Dataset)
arguments={'run_number': run_number}
arguments = {"run_number": run_number}

data = {
"reference_runreconstruction": ref_runReconstruction.pk,
Expand All @@ -49,20 +51,71 @@ def test_certify_valid(self):
assert {} == form.errors
assert form.is_valid()

req = RequestFactory().post(reverse("certify", kwargs=arguments), data=form.data)
req = RequestFactory().post(
reverse("certify", kwargs=arguments), data=form.data
)
req.user = mixer.blend(User)

resp = views.certify(req, run_number)

assert 302 == resp.status_code, "should redirect to success view"
assert TrackerCertification.objects.exists()

def test_certify_other_users_certification(self):
"""
Trying to overwrite an existing certification done
by another user should fail
"""
user_a = mixer.blend(User)
user_b = mixer.blend(User)
run_number = 321123
run = mixer.blend(OmsRun, run_number=run_number, run_type=OmsRun.COLLISIONS)
run_reconstruction = mixer.blend(RunReconstruction, run=run, is_reference=False)
ref_run_reconstruction = mixer.blend(RunReconstruction, is_reference=True)
bad_reason = mixer.blend(BadReason)
dataset = mixer.blend(Dataset)

# Certification made by User A
c = TrackerCertification.objects.create(
user=user_a,
runreconstruction=run_reconstruction,
reference_runreconstruction=ref_run_reconstruction,
dataset=dataset,
bad_reason=bad_reason,
comment="test",
date="2018-01-01",
trackermap="exists",
pixel="good",
strip="good",
tracking="good",
)

# Create a form using the same data
form = CertifyForm(instance=c)

# Create a POST request with this data
req_url = reverse(
"certify",
kwargs={
"run_number": run_number,
"reco": run_reconstruction.reconstruction,
},
)
req = RequestFactory().post(req_url, data=form.data)
# Try to certify as user_b
req.user = user_b

resp = views.certify(
req, run_number=run_number, reco=run_reconstruction.reconstruction
)
assert resp.status_code == 400

def test_certify_invalid_bad_run_number(self):
run_number = 999999999
ref_runReconstruction = mixer.blend(RunReconstruction, is_reference=True)
bad_reason = mixer.blend(BadReason)
dataset = mixer.blend(Dataset)
arguments={'run_number': run_number}
arguments = {"run_number": run_number}

data = {
"reference_runreconstruction": ref_runReconstruction.pk,
Expand All @@ -81,7 +134,9 @@ def test_certify_invalid_bad_run_number(self):
assert {} == form.errors
assert form.is_valid()

req = RequestFactory().post(reverse("certify", kwargs=arguments), data=form.data)
req = RequestFactory().post(
reverse("certify", kwargs=arguments), data=form.data
)

req.user = mixer.blend(User)

Expand All @@ -94,7 +149,7 @@ def test_certify_invalid_no_selection(self):
run_number = 321123
ref_runReconstruction = mixer.blend(RunReconstruction)
bad_reason = mixer.blend(BadReason)
arguments={'run_number': run_number}
arguments = {"run_number": run_number}

data = {
"reference_runreconstruction": ref_runReconstruction.pk,
Expand All @@ -109,7 +164,9 @@ def test_certify_invalid_no_selection(self):
assert {} != form.errors
assert form.is_valid() == False

req = RequestFactory().post(reverse("certify", kwargs=arguments), data=form.data)
req = RequestFactory().post(
reverse("certify", kwargs=arguments), data=form.data
)
req.user = mixer.blend(User)

resp = views.certify(req, run_number)
Expand Down
27 changes: 5 additions & 22 deletions certifier/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,7 @@ def promoteToReference(request, run_number, reco):
@login_required
def certify(request, run_number, reco=None):

logger.debug(f"Requesting certification of run {run_number}")

# Check if run is already booked
# try:
# open_run = OpenRuns.objects.get(run_number=run_number)
# if request.user != open_run.user:
# msg = f"Run {run_number} is already booked by another user"
# logger.warning(msg)
# return render(
# request,
# "certifier/http_error.html",
# context={
# "error_num": 400,
# "message": msg
# },
# )

# except OpenRuns.DoesNotExist as e:
# # Means that OpenRun does not exist
# logger.debug(f"Open run for {run_number} does not exist yet")
logger.debug(f"Requesting certification of run {run_number} {reco if reco else ''}")

# Check if already certified
try:
Expand All @@ -81,17 +62,19 @@ def certify(request, run_number, reco=None):
runreconstruction__reconstruction=reco,
)
if request.user != certification.user:
print(request.user, certification.user.username)
msg = f"Reconstruction {run_number} {reco} is already certified by another user"
logger.warning(msg)
return render(
request,
"certifier/http_error.html",
context={"error_num": 400, "message": msg},
status=400,
)
except TrackerCertification.DoesNotExist as e:
# Means that this specific certification does not exist yet
logger.debug(f"Certification for {run_number} {reco} does not exist yet")
logger.debug(
f"Certification for {run_number} {reco if reco else ''} does not exist yet"
)

# From openruns colored boxes
if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
Expand Down
18 changes: 9 additions & 9 deletions dqmhelper/test_ci_settings.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
from .settings import *

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'testdb',
'USER': 'travis',
# 'HOST': 'localhost'
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "postgres",
"USER": "postgres",
"HOST": "127.0.0.1",
"PASSWORD": "postgres",
"PORT": 5432,
}
}

DYNAMIC_PREFERENCES = {
'ENABLE_CACHE': False,
"ENABLE_CACHE": False,
}

CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("localhost", 6379)],
},
"CONFIG": {"hosts": [("localhost", 6379)],},
},
}
Binary file added home/static/home/img/_addReferenceRun.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added home/static/home/img/_cablingMap.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added home/static/home/img/_certifyRun.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added home/static/home/img/_dailyReport.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added home/static/home/img/_listCertRuns.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added home/static/home/img/_mlPlayground2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added home/static/home/img/_shiftleader.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added home/static/home/img/_trackermaps.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added home/static/home/img/_tutorialsTwiki.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions oms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_reco_from_dataset(dataset):
return "rereco"


def retrieve_dataset_by_reco(run_number, reco):
def retrieve_dataset_by_reco(run_number, reco): # pragma: no cover
datasets = runregistry.get_datasets(filter={"run_number": {"=": run_number}})

for dataset in datasets:
Expand All @@ -37,7 +37,7 @@ def retrieve_dataset_by_reco(run_number, reco):
raise Exception(f"Could not find reconstruction:{reco} for run {run_number}")


def retrieve_dataset(run_number):
def retrieve_dataset(run_number): # pragma: no cover
datasets = runregistry.get_datasets(filter={"run_number": {"=": run_number}})

for dataset in datasets:
Expand All @@ -55,7 +55,7 @@ def retrieve_dataset(run_number):
raise Exception(f"Run {run_number} has been fully certified")


def retrieve_fill(fill_number):
def retrieve_fill(fill_number): # pragma: no cover
fill_check = OmsFill.objects.filter(fill_number=fill_number)

if fill_check.exists():
Expand Down Expand Up @@ -134,7 +134,7 @@ def retrieve_fill(fill_number):
return OmsFill.objects.get(fill_number=fill_number)


def retrieve_run(run_number):
def retrieve_run(run_number): # pragma: no cover
"""
Helper function that, given a run number, tries to retrieve it
by looking into the DB first, then the OMS API.
Expand Down
Loading