Skip to content

Commit 74198dc

Browse files
Merge pull request #223 from guzman-raphael/fix-update-date-null
Fix null logic, add tests, and fix linting
2 parents c907c35 + c1e3af3 commit 74198dc

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
lines changed

+dj/Relvar.m

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,14 @@ function update(self, attrname, value)
343343
% update(v2p.Mice & key, 'mouse_dob', '2011-01-01')
344344
% update(v2p.Scan & key, 'lens') % set the value to NULL
345345

346-
assert(count(self)==1, 'Update is only allowed on one tuple at a time')
347-
isNull = nargin<3;
346+
assert(count(self)==1, 'Update is only allowed on one tuple at a time');
348347
header = self.header;
349348
ix = find(strcmp(attrname,header.names));
350-
assert(numel(ix)==1, 'invalid attribute name')
351-
assert(~header.attributes(ix).iskey, 'cannot update a key value. Use insert(..,''REPLACE'') instead')
349+
assert(numel(ix)==1, 'invalid attribute name');
350+
assert(~header.attributes(ix).iskey, ...
351+
'cannot update a key value. Use insert(..,''REPLACE'') instead');
352+
isNull = nargin<3 || (header.attributes(ix).isNumeric && isnan(value)) || ...
353+
(~header.attributes(ix).isNumeric && ~ischar(value) && isempty(value));
352354

353355
switch true
354356
case isNull
@@ -358,36 +360,15 @@ function update(self, attrname, value)
358360
value = {};
359361
case header.attributes(ix).isString
360362
assert(dj.lib.isString(value), 'Value must be a string')
361-
if isempty(value)
362-
assert(header.attributes(ix).isnullable, ...
363-
'attribute `%s` is not nullable.', attrname)
364-
valueStr = 'NULL';
365-
value = {};
366-
else
367363
valueStr = '"{S}"';
368364
value = {char(value)};
369-
end
370365
case header.attributes(ix).isBlob
371-
if isempty(value) && header.attributes(ix).isnullable
372-
assert(header.attributes(ix).isnullable, ...
373-
'attribute `%s` is not nullable.', attrname)
374-
valueStr = 'NULL';
375-
value = {};
376-
else
377-
valueStr = '"{M}"';
378-
value = {value};
379-
end
366+
valueStr = '"{M}"';
367+
value = {value};
380368
case header.attributes(ix).isNumeric
381369
assert(isscalar(value) && isnumeric(value), 'Numeric value must be scalar')
382-
if isnan(value)
383-
assert(header.attributes(ix).isnullable, ...
384-
'attribute `%s` is not nullable. NaNs not allowed', attrname)
385-
valueStr = 'NULL';
386-
value = {};
387-
else
388-
valueStr = sprintf('%1.16g',value);
389-
value = {};
390-
end
370+
valueStr = sprintf('%1.16g',value);
371+
value = {};
391372
otherwise
392373
error 'invalid update command'
393374
end

+tests/TestRelationalOperand.m

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,71 @@ function TestRelationalOperand_testUpdateDate(testCase)
2121
));
2222
q = University.All & 'id=2';
2323

24-
new_value = '';
24+
new_value = [];
2525
q.update('date', new_value);
26-
testCase.verifyEqual(q.fetch1('date'), new_value);
26+
res = mym(['select date from `' testCase.PREFIX ...
27+
'_university`.`all` where id=2 and date is null;']);
28+
testCase.verifyEqual(length(res.date), 1);
2729

2830
new_value = '2020-04-14';
2931
q.update('date', new_value);
30-
testCase.verifyEqual(q.fetch1('date'), new_value);
32+
res = mym(['select date from `' testCase.PREFIX ...
33+
'_university`.`all` where id=2 and date like ''' new_value ''';']);
34+
testCase.verifyEqual(length(res.date), 1);
3135

3236
q.update('date');
33-
testCase.verifyEqual(q.fetch1('date'), '');
37+
res = mym(['select date from `' testCase.PREFIX ...
38+
'_university`.`all` where id=2 and date is null;']);
39+
testCase.verifyEqual(length(res.date), 1);
40+
end
41+
function TestRelationalOperand_testUpdateString(testCase)
42+
st = dbstack;
43+
disp(['---------------' st(1).name '---------------']);
44+
% related https://github.com/datajoint/datajoint-matlab/issues/211
45+
package = 'University';
46+
47+
c1 = dj.conn(...
48+
testCase.CONN_INFO.host,...
49+
testCase.CONN_INFO.user,...
50+
testCase.CONN_INFO.password,'',true);
51+
52+
dj.createSchema(package,[testCase.test_root '/test_schemas'], ...
53+
[testCase.PREFIX '_university']);
54+
55+
insert(University.All, struct( ...
56+
'id', 3, ...
57+
'string', 'normal' ...
58+
));
59+
q = University.All & 'id=3';
60+
61+
new_value = '';
62+
q.update('string', new_value);
63+
res = mym(['select string from `' testCase.PREFIX ...
64+
'_university`.`all` where id=3 and string like ''' new_value ''';']);
65+
testCase.verifyEqual(length(res.string), 1);
66+
67+
new_value = ' ';
68+
q.update('string', new_value);
69+
res = mym(['select string from `' testCase.PREFIX ...
70+
'_university`.`all` where id=3 and string like ''' new_value ''';']);
71+
testCase.verifyEqual(length(res.string), 1);
72+
73+
new_value = [];
74+
q.update('string', new_value);
75+
res = mym(['select string from `' testCase.PREFIX ...
76+
'_university`.`all` where id=3 and string is null;']);
77+
testCase.verifyEqual(length(res.string), 1);
78+
79+
new_value = 'diff';
80+
q.update('string', new_value);
81+
res = mym(['select string from `' testCase.PREFIX ...
82+
'_university`.`all` where id=3 and string like ''' new_value ''';']);
83+
testCase.verifyEqual(length(res.string), 1);
84+
85+
q.update('string');
86+
res = mym(['select string from `' testCase.PREFIX ...
87+
'_university`.`all` where id=3 and string is null;']);
88+
testCase.verifyEqual(length(res.string), 1);
3489
end
3590
end
3691
end

0 commit comments

Comments
 (0)