Skip to content

Commit

Permalink
[db] added random id for user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Lenz committed Mar 7, 2018
1 parent 6373604 commit c6bac7e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/database/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import database from './db';
const realm = require('realm');
const _0Schema = require('./schema/v0.js');
const _1Schema = require('./schema/v1.js');
const _2Schema = require('./schema/v2.js');
const execSync = require('child_process').execSync;

const dbPath = () => 'database/'+Math.random();
Expand Down Expand Up @@ -173,6 +174,67 @@ describe('migrate', () => {

});

//If we can open the same path without a promise rejection that mean's we succeed.
//Realm will reject the promise if we miss somethig
test('migrate from 0.3.2 -> 0.3.3', (done) => {

const dbp = dbPath();

const _032Factory = () => realm.open({
path: dbp,
schema: _1Schema.schemata,
schemaVersion: 1,
migration: _1Schema.migration
});

const _033Factory = () => realm.open({
path: dbp,
schema: _2Schema.schemata,
schemaVersion: 2,
migration: _2Schema.migration
});

_032Factory()
.then(db => {

//Persist test data
db.write(() => {
db.create('Profile', {
id: 1,
name: "Florian",
location: "germany",
latitude: "-",
longitude: "-",
description: "",
image: "base64",
version: "1.0.0",
});
});

db.close();
return _033Factory();
})
.then(db => {

const p = db.objects('Profile')[0];

expect(p.id).toEqual(1);
expect(p.name).toEqual("Florian");
expect(p.location).toEqual("germany");
expect(p.latitude).toEqual("-");
expect(p.longitude).toEqual("-");
expect(p.description).toEqual("");
expect(p.image).toEqual("base64");
expect(p.version).toEqual("1.0.0");
expect(typeof p.uid).toEqual("string");

done();

})
.catch(done.fail)

});

test('migrationtest', async () => {

const path = dbPath();
Expand Down
2 changes: 1 addition & 1 deletion src/database/schemata.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export {

// This must be bumped each time a new file is added
/** The latest schema version present in this codebase. */
export const LatestSchemaVersion : number = 1;
export const LatestSchemaVersion : number = 2;

const schemaModules = [
require('./schema/v0.js'), // Path must be hard-coded
Expand Down
3 changes: 3 additions & 0 deletions src/profile/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {PublicProfile} from '../specification/publicProfile.js';
import type {ProfileType} from '../database/schemata';
import type {EthUtilsInterface} from '../ethereum/utils';
import type {PrivateKeyType} from '../specification/privateKey';
const uString = require('unique-string');
export const PROFILE_VERSION = '1.0.0';

/**
Expand Down Expand Up @@ -53,6 +54,7 @@ export default function profileFactory(db: DBInterface, ethUtils: EthUtilsInterf
.write((realm: any) => {
// Since we only support one profile at the moment, we can just set this always to 1
profile.id = 1;
profile.uid = uString();

realm.create('Profile', profile, true);
})
Expand All @@ -78,6 +80,7 @@ export default function profileFactory(db: DBInterface, ethUtils: EthUtilsInterf
longitude: profiles[0].longitude,
image: profiles[0].image,
version: profiles[0].version,
uid: profiles[0].uid,
};

res(profile);
Expand Down

0 comments on commit c6bac7e

Please sign in to comment.