Skip to content

Commit

Permalink
extract run_preprocessor for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kakadus committed Oct 16, 2023
1 parent 6a4c740 commit 7084323
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
7 changes: 7 additions & 0 deletions evap/staff/fixtures/excel_files_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@
['Bachelor', 'Manilium', 'Lucilia', '[email protected]', 'Vorlesung', 'no', 'Schütteln', 'Shake', 'Prof. Dr.', 'Prorsus', 'Christoph', '[email protected]']
]
}
# user.title, user.last_name, user.first_name, user.email
valid_user_courses_import_users = [
['', 'Quid', 'Bastius', '[email protected]'],
['', 'Sed', 'Diam', '[email protected]'],
['', 'Manilium', 'Lucilia', '[email protected]'],
['Prof. Dr.', 'Prorsus', 'Christoph', '[email protected]']
]

random_file_content = b"Hallo Welt\n"

Expand Down
47 changes: 46 additions & 1 deletion evap/staff/tests/test_tools.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import csv
from io import BytesIO, StringIO
from itertools import cycle
from typing import TextIO
from unittest.mock import MagicMock, patch

from django.contrib.auth.models import Group
from django.test import TestCase
from django.utils.html import escape
from django_webtest import WebTest
from model_bakery import baker
from openpyxl import load_workbook

from evap.evaluation.models import Contribution, Course, Evaluation, UserProfile
from evap.evaluation.tests.tools import assert_no_database_modifications
from evap.evaluation.tests.tools import assert_no_database_modifications, make_manager
from evap.rewards.models import RewardPointGranting, RewardPointRedemption
from evap.staff.fixtures.excel_files_test_data import (
create_memory_excel_file,
valid_user_courses_import_filedata,
valid_user_courses_import_users,
)
from evap.staff.tools import (
conditional_escape,
merge_users,
remove_user_from_represented_and_ccing_users,
user_edit_link,
)
from tools.enrollment_preprocessor import run_preprocessor


class MergeUsersTest(TestCase):
Expand Down Expand Up @@ -216,3 +230,34 @@ def test_conditional_escape(self):
self.assertEqual(conditional_escape("<script>"), "&lt;script&gt;")
self.assertEqual(conditional_escape(escape("<script>")), "&lt;script&gt;")
self.assertEqual(conditional_escape("safe"), "safe")


class EnrolllmentPreprocessorTest(WebTest):
@classmethod
def setUpTestData(cls) -> None:
cls.xslx_file = BytesIO(create_memory_excel_file(valid_user_courses_import_filedata))
cls.data = valid_user_courses_import_users

def create_memory_csv_file(self) -> TextIO:
io = StringIO()
writer = csv.writer(io, delimiter=";", lineterminator="\n")
writer.writerows(self.data)
io.seek(0)
return io

def test_parse_successful(self):
run_preprocessor(self.xslx_file, self.create_memory_csv_file())

@patch("builtins.input", side_effect=cycle(("n", "y")))
def test_parse_conflict(self, input_patch: MagicMock):
self.data[0][1] = "Conflicting Lastname"
self.data[1][0] = "Conflicting Title"
self.data[2][2] = "Conflicting Firstname"
self.data[3][3] = "[email protected]"
run_preprocessor(self.xslx_file, self.create_memory_csv_file())
self.assertEqual(input_patch.call_count, 3)
workbook = load_workbook(self.xslx_file, read_only=True)
self.assertEqual(workbook["MA Belegungen"]["B2"].value, "Quid") # conflicting lastname declined
self.assertEqual(workbook["MA Belegungen"]["I2"].value, "Conflicting Title") # conflicting title accepted
self.assertEqual(workbook["BA Belegungen"]["C2"].value, "Lucilia") # conflicting Firstname declined
self.assertEqual(workbook["BA Belegungen"]["L2"].value, "[email protected]") # different email is no conflict
33 changes: 19 additions & 14 deletions tools/enrollment_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import sys
from argparse import ArgumentParser
from dataclasses import dataclass
from io import BytesIO
from typing import Iterator, TextIO

from openpyxl import load_workbook
from openpyxl.cell import Cell
Expand All @@ -25,7 +27,7 @@ class UserCells:

def value(self):
return User(
self.title.value if self.title else "", self.last_name.value, self.first_name.value, self.email.value
self.title.value or "" if self.title else "", self.last_name.value, self.first_name.value, self.email.value
)


Expand All @@ -46,12 +48,25 @@ def fix_users(users: dict[str, User], imported_cells: UserCells):
print("There is a conflict in the user data.")
print(f"existing: {existing}.")
print(f"imported: {imported}.")

if imported_cells.title:
if imported_cells.title is not None:
imported_cells.title.value = user_decision("title", existing.title, imported.title)
imported_cells.last_name.value = user_decision("last name", existing.last_name, imported.last_name)
imported_cells.first_name.value = user_decision("first name", existing.first_name, imported.first_name)
imported_cells.email.value = user_decision("email", existing.email, imported.email)
print()


def run_preprocessor(enrollment_data: str | BytesIO, user_data: TextIO):
workbook = load_workbook(enrollment_data)
users = {}
reader = csv.reader(user_data, delimiter=";", lineterminator="\n")
for row in reader:
users[row[-1]] = User(*row)
for sheet_name in ["MA Belegungen", "BA Belegungen"]:
for wb_row in workbook[sheet_name].iter_rows(min_row=2, min_col=2):
fix_users(users, UserCells(None, *wb_row[:3]))
fix_users(users, UserCells(*wb_row[7:]))
workbook.save(enrollment_data)


if __name__ == "__main__":
Expand All @@ -63,15 +78,5 @@ def fix_users(users: dict[str, User], imported_cells: UserCells):
"-e", "--enrollment-data", help="Path to the enrollment data in xlsx format for import.", required=True
)
ns = parser.parse_args(sys.argv[1:])

workbook = load_workbook(ns.enrollment_data)
users = {}
with open(ns.user_data, encoding="utf-8") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
users[row[-1]] = User(*row)
for sheet_name in ["MA Belegungen", "BA Belegungen"]:
for row in workbook[sheet_name].iter_rows(min_row=2, min_col=2):
fix_users(users, UserCells(None, *row[:3]))
fix_users(users, UserCells(*row[7:]))
workbook.save(ns.enrollment_data)
run_preprocessor(ns.enrollment_data, csvfile)

0 comments on commit 7084323

Please sign in to comment.