def test_update(self): """ Should be able to update an employee after creating it. """ with transaction(): employee = Employee( first="first", last="last", company_id=self.company.id, ).create() with transaction(): updated_employee = Employee( id=employee.id, first="Jane", last="Doe", ).update() assert_that(updated_employee.first, is_(equal_to("Jane"))) assert_that(updated_employee.last, is_(equal_to("Doe"))) assert_that(updated_employee.company_id, is_(equal_to(self.company.id))) with transaction(): retrieved_employee = Employee.retrieve(employee.id) assert_that(retrieved_employee.first, is_(equal_to("Jane"))) assert_that(retrieved_employee.last, is_(equal_to("Doe"))) assert_that(Employee.count(), is_(equal_to(1)))
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 test_update_with_diff(self): """ Should be able to update an employee after creating it and get a diff. """ with transaction(): employee = Employee( first="first", last="last", company_id=self.company.id, ).create() with transaction(): _, diff = Employee( id=employee.id, last="Doe", ).update_with_diff() assert_that(list(diff.keys()), contains_inanyorder("last", "updated_at")) assert_that(diff["last"].before, is_(equal_to("last"))) assert_that(diff["last"].after, is_(equal_to("Doe"))) with transaction(): retrieved_employee = Employee.retrieve(employee.id) assert_that(retrieved_employee.first, is_(equal_to("first"))) assert_that(retrieved_employee.last, is_(equal_to("Doe"))) assert_that(Employee.count(), is_(equal_to(1)))
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 test_update_with_diff_employee_that_exists(self): """ Should be able to update an employee after creating it and get a diff. """ with transaction(): employee = Employee( first="first", last="last", company_id=self.company.id, ).create() with transaction(): _, diff = Employee( id=employee.id, last="Doe", ).update_with_diff() assert_that(list(diff.keys()), contains_inanyorder("last", "updated_at")) assert_that(diff["last"].before, is_(equal_to("last"))) assert_that(diff["last"].after, is_(equal_to("Doe"))) with transaction(): retrieved_employee = Employee.retrieve(employee.id) assert_that(retrieved_employee.first, is_(equal_to("first"))) assert_that(retrieved_employee.last, is_(equal_to("Doe"))) assert_that(Employee.count(), is_(equal_to(1)))
def iter_events(self, simple_test_object=None): with transaction(): event = self.event_store.create( SimpleTestObjectEvent( event_type=str(SimpleTestObjectEventType.CREATED), simple_test_object_id=simple_test_object.id, ), ) yield event with transaction(): event = self.event_store.create( SimpleTestObjectEvent( event_type=str(SimpleTestObjectEventType.READY), parent_id=event.id, simple_test_object_id=simple_test_object.id, ), ) yield event with transaction(): event = self.event_store.create( SimpleTestObjectEvent( event_type=str(SimpleTestObjectEventType.DONE), parent_id=event.id, simple_test_object_id=simple_test_object.id, ), ) yield event
def test_replace_employee_that_exists(self): """ Should be able to replace an employee after creating it. """ with transaction(): employee = Employee( first="first", last="last", company_id=self.company.id, ).create() with transaction(): updated_employee = Employee( id=employee.id, first="Jane", last="Doe", ).replace() assert_that(updated_employee.first, is_(equal_to("Jane"))) assert_that(updated_employee.last, is_(equal_to("Doe"))) with transaction(): retrieved_employee = Employee.retrieve(employee.id) assert_that(retrieved_employee.first, is_(equal_to("Jane"))) assert_that(retrieved_employee.last, is_(equal_to("Doe"))) assert_that(Employee.count(), is_(equal_to(1)))
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_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 test_create_delete_company(self): """ Should not be able to retrieve a company after deleting it. """ with transaction(): company = Company(name="name").create() with transaction(): company.delete() assert_that(calling(Company.retrieve).with_args(company.id), raises(ModelNotFoundError))
def test_delete_does_not_reset_sequence(self): """ Deletion does not reset the sequence. """ with transaction(): example = self.store.create(Sequential()) with transaction(): self.store.delete(example.id) with transaction(): example = self.store.create(Sequential()) assert_that(example.value, is_(equal_to(2)))
def test_create_delete_company(self): """ Should not be able to retrieve a company after deleting it. """ with transaction(): company = Company(name="name").create() with transaction(): company.delete() assert_that( calling(Company.retrieve).with_args(company.id), raises(ModelNotFoundError, pattern="Company not found"), )
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.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 test_retrieve_with_update_lock_exception(self): with transaction(): created_event = TaskEvent( event_type=TaskEventType.CREATED, task_id=self.task.id, ) self.store.create(created_event) with patch.object(Query, 'with_for_update') as mocked_with_for_update: mocked_with_for_update.side_effect = OperationalError( statement="", params="", orig=psycopg2.errors.LockNotAvailable()) assert_that( calling(self.store.retrieve_most_recent_with_update_lock). with_args(task_id=self.task.id, ), raises(ContainerLockNotAvailableRetry), ) with patch.object(Query, 'with_for_update') as mocked_with_for_update: mocked_with_for_update.side_effect = Exception() assert_that( calling(self.store.retrieve_most_recent_with_update_lock). with_args(task_id=self.task.id, ), raises(Exception), )
def test_delete_does_not_reset_sequence(self): """ Deletion does not reset the sequence. """ with SessionContext(self.graph): with transaction(): example = self.store.create(WithSerial()) with transaction(): self.store.delete(example.id) with transaction(): example = self.store.create(WithSerial()) assert_that(example.value, is_(equal_to(2)))
def test_upsert_on_index_elements(self): """ Events with a duplicate index elements can be upserted. """ with transaction(): created_event = TaskEvent( event_type=TaskEventType.CREATED, task_id=self.task.id, ) self.store.create(created_event) task_event = TaskEvent( event_type=TaskEventType.CREATED, parent_id=created_event.id, task_id=self.task.id, ) self.store.create(task_event) upserted = self.store.upsert_on_index_elements( TaskEvent( event_type=TaskEventType.CREATED, parent_id=created_event.id, task_id=self.task.id, )) assert_that(task_event.id, is_(equal_to(upserted.id)))
def test_search_filter_employees_by_company(self): """ Should be able to filter searches using kwargs. """ with transaction(): employee1 = Employee( first="first", last="last", company_id=self.company.id, ).create() employee2 = Employee( first="Jane", last="Doe", company_id=self.company.id, ).create() company2 = Company(name="other").create() employee3 = Employee( first="John", last="Doe", company_id=company2.id, ).create() assert_that(Employee.count(), is_(equal_to(3))) assert_that( [employee.id for employee in self.employee_store.search(company_id=self.company.id, offset=0, limit=10)], contains_inanyorder(employee1.id, employee2.id) ) assert_that(self.employee_store.count(company_id=self.company.id, offset=0, limit=10), is_(equal_to(2))) assert_that( [employee.id for employee in self.employee_store.search(company_id=company2.id, offset=0, limit=10)], contains_inanyorder(employee3.id) ) assert_that(self.employee_store.count(company_id=company2.id, offset=0, limit=10), is_(equal_to(1)))
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_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 _make_company(self): with SessionContext(self.graph): with transaction(): return Company( name="name", type=CompanyType.private, ).create()
def test_multiple_children_per_parent(self): """ Events are not unique per parent for False unique_parent events. """ with transaction(): self.activity = Activity().create() created_event = ActivityEvent( event_type=ActivityEventType.CREATED, activity_id=self.activity.id, ) self.store.create(created_event) task_event = ActivityEvent( event_type=ActivityEventType.CANCELED, parent_id=created_event.id, activity_id=self.activity.id, ) self.store.create(task_event) same_parent_task_event = ActivityEvent( event_type=ActivityEventType.CANCELED, parent_id=created_event.id, activity_id=self.activity.id, ) self.store.create(same_parent_task_event) assert_that(same_parent_task_event.parent_id, is_(created_event.id))
def test_edge_case_delete_number_of_events(self): """ Test that sql proc_events_delete function: * delete events * replaces parent_id (even if the new parent is not directly refrenced by the deleted event) """ with transaction(): self.store.session.execute(""" CREATE TEMP TABLE events_to_remove AS ( SELECT id FROM task_event WHERE event_type='SCHEDULED' OR event_type='ASSIGNED' ); SELECT proc_events_delete('task_event', 'events_to_remove', 'task_id'); """) results = self.store.search() assert_that(results, has_length(2)) assert_that(results, contains( has_properties( event_type=TaskEventType.STARTED, state=[TaskEventType.STARTED], id=self.started_event.id, parent_id=self.created_event.id, ), has_properties( event_type=TaskEventType.CREATED, state=[TaskEventType.CREATED], id=self.created_event.id, parent_id=None, ), ))
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 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_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 test_edge_case_delete_last_event(self): """ Test that proc_event_type_replace sql function can delete last event of a model """ with transaction(): self.store.session.execute(""" CREATE TEMP TABLE events_to_remove AS ( SELECT id FROM task_event WHERE event_type='STARTED' ); SELECT proc_events_delete('task_event', 'events_to_remove', 'task_id'); """) results = self.store.search() assert_that(results, has_length(3)) assert_that(results, contains( has_properties( event_type=TaskEventType.ASSIGNED, state=[TaskEventType.ASSIGNED, TaskEventType.CREATED, TaskEventType.SCHEDULED], id=self.assigned_event.id, parent_id=self.scheduled_event.id, ), has_properties( event_type=TaskEventType.SCHEDULED, state=[TaskEventType.CREATED, TaskEventType.SCHEDULED], id=self.scheduled_event.id, parent_id=self.created_event.id, ), has_properties( event_type=TaskEventType.CREATED, state=[TaskEventType.CREATED], id=self.created_event.id, parent_id=None, ), ))
def test_create_duplicate(self): user1 = User( username=self.username, email=self.email, first_name=self.first_name, last_name=self.last_name, title=self.title, bio=self.bio, ) user2 = User( username=self.username, email=self.email, first_name=self.first_name, last_name=self.last_name, title=self.title, bio=self.bio, ) with transaction(): self.user_store.create(user1) assert_that( calling(self.user_store.create).with_args(user2), raises(DuplicateModelError), )
def test_upsert_on_index_elements_mismatch(self): """ Events with a duplicate index elements cannot be upsert if they don't match. """ with transaction(): created_event = TaskEvent( event_type=TaskEventType.CREATED, task_id=self.task.id, ) self.store.create(created_event) task_event = TaskEvent( event_type=TaskEventType.CREATED, parent_id=created_event.id, task_id=self.task.id, ) self.store.create(task_event) assert_that( calling(self.store.upsert_on_index_elements).with_args( TaskEvent( event_type=TaskEventType.REVISED, parent_id=created_event.id, task_id=self.task.id, )), raises(ConcurrentStateConflictError), )
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 test_unique_parent_id(self): """ Events are unique per parent. """ with transaction(): created_event = TaskEvent( event_type=TaskEventType.CREATED, task_id=self.task.id, ) self.store.create(created_event) task_event = TaskEvent( event_type=TaskEventType.CREATED, parent_id=created_event.id, task_id=self.task.id, ) self.store.create(task_event) assert_that( calling(self.store.create).with_args( TaskEvent( event_type=TaskEventType.CREATED, parent_id=created_event.id, task_id=self.task.id, ), ), raises(DuplicateModelError), )
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 test_proc_event_type_delete(self): """ Test that sql proc_event_type_delete function: * delete events * replaces parent_id * updates states """ with transaction(): self.store.session.execute("SELECT proc_event_type_delete('task_event', 'SCHEDULED', 'task_id');") results = self.store.search() assert_that(results, has_length(3)) assert_that(results, contains( has_properties( event_type=TaskEventType.STARTED, state=[TaskEventType.STARTED], id=self.started_event.id, parent_id=self.assigned_event.id, ), has_properties( event_type=TaskEventType.ASSIGNED, state=[TaskEventType.ASSIGNED, TaskEventType.CREATED], id=self.assigned_event.id, parent_id=self.created_event.id, ), has_properties( event_type=TaskEventType.CREATED, state=[TaskEventType.CREATED], id=self.created_event.id, parent_id=None, ), ))
def test_replace_employee_that_does_not_exist(self): """ Should be able to replace an employee that does not exist. """ with transaction(): employee = Employee( first="first", last="last", company_id=self.company.id, ).replace() with transaction(): retrieved_employee = Employee.retrieve(employee.id) assert_that(retrieved_employee.first, is_(equal_to("first"))) assert_that(retrieved_employee.last, is_(equal_to("last"))) assert_that(Employee.count(), is_(equal_to(1)))
def test_replace_not_found(self): """ Should be able to replace an employee that does not exist. """ with transaction(): employee = Employee( first="first", last="last", company_id=self.company.id, ).replace() with transaction(): retrieved_employee = Employee.retrieve(employee.id) assert_that(retrieved_employee.first, is_(equal_to("first"))) assert_that(retrieved_employee.last, is_(equal_to("last"))) assert_that(Employee.count(), is_(equal_to(1)))
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_update_sequence(self): """ Updating a sequence is allowed (but not advised). """ with SessionContext(self.graph) as context: with transaction(): example = self.store.create(WithSerial()) example.value = example.value + 1 with transaction(): self.store.replace(example.id, example) context.session.expunge(example) example = self.store.retrieve(example.id) assert_that(example.value, is_(equal_to(2)))
def test_create_duplicate_company(self): """ Should not be able to retrieve a company with a duplicate name. """ with transaction(): Company(name="name").create() company = Company(name="name") assert_that(calling(company.create), raises(DuplicateModelError))
def test_clone(self): with transaction(): company = Company( name="name", type=CompanyType.private, ).create() copy = clone(company, dict(name="newname")) assert_that(copy.id, is_not(equal_to(company.id))) assert_that(self.company_store.retrieve(copy.id), is_(not_none()))
def test_upsert_into(self): with SessionContext(self.graph): with transaction(): # NB: create() will set the id of companies[0] self.companies[0].create() with transaction(): with transient(Company) as transient_company: assert_that( transient_company.insert_many(self.companies), is_(equal_to(3)), ) assert_that( transient_company.upsert_into(Company), is_(equal_to(2)), ) assert_that( self.company_store.count(), is_(equal_to(3)), )
def test_create_sequence_values(self): """ Creating new values should trigger auto increments. """ with SessionContext(self.graph), transaction(): for index in range(10): example = self.store.create(WithSerial()) assert_that(example.id, is_(not_none())) assert_that(example.value, is_(equal_to(index + 1)))
def test_create_update_company(self): """ Should be able to update a company after creating it. """ with transaction(): company = Company( name="name", ).create() with transaction(): updated_company = Company( id=company.id, name="new_name", ).update() assert_that(updated_company.name, is_(equal_to("new_name"))) with transaction(): retrieved_company = Company.retrieve(company.id) assert_that(retrieved_company.name, is_(equal_to("new_name")))
def test_create_update_with_diff_company(self): """ Should be able to update a company after creating it and get a diff. """ with transaction(): company = Company(name="name").create() with transaction(): _, diff = Company( id=company.id, name="new_name", ).update_with_diff() assert_that(list(diff.keys()), contains_inanyorder("name", "updated_at")) assert_that(diff["name"].before, is_(equal_to("name"))) assert_that(diff["name"].after, is_(equal_to("new_name"))) with transaction(): retrieved_company = Company.retrieve(company.id) assert_that(retrieved_company.name, is_(equal_to("new_name")))
def test_create_update_duplicate_company(self): """ Should be not able to update a company to a duplicate name. """ with transaction(): Company(name="name1").create() company = Company(name="name2").create() company.name = "name1" assert_that(calling(company.update), raises(DuplicateModelError))
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)), ) res = self.encryptable_store.retrieve(encryptable.id) assert_that( res, has_properties( key=is_(equal_to("key")), value=is_(equal_to("value")), encrypted_id=is_(none()), ), ) with transaction(): self.encryptable_store.delete(encryptable.id) assert_that( self.encryptable_store.count(), is_(equal_to(0)), )
def test_update_not_found(self): """ Should not be able to update an employee that does not exist. """ with transaction(): employee = Employee( first="first", last="last", company_id=self.company.id, ) assert_that(calling(employee.update), raises(ModelNotFoundError))
def test_delete_company_with_employees(self): """ Should be not able to delete a company with employees. """ with transaction(): Company( name="name1", ).create() company = Company( name="name2", ).create() with transaction(): Employee( first="first", last="last", company_id=company.id, ).create() assert_that(calling(company.delete), raises(ReferencedModelError))
def test_retrieve(self): with SessionContext(self.graph), transaction(): self.example1.create() uri = f"/api/v1/example/{self.example1.id}" response = self.client.get(uri) data = loads(response.data) assert_that(data, has_entries( id=str(self.example1.id), name=self.example1.name, ))
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() with transaction(): self.company = Company( name="name" ).create()
def test_create(self): """ Examples can be persisted. """ new_example = Example( name=self.name, ) with transaction(): self.example_store.create(new_example) retrieved_example = self.example_store.retrieve(new_example.id) assert_that(retrieved_example, is_(equal_to(new_example)))
def test_retrieve_by_sequence_value(self): """ Retrieving existing values should return the previously generated sequence. """ with SessionContext(self.graph): with transaction(): example = self.store.create(WithSerial()) retrieved = self.store._query().filter( WithSerial.value == example.value, ).one() assert_that(retrieved.id, is_(equal_to(example.id)))
def test_retrieve_sequence_value(self): """ Retrieving existing values should return the previously generated sequence. """ with SessionContext(self.graph) as context: with transaction(): example = self.store.create(WithSerial()) context.session.expunge(example) example = self.store.retrieve(example.id) assert_that(example.value, is_(equal_to(1)))
def test_create_retrieve_company(self): """ Should be able to retrieve a company after creating it. """ with transaction(): company = Company( name="name", type=CompanyType.private, ).create() retrieved_company = Company.retrieve(company.id) assert_that(retrieved_company.name, is_(equal_to("name"))) assert_that(retrieved_company.type, is_(equal_to(CompanyType.private)))
def test_create_search_count_company(self): """ Should be able to search and count companies after creation. """ with transaction(): company1 = Company(name="name1").create() company2 = Company(name="name2").create() assert_that(Company.count(), is_(equal_to(2))) # Pagination fields do not affect count calculations assert_that(self.company_store.count(offset=1, limit=1), is_(equal_to(2))) assert_that([company.id for company in Company.search()], contains_inanyorder(company1.id, company2.id))
def test_create(self): """ Should be able to retrieve an employee after creating it. """ with transaction(): employee = Employee( first="first", last="last", company_id=self.company.id, ).create() retrieved_employee = Employee.retrieve(employee.id) assert_that(retrieved_employee.first, is_(equal_to("first"))) assert_that(retrieved_employee.last, is_(equal_to("last")))
def test_create(self): """ Should be able to retrieve an employee after creating it. """ with SessionContext(self.graph), transaction(): employee_data = self.employee_data_store.create( EmployeeData( password="******", employee_id=self.employee.id, ), ) retrieved_employee_data = self.employee_data_store.retrieve(employee_data.id) assert_that(retrieved_employee_data.password, is_(equal_to("secret")))
def test_search_company(self): """ Should be able to search for companies. """ with transaction(): company = Company( name="name", type=CompanyType.private, ).create() assert_that(Company.search(), contains(company)) assert_that(Company.search(name="whatever"), is_(empty())) assert_that(Company.search(name=company.name), contains(company)) # NB: filtering is skipped if None assert_that(Company.search(name=None), contains(company))
def test_retrieve_by_name(self): """ Examples can be retrieved by name. """ new_example = Example( name=self.name, ) with transaction(): self.example_store.create(new_example) retrieved_example = self.example_store.retrieve_by_name( self.name ) assert_that(retrieved_example, is_(equal_to(new_example)))