Skip to content

Instantly share code, notes, and snippets.

View efueyo's full-sized avatar

Enrique Fueyo efueyo

View GitHub Profile
@efueyo
efueyo / server.ts
Created April 24, 2021 01:38
Apollo-Sentry-Server
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
@efueyo
efueyo / sentry-plugin.ts
Last active April 26, 2021 21:31
Apollo-Sentry-Plugin
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
@efueyo
efueyo / Context.ts
Last active April 24, 2021 01:32
Apollo-Sentry-Context
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({
@efueyo
efueyo / NewBooksServer.go
Created November 2, 2019 19:34
MockedServer
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" }
`
@efueyo
efueyo / TestServer.go
Last active October 29, 2019 22:25
httptest.Server example
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.
@efueyo
efueyo / MustJSONMarshall.go
Created October 25, 2019 23:46
HelperFunctions.go
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)
@efueyo
efueyo / TestFixtures.go
Last active October 25, 2019 23:44
TestFixtures.go
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:
@efueyo
efueyo / GoldenHelpers.go
Created October 25, 2019 23:30
GoldeFiles
// 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")
@efueyo
efueyo / Config.go
Last active October 25, 2019 23:25
Golang Configuration
// Bad
const bark = "woof"
type Dog struct{}
// Bark prints the dog's bark
func (d *Dog) Bark() {
fmt.Println(bark)
}
@efueyo
efueyo / tfidf.py
Last active July 27, 2018 17:04
TF-IDF
# Install dependencies first
#  pip install spacy wikipedia
#  python -m spacy download en
# Run
# python tfidf.py
from collections import Counter
import math
import wikipedia