def testDeleteIndexes_deleteOne(self):
     current_indexes = [
         index_api.BuildIndexProto(
             index_api.NO_ANCESTOR, 'Cat', self.Project(), [
                 datastore_index.Property(name=str('name'),
                                          direction='asc'),
                 datastore_index.Property(name=str('age'),
                                          direction='desc'),
                 datastore_index.Property(name=str('y'), direction='asc')
             ]),
         index_api.BuildIndexProto(
             index_api.ALL_ANCESTORS, 'Cat', self.Project(), [
                 datastore_index.Property(name=str('name'),
                                          direction='desc'),
                 datastore_index.Property(name=str('age'), direction='asc')
             ])
     ]
     current_indexes[0].indexId = str('123')
     current_indexes[1].indexId = str('456')
     self.projects_indexes.List.Expect(
         request=self.messages.DatastoreProjectsIndexesListRequest(
             projectId=self.Project()),
         response=self.messages.GoogleDatastoreAdminV1ListIndexesResponse(
             indexes=current_indexes))
     self.MakeApp()
     self.WriteInput('y\n')
     self.Run('datastore indexes cleanup ' + self.FullPath('index.yaml'))
     expected_indexes_to_be_deleted = {
         index_api.ApiMessageToIndexDefinition(current_indexes[1])[0]
     }
     self.delete_indexes.assert_called_once_with(
         'fakeproject', expected_indexes_to_be_deleted)
    def testDeleteIndexes_deleteAll(self):
        current_indexes = [
            index_api.BuildIndexProto(
                index_api.NO_ANCESTOR, 'Cat', self.Project(), [
                    datastore_index.Property(name=str('name'),
                                             direction='asc'),
                    datastore_index.Property(name=str('age'), direction='asc')
                ]),
            index_api.BuildIndexProto(
                index_api.ALL_ANCESTORS, 'Cat', self.Project(), [
                    datastore_index.Property(name=str('name'),
                                             direction='desc'),
                    datastore_index.Property(name=str('age'), direction='asc')
                ])
        ]
        self.projects_indexes.List.Expect(
            request=self.messages.DatastoreProjectsIndexesListRequest(
                projectId=self.Project()),
            response=self.messages.GoogleDatastoreAdminV1ListIndexesResponse(
                indexes=current_indexes))

        f = self.Touch(self.temp_path, 'index.yaml', 'indexes:\n')
        self.WriteInput('y\n')
        self.Run('datastore indexes cleanup ' + f)
        expected_indexes_to_be_deleted = {
            index_api.ApiMessageToIndexDefinition(index)[0]
            for index in current_indexes
        }
        self.delete_indexes.assert_called_once_with(
            'fakeproject', expected_indexes_to_be_deleted)
 def testIndexProtoToDefinitionRoundTrip(self):
   proto = index_api.BuildIndexProto(
       index_api.ALL_ANCESTORS, KIND, PROJECT_ID, [
           datastore_index.Property(name=str('prop1'), direction=str('asc')),
           datastore_index.Property(name=str('prop2'), direction=str('desc')),
       ])
   self.assertEqual(
       proto,
       self._IndexDefinitionToApiMessage(
           PROJECT_ID,
           index_api.ApiMessageToIndexDefinition(proto)[1]))
