|
| 1 | +from nose.tools import assert_true, assert_false, assert_equal, raises |
| 2 | +import os |
| 3 | +import numpy as np |
| 4 | +from pathlib import Path |
| 5 | +import tempfile |
| 6 | +import datajoint as dj |
| 7 | +from . import PREFIX, CONN_INFO |
| 8 | +from datajoint import DataJointError |
| 9 | + |
| 10 | +schema = dj.Schema(PREFIX + "_update1", connection=dj.conn(**CONN_INFO)) |
| 11 | + |
| 12 | +dj.config["stores"]["update_store"] = dict(protocol="file", location=tempfile.mkdtemp()) |
| 13 | + |
| 14 | +dj.config["stores"]["update_repo"] = dict( |
| 15 | + stage=tempfile.mkdtemp(), protocol="file", location=tempfile.mkdtemp() |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +scratch_folder = tempfile.mkdtemp() |
| 20 | + |
| 21 | +dj.errors._switch_filepath_types(True) |
| 22 | + |
| 23 | + |
| 24 | +@schema |
| 25 | +class Thing(dj.Manual): |
| 26 | + definition = """ |
| 27 | + thing : int |
| 28 | + --- |
| 29 | + number=0 : int |
| 30 | + frac : float |
| 31 | + picture = null : attach@update_store |
| 32 | + params = null : longblob |
| 33 | + img_file = null: filepath@update_repo |
| 34 | + timestamp = CURRENT_TIMESTAMP : datetime |
| 35 | + """ |
| 36 | + |
| 37 | + |
| 38 | +def test_update1(): |
| 39 | + """test normal updates""" |
| 40 | + |
| 41 | + dj.errors._switch_filepath_types(True) |
| 42 | + # CHECK 1 -- initial insert |
| 43 | + key = dict(thing=1) |
| 44 | + Thing.insert1(dict(key, frac=0.5)) |
| 45 | + check1 = Thing.fetch1() |
| 46 | + |
| 47 | + # CHECK 2 -- some updates |
| 48 | + # numbers and datetimes |
| 49 | + Thing.update1(dict(key, number=3, frac=30, timestamp="2020-01-01 10:00:00")) |
| 50 | + # attachment |
| 51 | + attach_file = Path(scratch_folder, "attach1.dat") |
| 52 | + buffer1 = os.urandom(100) |
| 53 | + attach_file.write_bytes(buffer1) |
| 54 | + Thing.update1(dict(key, picture=attach_file)) |
| 55 | + attach_file.unlink() |
| 56 | + assert_false(attach_file.is_file()) |
| 57 | + |
| 58 | + # filepath |
| 59 | + stage_path = dj.config["stores"]["update_repo"]["stage"] |
| 60 | + relpath, filename = "one/two/three", "picture.dat" |
| 61 | + managed_file = Path(stage_path, relpath, filename) |
| 62 | + managed_file.parent.mkdir(parents=True, exist_ok=True) |
| 63 | + original_file_data = os.urandom(3000) |
| 64 | + with managed_file.open("wb") as f: |
| 65 | + f.write(original_file_data) |
| 66 | + Thing.update1(dict(key, img_file=managed_file)) |
| 67 | + managed_file.unlink() |
| 68 | + assert_false(managed_file.is_file()) |
| 69 | + |
| 70 | + check2 = Thing.fetch1(download_path=scratch_folder) |
| 71 | + buffer2 = Path(check2["picture"]).read_bytes() # read attachment |
| 72 | + final_file_data = managed_file.read_bytes() # read filepath |
| 73 | + |
| 74 | + # CHECK 3 -- reset to default values using None |
| 75 | + Thing.update1( |
| 76 | + dict( |
| 77 | + key, |
| 78 | + number=None, |
| 79 | + timestamp=None, |
| 80 | + picture=None, |
| 81 | + img_file=None, |
| 82 | + params=np.random.randn(3, 3), |
| 83 | + ) |
| 84 | + ) |
| 85 | + check3 = Thing.fetch1() |
| 86 | + |
| 87 | + assert_true( |
| 88 | + check1["number"] == 0 and check1["picture"] is None and check1["params"] is None |
| 89 | + ) |
| 90 | + |
| 91 | + assert_true( |
| 92 | + check2["number"] == 3 |
| 93 | + and check2["frac"] == 30.0 |
| 94 | + and check2["picture"] is not None |
| 95 | + and check2["params"] is None |
| 96 | + and buffer1 == buffer2 |
| 97 | + ) |
| 98 | + |
| 99 | + assert_true( |
| 100 | + check3["number"] == 0 |
| 101 | + and check3["frac"] == 30.0 |
| 102 | + and check3["picture"] is None |
| 103 | + and check3["img_file"] is None |
| 104 | + and isinstance(check3["params"], np.ndarray) |
| 105 | + ) |
| 106 | + |
| 107 | + assert_true(check3["timestamp"] > check2["timestamp"]) |
| 108 | + assert_equal(buffer1, buffer2) |
| 109 | + assert_equal(original_file_data, final_file_data) |
| 110 | + |
| 111 | + |
| 112 | +@raises(DataJointError) |
| 113 | +def test_update1_nonexistent(): |
| 114 | + Thing.update1(dict(thing=100, frac=0.5)) # updating a non-existent entry |
| 115 | + |
| 116 | + |
| 117 | +@raises(DataJointError) |
| 118 | +def test_update1_noprimary(): |
| 119 | + Thing.update1(dict(number=None)) # missing primary key |
| 120 | + |
| 121 | + |
| 122 | +@raises(DataJointError) |
| 123 | +def test_update1_misspelled_attribute(): |
| 124 | + key = dict(thing=17) |
| 125 | + Thing.insert1(dict(key, frac=1.5)) |
| 126 | + Thing.update1(dict(key, numer=3)) # misspelled attribute |
0 commit comments