Skip to content

Commit d8d8936

Browse files
add tests for issues #151 and #374
1 parent 5929f95 commit d8d8936

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

tests/schema_simple.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import random
55
import datajoint as dj
66
import itertools
7+
import hashlib
8+
import uuid
9+
import faker
10+
711

812
from . import PREFIX, CONN_INFO
913
import numpy as np
@@ -151,6 +155,53 @@ class DataB(dj.Lookup):
151155
contents = list(zip(range(5), range(5, 10)))
152156

153157

158+
@schema
159+
class Website(dj.Lookup):
160+
definition = """
161+
url_hash : uuid
162+
---
163+
url : varchar(1000)
164+
"""
165+
166+
def insert1_url(self, url):
167+
hashed = hashlib.sha1()
168+
hashed.update(url.encode())
169+
url_hash = uuid.UUID(bytes=hashed.digest()[:16])
170+
self.insert1(dict(url=url, url_hash=url_hash), skip_duplicates=True)
171+
return url_hash
172+
173+
174+
@schema
175+
class Profile(dj.Manual):
176+
definition = """
177+
ssn : char(11)
178+
---
179+
name : varchar(70)
180+
residence : varchar(255)
181+
blood_group : enum('A+', 'A-', 'AB+', 'AB-', 'B+', 'B-', 'O+', 'O-')
182+
username : varchar(120)
183+
birthdate : date
184+
job : varchar(120)
185+
sex : enum('M', 'F')
186+
"""
187+
188+
class Website(dj.Part):
189+
definition = """
190+
-> master
191+
-> Website
192+
"""
193+
194+
def populate_random(self, n=10):
195+
fake = faker.Faker()
196+
for _ in range(n):
197+
profile = fake.profile()
198+
with self.connection.transaction:
199+
self.insert1(profile, ignore_extra_fields=True)
200+
for url in profile['website']:
201+
self.Website().insert1(
202+
dict(ssn=profile['ssn'], url_hash=Website().insert1_url(url)))
203+
204+
154205
@schema
155206
class TTestUpdate(dj.Lookup):
156207
definition = """

tests/test_cascading_delete.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from nose.tools import assert_false, assert_true, assert_equal
1+
from nose.tools import assert_false, assert_true, assert_equal, raises
22
import datajoint as dj
3-
from .schema_simple import A, B, D, E, L
3+
from .schema_simple import A, B, D, E, L, Website, Profile
44
from .schema import ComplexChild, ComplexParent
55

66

@@ -27,8 +27,19 @@ def test_delete_tree():
2727

2828
@staticmethod
2929
def test_stepwise_delete():
30-
assert_false(dj.config['safemode'], 'safemode must be off for testing') #TODO: just turn it off instead of warning
31-
assert_true(L() and A() and B() and B.C(), 'schema population failed as a precondition to test')
30+
assert_false(dj.config['safemode'], 'safemode must be off for testing')
31+
assert_true(L() and A() and B() and B.C(),
32+
'schema population failed as a precondition to test')
33+
B.C().delete(force=True)
34+
assert_false(B.C(), 'failed to delete child tables')
35+
B().delete()
36+
assert_false(B(), 'failed to delete the parent table following child table deletion')
37+
38+
@staticmethod
39+
def test_stepwise_delete():
40+
assert_false(dj.config['safemode'], 'safemode must be off for testing')
41+
assert_true(L() and A() and B() and B.C(),
42+
'schema population failed as a precondition to test')
3243
B.C().delete(force=True)
3344
assert_false(B.C(), 'failed to delete child tables')
3445
B().delete()
@@ -96,3 +107,18 @@ def test_delete_complex_keys():
96107
(ComplexParent & restriction).delete()
97108
assert len(ComplexParent & restriction) == 0, 'Parent record was not deleted'
98109
assert len(ComplexChild & restriction) == 0, 'Child record was not deleted'
110+
111+
def test_delete_master(self):
112+
Profile().populate_random()
113+
Profile().delete()
114+
115+
@raises(dj.DataJointError)
116+
def test_delete_parts(self):
117+
"""test issue #151"""
118+
Profile().populate_random()
119+
Website().delete()
120+
121+
@raises(dj.DataJointError)
122+
def test_drop_part(self):
123+
"""test issue #374"""
124+
Website().drop()

tests/test_schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def test_list_tables():
124124
# https://github.com/datajoint/datajoint-python/issues/838
125125
assert(set(['reserved_word', '#l', '#a', '__d', '__b', '__b__c', '__e', '__e__f',
126126
'#outfit_launch', '#outfit_launch__outfit_piece', '#i_j', '#j_i',
127-
'#t_test_update', '#data_a', '#data_b', 'f', '#argmax_test'
127+
'#t_test_update', '#data_a', '#data_b', 'f', '#argmax_test',
128+
'#website', 'profile', 'profile__website'
128129
]) == set(schema_simple.list_tables()))
129130

130131

0 commit comments

Comments
 (0)