Skip to content

Commit

Permalink
Test new generalized assess_commit_date_interval
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofachada committed Feb 27, 2024
1 parent 3b9a442 commit bd171a1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 36 deletions.
15 changes: 0 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
"""Fixtures and configurations to be used by test functions."""

import pytest

from egrader.git import git_at


@pytest.fixture()
def git_repo_empty(tmp_path):
"""Create and configure an empty Git repository."""
git_at(tmp_path, "init")
git_at(
tmp_path, "config", "user.email", "github-actions[bot]@users.noreply.github.com"
)
git_at(tmp_path, "config", "user.name", "github-actions")
return tmp_path
40 changes: 40 additions & 0 deletions tests/plugins/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Fixtures and configurations to be used by intra-repo plugin tests."""

from datetime import datetime
from pathlib import Path

import pytest

from egrader.git import git_at


@pytest.fixture()
def git_repo(tmp_path, monkeypatch):
"""Create and configure an empty Git repository."""
monkeypatch.setenv("GIT_AUTHOR_NAME", "github-actions")
monkeypatch.setenv(
"GIT_AUTHOR_EMAIL", "github-actions[bot]@users.noreply.github.com"
)
monkeypatch.setenv("GIT_COMMITTER_NAME", "github-actions")
monkeypatch.setenv(
"GIT_COMMITTER_EMAIL", "github-actions[bot]@users.noreply.github.com"
)
git_at(tmp_path, "init")
return tmp_path


@pytest.fixture()
def make_commit(monkeypatch):
"""Fixture to make a simple commit."""
now: datetime = datetime.now()

def _make_commit(repo: Path, dt: datetime = now):
"""Helper function which makes a simple commit on the specified repository."""
monkeypatch.setenv("GIT_COMMITTER_DATE", str(dt))
some_file_path = Path(repo, "some_file.txt")
with open(some_file_path, "a") as some_file:
some_file.write("Some more text")
git_at(repo, "add", some_file_path.name)
git_at(repo, "commit", "-m", '"Yet another commit"', f"--date={dt!r}")

return _make_commit
67 changes: 46 additions & 21 deletions tests/plugins/test_repo.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,60 @@
"""Tests for repository plug-ins."""

from pathlib import Path

from egrader.git import git_at
from egrader.plugins.repo import assess_min_commits
from egrader.types import StudentGit
from datetime import datetime, timedelta

import numpy as np

def do_tmpfile_commit(tmp_file_path: Path):
"""Helper functions which makes a simple commit on the specified repository."""
with open(tmp_file_path, "a") as tmp_file:
tmp_file.write("Some more text")
git_at(tmp_file_path.parent, "add", tmp_file_path.name)
git_at(tmp_file_path.parent, "commit", "-m", '"Yet another commit"')
from egrader.plugins.repo import assess_commit_date_interval, assess_min_commits
from egrader.types import StudentGit


def test_repo_assess_min_commits_no(git_repo_empty):
def test_repo_assess_min_commits_no(git_repo, make_commit):
"""Test if a minimum number of commits is not confirmed."""
some_file_path = Path(git_repo_empty, "some_file.txt")
do_tmpfile_commit(some_file_path)
make_commit(git_repo)
stdgit = StudentGit("", "", "")
result = assess_min_commits(stdgit, str(git_repo_empty), 2)
result = assess_min_commits(stdgit, str(git_repo), 2)
assert result == 0


def test_repo_assess_min_commits_yes(git_repo_empty):
def test_repo_assess_min_commits_yes(git_repo, make_commit):
"""Test if a minimum number of commits is confirmed."""
some_file_path = Path(git_repo_empty, "some_file.txt")
do_tmpfile_commit(some_file_path)
do_tmpfile_commit(some_file_path)
do_tmpfile_commit(some_file_path)
make_commit(git_repo)
make_commit(git_repo)
make_commit(git_repo)
stdgit = StudentGit("", "", "")
result = assess_min_commits(stdgit, str(git_repo_empty), 2)
result = assess_min_commits(stdgit, str(git_repo), 2)
assert result == 1


def test_repo_assess_commit_date_interval(git_repo, make_commit):
"""Test if the percentage of commits done in a date interval is correct."""
# Interval for valid commits
before_date: datetime = datetime.now() - timedelta(days=1)
after_date: datetime = datetime.now() - timedelta(days=4)

# Commits done before the after date
n_commits_before = 2
make_commit(git_repo, after_date - timedelta(hours=1, minutes=30))
make_commit(git_repo, after_date - timedelta(seconds=30))

# Commits done within the valid time interval
n_commits_within = 5
make_commit(git_repo, after_date + timedelta(seconds=30))
make_commit(git_repo, after_date + timedelta(hours=1, minutes=30))
make_commit(git_repo, before_date - timedelta(days=1, hours=5))
make_commit(git_repo, before_date - timedelta(minutes=45))
make_commit(git_repo, before_date - timedelta(seconds=30))

# Commits done after the before date
n_commits_after = 1
make_commit(git_repo, before_date + timedelta(seconds=10))

# Check if plugin returns the expected result
n_commits = n_commits_before + n_commits_within + n_commits_after
stdgit = StudentGit("", "", "")
np.testing.assert_allclose(
n_commits_within / n_commits,
assess_commit_date_interval(
stdgit, str(git_repo), before_date=before_date, after_date=after_date
),
)

0 comments on commit bd171a1

Please sign in to comment.