Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

refactor: rename $contractId to $dataContractId in Document #158

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/document/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Document {
delete data.$type;
}

if (Object.prototype.hasOwnProperty.call(rawDocument, '$contractId')) {
this.contractId = rawDocument.$contractId;
delete data.$contractId;
if (Object.prototype.hasOwnProperty.call(rawDocument, '$dataContractId')) {
this.dataContractId = rawDocument.$dataContractId;
delete data.$dataContractId;
}

if (Object.prototype.hasOwnProperty.call(rawDocument, '$ownerId')) {
Expand Down Expand Up @@ -65,7 +65,7 @@ class Document {
* @return {string}
*/
getDataContractId() {
return this.contractId;
return this.dataContractId;
}

/**
Expand Down Expand Up @@ -172,7 +172,7 @@ class Document {
return {
$id: this.getId(),
$type: this.getType(),
$contractId: this.getDataContractId(),
$dataContractId: this.getDataContractId(),
$ownerId: this.getOwnerId(),
$revision: this.getRevision(),
...this.getData(),
Expand Down Expand Up @@ -204,7 +204,7 @@ class Document {
* @typedef {Object} RawDocument
* @property {string} $id
* @property {string} $type
* @property {string} $contractId
* @property {string} $dataContractId
* @property {string} $ownerId
* @property {number} $revision
*/
Expand Down
6 changes: 3 additions & 3 deletions lib/document/DocumentFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class DocumentFactory {
}

const documentEntropy = entropy.generate();
const contractId = dataContract.getId();
const dataContractId = dataContract.getId();

const id = generateDocumentId(
contractId,
dataContractId,
ownerId,
type,
documentEntropy,
Expand All @@ -57,7 +57,7 @@ class DocumentFactory {
const rawDocument = {
$id: id,
$type: type,
$contractId: contractId,
$dataContractId: dataContractId,
$ownerId: ownerId,
$revision: DocumentCreateTransition.INITIAL_REVISION,
...data,
Expand Down
4 changes: 2 additions & 2 deletions lib/document/fetchAndValidateDataContractFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function fetchAndValidateDataContractFactory(stateRepository) {

const result = new ValidationResult();

if (!Object.prototype.hasOwnProperty.call(rawDocument, '$contractId')) {
if (!Object.prototype.hasOwnProperty.call(rawDocument, '$dataContractId')) {
result.addError(
new MissingDocumentContractIdError(rawDocument),
);
Expand All @@ -30,7 +30,7 @@ function fetchAndValidateDataContractFactory(stateRepository) {
return result;
}

const dataContractId = rawDocument.$contractId;
const dataContractId = rawDocument.$dataContractId;

const dataContract = await stateRepository.fetchDataContract(dataContractId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function applyDocumentsBatchTransitionFactory(
const newDocument = new Document({
$id: documentTransition.getId(),
$type: documentTransition.getType(),
$contractId: stateTransition.getDataContractId(),
$dataContractId: stateTransition.getDataContractId(),
$ownerId: stateTransition.getOwnerId(),
...documentTransition.getData(),
});
Expand Down
2 changes: 1 addition & 1 deletion lib/document/validateDocumentFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = function validateDocumentFactory(
return result;
}

if (rawDocument.$contractId !== dataContract.getId()) {
if (rawDocument.$dataContractId !== dataContract.getId()) {
result.addError(
new MismatchDocumentContractIdAndDataContractError(rawDocument, dataContract),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/MissingDocumentContractIdError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const ConsensusError = require('./ConsensusError');

class MissingDocumentContractIdError extends ConsensusError {
constructor(rawDocument) {
super('$contractId is not present');
super('$dataContractId is not present');

this.rawDocument = rawDocument;
}
Expand Down
4 changes: 2 additions & 2 deletions schema/document/documentBase.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"multipleOf": 1.0,
"minimum": 1
},
"$contractId": {
"$dataContractId": {
"type": "string",
"minLength": 42,
"maxLength": 44,
Expand All @@ -33,7 +33,7 @@
"$id",
"$type",
"$revision",
"$contractId",
"$dataContractId",
"$ownerId"
],
"additionalProperties": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('executeDataTriggersFactory', () => {
});

it("should call only one trigger if there's one document with a trigger and one without", async () => {
childDocument.contractId = getDocumentsFixture.dataContract.getId();
childDocument.dataContractId = getDocumentsFixture.dataContract.getId();
childDocument.ownerId = getDocumentsFixture.ownerId;

documentTransitions = getDocumentTransitionsFixture({
Expand Down
24 changes: 12 additions & 12 deletions test/integration/document/validateDocumentFactory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ describe('validateDocumentFactory', () => {
});
});

describe('$contractId', () => {
describe('$dataContractId', () => {
it('should be present', () => {
delete rawDocument.$contractId;
delete rawDocument.$dataContractId;

const result = validateDocument(rawDocument, dataContract);

Expand All @@ -229,50 +229,50 @@ describe('validateDocumentFactory', () => {

expect(error.dataPath).to.equal('');
expect(error.keyword).to.equal('required');
expect(error.params.missingProperty).to.equal('$contractId');
expect(error.params.missingProperty).to.equal('$dataContractId');
});

it('should be a string', () => {
rawDocument.$contractId = 1;
rawDocument.$dataContractId = 1;

const result = validateDocument(rawDocument, dataContract);

expectJsonSchemaError(result);

const [error] = result.getErrors();

expect(error.dataPath).to.equal('.$contractId');
expect(error.dataPath).to.equal('.$dataContractId');
expect(error.keyword).to.equal('type');
});

it('should be no less than 42 chars', () => {
rawDocument.$contractId = '1'.repeat(41);
rawDocument.$dataContractId = '1'.repeat(41);

const result = validateDocument(rawDocument, dataContract);

expectJsonSchemaError(result);

const [error] = result.getErrors();

expect(error.dataPath).to.equal('.$contractId');
expect(error.dataPath).to.equal('.$dataContractId');
expect(error.keyword).to.equal('minLength');
});

it('should be no longer than 44 chars', () => {
rawDocument.$contractId = '1'.repeat(45);
rawDocument.$dataContractId = '1'.repeat(45);

const result = validateDocument(rawDocument, dataContract);

expectJsonSchemaError(result);

const [error] = result.getErrors();

expect(error.dataPath).to.equal('.$contractId');
expect(error.dataPath).to.equal('.$dataContractId');
expect(error.keyword).to.equal('maxLength');
});

it('should be base58 encoded', () => {
rawDocument.$contractId = '&'.repeat(44);
rawDocument.$dataContractId = '&'.repeat(44);

const result = validateDocument(rawDocument, dataContract);

Expand All @@ -281,7 +281,7 @@ describe('validateDocumentFactory', () => {
const [error] = result.getErrors();

expect(error.keyword).to.equal('pattern');
expect(error.dataPath).to.equal('.$contractId');
expect(error.dataPath).to.equal('.$dataContractId');
});
});

Expand Down Expand Up @@ -383,7 +383,7 @@ describe('validateDocumentFactory', () => {
});

it('should return invalid result if a document contractId is not equal to Data Contract ID', () => {
rawDocument.$contractId = generateRandomId();
rawDocument.$dataContractId = generateRandomId();

const result = validateDocument(
rawDocument,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/dataContract/DataContract.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('DataContract', () => {
});

describe('#getJsonSchemaId', () => {
it('should return JSON Schema $contractId', () => {
it('should return JSON Schema ID', () => {
const hashed = Buffer.from(ownerId + entropy);
hashMock.returns(hashed);

Expand Down
12 changes: 6 additions & 6 deletions test/unit/document/Document.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Document', () => {
rawDocument = {
$id: 'D3AT6rBtyTqx3hXFckwtP81ncu49y5ndE7ot9JkuNSeB',
$type: 'test',
$contractId: generateRandomId(),
$dataContractId: generateRandomId(),
$ownerId: generateRandomId(),
$revision: DocumentCreateTransition.INITIAL_REVISION,
};
Expand Down Expand Up @@ -75,19 +75,19 @@ describe('Document', () => {
expect(Document.prototype.setData).to.have.been.calledOnceWith(data);
});

it('should create Document with $contractId and data if present', () => {
it('should create Document with $dataContractId and data if present', () => {
const data = {
test: 1,
};

rawDocument = {
$contractId: generateRandomId(),
$dataContractId: generateRandomId(),
...data,
};

document = new Document(rawDocument);

expect(document.contractId).to.equal(rawDocument.$contractId);
expect(document.dataContractId).to.equal(rawDocument.$dataContractId);
expect(Document.prototype.setData).to.have.been.calledOnceWith(data);
});

Expand Down Expand Up @@ -165,8 +165,8 @@ describe('Document', () => {
});
});

describe('#getContractId', () => {
it('should return $contractId', () => {
describe('#getDataContractId', () => {
it('should return $dataContractId', () => {
expect(document.getOwnerId()).to.equal(rawDocument.$ownerId);
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/unit/document/DocumentFactory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ describe('DocumentFactory', () => {

expect(newDocument.get('name')).to.equal(name);

expect(newDocument.contractId).to.equal(contractId);
expect(newDocument.ownerId).to.equal(ownerId);
expect(newDocument.getDataContractId()).to.equal(contractId);
expect(newDocument.getOwnerId()).to.equal(ownerId);

expect(generateMock).to.have.been.calledOnce();
expect(newDocument.entropy).to.equal(entropy);
expect(newDocument.getEntropy()).to.equal(entropy);

expect(newDocument.getRevision()).to.equal(DocumentCreateTransition.INITIAL_REVISION);

Expand Down Expand Up @@ -262,7 +262,7 @@ describe('DocumentFactory', () => {
});

it('should throw and error if documents have mixed contract ids', () => {
documents[0].contractId = generateRandomId();
documents[0].dataContractId = generateRandomId();
try {
factory.createStateTransition({
create: documents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ describe('fetchAndValidateDataContractFactory', () => {
);
});

it('should return with invalid result if $contractId is not present', async () => {
it('should return with invalid result if $dataContractId is not present', async () => {
const rawDocument = document.toJSON();

delete rawDocument.$contractId;
delete rawDocument.$dataContractId;

const result = await fetchAndValidateDataContract(rawDocument);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('DocumentsBatchTransition', () => {
expect(stateTransition.toJSON()).to.deep.equal({
protocolVersion: 0,
type: stateTransitionTypes.DOCUMENTS_BATCH,
contractId: documents[0].contractId,
ownerId: documents[0].ownerId,
contractId: documents[0].getDataContractId(),
ownerId: documents[0].getOwnerId(),
transitions: stateTransition.getTransitions().map((d) => d.toJSON()),
signaturePublicKeyId: null,
signature: null,
Expand Down