Skip to content

Commit

Permalink
Fix bug capacity null or 0
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Jun 29, 2023
1 parent 8ec1c56 commit bbffe7d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
19 changes: 10 additions & 9 deletions backend/core/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ def query_stats_animations_per_month(params):


def query_stats_bilan(params):
query = GTEvents.query
query = GTEvents.query.filter(GTEvents.deleted != True)
if "year" in params:
query = query.filter(
func.date_part("year", GTEvents.begin_date) == params["year"]
)
nb_events = query.count()
events = query.all()
sum_nb_participant = sum([d.sum_participants for d in events])
sum_clean_nb_participants = sum([d.capacity for d in events])

# events with capacity
events_capacity = [e for e in events if e.capacity and e.capacity > 0]
sum_nb_participant = sum([d.sum_participants for d in events_capacity])
sum_clean_nb_participants = sum([d.capacity for d in events_capacity])
# Taux de remplissage de toutes les animations
taux_remplissage = (
sum([d.sum_participants / d.capacity for d in events if d.capacity > 0])
sum([d.sum_participants / d.capacity for d in events_capacity])
/ nb_events
)

Expand All @@ -60,10 +63,9 @@ def query_stats_bilan(params):
sum(
[
d.sum_participants / d.capacity
for d in events
for d in events_capacity
if (
(d.end_date or datetime.now().date()) < datetime.now().date()
and d.capacity > 0
# and not getattr(d, "bilan", {}).annulation
)
]
Expand All @@ -72,9 +74,8 @@ def query_stats_bilan(params):
)

query = (
db.session.query(func.count(TAnimationsBilans.id_bilan))
.filter(TAnimationsBilans.annulation == True)
.join(GTEvents, GTEvents.id == TAnimationsBilans.id_event)
db.session.query(func.count(GTEvents.id))
.filter(GTEvents.cancelled == True)
)
if "year" in params:
query = query.filter(
Expand Down
24 changes: 23 additions & 1 deletion backend/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import logging

from core.models import GTEvents, TReservations, TAnimationsBilans
from core.models import GTEvents, TReservations
from core.models import TTokens

from .fixtures import (
Expand Down Expand Up @@ -154,3 +154,25 @@ def test_post_export_and_cancel_one_reservation(self, events):
# data_bilan["id_event"] = event.id
# resp = post_json(self.client, url_for("app_routes.post_bilans"), data_bilan)
# assert resp == 200

def test_bilan_global(self):
login(self.client)
donnees_exemple = {
"nb_animations": 1,
"nb_annulation": 0,
"sum_nb_inscriptions": 2,
"sum_nb_participants_possible": 2089,
"taux_remplissage": 0.0007017543859649122,
"taux_remplissage_passe": 0.0007017543859649122
}
response = self.client.get(url_for("app_routes.get_stats_global"))
assert response.status_code == 200

data = json_of_response(response)

assert set(data.keys()) == set(donnees_exemple.keys())
# Todo test return value

response = self.client.get(url_for("app_routes.get_stats_global", year='2023'))
assert response.status_code == 200

0 comments on commit bbffe7d

Please sign in to comment.