def test_collect_query_parameters(self): json = { "find": ["id", "title", "content", { "comments": ["id", "comment"] }], "where": { "id": 13 } } initial_state = Immutable(data=None, errors=[]) process = compose_list([ change("json", json), change("type", Article), collect_query_parameters ]) final_state = process(initial_state) qp = final_state.query_parameters self.assertEquals(qp["properties"], [ Article.id, Article.title, Article.content, Comment.id, Comment.comment ]) self.assertEquals(qp["joins"], [Article.comments]) self.assertEquals(str(qp["conditions"][0]), str(Article.id == 13)) self.assertEquals(qp["first_property"], Article.id)
def get_transform_from_json(self, json): initial_state = Immutable(data=None, errors=[]) process = compose_list([ change("json", json), change("data", Article()), change("type", Article), transform_from_json ]) return process(initial_state)
def get_database_object_for(self, json, session): initial_state = Immutable(data=None, errors=[]) process = compose_list([ change("json", json), change("session", session), change("type", Article), get_database_object ]) return process(initial_state)
def test_run_queries(self): #given an article in the database session = Session() new_article = Article(title="Title", content="Content", comments=[ Comment(comment="my first comment"), Comment(comment="my second comment") ]) session.add(new_article) session.commit() id = new_article.id session.close() session = Session() queries = { "query_ids": session.query(Article.id).outerjoin(Article.comments).filter( Article.id == id).group_by(Article.id), "query": session.query(Article.id, Article.title, Article.content, Comment.id, Comment.comment).outerjoin( Article.comments).group_by( Article.id, Article.title, Article.content, Comment.id, Comment.comment), "first_property": Article.id } initial_state = Immutable(data=None, errors=[]) process = compose_list([ change("queries", queries), change("type", Article), change("session", session), run_queries ]) final_state = process(initial_state) results = final_state.data self.assertEquals(results[0][0], id) self.assertEquals(results[0][1], "Title") self.assertEquals(results[0][2], "Content") self.assertEquals(results[0][4], "my first comment") self.assertEquals(results[1][0], id) self.assertEquals(results[1][1], "Title") self.assertEquals(results[1][2], "Content") self.assertEquals(results[1][4], "my second comment")
def test_transform_to_json_article(self): initial_state = Immutable(data=None, errors=[]) process = compose_list([ change("data", Article(title="title", content="content")), change("type", Article), transform_to_json ]) final_state = process(initial_state) #assert the object contains the values json = value("data")(final_state) self.assertTrue(json.has_key("id")) self.assertEquals("title", json["title"]) self.assertEquals("content", json["content"])
def save(json, session): initial_state = Immutable(data=None, errors=[]) process = compose_list([ change("json", json), change("session", session), change("type", Article), get_database_object, transform_from_json, save_database_object, transform_to_json, to_json ]) final_state = process(initial_state) return final_state
def query(json, session): initial_state = Immutable(data=None, errors=[]) process = compose_list([ change("json", json), change("session", session), change("type", Article), collect_query_parameters, generate_queries, run_queries, transform_results, to_json ]) # transform("data", "data"), final_state = process(initial_state) return final_state
def test_save_database_object(self): initial_state = Immutable(data=None, errors=[]) title = "Saved "+str(random()) process = compose_list([ change("data", Article(title=title, content="Added content")), change("session", Session()), save_database_object ]) final_state = process(initial_state) # assert it exists in the database saved_object = Session().query(Article).filter(Article.title==title).first() self.assertEquals(title, saved_object.title) self.assertEquals("Added content", saved_object.content)
def save_database_object(state): debug(save_database_object.__name__, "obj:", state.data) obj = state.data if not obj.id: state.session.add(obj) state.session.commit() state.session.refresh(obj) return change("data", obj)(state)
def test_to_json(self, to_json): json = {"title": "A title", "content": "Some content"} initial_state = Immutable(data=None, errors=[]) process = compose_list([change("data", json), to_json]) final_state = process(initial_state) json_final = loads(final_state) #assert the object contains the values self.compareRecursively({"data": json}, json_final, [])
def test_change(self): #given an immutable object initial_state = Immutable(data=None, errors=[]) #when we channge it json = {"name": "Laci"} final_state = change("json", json)(initial_state) #assert it has the new value self.assertEquals(json, value("json")(final_state))
def transform_to_json(state): debug(transform_to_json.__name__, "data:", state.data) obj = value("data")(state) json = {} transformer_cls = transformers[state.type.__name__] transformer = transformer_cls() transformer.to_json(json, obj) return change("data", json)(state)
def get_database_object(state): debug(get_database_object.__name__, "json:", state.json) database_obj=None json = state.json if json.has_key("id") and (isinstance(json["id"],int) or isinstance(json["id"],long)): database_obj = state.session.query(state.type).get(json["id"]) if not database_obj: database_obj = state.type() return change("data", database_obj)(state)
def collect_query_parameters(state): query_parameters = {"properties": [], "joins": [], "conditions": []} type_dict = state.type.__dict__ collect_properties_and_relations(query_parameters, state.json["find"], type_dict) for where_key in state.json["where"].keys(): val = state.json["where"][where_key] query_parameters["conditions"].append(type_dict[where_key] == val) query_parameters["first_property"] = query_parameters["properties"][0] return change("query_parameters", query_parameters)(state)
def test_transform_results(self): results = [(1, "my title", "my content", 10, "my first comment"), (1, "my title", "my content", 11, "my second comment")] initial_state = Immutable(data=None, errors=[]) process = compose_list([ change( "json", { "find": [ "id", "title", "content", { "comments": ["id", "comment"] } ] }), change("data", results), transform_results ]) final_state = process(initial_state) expected_json = { 1: { "id": 1, "title": "my title", "content": "my content", "comments": { 10: { "id": 10, "comment": "my first comment" }, 11: { "id": 11, "comment": "my second comment" } } } } self.compareRecursively(expected_json, final_state.data, [])
def test_generate_queries(self): qp = { "properties": [ Article.id, Article.title, Article.content, Comment.id, Comment.comment ], "joins": [Article.comments], "conditions": [Article.id == 13] } session = Session() initial_state = Immutable(data=None, errors=[]) process = compose_list([ change("query_parameters", qp), change("type", Article), change("session", session), generate_queries ]) final_state = process(initial_state) queries = final_state.queries self.assertEquals( str(queries["query_ids"]), str( session.query(Article.id).outerjoin(Article.comments).filter( Article.id == 13).group_by(Article.id))) self.assertEquals( str(queries["query"]), str( session.query( Article.id, Article.title, Article.content, Comment.id, Comment.comment).outerjoin(Article.comments).group_by( Article.id, Article.title, Article.content, Comment.id, Comment.comment))) self.assertEquals(queries["first_property"], Article.id)
def run_queries(state): debug("run_queries") queries = state.queries query_ids = queries["query_ids"] query = queries["query"] first_property = queries["first_property"] debug(" query_ids", str(query_ids)) ids = query_ids.all() if len(ids) > 0: query = query.filter(first_property.in_(map(lambda id: id[0], ids))) debug(" query", str(query)) results = query.all() else: results = [] return change("data", results)(state)
def generate_queries(state): query_parameters = state.query_parameters session = state.session properties = query_parameters["properties"] joins = query_parameters["joins"] where_clauses = query_parameters["conditions"] first_property = properties[0] query_ids = session.query(first_property).outerjoin(*joins) query = session.query(*properties).outerjoin(*joins) for where_clause in where_clauses: query_ids = query_ids.filter(where_clause) query = query.group_by(*properties) query_ids = query_ids.group_by(first_property) return change( "queries", dict(query_ids=query_ids, query=query, first_property=first_property))(state)
def transform_results(state): json = transform_results_to_json(state, state.data) return change("data", json)(state)