def BuildIndex(is_ancestor, kind, properties):
    index = datastore_index.Index(kind=str(kind),
                                  properties=[
                                      datastore_index.Property(
                                          name=str(prop[0]), direction=prop[1])
                                      for prop in properties
                                  ])
    index.ancestor = is_ancestor
    return index
    def testDeleteIndexes_withKeyProperty(self):
        current_indexes = [
            index_api.BuildIndexProto(
                index_api.NO_ANCESTOR, 'Cat', self.Project(), [
                    datastore_index.Property(name=str('name'),
                                             direction='asc'),
                    datastore_index.Property(name=str('age'), direction='desc')
                ]),
            index_api.BuildIndexProto(
                index_api.NO_ANCESTOR, 'Cat', self.Project(), [
                    datastore_index.Property(name=str('name'),
                                             direction='asc'),
                    datastore_index.Property(name=str('city'),
                                             direction='desc')
                ])
        ]
        self.projects_indexes.List.Expect(
            request=self.messages.DatastoreProjectsIndexesListRequest(
                projectId=self.Project()),
            response=self.messages.GoogleDatastoreAdminV1ListIndexesResponse(
                indexes=current_indexes))
        self.MakeApp()
        self.WriteConfig(('index.yaml', """
    indexes:
    - kind: Cat
      ancestor: no
      properties:
      - name: name
      - name: age
        direction: desc
    - kind: Cat
      ancestor: no
      properties:
      - name: name
      - name: city
        direction: desc
      - name: __key__
    """))

        self.WriteInput('y\n')
        self.Run('datastore indexes cleanup ' + self.FullPath('index.yaml'))
        expected_indexes_to_be_deleted = set([])
        self.delete_indexes.assert_called_once_with(
            'fakeproject', expected_indexes_to_be_deleted)
 def testDeleteIndexes_nothingToDelete(self):
     current_indexes = [
         index_api.BuildIndexProto(
             index_api.NO_ANCESTOR, 'Cat', self.Project(), [
                 datastore_index.Property(name=str('name'),
                                          direction='asc'),
                 datastore_index.Property(name=str('age'),
                                          direction='desc'),
                 datastore_index.Property(name=str('y'), direction='asc')
             ]),
     ]
     self.projects_indexes.List.Expect(
         request=self.messages.DatastoreProjectsIndexesListRequest(
             projectId=self.Project()),
         response=self.messages.GoogleDatastoreAdminV1ListIndexesResponse(
             indexes=current_indexes))
     self.MakeApp()
     self.WriteInput('y\n')
     self.Run('datastore indexes cleanup ' + self.FullPath('index.yaml'))
     self.delete_indexes.assert_called_once_with('fakeproject', set([]))
def ApiMessageToIndexDefinition(proto):
    """Converts a GoogleDatastoreAdminV1Index to an index definition structure."""
    properties = []
    for prop_proto in proto.properties:
        prop_definition = datastore_index.Property(name=str(prop_proto.name))
        if prop_proto.direction == DESCENDING:
            prop_definition.direction = 'desc'
        else:
            prop_definition.direction = 'asc'
        properties.append(prop_definition)
    index = datastore_index.Index(kind=str(proto.kind), properties=properties)
    if proto.ancestor is not NO_ANCESTOR:
        index.ancestor = True
    return proto.indexId, index
Beispiel #8
0
 def testCreate_indexAlreadyExists(self, create_indexes):
     self.projects_indexes.List.Expect(
         request=self.messages.DatastoreProjectsIndexesListRequest(
             projectId=self.Project()),
         response=self.messages.GoogleDatastoreAdminV1ListIndexesResponse(
             indexes=[
                 index_api.BuildIndexProto(
                     index_api.NO_ANCESTOR, 'Cat', self.Project(), [
                         datastore_index.Property(name=str('name'),
                                                  direction=str('asc')),
                         datastore_index.Property(name=str('age'),
                                                  direction=str('desc')),
                     ])
             ]))
     self.strict = False
     self.MakeApp()
     self.WriteInput('y\n')
     self.WriteConfig(('index.yaml', """
 indexes:
 - kind: Cat
   ancestor: no
   properties:
   - name: name
   - name: age
 - kind: Cat
   ancestor: no
   properties:
   - name: name
   - name: age
     direction: desc
 """))
     self.Run('datastore indexes create ' + self.FullPath('index.yaml'))
     create_indexes.assert_called_once_with(
         'fakeproject', {
             index_api.BuildIndex(False, str('Cat'), [(str('name'), 'asc'),
                                                      (str('age'), 'asc')])
         })