Juan,

I do local testing with the GAE development server, Python unittest and
Pylon's test framework WebTest
<http://webtest.pythonpaste.org/en/latest/index.html>. I use the datastore
stub, too, and some other stubs.

My app is based on my own framework with plenty of specific stuff, but I
haven't wrapped the NDB library. However, you probably don't need to care
about your NDB wrapper, since you can just test your handlers like any
other client (and with plain NDB calls, e.g. ndb.Query(), or key.get() to
validate what actually is in the datastore while running a test.

Maybe this is all you will need: write some test-only handlers that
demonstrate features of your NDB wrapper or framework. Then write some
tests that just use HTTP request/response and maybe simple NDB calls to
verify your NDB wrapper is intact.

This is an example how I test handlers:

import unittest

class MyTestCase(unittest.TestCase):
  def setup()
    import webtest
    # app from your webapp2.WSGIApplication() for example
    self.testapp = webtest.TestApp(app)
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    # Create a consistency policy that will simulate the
    # High Replication consistency model.
    # See 
https://cloud.google.com/appengine/docs/python/tools/localunittesting#Python_Writing_High_Replication_Datastore_tests
    self.policy =
datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)
    # Initialize the datastore stub with this policy.
    self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
    # you eventually want to add more stubs
    # ...
    # Clear ndb's in-context cache between tests.
    # This prevents data from leaking between tests.
    # Alternatively, you could disable caching by
    # using ndb.get_context().set_cache_policy(False)
    # ndb.get_context().clear_cache()
    ndb.get_context().set_cache_policy(False)

    # If you need initial data in datastore for test,
    # add them here (probably better to use the original NDB lib)

  def tearDown(self):
    self.testbed.deactivate()

  def testCreateRead(self):
    """Test the handler that create objects of entity
    kind 'Foo' and the handler that read Foo object with
    given ID.

    POST request will return a JSON object which contains the
    created foo objects in the `results` array
    GET request will return a JSON object which represents
    the retrieved foo object

    """
    # POST request to produce 2 foo objects, each with title
    payload = [
      {'title': 'hello'},
      {'title': 'world'}
    ]
    response = self.testapp.post_json('/api/v1/foos/create', payload,
headers={'Accept': 'application/json'}, status='*')
    # assertions as in
https://docs.python.org/2/library/unittest.html#test-cases
    self.assertEqual(response.status_int, 201)
    self.assertTrue('results' in response.json, 'response to create
foos has no results array')
    self.assertDictEqual(response.json, {'results': [{'id': 1,
'title': 'hello'}, {'id': 2, 'title': 'world'}]}, 'result in create
foo response is wrong')
    # you can also use NDB in tests
    self.assertEqual(ndb.Query(kind='Foo').count(), 2, 'expected 2 foo created')

    # GET request with JSON response
    response = self.testapp.get('/api/v1/foo/1', headers={'Accept':
'application/json'}, status='*', xhr=True)
    self.assertEqual(response.status_int, 200)
    self.assertDictEqual(response.json, {'id': 1, 'title': 'hello'},
'response to getting foo #1 is wrong')


if __name__ == '__main__':
  unittest.main()



I haven't run this particular code sample though ;-)

Good luck,
Ani


On 10 June 2016 at 13:55, Juan Antonio Fernández Sánchez <
[email protected]> wrote:

>
> I'm really confused with the way to try Datastore in local. Please, give
> me a minute to explain.
>
> I'm developing a app composed to few microservices like a only gae app. In
> a  parte of the app, I use the datastore. So when I run my app, I use the
> development server and when I save something in the datastore calling some
> method I can see perfectly the entity in the gae's admin web portal.
>
> Well, now, instead of calling directly to ndb library and his methods I've
> built a  small library over ndb to abstract his functionallity, then I can
> call insertUser() instead of work directly with ndb. So, the problems
> appear when I try test this small library that I built (I've written a
> test.py file to do this).
> At first, I thought  that this does not  can work because this test was
> executing without the deveserver running. After I searched info about how
> simulated the datastore in the local and I found this, but after I found
> too the unittest in local with the stubs, and now I don't understand
> nothing.
>
> I've tried both (gcloud datastore emulator and stub with unittest)  and I
> don't get  do simple example:
> I want test that a entity is saved in Datastore and after I want test that
> I can read this entity
>
> I suppose that dev_server (in SDK) emulate the datastore (because I can
> see the list of my entities there), but then, why use the datastore
> emulator in local dev?, and then, why is necesary uses the stub to
> datastore if we have a datastore emulator to do all test that I want? I
> don't understand.
>
> I understand that maybe my question is more of concepts than code but I
> need understand really right how is the best way to work with this.
>
> LocalDev Server -->
> https://cloud.google.com/appengine/docs/python/tools/using-local-server
> UnitTest with Stub -->
> https://cloud.google.com/appengine/docs/python/tools/localunittesting
> DataStore Emulator -->
> https://cloud.google.com/datastore/docs/tools/datastore-emulator
>
> Thanks you so much, any help will be welcome :)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/google-appengine.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/google-appengine/cd123e20-c5fc-4faa-8717-49ddd012bd22%40googlegroups.com
> <https://groups.google.com/d/msgid/google-appengine/cd123e20-c5fc-4faa-8717-49ddd012bd22%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 

Mit freundlichen Grüßen / Kind regards

i. A.
Anastasios Hatzis

Fon: +49 8374 930813
Fax: +49 8374 930810
Mobil: +49 1520 8592878

-- 
HATZIS Edelstahlbearbeitung GmbH
Hojen 2
87490 Haldenwang (Allgäu)
Germany

Handelsregister Kempten (Allgäu): HRB 4204
Geschäftsführer: Paulos Hatzis, Charalampos Hatzis
Umsatzsteuer-Identifikationsnummer: DE 128791802
GLN: 42 504331 0000 6

http://www.hatzis.de/

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-appengine/CAABQH8Y_Gm-yd_y%2BCjDd5nuvsxjF1rEN65_Q7v6S80z_KCnQTA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to