def test_get_new_database_object(self):
        # when invoking the function with no id in the json
        final_state = self.get_database_object_for({}, Session())

        #assert it returns a new object, associated with the session
        new_object = value("data")(final_state)
        self.assertEquals(Article, type(new_object))
    def test_transform_from_json_article(self):
        # when invoking the function
        json = {"title": "A title", "content": "Some content"}
        final_state = self.get_transform_from_json(json)

        #assert the object contains the values
        transformed_object = value("data")(final_state)

        self.assertEquals(json["title"], transformed_object.title)
        self.assertEquals(json["content"], transformed_object.content)
Пример #3
0
    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))
Пример #4
0
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 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 test_get_existing_database_object(self):
        #given an exising object in the database
        session = Session()
        new_article = Article(title="Title", content="Content")
        session.add(new_article)
        session.commit()
        id = new_article.id
        session.close()

        # when invoking the function with no id in the json
        final_state = self.get_database_object_for({"id":id}, Session())

        #assert it returns a new object, associated with the session
        existing_object = value("data")(final_state)
        self.assertEquals(Article, type(existing_object))
        self.assertEquals("Title", existing_object.title)
    def test_transform_from_json_article_with_comments(self):
        # when invoking the function
        json = {
            "title":
            "A title",
            "content":
            "Some content",
            "comments": [{
                "comment": "This was awesome!"
            }, {
                "comment": "I loved it as well!"
            }]
        }
        final_state = self.get_transform_from_json(json)

        #assert the object contains the values
        transformed_object = value("data")(final_state)

        self.assertEquals(json["title"], transformed_object.title)
        self.assertEquals(json["content"], transformed_object.content)
        self.assertEquals(json["comments"][0]["comment"],
                          transformed_object.comments[0].comment)
        self.assertEquals(json["comments"][1]["comment"],
                          transformed_object.comments[1].comment)