This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ApolloServer } from "apollo-server-micro" | |
import { createContext } from "./context" | |
import SentryPlugin from "./sentry-plugin" | |
const apolloServer = new ApolloServer({ | |
// ... your ApolloServer options | |
// Create context function | |
context: ({ req, connection }) => createContext({ req, connection }), | |
// Add our sentry plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ApolloServerPlugin } from "apollo-server-plugin-base" | |
import { Context } from "./context" | |
const plugin: ApolloServerPlugin<Context> = { | |
requestDidStart({ request, context }) { | |
if (!!request.operationName) { // set the transaction Name if we have named queries | |
context.transaction.setName(request.operationName!) | |
} | |
return { | |
willSendResponse({ context }) { // hook for transaction finished |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Transaction } from "@sentry/types" | |
export interface Context { | |
// ... other context fields for your context | |
transaction: Transaction | |
} | |
export async function createContext(): Promise<Context> { { | |
// ... create other context fields | |
const transaction = Sentry.startTransaction({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const books string = ` | |
[ | |
{ "id": 1, "title": "Pedro Páramo", author: "Juan Rulfo" }, | |
{ "id": 2, "title": "Meditations", author: "Marcus Aurelius" }, | |
{ "id": 3, "title": "Walden", author: "Henry David Thoreau" } | |
] | |
` | |
const book1 string = ` | |
{ "id": 1, "title": "Pedro Páramo", author: "Juan Rulfo" } | |
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main_test | |
import ( | |
"fmt" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
) | |
// GetStatus is the function we want to test. It just return the response.Status. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func MustJSONMarshall(t *testing.T, a interface{}) []byte { | |
res, err := json.Marshal(a) | |
if err != nil { | |
t.FatalF("err: %s", err) | |
} | |
return res | |
} | |
func TestWhatver(t *testing.T) { | |
elem := MyStruct{Field:"something"} | |
elemB := MustJSONMarshall(t, elem) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fixtures *testfixtures.Context | |
func TestMain(m *testing.M) { // errors skipped for brevity here | |
db, _ := sql.Open("postgres", "dbname=myapp_test") | |
fixtures, _ = testfixtures.NewFolder(db, &testfixtures.PostgreSQL{}, "testdata/fixtures") | |
os.Exit(m.Run()) | |
} | |
// now remember to call fixtures.Load() at the beginning of every tests and check for errors | |
// in Ginkgo you can use BeforeEach like: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MustWriteGolden updates the golden file for a given test t. | |
func MustWriteGolden(t *testing.T, content []byte) { | |
gp := filepath.Join("testdata", t.Name() +".golden") | |
if err := ioutil.WriteFile(gp, content, 0644); err != nil { | |
t.FailF("failed to update golden file: %s", err) | |
} | |
} | |
// MustReadGolden reads the golden file for a given test t. | |
func MustReadGolden(t *testing.T) (content []byte) { | |
gp := filepath.Join("testdata", t.Name() +".golden") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bad | |
const bark = "woof" | |
type Dog struct{} | |
// Bark prints the dog's bark | |
func (d *Dog) Bark() { | |
fmt.Println(bark) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install dependencies first | |
# pip install spacy wikipedia | |
# python -m spacy download en | |
# Run | |
# python tfidf.py | |
from collections import Counter | |
import math | |
import wikipedia |
NewerOlder