def test_application_of_changeset(self): c = connections['lds_sample'].cursor() self.lds_sample = Dataset.objects.get(pk='lds_sample') self.root_user = User.objects.get(username='******') self.layer_in_dataset = self.lds_sample.layerindataset_set.all()[0] self.dataset_update = DatasetUpdate.objects.create( dataset=self.lds_sample, from_version="2012-01-01", to_version="2012-07-01", seq=1, owner=self.root_user) self.assertTrue(self.dataset_update.id) osm.apply_changeset_to_dataset(self.dataset_update, "sign_pnt_update_2012_07_01", self.layer_in_dataset) # Should have updated 2 and 5 c.execute("SELECT name, ST_AsEWKT(wkb_geometry) FROM sign_pnt WHERE id = 2;"); r = c.fetchone() self.assertEqual("Greater Beaver Metro Area", r[0]) beaver_point = geos.GEOSGeometry(r[1]) self.assertEqual(4326, beaver_point.srid) self.assertEqual(169, beaver_point.x) self.assertEqual(-41, beaver_point.y) c.execute("SELECT name FROM sign_pnt WHERE id = 5;"); self.assertEqual("Elephantopolis", c.fetchone()[0]) # Should have marked the UPDATE on 2 onto the WorksliceFeature wf = WorksliceFeature.objects.get(layer_in_dataset=self.layer_in_dataset, feature_id=2) self.assertEqual("updated", wf.get_dirty_display()) # Should have deleted 3 and 6 c.execute("SELECT count(*) FROM sign_pnt WHERE id IN (3, 6);") self.assertEqual(0, c.fetchone()[0]) # Should have marked the DELETE on 3 onto the WorksliceFeature wf = WorksliceFeature.objects.get(layer_in_dataset=self.layer_in_dataset, feature_id=3) self.assertEqual("deleted", wf.get_dirty_display()) # Should have inserted 7 c.execute("SELECT name FROM sign_pnt WHERE id = 7;") self.assertEqual("Giraffeside", c.fetchone()[0]) # Should have updated the version on the dataset # FIXME: this happens in DatasetUpdate#run, so can't be tested without testing that method #self.lds_sample = Dataset.objects.get(name=self.lds_sample.name) #self.assertEqual("2012-07-01", self.lds_sample.version) # Should have updated the feature count on the dataset self.layer_in_dataset = LayerInDataset.objects.get(pk=self.layer_in_dataset.id) self.assertEqual(5, self.layer_in_dataset.features_total)
def run(self): # FIXME: tests for changeset retrieval, etc. try: for lid in self.dataset.layerindataset_set.all(): layer = lid.layer table_name = "%s_update_%s" % (layer.name, self.to_version.replace("-", "_")) import_proc = subprocess.Popen([ settings.LINZ2OSM_SCRIPT_ROOT + '/load_lds_dataset.sh', 'update', self.dataset.database_name, layer.wfs_type_name, table_name, settings.LINZ_DATA_SERVICE_API_KEY, 'from:%s;to:%s' % (self.from_version, self.to_version), urllib.quote(layer.wfs_cql_filter) ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) import_proc.wait() import_output = import_proc.stdout.read() print import_output if import_proc.returncode != 0: raise DatasetUpdateError("Import of %s failed with return code %d and output:\n%s" % (layer.name, import_proc.returncode, import_output)) with transaction.commit_manually(): with transaction.commit_manually(using=self.dataset.name): try: osm.apply_changeset_to_dataset(self, table_name, lid) except: print "ERROR - ROLLBACK" transaction.rollback(using=self.dataset.name) transaction.rollback() raise else: transaction.commit(using=self.dataset.name) transaction.commit() except DatasetUpdateError as e: self.error = unicode(e) except Exception as e: print "Unexpected other exception:", sys.exc_info()[0] self.error = unicode(e) raise else: self.complete = True self.dataset.version = self.to_version self.dataset.save() finally: self.save()