Skip to content

Commit

Permalink
Fix field compare bug, remove some debug output
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Cassidy <[email protected]>
  • Loading branch information
stevecassidy committed Oct 22, 2023
1 parent aaf2a98 commit cf0bdc3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "faims3-datamodel",
"version": "1.0.3",
"version": "1.0.4",
"description": "Database access layer for FAIMS3",
"main": "./build/src/index.js",
"exports": {
Expand Down
6 changes: 3 additions & 3 deletions src/data_storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ export async function upsertFAIMSData(
}
const revision_id = generateFAIMSRevisionID();
if (record.revision_id === null) {
//console.info('New record', record);
// console.info('New record', record);
await createNewRecord(project_id, record, revision_id);
await addNewRevisionFromForm(project_id, record, revision_id);
} else {
//console.info('Update existing record', record);
// console.info('Update existing record', record);
await addNewRevisionFromForm(project_id, record, revision_id);
await updateHeads(
project_id,
Expand Down Expand Up @@ -431,7 +431,7 @@ async function filterRecordMetadata(
filter_deleted: boolean
): Promise<RecordMetadata[]> {
const new_record_list: RecordMetadata[] = [];
for (const metadata of record_list) {
for (const metadata of record_list) {
if (
!(metadata.deleted && filter_deleted) &&
(await shouldDisplayRecord(project_id, metadata))
Expand Down
15 changes: 0 additions & 15 deletions src/data_storage/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ export async function getHRID(
}

if (hrid_name === null) {
console.warn('No HRID field found');
return null;
}
const hrid_avp_id = revision.avps[hrid_name];
Expand Down Expand Up @@ -252,19 +251,12 @@ export async function listRecordMetadata(
const revision = revisions[revision_id];
if (revision === undefined) {
// We don't have that revision, so skip
console.warn(
'Unable to find revision',
project_id,
record_id,
revision_id
);
continue;
}
const hrid =
revision === undefined
? record_id
: (await getHRID(project_id, revision)) ?? record_id;
console.debug('hrid:', hrid);

out[record_id] = {
project_id: project_id,
Expand All @@ -281,10 +273,8 @@ export async function listRecordMetadata(
relationship: revision.relationship,
};
}
console.debug('Record metadata list', out);
return out;
} catch (err) {
console.warn('Failed to get metadata', err);
throw Error('failed to get metadata');
}
}
Expand Down Expand Up @@ -469,11 +459,6 @@ async function addNewAttributeValuePairs(
docs_to_dump.push(...dumpAttributeValuePair(new_avp));
avp_map[field_name] = new_avp_id;
} else {
console.debug(
'Using existing AVP, the following are equal',
stored_data,
field_value
);
if (revision.avps !== undefined) {
avp_map[field_name] = revision.avps[field_name];
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/datamodel/typesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Description:
* TODO
*/
import {isEqual} from 'lodash';
import {isEqual, isNull} from 'lodash';
import {logError} from '../logging';
import {AttributeValuePair, FAIMSAttachment} from '../types';

Expand Down Expand Up @@ -86,7 +86,7 @@ export function setAttachmentDumperForType(
// browser and node environments
// use duck typing instead, blobs have 'size' and 'type' properties
function isAttachment(a: any): boolean {
return a.size !== undefined && a.type !== undefined;
return !isNull(a) && a.size !== undefined && a.type !== undefined;
}

/*
Expand Down
74 changes: 74 additions & 0 deletions tests/data_storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,80 @@ describe('round-trip reading and writing to db', () => {
);
});

test('updating an existing record with extended data', async () => {
try {
await cleanDataDBS();
} catch (err) {
fail('Failed to clean dbs');
}

const project_id = 'test';
const record_id = generateFAIMSDataID();
const fullType = 'test::test';
const data = {
name: 'Bob Bobalooba',
age: 42,
};
const fieldTypes = {
name: 'faims::string',
age: 'faims::integer',
};
const new_data = {
name: 'Bob Bobalooba',
age: 54,
occupation: 'Software Engineer',
};
const new_field_types = {
name: 'faims::string',
age: 'faims::integer',
occupation: 'faims::string',
};
const userID = 'user';
const time = new Date();

const doc: Record = {
project_id: project_id,
record_id: record_id,
revision_id: null,
type: fullType,
data: data,
created_by: userID,
updated_by: userID,
created: time,
updated: time,
annotations: {},
field_types: fieldTypes,
relationship: undefined,
deleted: false,
};

return upsertFAIMSData(project_id, doc)
.then(revision_id => {
return getFullRecordData(project_id, record_id, revision_id);
})
.then(result => {
if (result) {
expect(result.data).toEqual(data);
// now we change the data and update
result.data = new_data;
result.field_types = new_field_types;
return upsertFAIMSData(project_id, result)
.then(new_revision_id => {
return getFullRecordData(project_id, record_id, new_revision_id);
})
.then(result => {
if (result) {
expect(result.data).toEqual(new_data);
} else {
fail('Failed to get record');
}
});
} else {
fail('Failed to get record');
}
});
});

describe('CRUD for data', () => {
test.prop([
fc.fullUnicodeString(), // project_id
Expand Down

0 comments on commit cf0bdc3

Please sign in to comment.