Greg Claude Opus 4.7 (1M context) commited on
Commit
5fc7b3b
·
1 Parent(s): 82f61de

feat(utils): add count_by_sex helper for M/F/null aggregations

Browse files

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

backend/app/utils/sex_counts.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Aggregate user counts by sex (M/F/null) for reports and dashboards."""
2
+ from typing import Iterable
3
+ from sqlalchemy import func
4
+ from sqlalchemy.orm import Session
5
+
6
+ from app.models.user import User
7
+
8
+
9
+ def count_by_sex(
10
+ db: Session,
11
+ *,
12
+ account_id: int,
13
+ student_ids: Iterable[int],
14
+ ) -> dict[str, int]:
15
+ """Return {'masculino', 'femenino', 'sin_especificar', 'total'} over the given ids.
16
+
17
+ Users outside `account_id` are ignored (defensive scoping). `sex` of '' or NULL
18
+ folds into 'sin_especificar'. Empty input returns all zeros.
19
+ """
20
+ ids = list(student_ids)
21
+ out = {"masculino": 0, "femenino": 0, "sin_especificar": 0, "total": 0}
22
+ if not ids:
23
+ return out
24
+
25
+ rows = (
26
+ db.query(User.sex, func.count(User.id))
27
+ .filter(User.account_id == account_id, User.id.in_(ids))
28
+ .group_by(User.sex)
29
+ .all()
30
+ )
31
+ for sex, cnt in rows:
32
+ if sex == "M":
33
+ out["masculino"] += cnt
34
+ elif sex == "F":
35
+ out["femenino"] += cnt
36
+ else:
37
+ out["sin_especificar"] += cnt
38
+ out["total"] += cnt
39
+ return out
backend/tests/test_sex_counts.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from sqlalchemy.orm import Session
3
+
4
+ from app.core.database import engine, SessionLocal, Base
5
+ from app.models.account import Account
6
+ from app.models.plan import Plan
7
+ from app.models.user import User
8
+ from app.utils.sex_counts import count_by_sex
9
+
10
+
11
+ @pytest.fixture
12
+ def db():
13
+ Base.metadata.create_all(bind=engine)
14
+ s = SessionLocal()
15
+ yield s
16
+ s.close()
17
+
18
+
19
+ @pytest.fixture
20
+ def acc(db: Session):
21
+ plan = db.query(Plan).first()
22
+ if not plan:
23
+ plan = Plan(name="basic", features=["manual_checkin"], price=0)
24
+ db.add(plan); db.commit(); db.refresh(plan)
25
+ a = Account(organization_name="TestSC", plan_id=plan.id, category="school")
26
+ db.add(a); db.commit(); db.refresh(a)
27
+ yield a
28
+ db.query(User).filter(User.account_id == a.id).delete()
29
+ db.delete(a); db.commit()
30
+
31
+
32
+ def _mkuser(db, aid, name, sex):
33
+ u = User(account_id=aid, name=name, lastname="X", role="student", sex=sex)
34
+ db.add(u); db.commit(); db.refresh(u)
35
+ return u
36
+
37
+
38
+ def test_count_by_sex_empty_returns_zeros(db, acc):
39
+ assert count_by_sex(db, account_id=acc.id, student_ids=[]) == {
40
+ "masculino": 0, "femenino": 0, "sin_especificar": 0, "total": 0,
41
+ }
42
+
43
+
44
+ def test_count_by_sex_buckets_m_f_and_null(db, acc):
45
+ u1 = _mkuser(db, acc.id, "Ana", "F")
46
+ u2 = _mkuser(db, acc.id, "Luis", "M")
47
+ u3 = _mkuser(db, acc.id, "Pat", None)
48
+ res = count_by_sex(db, account_id=acc.id, student_ids=[u1.id, u2.id, u3.id])
49
+ assert res == {"masculino": 1, "femenino": 1, "sin_especificar": 1, "total": 3}
50
+
51
+
52
+ def test_count_by_sex_ignores_users_from_other_accounts(db, acc):
53
+ other = Account(organization_name="OtherSC", plan_id=acc.plan_id, category="school")
54
+ db.add(other); db.commit(); db.refresh(other)
55
+ u_self = _mkuser(db, acc.id, "Ana", "F")
56
+ u_other = _mkuser(db, other.id, "Lia", "F")
57
+ res = count_by_sex(db, account_id=acc.id, student_ids=[u_self.id, u_other.id])
58
+ assert res == {"masculino": 0, "femenino": 1, "sin_especificar": 0, "total": 1}
59
+ db.query(User).filter(User.account_id == other.id).delete()
60
+ db.delete(other); db.commit()