Skip to content

Commit fc363f4

Browse files
committed
First pass at migrating test_university
1 parent b40d4ec commit fc363f4

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

tests/schema_university.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import datajoint as dj
2+
import inspect
23

3-
schema = dj.Schema()
44

5-
6-
@schema
75
class Student(dj.Manual):
86
definition = """
97
student_id : int unsigned # university-wide ID number
@@ -20,7 +18,6 @@ class Student(dj.Manual):
2018
"""
2119

2220

23-
@schema
2421
class Department(dj.Manual):
2522
definition = """
2623
dept : varchar(6) # abbreviated department name, e.g. BIOL
@@ -31,7 +28,6 @@ class Department(dj.Manual):
3128
"""
3229

3330

34-
@schema
3531
class StudentMajor(dj.Manual):
3632
definition = """
3733
-> Student
@@ -41,7 +37,6 @@ class StudentMajor(dj.Manual):
4137
"""
4238

4339

44-
@schema
4540
class Course(dj.Manual):
4641
definition = """
4742
-> Department
@@ -52,15 +47,13 @@ class Course(dj.Manual):
5247
"""
5348

5449

55-
@schema
5650
class Term(dj.Manual):
5751
definition = """
5852
term_year : year
5953
term : enum('Spring', 'Summer', 'Fall')
6054
"""
6155

6256

63-
@schema
6457
class Section(dj.Manual):
6558
definition = """
6659
-> Course
@@ -71,7 +64,6 @@ class Section(dj.Manual):
7164
"""
7265

7366

74-
@schema
7567
class CurrentTerm(dj.Manual):
7668
definition = """
7769
omega=0 : tinyint
@@ -80,15 +72,13 @@ class CurrentTerm(dj.Manual):
8072
"""
8173

8274

83-
@schema
8475
class Enroll(dj.Manual):
8576
definition = """
8677
-> Student
8778
-> Section
8879
"""
8980

9081

91-
@schema
9282
class LetterGrade(dj.Lookup):
9383
definition = """
9484
grade : char(2)
@@ -110,10 +100,13 @@ class LetterGrade(dj.Lookup):
110100
]
111101

112102

113-
@schema
114103
class Grade(dj.Manual):
115104
definition = """
116105
-> Enroll
117106
---
118107
-> LetterGrade
119108
"""
109+
110+
111+
LOCALS_UNI = {k: v for k, v in locals().items() if inspect.isclass(v)}
112+
__all__ = list(LOCALS_UNI)

tests/test_university.py

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
1-
from nose.tools import assert_true, assert_list_equal, assert_false, raises
1+
import pytest
22
import hashlib
3+
from pathlib import Path
34
from datajoint import DataJointError
5+
import datajoint as dj
46
from .schema_university import *
5-
from . import PREFIX, CONN_INFO
7+
from . import PREFIX, schema_university
68

79

810
def _hash4(table):
9-
"""hash of table contents"""
11+
"""Hash of table contents"""
1012
data = table.fetch(order_by="KEY", as_dict=True)
1113
blob = dj.blob.pack(data, compress=False)
1214
return hashlib.md5(blob).digest().hex()[:4]
1315

1416

15-
@raises(DataJointError)
16-
def test_activate_unauthorized():
17-
schema.activate("unauthorized", connection=dj.conn(**CONN_INFO))
18-
19-
20-
def test_activate():
21-
schema.activate(
22-
PREFIX + "_university", connection=dj.conn(**CONN_INFO)
23-
) # deferred activation
17+
@pytest.fixture
18+
def schema_uni_inactive():
19+
schema = dj.Schema(context=schema_university.LOCALS_UNI)
20+
schema(Student)
21+
schema(Department)
22+
schema(StudentMajor)
23+
schema(Course)
24+
schema(Term)
25+
schema(Section)
26+
schema(CurrentTerm)
27+
schema(Enroll)
28+
schema(LetterGrade)
29+
schema(Grade)
30+
yield schema
31+
schema.drop()
32+
33+
34+
@pytest.fixture
35+
def schema_uni(db_creds_test, schema_uni_inactive, connection_test):
36+
# Deferred activation
37+
schema_uni_inactive.activate(
38+
PREFIX + "_university", connection=dj.conn(**db_creds_test)
39+
)
2440
# --------------- Fill University -------------------
41+
test_data_dir = Path(__file__).parent / "data"
2542
for table in (
2643
Student,
2744
Department,
@@ -33,12 +50,19 @@ def test_activate():
3350
Enroll,
3451
Grade,
3552
):
36-
from pathlib import Path
53+
path = test_data_dir / Path(table.__name__ + ".csv")
54+
assert path.is_file(), f"File {path} is not a file"
55+
assert path.exists(), f"File {path} does not exist"
56+
table().insert(path)
57+
return schema_uni_inactive
58+
3759

38-
table().insert(Path("./data/" + table.__name__ + ".csv"))
60+
def test_activate_unauthorized(schema_uni_inactive, db_creds_test, connection_test):
61+
with pytest.raises(DataJointError):
62+
schema_uni_inactive.activate("unauthorized", connection=dj.conn(**db_creds_test))
3963

4064

41-
def test_fill():
65+
def test_fill(schema_uni):
4266
"""check that the randomized tables are consistently defined"""
4367
# check randomized tables
4468
assert len(Student()) == 300 and _hash4(Student) == "1e1a"
@@ -48,7 +72,7 @@ def test_fill():
4872
assert len(Grade()) == 3027 and _hash4(Grade) == "4a9d"
4973

5074

51-
def test_restrict():
75+
def test_restrict(schema_uni):
5276
"""
5377
test diverse restrictions from the university database.
5478
This test relies on a specific instantiation of the database.
@@ -90,7 +114,7 @@ def test_restrict():
90114
assert len(special) == 158
91115

92116

93-
def test_advanced_join():
117+
def test_advanced_join(schema_uni):
94118
"""test advanced joins"""
95119
# Students with ungraded courses in current term
96120
ungraded = Enroll * CurrentTerm - Grade
@@ -102,14 +126,14 @@ def test_advanced_join():
102126
assert len(ungraded.join(major)) == len(ungraded & major) == 31
103127

104128

105-
def test_union():
129+
def test_union(schema_uni):
106130
# effective left join Enroll with Major
107131
q1 = (Enroll & "student_id=101") + (Enroll & "student_id=102")
108132
q2 = Enroll & "student_id in (101, 102)"
109133
assert len(q1) == len(q2) == 41
110134

111135

112-
def test_aggr():
136+
def test_aggr(schema_uni):
113137
avg_grade_per_course = Course.aggr(
114138
Grade * LetterGrade, avg_grade="round(avg(points), 2)"
115139
)

0 commit comments

Comments
 (0)