Skip to content

Commit

Permalink
Merge pull request #40 from TravisFrankMTG/tests
Browse files Browse the repository at this point in the history
Tests + Id Fix
  • Loading branch information
travismfrank authored Oct 12, 2020
2 parents 3962f07 + d211b9d commit 62b9c00
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 20 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_URI=postgres://zjxjnswd:[email protected]:5432/zjxjnswd
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
demo/dump.rdb
.DS_Store
.env
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sudo: required
services:
- docker
before_install:
- docker build -t open-source-labs/obsidian .
script:
- docker run open-source-labs/obsidian test --allow-net --allow-read --allow-env --unstable deno.test.ts
env:
global:
secure: sfaHXoKGXnAkwS/QK2pdTPC1NVqd9+pVWImEcz8W9IXFRsOcHpt9lVsmB0dFvDpVm+9KFpcBwnpfOtiyoj6Q9NGIY71jG58kYHdbcWBlR3onS7/JBvgEu94DC7HZR+rQ4/GW+ROh4avBt6RjDSuLk4qQ73Yc3+SDKAl+M0PTADlVZpkicCID59qcdynbAjXu5W8lW2Hp0hqO72Prx/8hgmchI0I7zSYcPBFSy3WaEPJa52yKesVwsHcFtzOBMrDAdE+R028AzdBAXUoiqh6cTVeLSTL1jnIWbCBtfAROlTR82cZyo4c7PJxYyqT3mhRSZvBN/3hdW7+xMOzq6gmpmcl1UO2Q5i4xXEGnatfuzMVa/8SqJZoG2IFIWZ4mvelwufHVuLgF+6JvK2BKSpjFfSUGo0p9G0bMg+GHwRipTPIq1If3ELkflAM6QJwL7TritwtWzWXfAfoZ3KALdPTiFzJAKyQfFvSwWbfXqAgqZIbLjlzSgOJ4QKWD6CBksU7b4Oky6hr/+R+ZihzQLtWKkk/8cklEG/NJlknS2vPRG8xRRF7/C+vSFPrCkmsakPc8c1iGfai8J3Vc09Pg0UeShJDWkSQ6QP165ub6LEL5nz0Qzp0CD1sSQu5re5/M5ef9V69L2pdYhEj0RaZ241DF5efzYAgLI8SvMr5TcTr06+8=
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM hayd/alpine-deno:1.3.0

EXPOSE 3000

WORKDIR /usr/app

COPY . .

CMD [ "run", "--unstable", "--allow-net", "--allow-env", "--allow-read", "testServer.ts", "-c", "tsconfig.json" ]

# deno run --unstable --allow-net --allow-env --allow-read testServer.ts -c tsconfig.json
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ Optimized for use in server-side rendered React apps built with Deno, full stack
In the server:

```javascript
import { ObsidianRouter } from 'https://deno.land/x/[email protected].0/mod.ts';
import { ObsidianRouter } from 'https://deno.land/x/[email protected].1/mod.ts';
```

In the app:
In the app:

```javascript
import { ObsidianWrapper } from 'https://deno.land/x/[email protected].0/mod.ts';
import { ObsidianWrapper } from 'https://deno.land/x/[email protected].1/clientMod.ts';
```

## Creating the Router

```javascript
import { Application, Router } from 'https://deno.land/x/[email protected]/mod.ts';
import { ObsidianRouter, gql } from 'https://deno.land/x/[email protected].0/mod.ts';
import { ObsidianRouter, gql } from 'https://deno.land/x/[email protected].1/mod.ts';

const PORT = 8000;

Expand Down Expand Up @@ -131,7 +131,7 @@ app.use(router.routes(), router.allowedMethods());
## Creating the Wrapper

```javascript
import { ObsidianWrapper } from 'https://deno.land/x/[email protected].0/mod.ts';
import { ObsidianWrapper } from 'https://deno.land/x/[email protected].1/clientMod.ts';

