|
| 1 | +import pytest |
| 2 | +import uuid |
| 3 | +from .schema_uuid import Basic, Item, Topic |
| 4 | +from datajoint import DataJointError |
| 5 | +from itertools import count |
| 6 | + |
| 7 | + |
| 8 | +def test_uuid(schema_uuid): |
| 9 | + """test inserting and fetching of UUID attributes and restricting by UUID attributes""" |
| 10 | + u, n = uuid.uuid4(), -1 |
| 11 | + Basic().insert1(dict(item=u, number=n)) |
| 12 | + Basic().insert(zip(map(uuid.uuid1, range(20)), count())) |
| 13 | + number = (Basic() & {"item": u}).fetch1("number") |
| 14 | + assert number == n |
| 15 | + item = (Basic & {"number": n}).fetch1("item") |
| 16 | + assert u == item |
| 17 | + |
| 18 | + |
| 19 | +def test_string_uuid(schema_uuid): |
| 20 | + """test that only UUID objects are accepted when inserting UUID fields""" |
| 21 | + u, n = "00000000-0000-0000-0000-000000000000", 24601 |
| 22 | + Basic().insert1(dict(item=u, number=n)) |
| 23 | + k, m = (Basic & {"item": u}).fetch1("KEY", "number") |
| 24 | + assert m == n |
| 25 | + assert isinstance(k["item"], uuid.UUID) |
| 26 | + |
| 27 | + |
| 28 | +def test_invalid_uuid_insert1(schema_uuid): |
| 29 | + """test that only UUID objects are accepted when inserting UUID fields""" |
| 30 | + u, n = 0, 24601 |
| 31 | + with pytest.raises(DataJointError): |
| 32 | + Basic().insert1(dict(item=u, number=n)) |
| 33 | + |
| 34 | + |
| 35 | +def test_invalid_uuid_insert2(schema_uuid): |
| 36 | + """test that only UUID objects are accepted when inserting UUID fields""" |
| 37 | + u, n = "abc", 24601 |
| 38 | + with pytest.raises(DataJointError): |
| 39 | + Basic().insert1(dict(item=u, number=n)) |
| 40 | + |
| 41 | + |
| 42 | +def test_invalid_uuid_restrict1(schema_uuid): |
| 43 | + """test that only UUID objects are accepted when inserting UUID fields""" |
| 44 | + u = 0 |
| 45 | + with pytest.raises(DataJointError): |
| 46 | + k, m = (Basic & {"item": u}).fetch1("KEY", "number") |
| 47 | + |
| 48 | + |
| 49 | +def test_invalid_uuid_restrict1(schema_uuid): |
| 50 | + """test that only UUID objects are accepted when inserting UUID fields""" |
| 51 | + u = "abc" |
| 52 | + with pytest.raises(DataJointError): |
| 53 | + k, m = (Basic & {"item": u}).fetch1("KEY", "number") |
| 54 | + |
| 55 | + |
| 56 | +def test_uuid_dependencies(schema_uuid): |
| 57 | + """test the use of UUID in foreign keys""" |
| 58 | + for word in ( |
| 59 | + "Neuroscience", |
| 60 | + "Knowledge", |
| 61 | + "Curiosity", |
| 62 | + "Inspiration", |
| 63 | + "Science", |
| 64 | + "Philosophy", |
| 65 | + "Conscience", |
| 66 | + ): |
| 67 | + Topic().add(word) |
| 68 | + Item.populate() |
| 69 | + assert Item().progress() == (0, len(Topic())) |
0 commit comments