Note that there are some explanatory texts on larger screens.

plurals
  1. POATDD Google App Engine with Python - Trouble with Keys
    primarykey
    data
    text
    <p>My approach to this question might be completely wrong, so please don't hesitate to correct it. I also added ATDD in the question title, because I am trying to test the output of my web api which is bigger than just a unit test.</p> <p>I am using:</p> <ul> <li>Google App Engine 1.7.1</li> <li>GAE High Replication Datastore </li> <li>Python 2.7.3</li> </ul> <p>I setup my tests using the boilerplate code:</p> <pre><code>self.testbed = testbed.Testbed() self.testbed.activate() self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=0) self.testbed.init_datastore_v3_stub() </code></pre> <p>Then I call a mock object setup to setup my entities and persist them to the testbed datastore. I use a counter to add 5 artists with artist_id 1 - 5:</p> <pre><code>def add_mock_artist_with(self, artist_id, link_id, name): new_artist = dto.Artist(key_name=str(artist_id), name=name, link_id= str(link_id)) new_artist.put() </code></pre> <p>My test is that my web api returns:</p> <pre><code>{ "artists": [ { "artist": { "name": "Artist 1", "links": { "self": { "href": "https://my_server.com/artist/1/" } } } }, . . . { "artist": { "name": "Artist 5", "links": { "self": { "href": "https://my_server.com/artist/5/" } } } } ], "links": { "self": { "href": "https://my_server.com/artists/" } } } </code></pre> <p>Initially I thought that if I were to startup a new test bed every time I run the tests, I could count on my artists being entered sequentially into the data store and therefore would get ids 1 - 5. My tests passed initially, but over time started to fail because of ids not matching (I would get a link like: "href": "https://my_server.com/artist/78/"). I felt a little guilty relying on ids being generated sequentially, so I resolved to fix it. I stumbled upon the concept of a key being either a name or a generated id. I updated my templates for the returned JSON to use: </p> <pre><code>artist.key().id_or_name() </code></pre> <p>In the case of a mock object, I supplied the key name on construction with:</p> <pre><code>key_name=str(artist_id) </code></pre> <p>For non-mock construction, I did not include that line of code and let GAE assign the id. </p> <p>Since my template used key().id_or_name() to output the property, all went well and the tests passed. </p> <p>However, now when I test the return of an individual artist which would be available by following <a href="http://my_server.com/artist/5/" rel="nofollow">http://my_server.com/artist/5/</a>, my test fails. To grab the artist out of the datastore, I use the following code:</p> <pre><code>def retrieve_artist_by(id): artist = dto.Artist.get_by_id() </code></pre> <p>In production, this will work fine because it will all be id based. However, in my test, it is not finding anything because I have used the key_name=str(artist_id) in my mock construction, and the name is not the same as the id.</p> <p>I was hoping for something similar to:</p> <pre><code>artist = dto.Artist.get_by_id_or_name() </code></pre> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload