Skip to content

Commit

Permalink
Increased coverage, fixed a bug (set unique should be idempotent)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonask committed Dec 2, 2015
1 parent 5cef13d commit e859640
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/realm/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2541,15 +2541,23 @@ void Table::set_int_unique(size_t col_ndx, size_t ndx, int_fast64_t value)

if (is_nullable(col_ndx)) {
auto& col = get_column_int_null(col_ndx);
if (col.find_first(value) != not_found) {
throw LogicError{LogicError::unique_constraint_violation};
size_t found = col.find_first(value);
if (found != not_found) {
if (found == ndx)
found = col.find_first(value, found + 1);
if (found != not_found)
throw LogicError{LogicError::unique_constraint_violation};
}
col.set(ndx, value);
}
else {
auto& col = get_column(col_ndx);
if (col.find_first(value) != not_found) {
throw LogicError{LogicError::unique_constraint_violation};
size_t found = col.find_first(value);
if (found != not_found) {
if (found == ndx)
found = col.find_first(value, found + 1);
if (found != not_found)
throw LogicError{LogicError::unique_constraint_violation};
}
col.set(ndx, value);
}
Expand Down Expand Up @@ -2760,9 +2768,13 @@ void Table::set_string_unique(size_t col_ndx, size_t ndx, StringData value)
bump_version();

StringColumn& col = get_column_string(col_ndx);
if (col.find_first(value) != not_found)
throw LogicError(LogicError::unique_constraint_violation);

size_t found = col.find_first(value);
if (found != not_found) {
if (found == ndx)
found = col.find_first(value, found + 1);
if (found != not_found)
throw LogicError{LogicError::unique_constraint_violation};
}
col.set_string(ndx, value); // Throws

if (Replication* repl = get_repl())
Expand Down
2 changes: 2 additions & 0 deletions test/test_lang_bind_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2437,9 +2437,11 @@ TEST(LangBindHelper_AdvanceReadTransact_RowAccessors)
WriteTransaction wt(sg_w);
TableRef parent_w = wt.add_table("parent");
parent_w->add_column(type_Int, "a");
parent_w->add_search_index(0);
parent_w->add_empty_row(2);
parent_w->set_int(0, 0, 27);
parent_w->set_int(0, 1, 227);
parent_w->set_int_unique(0, 1, 227);
wt.commit();
}
LangBindHelper::advance_read(sg, hist);
Expand Down

0 comments on commit e859640

Please sign in to comment.