samedi 31 janvier 2015

To test if a model was saved before passed on to task queue?

In the following method the correct behaviour would to to save the news model before passing its id to the asynchronous Task queue for further processing. Otherwise the task queue has the older version.


As you can see I have made a mistake and am saving the model after sending its id to the task queue.



def save_scraped_body_into_model(url_string):
news = ndb.Key(urlsafe=url_string).get()
...
news.body = result
news.stage = 1
taskqueue.Task(url='/api/v1.0/worker/bbc-stage-2', headers=header,
payload=json.dumps({'news_url_string': news.key.urlsafe()})).add(queue_name='newstasks')
news.put()


Do I possibly test for that? My test below passes as long as the model was saved. But thats wrong, the order matters, and this test doesn't capture it !!! Is there a way to achieve this with mock?



def test_news_instance_saved_before_next_stage(self, get_head):
BBCSpider.save_scraped_body_into_model(self.news.key.urlsafe())
context = ndb.get_context()
context.clear_cache()
news = ndb.Key(urlsafe=self.news.key.urlsafe()).get()
self.assertEqual(news.stage, 1)

Aucun commentaire:

Enregistrer un commentaire