Code filter function by using Array.map and Array.flat
Example usage:
> filter([1,2,3], x => x !== 2)
[1,3]| import pytest | |
| from unittest.mock import MagicMock | |
| @pytest.fixture() | |
| def patch_user_service(): | |
| """ | |
| reusable patcher, | |
| handy when there are lots of test cases | |
| """ | |
| user_connector = MagicMock() |
| import requests | |
| API_URL = "https://example.com" # often imported from settings | |
| # | |
| # Code | |
| # | |
| def get_user_with_org(id: str): | |
| """Gets a User object with the User's Organisation""" |
| # I'm using Django models as inspiration in this example | |
| from mymodels import User, Org | |
| # You could also call these Getters or DAOs etc | |
| # The point is that they perform some kind of | |
| # side effect like talking to a DB or an API | |
| @dataclass(frozen=True) | |
| class UserConnector: | |
| def get_user(id: str) -> User: |
| # dataclass saves the trouble of writing __init__ implementations | |
| # frozen=True makes the class instances immutable (so no hidden mutable state) | |
| @dataclass(frozen=True) | |
| class UserConnector: | |
| def get_user(id: str) -> User: | |
| return User.objects.get(id=id) | |
| @dataclass(frozen=True) | |
| class OrgConnector: | |
| def get_org(id: str) -> Org: |
| -- needed for `gen_random_bytes` | |
| CREATE EXTENSION IF NOT EXISTS pgcrypto; | |
| /** | |
| Generate a unique Order ID based on current_timestamp, base32 and two bits of entropy. | |
| If two Orders are created at the exact same millisecond, we rely on the entropy to provide uniqueness. | |
| Usage: | |
| > select * from generate_order_id(); |
| -- taken from https://rob.conery.io/2014/05/28/a-better-id-generator-for-postgresql/ | |
| create schema idgen; | |
| create sequence idgen.global_id_sequence; | |
| CREATE OR REPLACE FUNCTION idgen.id_generator( | |
| myts timestamp, | |
| OUT result bigint) AS $$ | |
| DECLARE | |
| our_epoch bigint := 1314220021721; |
| module type MyGraphQLMutation = { | |
| module GQL: ReasonApolloTypes.Config; | |
| include (module type of ReasonApolloMutation.Make(GQL)); | |
| }; | |
| module type MyGraphQLQuery = { | |
| module GQL: ReasonApolloTypes.Config; | |
| include (module type of ReasonApolloQuery.Make(GQL)); | |
| }; |
| /** Schema for translations, with @genType for TS/JS usage */ | |
| [@genType] | |
| type translations = { | |
| helloMessage: string, | |
| helloMessageWithVariable: (~name: string) => string, | |
| }; | |
| /** English translations */ | |
| [@genType] | |
| let translationsEN = { |