const App = () => {
return (
Expand All @@ -145,7 +145,7 @@ const App = () => {
## Making a Query

```javascript
import { useObsidian } from 'https://deno.land/x/[email protected].0/mod.ts';
import { useObsidian } from 'https://deno.land/x/[email protected].1/clientMod.ts';

const WeatherApp = () => {
const { gather } = useObsidian();
Expand All @@ -163,11 +163,15 @@ const WeatherApp = () => {
};
```

## Documentation

[obsidian.land](http://obsidian.land)

## Authors

*Lascaux* Engineers

[Alonso Garza](https://github.com/Alonsog66)
[Burak Caliskan](https://github.com/CaliskanBurak)
[Matt Meigs](https://github.com/mmeigs)
[Travis Frank](https://github.com/TravisFrankMTG/)
[Travis Frank](https://github.com/TravisFrankMTG/)
7 changes: 7 additions & 0 deletions clientMod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {
ObsidianWrapper,
useObsidian,
ObsidianClient,
} from './ObsidianWrapper/ObsidianWrapper.jsx';

export { ObsidianWrapper, useObsidian, ObsidianClient };
14 changes: 14 additions & 0 deletions deno.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { superoak } from 'https://deno.land/x/[email protected]/mod.ts';
import { describe, it } from 'https://deno.land/x/[email protected]/test/utils.ts';
import { expect } from 'https://deno.land/x/[email protected]/test/deps.ts';
import { app } from './testServer.ts';

describe('GET request to root url', () => {
it('Sends 200 Status and Content Type text/html', async (done) => {
(await superoak(app)).get('/').end((err, res) => {
expect(res.status).toEqual(200);
expect(res.type).toEqual('text/html');
done();
});
});
});
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3.8"

services:
site:
build: .
restart: always
volumes:
- ./:/usr/app
ports:
- 3000:3000
Binary file added dump.rdb
Binary file not shown.
107 changes: 107 additions & 0 deletions localObsidian.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { superoak } from 'https://deno.land/x/[email protected]/mod.ts';
import { describe, it } from 'https://deno.land/x/[email protected]/test/utils.ts';
import { expect } from 'https://deno.land/x/[email protected]/test/deps.ts';
import { app } from './testServer.ts';

describe('GraphQL query response testing', () => {
it('getBook query succeeds', async (done: any) => {
(await superoak(app))
.post('/graphql')
.send({ query: '{getBook(id:1){ id title author }}' })
.end((err: any, res: any) => {
// console.log(res);
expect(res.status).toEqual(200);
expect(res.body.data.getBook.id).toEqual('1');
expect(res.body.data.getBook.title).toEqual('Lets Go');
expect(res.body.data.getBook.author).toEqual('NotJehooo');

setTimeout(async () => {
if (true) {
// @ts-ignore
const dbOps = await import('./src/dbOps.js');
dbOps.redis.close();
}
done();
}, 2000);
});
});

it('Invalid getBook query fails', async (done) => {
(await superoak(app))
.post('/graphql')
.send({ query: '{getBook{ id title author }}' })
.end((err, res) => {
// console.log('error', err);
// console.log(res);
expect(res.status).toEqual(200);
expect(res.body.errors).toBeTruthy();
setTimeout(async () => {
if (true) {
// @ts-ignore
const dbOps = await import('./src/dbOps.js');
dbOps.redis.close();
}
done();
}, 2000);
});
});

it('getEightBooks succeeds', async (done) => {
(await superoak(app))
.post('/graphql')
.send({ query: '{getEightBooks(id:1){ id title author }}' })
.end((err, res) => {
// console.log(res);
expect(res.status).toEqual(200);
expect(res.body.data.getEightBooks).toHaveLength(8);
setTimeout(async () => {
if (true) {
// @ts-ignore
const dbOps = await import('./src/dbOps.js');
dbOps.redis.close();
}
done();
}, 2000);
});
});
});

describe('Redis cache testing', () => {
it('second query takes less than half time of first', async (done) => {
let firstResTime: number;
(await superoak(app))
.post('/graphql')
.send({ query: '{getBook(id:2){ id title author }}' })
.end((err, res) => {
// console.log(res);
firstResTime = Number(res.header['x-response-time'].slice(0, -2));
});

setTimeout(async () => {
(await superoak(app))
.post('/graphql')
.send({ query: '{getBook(id:2){ id title author }}' })
.end((err, res) => {
const newTime: number = Number(res.header['x-response-time'].slice(0, -2));

console.log('firstTime', firstResTime);
console.log('newTime', newTime);

expect(newTime < firstResTime).toBeTruthy();
setTimeout(async () => {
if (true) {
// @ts-ignore
const dbOps = await import('./src/dbOps.js');
dbOps.redis.close();
}
done();
}, 1000);
});
if (true) {
// @ts-ignore
const dbOps = await import('./src/dbOps.js');
dbOps.redis.close();
}
}, 1000);
});
});
7 changes: 1 addition & 6 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
ObsidianWrapper,
useObsidian,
ObsidianClient,
} from './ObsidianWrapper/ObsidianWrapper.jsx';
import { ObsidianRouter } from './src/obsidian.ts';
import gql from 'https://deno.land/x/[email protected]/graphql-tag/index.ts';

export { ObsidianWrapper, useObsidian, ObsidianClient, ObsidianRouter, gql };
export { ObsidianRouter, gql };
11 changes: 11 additions & 0 deletions sqlclient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Client } from "https://deno.land/x/postgres/mod.ts";
import { config } from "https://deno.land/x/dotenv/mod.ts";

// Create a new client
const uri = config().POSTGRES_URI;
const client = new Client(uri);

// Connect it
await client.connect();

export default client;
7 changes: 3 additions & 4 deletions src/dbOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ async function checkAndRetrieveQuery(hash, cache) {
if (browser) {
return cache[hash];
} else {
if (!redis) {
redis = await connectFunc(browser);
}
if (!redis || redis.isClosed) redis = await connectFunc(browser);
let ifCached = await redis.get(hash);
if (ifCached) ifCached = JSON.parse(ifCached);
return ifCached;
Expand Down Expand Up @@ -118,5 +116,6 @@ export {
checkAndRetrieveQuery,
retrieveScalar,
retrieveComplex,
clearRedis
clearRedis,
redis
}
19 changes: 16 additions & 3 deletions src/destructureQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,21 @@ async function buildResultsObject(hashes, obsidianSchema, queryObj, cache) {

// If have not created an object at this id, make one //
if (!queryResult[id]) {
queryResult[id] = {
id
queryResult[id] = {}

// Really inelegant solution to ensuring the id remains in the correct format //
if (obsidianSchema.obsidianTypeSchema[typeSchemaName].id) {
queryResult[id].id = id;
} else if (obsidianSchema.obsidianTypeSchema[typeSchemaName]._id) {
queryResult[id]._id = id;
} else if (obsidianSchema.obsidianTypeSchema[typeSchemaName].ID) {
queryResult[id].ID = id;
} else if (obsidianSchema.obsidianTypeSchema[typeSchemaName]._ID) {
queryResult[id]._ID = id;
} else if (obsidianSchema.obsidianTypeSchema[typeSchemaName].Id) {
queryResult[id].Id = id;
} else if (obsidianSchema.obsidianTypeSchema[typeSchemaName]._Id) {
queryResult[id]._Id = id;
}
}

Expand All @@ -71,7 +84,7 @@ async function buildResultsObject(hashes, obsidianSchema, queryObj, cache) {
} else {
propVal = await retrieveScalar(hashes[j], cache);
if (propVal && propVal.slice(1, 5) === 'null') propVal = null;
}
}
queryResult[id][property] = propVal;
// If this field is complex, recursive call to build nested property object //
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { checkAndInsert } from './dbOps.js';
// Normalizes responses from GraphQL queries //
export default async function normalizeResult(query, result, obsidianSchema, cache) {

if (!result.data) return;

const { returnTypes, obsidianTypeSchema } = obsidianSchema;

const specificQueryArray = Object.keys(result.data);
Expand Down
Loading

0 comments on commit 62b9c00

Please sign in to comment.