Skip to content

Commit

Permalink
Merge pull request #1589 from demergent-labs/fs_tests
Browse files Browse the repository at this point in the history
Fs tests
  • Loading branch information
lastmjs authored Jan 30, 2024
2 parents ad93ee8 + f28d82d commit ee87d86
Show file tree
Hide file tree
Showing 27 changed files with 7,308 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ jobs:
"examples/date",
"examples/ethereum_json_rpc",
"examples/express",
"examples/fs",
"examples/func_types",
"examples/guard_functions",
"examples/heartbeat",
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/apollo_server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.azle
.dfx
node_modules
12 changes: 12 additions & 0 deletions examples/apollo_server/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"canisters": {
"apollo_server": {
"type": "custom",
"main": "src/index.ts",
"candid": "src/index.did",
"build": "npx azle apollo_server",
"wasm": ".azle/apollo_server/apollo_server.wasm",
"gzip": true
}
}
}
3,707 changes: 3,707 additions & 0 deletions examples/apollo_server/package-lock.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions examples/apollo_server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"scripts": {
"pretest": "ts-node --transpile-only --ignore=false test/pretest.ts",
"test": "ts-node --transpile-only --ignore=false test/test.ts"
},
"dependencies": {
"@apollo/server": "^4.10.0",
"azle": "0.19.0",
"express": "^4.18.2",
"graphql": "^16.8.1"
},
"devDependencies": {
"@types/express": "^4.17.21",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
21 changes: 21 additions & 0 deletions examples/apollo_server/src/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016-2020 Apollo Graph, Inc. (Formerly Meteor Development Group, Inc.)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions examples/apollo_server/src/index.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
service: () -> {
http_request: (record {url:text; method:text; body:vec nat8; headers:vec record {text; text}; certificate_version:opt nat16}) -> (record {body:vec nat8; headers:vec record {text; text}; upgrade:opt bool; streaming_strategy:opt variant {Callback:record {token:record {arbitrary_data:text}; callback:func (text) -> (record {token:opt record {arbitrary_data:text}; body:vec nat8}) query}}; status_code:nat16}) query;
http_request_update: (record {url:text; method:text; body:vec nat8; headers:vec record {text; text}; certificate_version:opt nat16}) -> (record {body:vec nat8; headers:vec record {text; text}; upgrade:opt bool; streaming_strategy:opt variant {Callback:record {token:record {arbitrary_data:text}; callback:func (text) -> (record {token:opt record {arbitrary_data:text}; body:vec nat8}) query}}; status_code:nat16});
}
102 changes: 102 additions & 0 deletions examples/apollo_server/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// TODO once we have dfx 0.16.x working reenable these tests in CI

import { Server } from 'azle';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import express from 'express';

// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = `#graphql
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
# This "Book" type defines the queryable fields for every book in our data source.
type Book {
id: String
title: String
rating: Int
}
type Author {
id: String
name: String
}
# The "Query" type is special: it lists all of the available queries that
# clients can execute, along with the return type for each. In this
# case, the "books" query returns an array of zero or more Books (defined above).
type Query {
books: [Book]
authors: [Author]
titles: [String]
}
type Mutation {
createBook(title: String!, rating: Int!): Book
}
`;

let books = [
{
id: '0',
title: 'The Awakening',
rating: 34
},
{
id: '1',
title: 'City of Glass',
rating: 63
}
];

const authors = [
{
id: '0',
name: 'Jordan'
},
{
id: '1',
name: 'Ben'
}
];

// Resolvers define how to fetch the types defined in your schema.
// This resolver retrieves books from the "books" array above.
const resolvers = {
Query: {
books: () => books,
authors: () => authors,
titles: () => {
return books.map((book) => book.title);
}
},
Mutation: {
createBook: (parent: any, args: any) => {
const book = {
id: books.length.toString(),
title: args.title,
rating: args.rating
};

books = [...books, book];

return book;
}
}
};

export default Server(async () => {
const app = express();

const server = new ApolloServer({
typeDefs,
resolvers
});

await server.start();

app.use(express.json({ limit: '50mb' }), expressMiddleware(server, {}));

return app.listen();
});
15 changes: 15 additions & 0 deletions examples/apollo_server/test/pretest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { execSync } from 'child_process';

async function pretest() {
await new Promise((resolve) => setTimeout(resolve, 5000));

execSync(`dfx canister uninstall-code apollo_server || true`, {
stdio: 'inherit'
});

execSync(`dfx deploy`, {
stdio: 'inherit'
});
}

pretest();
6 changes: 6 additions & 0 deletions examples/apollo_server/test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getCanisterId, runTests } from 'azle/test';
import { getTests } from './tests';

const canisterId = getCanisterId('apollo_server');

runTests(getTests(canisterId));
74 changes: 74 additions & 0 deletions examples/apollo_server/test/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first');

import { Test } from 'azle/test';

export function getTests(canisterId: string): Test[] {
const origin = `http://${canisterId}.localhost:8000`;

return [
{
name: 'query books',
test: async () => {
try {
const response = await fetch(origin, {
method: 'POST',
headers: [['Content-Type', 'application/json']],
body: JSON.stringify({
query: `
query {
books {
id
title
}
}
`
})
});
const responseJson = await response.json();

return {
Ok:
responseJson.data.books.length === 2 &&
responseJson.data.books[0].title === 'The Awakening'
};
} catch (error: any) {
return {
Err: error
};
}
}
},
{
name: 'query authors',
test: async () => {
try {
const response = await fetch(origin, {
method: 'POST',
headers: [['Content-Type', 'application/json']],
body: JSON.stringify({
query: `
query {
authors {
name
}
}
`
})
});
const responseJson = await response.json();

return {
Ok:
responseJson.data.authors.length === 2 &&
responseJson.data.authors[0].name === 'Jordan'
};
} catch (error: any) {
return {
Err: error
};
}
}
}
];
}
10 changes: 10 additions & 0 deletions examples/apollo_server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"target": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"outDir": "HACK_BECAUSE_OF_ALLOW_JS",
"allowSyntheticDefaultImports": true
}
}
3 changes: 3 additions & 0 deletions examples/fs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.azle
.dfx
node_modules
12 changes: 12 additions & 0 deletions examples/fs/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"canisters": {
"fs": {
"type": "custom",
"main": "src/index.ts",
"candid": "src/index.did",
"build": "npx azle fs",
"wasm": ".azle/fs/fs.wasm",
"gzip": true
}
}
}
Loading

0 comments on commit ee87d86

Please sign in to comment.