Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8568587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:08:43+00:00 2026-06-11T18:08:43+00:00

My approach to this question might be completely wrong, so please don’t hesitate to

  • 0

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.

I am using:

  • Google App Engine 1.7.1
  • GAE High Replication Datastore
  • Python 2.7.3

I setup my tests using the boilerplate code:

self.testbed = testbed.Testbed()
self.testbed.activate()
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=0)
self.testbed.init_datastore_v3_stub()

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:

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()

My test is that my web api returns:

{
    "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/"
        }
    }
}

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:

artist.key().id_or_name()

In the case of a mock object, I supplied the key name on construction with:

key_name=str(artist_id) 

For non-mock construction, I did not include that line of code and let GAE assign the id.

Since my template used key().id_or_name() to output the property, all went well and the tests passed.

However, now when I test the return of an individual artist which would be available by following http://my_server.com/artist/5/, my test fails. To grab the artist out of the datastore, I use the following code:

def retrieve_artist_by(id):
    artist = dto.Artist.get_by_id()

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.

I was hoping for something similar to:

artist = dto.Artist.get_by_id_or_name()

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:08:45+00:00Added an answer on June 11, 2026 at 6:08 pm

    Here is what I am using right now. I don’t like it, but it should be performant in prod as I will be using generated ids. In test, if it doesn’t find it by id, it will attempt to look up by name.

    def retrieve_artist_by(id):
        artist = dto.Artist.get_by_id(id)
    
        if artist is None:
            artist = dto.Artist.get_by_key_name(str(id))
    
    
        return artist
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question, is this the correct approach to make a Generic Singleton?
This may be a bit of a silly question and I might also have
The very act of asking this question suggests that my approach to this problem
I find this question a little tricky. Maybe someone knows an approach to answer
This is more of an approach question, I haven't gotten to the code yet.
I'm not sure how to approach this SQL statement so please advise, even if
This question might have been asked many times, I have been having difficulty in
This question has been asked before in earlier versions of MVC. There is also
This is more of a challenge question than something I urgently need, so don't
I struggled with the title for this question, it is hard to summarize into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.