Skip to content

Commit

Permalink
Pull specs from N1
Browse files Browse the repository at this point in the history
  • Loading branch information
bengotow committed Oct 15, 2016
1 parent c2ac358 commit f794f26
Show file tree
Hide file tree
Showing 24 changed files with 2,530 additions and 28 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["electron", "react"],
"sourceMaps": "inline"
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
/node_modules
/example/sqlite.db-wal
/example/sqlite.db-shm
/sqlite-test.db-wal
/sqlite-test.db-shm
/sqlite-test.db
26 changes: 0 additions & 26 deletions npm-debug.log

This file was deleted.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
"dependencies": {
"better-sqlite3": "bengotow/better-sqlite3#30297b0c9448379dd8d803b860351218c5ee3035",
"lru-cache": "^4.0.1",
"node-gyp": "^3.4.0",
"promise-queue": "^2.2.3",
"promise.prototype.finally": "^2.0.1",
"rx-lite": "^4.0.8"
},
"devDependencies": {},
"devDependencies": {
"jasmine": "^2.5.2",
"electron-prebuilt-compile": "^1.4.2",
"babel-preset-electron": "^0.37.8",
"babel-preset-es2016-node5": "^1.1.2",
"babel-preset-react": "^6.16.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "electron ./spec/runner --enable-logging",
"postinstall": "node ./post-install.js"
},
"author": "",
"license": "ISC"
Expand Down
13 changes: 13 additions & 0 deletions post-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var execSync = require('child_process').execSync;
var nodeGypPath = '"' + require('path').resolve(__dirname, 'node_modules', '.bin', 'node-gyp') + '"';
var targetElectronVersion = '1.4.3';
var targetArch = require('os').arch();
var targetPlatform = process.platform;

if (targetPlatform == "win32") {
var targetArch = "ia32"
}

var cmd = "cd node_modules/better-sqlite3 && "+nodeGypPath+" configure rebuild --msvs_version=2013 --target="+targetElectronVersion+" --arch="+targetArch+" --target_platform="+targetPlatform+" --dist-url=https://atom.io/download/atom-shell";
console.log(cmd);
execSync(cmd);
39 changes: 39 additions & 0 deletions spec/database-object-registry-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint quote-props: 0 */
import Model from '../lib/model';
import Attributes from '../lib/attributes';
import DatabaseObjectRegistry from '../lib/database-object-registry';

class GoodTest extends Model {
static attributes = Object.assign({}, Model.attributes, {
"foo": Attributes.String({
modelKey: 'foo',
jsonKey: 'foo',
}),
});
}

describe('DatabaseObjectRegistry', function DatabaseObjectRegistrySpecs() {
beforeEach(() => DatabaseObjectRegistry.unregister("GoodTest"));

it("can register constructors", () => {
const testFn = () => GoodTest;
expect(() => DatabaseObjectRegistry.register("GoodTest", testFn)).not.toThrow();
expect(DatabaseObjectRegistry.get("GoodTest")).toBe(GoodTest);
});

it("Tests if a constructor is in the registry", () => {
DatabaseObjectRegistry.register("GoodTest", () => GoodTest);
expect(DatabaseObjectRegistry.isInRegistry("GoodTest")).toBe(true);
});

it("deserializes the objects for a constructor", () => {
DatabaseObjectRegistry.register("GoodTest", () => GoodTest);
const obj = DatabaseObjectRegistry.deserialize("GoodTest", {foo: "bar"});
expect(obj instanceof GoodTest).toBe(true);
expect(obj.foo).toBe("bar");
});

it("throws an error if the object can't be deserialized", () =>
expect(() => DatabaseObjectRegistry.deserialize("GoodTest", {foo: "bar"})).toThrow()
);
});
72 changes: 72 additions & 0 deletions spec/database-setup-query-builder-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint quote-props: 0 */
import TestModel from './fixtures/db-test-model';
import Attributes from '../lib/attributes';
import DatabaseSetupQueryBuilder from '../lib/database-setup-query-builder';

describe("DatabaseSetupQueryBuilder", function DatabaseSetupQueryBuilderSpecs() {
beforeEach(() => {
this.builder = new DatabaseSetupQueryBuilder();
});

describe("setupQueriesForTable", () => {
it("should return the queries for creating the table and the primary unique index", () => {
TestModel.attributes = {
'attrQueryable': Attributes.DateTime({
queryable: true,
modelKey: 'attrQueryable',
jsonKey: 'attr_queryable',
}),

'attrNonQueryable': Attributes.Collection({
modelKey: 'attrNonQueryable',
jsonKey: 'attr_non_queryable',
}),
};
const queries = this.builder.setupQueriesForTable(TestModel);
const expected = [
'CREATE TABLE IF NOT EXISTS `TestModel` (id TEXT PRIMARY KEY,data BLOB,attr_queryable INTEGER)',
'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_id` ON `TestModel` (`id`)',
];
queries.map((query, i) =>
expect(query).toBe(expected[i])
);
});

it("should correctly create join tables for models that have queryable collections", () => {
TestModel.configureWithCollectionAttribute();
const queries = this.builder.setupQueriesForTable(TestModel);
const expected = [
'CREATE TABLE IF NOT EXISTS `TestModel` (id TEXT PRIMARY KEY,data BLOB,client_id TEXT,server_id TEXT,other TEXT)',
'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_id` ON `TestModel` (`id`)',
'CREATE TABLE IF NOT EXISTS `TestModelCategory` (id TEXT KEY,`value` TEXT,other TEXT)',
'CREATE INDEX IF NOT EXISTS `TestModelCategory_id` ON `TestModelCategory` (`id` ASC)',
'CREATE UNIQUE INDEX IF NOT EXISTS `TestModelCategory_val_id` ON `TestModelCategory` (`value` ASC, `id` ASC)',
];
queries.map((query, i) =>
expect(query).toBe(expected[i])
);
});

it("should use the correct column type for each attribute", () => {
TestModel.configureWithAllAttributes();
const queries = this.builder.setupQueriesForTable(TestModel);
expect(queries[0]).toBe('CREATE TABLE IF NOT EXISTS `TestModel` (id TEXT PRIMARY KEY,data BLOB,datetime INTEGER,string-json-key TEXT,boolean INTEGER,number INTEGER)');
});

describe("when the model provides additional sqlite config", () => {
it("the setup method should return these queries", () => {
TestModel.configureWithAdditionalSQLiteConfig();
spyOn(TestModel.additionalSQLiteConfig, 'setup').andCallThrough();
const queries = this.builder.setupQueriesForTable(TestModel);
expect(TestModel.additionalSQLiteConfig.setup).toHaveBeenCalledWith();
expect(queries.pop()).toBe('CREATE INDEX IF NOT EXISTS ThreadListIndex ON Thread(last_message_received_timestamp DESC, account_id, id)');
});

it("should not fail if additional config is present, but setup is undefined", () => {
delete TestModel.additionalSQLiteConfig.setup;
this.m = new TestModel({id: 'local-6806434c-b0cd', body: 'hello world'});
expect(() => this.builder.setupQueriesForTable(TestModel)).not.toThrow();
});
});
});
});
Loading

0 comments on commit f794f26

Please sign in to comment.