def test_transition_to_revised(self): with SessionContext(self.graph), transaction(): reassigned_event = list(islice(self.iter_events(), 4))[-1] assert_that(reassigned_event.event_type, is_(equal_to(TaskEventType.STARTED))) response = self.client.post( "/api/v1/task_event", data=dumps( dict( taskId=str(self.task.id), eventType=TaskEventType.REVISED.name, )), ) assert_that(response.status_code, is_(equal_to(201))) data = loads(response.data.decode("utf-8")) assert_that(data, has_entry("taskId", str(self.task.id))) assert_that(data, has_entry("clock", 5)) assert_that(data, has_entry("parentId", str(reassigned_event.id))) assert_that(data, has_entry("version", 2)) self.graph.sns_producer.produce.assert_called_with( media_type= "application/vnd.globality.pubsub._.created.task_event.revised", uri="http://localhost/api/v1/task_event/{}".format(data["id"]), ) with SessionContext(self.graph), transaction(): task_event = self.graph.task_event_store.retrieve(data["id"]) assert_that(task_event.state, is_(contains(TaskEventType.CREATED)))
def setup(self): self.graph = create_app(testing=True) self.client = self.graph.flask.test_client() recreate_all(self.graph) self.username1 = "glen.runciter" self.user1 = User( username=self.username1, email="*****@*****.**", first_name="Glen", last_name="Runciter", title="Big Boss", bio="Ubik-- get it today!", ) self.username2 = "joe.chip" self.user2 = User( username=self.username2, email="*****@*****.**", first_name="Joe", last_name="Chip", title="Technician", bio="Ubik-- get it today!", ) with SessionContext(self.graph), transaction(): self.user1.create() self.user2.create() self.user1_follow_user2 = FollowerRelationship( id=new_object_id(), user_id=self.user2.id, follower_id=self.user1.id, ) self.user2_tweet_content1 = """ Friends, this is clean-up time and we’re discounting all our silent, electric Ubiks by this much money. Yes, we’re throwing away the blue-book. And remember: every Ubik on our lot has been used only as directed. """ self.user2_tweet_content2 = """ The best way to ask for beer is to sing out Ubik. Made from select hops, choice water, slow-aged for perfect flavor, Ubik is the nation’s number-one choice in beer. Made only in Cleveland. """ with SessionContext(self.graph), transaction(): self.user1_follow_user2.create() self.user2_tweet1 = Tweet( id=new_object_id(), user_id=self.user2.id, tweet_content=self.user2_tweet_content1, ).create() self.user2_tweet2 = Tweet( id=new_object_id(), user_id=self.user2.id, tweet_content=self.user2_tweet_content2, ).create()
def test_delete(self): with SessionContext(self.graph), transaction(): self.order1.create() response = self.client.delete(self.detail_uri) assert_that(response.status_code, is_(equal_to(204))) with SessionContext(self.graph) as session: assert session.session.query(Order).get(self.order_id) is None
def test_update_with_key(self): with SessionContext(self.graph): with transaction(): encryptable = self.encryptable_store.create( Encryptable( key="private", value="value", ), ) with SessionContext(self.graph): with transaction(): res = self.encryptable_store.update( encryptable.id, Encryptable( id=encryptable.id, # Pass the key key="private", value="new-value", ), ) assert_that( res, has_properties( key=is_(equal_to("private")), value=is_(equal_to("new-value")), encrypted_id=is_not(none()), ), ) with SessionContext(self.graph): encryptable = self.encryptable_store.retrieve(encryptable.id) assert_that( encryptable, has_properties( key=is_(equal_to("private")), value=is_(equal_to("new-value")), encrypted_id=is_not(none()), ), ) assert_that( self.encryptable_store.count(), is_(equal_to(1)), ) assert_that( self.encrypted_store.count(), is_(equal_to(1)), )
def setup(self): self.graph = create_app(testing=True) self.user_store = self.graph.user_store self.follower_relationship_store = self.graph.follower_relationship_store self.username1 = "glen.runciter" self.user1 = User( username=self.username1, email="*****@*****.**", first_name="Glen", last_name="Runciter", title="Big Boss", bio="Ubik-- get it today!", ) self.username2 = "joe.chip" self.user2 = User( username=self.username2, email="*****@*****.**", first_name="Joe", last_name="Chip", title="Technician", bio="Ubik-- get it today!", ) self.context = SessionContext(self.graph) self.context.recreate_all() self.context.open() with transaction(): self.user1.create() self.user2.create()
def setup(self): self.graph = create_app(testing=True) self.tweet_store = self.graph.tweet_store self.user_store = self.graph.user_store self.username = "******" self.user = User( username=self.username, email="*****@*****.**", first_name="Joe", last_name="Chip", title="Technician", bio="Ubik-- get it today!", ) self.context = SessionContext(self.graph) self.context.recreate_all() self.context.open() with transaction(): self.user_store.create(self.user) self.tweet_content = """ Friends, this is clean-up time and we’re discounting all our silent, electric Ubiks by this much money. Yes, we’re throwing away the blue-book. And remember: every Ubik on our lot has been used only as directed. """ self.tweet = Tweet( user_id=self.user.id, tweet_content=self.tweet_content, )
def setup(self): loaders = load_each( load_from_dict( multi_tenant_key_registry=dict( context_keys=[ "private", ], key_ids=[ "key_id", ], ), ), load_from_environ, ) self.graph = create_object_graph( name="example", testing=True, import_name="microcosm_postgres", loader=loaders, ) self.encryptable_store = self.graph.encryptable_store self.encrypted_store = self.graph.encrypted_store self.json_encryptable_store = self.graph.json_encryptable_store self.json_encrypted_store = self.graph.json_encrypted_store self.nullable_encryptable_store = self.graph.nullable_encryptable_store self.nullable_encrypted_store = self.graph.nullable_encrypted_store self.encryptor = self.graph.multi_tenant_encryptor with SessionContext(self.graph) as context: context.recreate_all()
def test_select_from_none(self): with SessionContext(self.graph): with transient(Company) as transient_company: assert_that( transient_company.select_from(Company), contains(), )
def test_create(self): uri = "/api/v1/pizza" with SessionContext(self.graph), transaction(): self.order.create() customer_id = str(new_object_id()) with patch.object(self.graph.pizza_store, "new_object_id") as mocked: mocked.return_value = self.pizza1.id response = self.client.post( uri, json=dict(customerId=customer_id, crustType="thin", size=12, orderId=self.order_id), ) assert_that(response.status_code, is_(equal_to(201))) assert_that( response.json, has_entries( id=str(self.pizza1.id), customerId=customer_id, crustType="thin", size=12, ), )
def test_not_encrypted(self): with SessionContext(self.graph): with transaction(): encryptable = self.encryptable_store.create( Encryptable( key="key", value="value", ), ) assert_that( encryptable, has_properties( key=is_(equal_to("key")), value=is_(equal_to("value")), encrypted_id=is_(none()), ), ) assert_that( self.encryptable_store.count(), is_(equal_to(1)), ) assert_that( self.encrypted_store.count(), is_(equal_to(0)), ) with transaction(): self.encryptable_store.delete(encryptable.id) assert_that( self.encryptable_store.count(), is_(equal_to(0)), )
def setup(self): loader = load_from_dict( secret=dict(postgres=dict(host=environ.get( "MICROCOSM_EVENTSOURCE__POSTGRES__HOST", "localhost"), ), ), postgres=dict(host=environ.get( "MICROCOSM_EVENTSOURCE__POSTGRES__HOST", "localhost"), ), sns_producer=dict(mock_sns=False, ), ) self.graph = create_object_graph( "microcosm_eventsource", loader=loader, root_path=dirname(__file__), testing=True, ) self.graph.use( "session_factory", "task_store", "task_event_store", "task_event_controller", "task_crud_routes", ) self.client = self.graph.flask.test_client() recreate_all(self.graph) with SessionContext(self.graph), transaction(): self.task = Task().create() self.graph.sns_producer.sns_client.reset_mock()
def test_clone(self): with SessionContext(self.graph): substitutions = dict(name="newname") dag = DAG.from_nodes(self.company, self.employee).clone(substitutions) assert_that(dag.nodes, has_length(2)) assert_that(dag.edges, has_length(1))
def setup(self): self.graph = create_object_graph( "microcosm_eventsource", root_path=join(dirname(__file__), pardir), testing=True, ) self.graph.use( "sub_task_store", "sub_task_event_store", ) self.store = SubTaskRollUpStore(self.graph) self.context = SessionContext(self.graph) self.context.recreate_all() self.context.open() with transaction(): self.sub_task = SubTask().create() self.sub_task_created_event = SubTaskEvent( event_type=SubTaskEventType.CREATED, sub_task_id=self.sub_task.id, ).create() self.sub_task_assigned_event = SubTaskEvent( assignee="Alice", event_type=SubTaskEventType.ASSIGNED, parent_id=self.sub_task_created_event.id, sub_task_id=self.sub_task.id, ).create()
def test_created_event(self): created_event_id = new_object_id() with patch.object(self.graph.task_event_store, "new_object_id") as mocked: mocked.return_value = created_event_id response = self.client.post( "/api/v1/task_event", data=dumps( dict( taskId=str(self.task.id), eventType=TaskEventType.CREATED.name, parentId=str(self.task.id), )), ) assert_that(response.status_code, is_(equal_to(201))) data = loads(response.data.decode("utf-8")) assert_that(data, has_entry("id", str(created_event_id))) assert_that(data, has_entry("taskId", str(self.task.id))) assert_that(data, has_entry("clock", 1)) assert_that(data, has_entry("parentId", none())) self.graph.sns_producer.produce.assert_called_with( media_type= "application/vnd.globality.pubsub._.created.task_event.created", uri="http://localhost/api/v1/task_event/{}".format(data["id"]), ) with SessionContext(self.graph), transaction(): task_event = self.graph.task_event_store.retrieve(data["id"]) assert_that(task_event.state, is_(contains(TaskEventType.CREATED)))
def test_search_task_events_by_clock(self): with SessionContext(self.graph), transaction(): created_event = list(islice(self.iter_events(), 4))[-1] assert_that(created_event.event_type, is_(equal_to(TaskEventType.STARTED))) descending_response = self.client.get( "/api/v1/task_event?sort_by_clock=true", ) assert_that(descending_response.status_code, is_(equal_to(200))) data = loads(descending_response.data.decode("utf-8")) descending_order_clock_list = [ event['clock'] for event in data["items"] ] assert_that(descending_order_clock_list, is_(equal_to([4, 3, 2, 1]))) ascending_response = self.client.get( "/api/v1/task_event?sort_by_clock=true&sort_clock_in_ascending_order=true", ) assert_that(ascending_response.status_code, is_(equal_to(200))) data = loads(ascending_response.data.decode("utf-8")) ascending_order_clock_list = [ event['clock'] for event in data["items"] ] assert_that(ascending_order_clock_list, is_(equal_to([1, 2, 3, 4]))) invalid_response = self.client.get( "/api/v1/task_event?sort_clock_in_ascending_order=true", ) assert_that(invalid_response.status_code, is_(equal_to(422)))
def setup(self): self.graph = create_app(testing=True) self.client = self.graph.flask.test_client() recreate_all(self.graph) with SessionContext(self.graph), transaction(): self.new_order = Order().create() self.new_pizza = Pizza( id=new_object_id(), order_id=self.new_order.id, pizza_size=PizzaSize.SMALL.name, pizza_type=PizzaType.HANDTOSSED.name, ).create() self.first_topping = Topping( id=new_object_id(), pizza_id=self.new_pizza.id, topping_type=ToppingType.ONIONS, ) self.second_topping = Topping( id=new_object_id(), pizza_id=self.new_pizza.id, topping_type=ToppingType.CHICKEN, )
def _make_company(self): with SessionContext(self.graph): with transaction(): return Company( name="name", type=CompanyType.private, ).create()
def setup(self): self.graph = create_object_graph( name="example", testing=True, import_name="microcosm_postgres", ) self.company_store = self.graph.company_store self.companies = [ Company( name="name1", type=CompanyType.private, ), Company( name="name2", type=CompanyType.private, ), Company( name="name3", type=CompanyType.private, ), ] with SessionContext(self.graph) as context: context.recreate_all()
def setup(self): self.graph = create_app(testing=True) self.account_store = self.graph.account_store self.name = "NAME" with SessionContext(self.graph) as context: context.recreate_all()
def setup(self): self.graph = create_object_graph(name="example", testing=True, import_name="microcosm_postgres") self.graph.use("company_store") context = SessionContext(self.graph) context.recreate_all()
def test_delete(self): with SessionContext(self.graph), transaction(): self.user1_tweet1.create() uri = "/api/v1/tweet/{}".format(self.user1_tweet1.id) response = self.client.delete(uri) assert_that(response.status_code, is_(equal_to(204)))
def setup(self): self.graph = create_object_graph(name="example", testing=True, import_name="microcosm_postgres") self.company_store = self.graph.company_store self.employee_store = self.graph.employee_store self.context = SessionContext(self.graph) self.context.recreate_all() self.context.open()
def setup(self): self.graph = create_object_graph(name="example", testing=True, import_name="microcosm_postgres") self.context = SessionContext(self.graph) self.context.recreate_all() self.context.open() self.store = Store(self.graph, Sequential)
def test_delete(self): with SessionContext(self.graph), transaction(): self.example1.create() uri = f"/api/v1/example/{self.example1.id}" response = self.client.delete(uri) assert_that(response.status_code, is_(equal_to(204)))
def test_delete(self): with SessionContext(self.graph), transaction(): self.user1_follow_user2.create() uri = "/api/v1/follower_relationship/{}".format( self.user1_follow_user2.id) response = self.client.delete(uri) assert_that(response.status_code, is_(equal_to(204)))
def test_encrypted(self): with SessionContext(self.graph): with transaction(): encryptable = self.encryptable_store.create( Encryptable( key="private", value="value", ), ) assert_that( encryptable, has_properties( key=is_(equal_to("private")), value=is_(none()), encrypted_id=is_not(none()), ), ) assert_that( encryptable._members(), is_(equal_to(dict( created_at=encryptable.created_at, encrypted_id=encryptable.encrypted_id, id=encryptable.id, key=encryptable.key, updated_at=encryptable.updated_at, ))), ) assert_that( self.encryptable_store.count(), is_(equal_to(1)), ) assert_that( self.encrypted_store.count(), is_(equal_to(1)), ) # NB: ORM events will not trigger if we can reuse the object from the session cache self.encryptable_store.expunge(encryptable) encryptable = self.encryptable_store.retrieve(encryptable.id) assert_that( encryptable, has_properties( key=is_(equal_to("private")), value=is_(equal_to("value")), encrypted_id=is_not(none()), ), ) with transaction(): self.encryptable_store.delete(encryptable.id) assert_that( self.encryptable_store.count(), is_(equal_to(0)), ) assert_that( self.encrypted_store.count(), is_(equal_to(0)), )
def setup(self): self.graph = create_app(testing=True) self.order_event_store = self.graph.order_event_store self.order_id = new_object_id() self.context = SessionContext(self.graph) self.context.recreate_all() self.context.open()
def setup(self): self.graph = create_app(testing=True) self.client = self.graph.flask.test_client() recreate_all(self.graph) with SessionContext(self.graph), transaction(): self.new_order = Order().create() self.customer_event = CustomerStartedOrder(id=new_object_id(), order_id=self.new_order.id)
def setup(self): self.graph = create_app(testing=True) self.pizza_store = self.graph.pizza_store self.name = "NAME" self.context = SessionContext(self.graph) self.context.recreate_all() self.context.open()
def test_auto_transition(self): with SessionContext(self.graph), transaction(): started_event = list(islice(self.iter_events(), 4))[-1] assert_that(started_event.event_type, is_(equal_to(TaskEventType.STARTED))) response = self.client.post( "/api/v1/task_event", data=dumps( dict( taskId=str(self.task.id), eventType=TaskEventType.COMPLETED.name, )), ) assert_that(response.status_code, is_(equal_to(201))) data = loads(response.data.decode("utf-8")) assert_that(data, has_entry("eventType", str(TaskEventType.COMPLETED))) with SessionContext(self.graph), transaction(): completed_task_event = self.graph.task_event_store.retrieve( data["id"]) assert_that(completed_task_event.event_type, is_(TaskEventType.COMPLETED)) assert_that(completed_task_event.clock, is_(5)) ended_task_event = self.graph.task_event_store.retrieve_most_recent( task_id=self.task.id) assert_that(ended_task_event.event_type, is_(TaskEventType.ENDED)) assert_that(ended_task_event.clock, is_(6)) self.graph.sns_producer.produce.assert_has_calls([ call( media_type= "application/vnd.globality.pubsub._.created.task_event.completed", uri="http://localhost/api/v1/task_event/{}".format(data["id"]), ), call( media_type= "application/vnd.globality.pubsub._.created.task_event.ended", uri="http://localhost/api/v1/task_event/{}".format( ended_task_event.id), ), ], )