Skip to content

Commit

Permalink
[eth] nations - added updated draft nation
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Lenz committed Feb 7, 2018
1 parent 89d3f0b commit 33b7178
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 3 deletions.
39 changes: 38 additions & 1 deletion src/ethereum/nation.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export interface NationInterface {
index() : Promise<void>,
joinNation(id: number) : Promise<void>,
leaveNation(id: number) : Promise<void>,
saveDraft(nation: NationInputType) : Promise<string>
saveDraft(nation: NationInputType) : Promise<string>,
updateDraft(nationId: number, nation: NationInputType) : Promise<string>
}

/**
Expand All @@ -62,6 +63,42 @@ export interface NationInterface {
*/
export default function(db: DBInterface, txQueue: TransactionQueueInterface, web3: Web3, ee: EventEmitter, nationContract: {...any}) {
const impl:NationInterface = {
updateDraft: (nationId: number, nation: NationInputType): Promise<string> => new Promise((res, rej) => {
db
.query((realm) => realm.objects('Nation').filtered(`id = "${nationId}"`))
.then((nations: Array<NationType>) => {
if (nations.length <= 0) {
return rej('system_error.nation.does_not_exist');
}

// it in smart contract is only >= 0 when the nation was written to the blockchain
// @todo we really really need to check for if the nation was already submitted. But we need to have the tx queue thing.
if (nations[0].idInSmartContract >= 0) {
return rej('system_error.nation.already_submitted');
}

db
.write((realm) => {
realm.create('Nation', {
id: nationId,
created: false,
nationName: nation.nationName,
nationDescription: nation.nationDescription,
exists: nation.exists,
virtualNation: nation.virtualNation,
nationCode: nation.nationCode,
lawEnforcementMechanism: nation.lawEnforcementMechanism,
profit: nation.profit,
nonCitizenUse: nation.nonCitizenUse,
diplomaticRecognition: nation.diplomaticRecognition,
decisionMakingProcess: nation.decisionMakingProcess,
governanceService: nation.governanceService,
}, true);
})
.then((_) => res('nation.draft.updated_successfully'))
.catch((_) => rej('system_error.db_write_failed'));
});
}),
saveDraft: (nation: NationInputType): Promise<string> => new Promise((res, rej) => {
db
.write((realm) => {
Expand Down
119 changes: 117 additions & 2 deletions src/ethereum/nation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ describe('nation', () => {
test('success', (done) => {
const db = dbFactory(randomPath());

const nations = nationsFactory(dbFactory(randomPath()), null, null, null, null);
const nations = nationsFactory(db, null, null, null, null);

const nationData = {
nationName: 'Bitnation',
Expand Down Expand Up @@ -399,7 +399,7 @@ describe('nation', () => {
test('fail', (done) => {
const db = dbFactory(randomPath());

const nations = nationsFactory(dbFactory(randomPath()), null, null, null, null);
const nations = nationsFactory(db, null, null, null, null);

const nationData = {};

Expand All @@ -417,6 +417,121 @@ describe('nation', () => {
.catch((response) => {
expect(response).toBe('nation.draft.saved_failed');

done();
});
});
});
describe('updateDraft', () => {
test('no nation found', (done) => {
const db = dbFactory(randomPath());

const nations = nationsFactory(db, null, null, null, null);

nations
.updateDraft(1, {})
.catch((e) => {
expect(e).toBe('system_error.nation.does_not_exist');

done();
});
});

test('updated successfully', (done) => {
const db = dbFactory(randomPath());

const nationService = nationsFactory(db, null, null, null, null);

const nationData = {
nationName: 'Bitnation',
nationDescription: 'We <3 cryptography',
exists: true,
virtualNation: false,
nationCode: 'Code civil',
lawEnforcementMechanism: 'xyz',
profit: true,
nonCitizenUse: false,
diplomaticRecognition: false,
decisionMakingProcess: 'dictatorship',
governanceService: 'Security',
};

nationService
// Persist nations
.saveDraft(nationData)
// Get all nations
.then((_) => db.query((realm) => realm.objects('Nation')))
.then((nations) => {
// Assert that nation exist with passed in data
// parse / stringify is done since the result is of class realm obj - I need a plain obj
expect(JSON.parse(JSON.stringify(nations[0]))).toEqual({
nationName: 'Bitnation',
nationDescription: 'We <3 cryptography',
exists: true,
virtualNation: false,
nationCode: 'Code civil',
lawEnforcementMechanism: 'xyz',
profit: true,
nonCitizenUse: false,
diplomaticRecognition: false,
decisionMakingProcess: 'dictatorship',
governanceService: 'Security',
created: false,
citizens: 0,
id: 1,
idInSmartContract: -1,
joined: false,
txHash: null,
});

nationData.nationName = 'updated nation name';

// Updated nation data again
return nationService.updateDraft(1, nationData);
})
// Assert updated transaction key
.then((response) => {
expect(response).toBe('nation.draft.updated_successfully');

// Fetch all the nation's
return db.query((realm) => realm.objects('Nation'));
})
.then((nations) => {
// Since we only perform an update - the amount of nation's should be 1
expect(nations.length).toBe(1);
expect(nations[0].nationName).toEqual('updated nation name');
done();
})
.catch(done.fail);
});
test('nation update (db error)', (done) => {
const db = dbFactory(randomPath());

const nations = nationsFactory(db, null, null, null, null);

const nationData = {
nationName: 'Bitnation',
nationDescription: 'We <3 cryptography',
exists: true,
virtualNation: false,
nationCode: 'Code civil',
lawEnforcementMechanism: 'xyz',
profit: true,
nonCitizenUse: false,
diplomaticRecognition: false,
decisionMakingProcess: 'dictatorship',
governanceService: 'Security',
};

nations
// Create nation
.saveDraft(nationData)
.then((_) => nations.updateDraft(1, {
// Update nationName with wrong value type - which result's in rejection
nationName: 3,
}))
.catch((e) => {
expect(e).toBe('system_error.db_write_failed');

done();
});
});
Expand Down

0 comments on commit 33b7178

Please sign in to comment.