示例#1
0
    def test_can_delete_records(self):
        with mock.patch('xml2kinto.records.kinto.Client'):
            records = KintoRecords(('issuerName', 'serialNumber'),
                                   options={
                                       'server': 'http://example.com/v1',
                                       'auth': ('user', 'pass'),
                                       'bucket_name': 'blocklists',
                                       'collection_name': 'certificates',
                                       'permissions': {}})

        with mock.patch.object(records, 'client'):
            records.delete({'id': 'foobar'})
            records.client.delete_record.assert_called_with('foobar')
示例#2
0
    def test_can_create_data(self):
        with mock.patch('xml2kinto.records.kinto.Client'):
            records = KintoRecords(('issuerName', 'serialNumber'),
                                   options={
                                       'server': 'http://example.com/v1',
                                       'auth': ('user', 'pass'),
                                       'bucket_name': 'blocklists',
                                       'collection_name': 'certificates',
                                       'permissions': {}})

        with mock.patch.object(records, 'client') as mocked_client:
            mocked_client.create_record = lambda x: x
            record = records.create({'issuerName': 'foo',
                                     'serialNumber': 'bar'})
            assert 'id' in record
示例#3
0
def synchronize(fields, xml_options, kinto_options):
    print("Working on %r" % kinto_options["server"])
    print("Reading data from %r" % xml_options["filename"])
    xml = XMLRecords(fields, options=xml_options)
    kinto = KintoRecords(fields, options=kinto_options)
    to_delete = []
    to_update = []
    to_create = []

    print("Syncing to %s%s/records" % (kinto_options["server"], kinto_options["collection_name"]))

    # looking at kinto to list records
    # to delete or to update
    for record in kinto.records:
        xml_rec = xml.find(record["id"])
        if xml_rec is None:
            to_delete.append(record)
        else:
            if not same_record(fields, xml_rec, record):
                to_update.append(xml_rec)

    # new records ?
    for record in xml.records:
        kinto_rec = kinto.find(record["id"])
        if not kinto_rec:
            to_create.append(record)

    print("- %d records to create." % len(to_create))
    print("- %d records to delete." % len(to_delete))
    print("- %d records to update." % len(to_update))

    for record in to_delete:
        try:
            kinto.delete(record)
        except KintoException as e:
            raise SynchronizationError(e.response.content)

    for record in to_create + to_update:
        try:
            kinto.create(record)
        except KintoException as e:
            raise SynchronizationError(e.response.content)

    print("Done!")