Example #1
0
    def test_tr55_job_runs_in_chain(self):
        # For the purposes of this test, there are no modifications
        self.model_input['modification_pieces'] = []

        # Get the job chain
        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        # Make sure the chain is well-formed
        self.assertTrue('geoprocessing.start' in str(job_chain[0]))
        self.assertTrue('geoprocessing.finish' in str(job_chain[1]))

        # Modify the chain to prevent it from trying to talk to endpoint
        job_chain = [get_test_histogram.s()] + job_chain[2:]
        task_list = chain(job_chain).apply_async()

        found_job = Job.objects.get(uuid=task_list.id)

        self.assertEqual(str(found_job.uuid),
                         str(task_list.id),
                         'Job not found')

        self.assertEqual(str(found_job.status),
                         'complete',
                         'Job found but incomplete.')
Example #2
0
    def test_tr55_chain_skips_census_if_census_is_up_to_date(self):
        # Census with current modification_hash
        self.model_input['census'] = {
            'cell_count':
            100,
            'distribution': {
                'c:commercial': {
                    'cell_count': 70
                },
                'a:deciduous_forest': {
                    'cell_count': 30
                }
            },
            'modification_hash':
            '39f488abec2de49f17652631ae843946',
            'modifications': [{
                'bmp': 'no_till',
                'cell_count': 1,
                'distribution': {
                    'a:deciduous_forest': {
                        'cell_count': 1
                    },
                }
            }]
        }

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        self.assertFalse('tasks.prepare_census' in str(job_chain),
                         'Census preparation should be skipped')
    def test_tr55_job_error_in_chain(self):
        model_input = {
            'inputs': [],
            'area_of_interest': {
                'type':
                'MultiPolygon',
                'coordinates': [[[[-75.06271362304688, 40.15893480687665],
                                  [-75.2728271484375, 39.97185812402586],
                                  [-74.99130249023438, 40.10958807474143],
                                  [-75.06271362304688, 40.15893480687665]]]]
            },
            'modification_pieces': [],
            'modifications': [],
            'modification_hash': 'j39fj9fg7yshb399h4nsdhf'
        }

        job_chain = views._construct_tr55_job_chain(model_input, self.job.id)

        self.assertTrue('geoprocessing.run' in str(job_chain[0]))

        job_chain = [get_test_histogram.s()] + job_chain[2:]

        with self.assertRaises(Exception) as context:
            chain(job_chain).apply_async()

        self.assertEqual(str(context.exception),
                         'No precipitation value defined',
                         'Unexpected exception occurred')
Example #4
0
    def test_tr55_chain_generates_census_if_census_is_stale(self):
        # Census with stale modification_hash
        self.model_input['census'] = {
            'cell_count':
            100,
            'distribution': {
                'c:commercial': {
                    'cell_count': 70
                },
                'a:deciduous_forest': {
                    'cell_count': 30
                }
            },
            'modification_hash':
            'j3jk3jk3jn3knm3nmn39usd',
            'modifications': [{
                'bmp': 'no_till',
                'cell_count': 1,
                'distribution': {
                    'a:deciduous_forest': {
                        'cell_count': 1
                    },
                }
            }]
        }

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        self.assertTrue('tasks.prepare_census' in str(job_chain),
                        'Census preparation should not be skipped')
Example #5
0
    def test_tr55_chain_generates_both_censuses_if_they_are_missing(self):
        """If neither the AoI censuses or the modification censuses exist,
        they are both generated.
        """
        self.model_input['aoi_census'] = None
        self.model_input['modification_censuses'] = None

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        skipped_tasks = []

        needed_tasks = [
            'start_histograms_job', 'get_histogram_job_results',
            'histograms_to_censuses', 'run_tr55'
        ]

        self.assertTrue(
            all([
                True if t in str(job_chain) else False for t in skipped_tasks
            ]), 'unnecessary job in chain')

        self.assertTrue(
            all([True if t in str(job_chain) else False
                 for t in needed_tasks]), 'missing necessary job in chain')
Example #6
0
    def test_tr55_chain_skips_census_if_census_is_up_to_date(self):
        # Census with current modification_hash
        self.model_input['census'] = {
            'cell_count': 100,
            'distribution': {
                'c:commercial': {
                    'cell_count': 70
                },
                'a:deciduous_forest': {
                    'cell_count': 30
                }
            },
            'modification_hash': '39f488abec2de49f17652631ae843946',
            'modifications': [{
                'bmp': 'no_till',
                'cell_count': 1,
                'distribution': {
                    'a:deciduous_forest': {'cell_count': 1},
                }
            }]
        }

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        self.assertFalse('tasks.prepare_census' in str(job_chain),
                         'Census preparation should be skipped')
Example #7
0
    def test_tr55_chain_generates_census_if_census_is_stale(self):
        # Census with stale modification_hash
        self.model_input['census'] = {
            'cell_count': 100,
            'distribution': {
                'c:commercial': {
                    'cell_count': 70
                },
                'a:deciduous_forest': {
                    'cell_count': 30
                }
            },
            'modification_hash': 'j3jk3jk3jn3knm3nmn39usd',
            'modifications': [{
                'bmp': 'no_till',
                'cell_count': 1,
                'distribution': {
                    'a:deciduous_forest': {'cell_count': 1},
                }
            }]
        }

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        self.assertTrue('tasks.prepare_census' in str(job_chain),
                        'Census preparation should not be skipped')
Example #8
0
    def test_tr55_job_error_in_chain(self):
        model_input = {
            'inputs': [],
            'area_of_interest': {
                'type': 'MultiPolygon',
                'coordinates': [[
                    [[-75.06271362304688, 40.15893480687665],
                     [-75.2728271484375, 39.97185812402586],
                     [-74.99130249023438, 40.10958807474143],
                     [-75.06271362304688, 40.15893480687665]]
                ]]
            },
            'modification_pieces': [],
            'modifications': [],
            'modification_hash': 'j39fj9fg7yshb399h4nsdhf'
        }

        job_chain = views._construct_tr55_job_chain(model_input,
                                                    self.job.id)
        self.assertTrue('tasks.start_histograms_job' in str(job_chain[0]))
        self.assertTrue('tasks.get_histogram_job_results' in str(job_chain[1]))
        job_chain = [get_test_histogram.s()] + job_chain[2:]

        with self.assertRaises(Exception) as context:
            chain(job_chain).apply_async()

        self.assertEqual(str(context.exception),
                         'No precipitation value defined',
                         'Unexpected exception occurred')
Example #9
0
    def test_tr55_chain_generates_both_censuses_if_they_are_missing(self):
        """If neither the AoI censuses or the modification censuses exist,
        they are both generated.
        """
        self.model_input['aoi_census'] = None
        self.model_input['modification_censuses'] = None

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        skipped_tasks = []

        needed_tasks = [
            'start_histograms_job',
            'get_histogram_job_results',
            'histograms_to_censuses',
            'run_tr55'
        ]

        self.assertTrue(all([True if t in str(job_chain)
                             else False for t in skipped_tasks]),
                        'unnecessary job in chain')

        self.assertTrue(all([True if t in str(job_chain)
                            else False for t in needed_tasks]),
                        'missing necessary job in chain')
Example #10
0
    def test_tr55_job_runs_in_chain(self):
        # For the purposes of this test, there are no modifications
        self.model_input['modification_pieces'] = []

        # Get the job chain
        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        # Make sure the chain is well-formed
        self.assertTrue('tasks.start_histograms_job' in str(job_chain[0]))
        self.assertTrue('tasks.get_histogram_job_results' in str(job_chain[1]))

        # Modify the chain to prevent it from trying to talk to endpoint
        job_chain = [get_test_histogram.s()] + job_chain[2:]
        task_list = chain(job_chain).apply_async()

        found_job = Job.objects.get(uuid=task_list.id)

        self.assertEqual(str(found_job.uuid),
                         str(task_list.id),
                         'Job not found')

        self.assertEqual(str(found_job.status),
                         'complete',
                         'Job found but incomplete.')
Example #11
0
    def test_tr55_chain_doesnt_generate_aoi_census_if_it_exists_and_mods(self):
        """If the AoI census exists in the model input, and there are modifications,
        it should be provided to TR-55 and not generated.
        """
        self.model_input['aoi_census'] = {
            "distribution": {
                "b:developed_med": {
                    "cell_count": 155
                },
                "a:developed_high": {
                    "cell_count": 1044
                },
                "b:developed_high": {
                    "cell_count": 543
                },
                "d:developed_high": {
                    "cell_count": 503
                },
                "d:developed_med": {
                    "cell_count": 164
                },
                "a:developed_med": {
                    "cell_count": 295
                }
            },
            "cell_count": 2704
        }

        skipped_tasks = []

        # Job chain is the same as if no census exists because
        # we still need to generate modification censuses
        needed_tasks = [
            'start_histograms_job', 'get_histogram_job_results',
            'histograms_to_censuses', 'run_tr55'
        ]

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        cached_argument = ("cached_aoi_census={u'distribution': "
                           "{u'b:developed_med'"
                           ": {u'cell_count': 155}, u'a:developed_high': "
                           "{u'cell_count': 1044}, u'b:developed_high': "
                           "{u'cell_count': 543}, u'd:developed_high': "
                           "{u'cell_count': 503}, u'd:developed_med': "
                           "{u'cell_count': 164}, u'a:developed_med': "
                           "{u'cell_count': 295}}, u'cell_count': 2704})")

        self.assertTrue(
            all([
                True if t in str(job_chain) else False for t in skipped_tasks
            ]), 'unnecessary job in chain')

        self.assertTrue(
            all([True if t in str(job_chain) else False
                 for t in needed_tasks]), 'missing necessary job in chain')

        self.assertTrue(cached_argument in str(job_chain[3]))
Example #12
0
    def test_tr55_chain_doesnt_generate_censuses_if_they_exist(self):
        """If the AoI census and modifications exist in the model input,
        and the modification censuses are up-to-date (meaning the hash
        stored with the census matches the model input hash, neither census
        is generated and the censuses are passed directly to the model.
        """
        self.model_input['aoi_census'] = {
            "distribution": {
                "b:developed_med": {"cell_count": 155},
                "a:developed_high": {"cell_count": 1044},
                "b:developed_high": {"cell_count": 543},
                "d:developed_high": {"cell_count": 503},
                "d:developed_med": {"cell_count": 164},
                "a:developed_med": {"cell_count": 295}
            },
            "cell_count": 2704
        }
        self.model_input['modification_censuses'] = {
            "modification_hash": "4c23321de9e52f12e1b37460afc28db2",
            "censuses": [
                {
                    "distribution": {
                        "b:developed_med": {"cell_count": 5},
                        "a:developed_high": {"cell_count": 10},
                        "b:developed_high": {"cell_count": 4},
                        "d:developed_high": {"cell_count": 5},
                        "d:developed_med": {"cell_count": 2},
                        "a:developed_med": {"cell_count": 2}
                    },
                    "cell_count": 28,
                    "change": ":deciduous_forest:"
                }
            ]
        }

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        skipped_tasks = [
            'start_histograms_job',
            'get_histogram_job_results',
            'histograms_to_censuses'
        ]

        needed_tasks = [
            'run_tr55'
        ]

        self.assertFalse(all([True if t in str(job_chain)
                             else False for t in skipped_tasks]),
                         'unnecessary job in chain')

        self.assertTrue(all([True if t in str(job_chain)
                            else False for t in needed_tasks]),
                        'missing necessary job in chain')
Example #13
0
    def test_tr55_chain_doesnt_generate_censuses_if_they_exist(self):
        """If the AoI census and modifications exist in the model input,
        and the modification censuses are up-to-date (meaning the hash
        stored with the census matches the model input hash, neither census
        is generated and the censuses are passed directly to the model.
        """
        self.model_input['aoi_census'] = {
            "distribution": {
                "b:developed_med": {"cell_count": 155},
                "a:developed_high": {"cell_count": 1044},
                "b:developed_high": {"cell_count": 543},
                "d:developed_high": {"cell_count": 503},
                "d:developed_med": {"cell_count": 164},
                "a:developed_med": {"cell_count": 295}
            },
            "cell_count": 2704
        }
        self.model_input['modification_censuses'] = {
            "modification_hash": "4c23321de9e52f12e1b37460afc28db2",
            "censuses": [
                {
                    "distribution": {
                        "b:developed_med": {"cell_count": 5},
                        "a:developed_high": {"cell_count": 10},
                        "b:developed_high": {"cell_count": 4},
                        "d:developed_high": {"cell_count": 5},
                        "d:developed_med": {"cell_count": 2},
                        "a:developed_med": {"cell_count": 2}
                    },
                    "cell_count": 28,
                    "change": ":deciduous_forest:"
                }
            ]
        }

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        skipped_tasks = [
            'start',
            'finish',
            'nlcd_soil_census'
        ]

        needed_tasks = [
            'run_tr55'
        ]

        self.assertFalse(all([True if t in str(job_chain)
                             else False for t in skipped_tasks]),
                         'unnecessary job in chain')

        self.assertTrue(all([True if t in str(job_chain)
                            else False for t in needed_tasks]),
                        'missing necessary job in chain')
Example #14
0
    def test_tr55_chain_generates_modification_censuses_if_they_are_old(self):
        """If they modification censuses exist in the model input, but the
        hash stored with the censuses does not match the hash passed in
        with the model input, the modification censuses are regenerated.
        """
        self.model_input['aoi_census'] = {
            "distribution": {
                "b:developed_med": {"cell_count": 155},
                "a:developed_high": {"cell_count": 1044},
                "b:developed_high": {"cell_count": 543},
                "d:developed_high": {"cell_count": 503},
                "d:developed_med": {"cell_count": 164},
                "a:developed_med": {"cell_count": 295}
            },
            "cell_count": 2704
        }
        self.model_input['modification_censuses'] = {
            "modification_hash": "out-of-date-3jk34n9j3knk3kv",
            "censuses": [
                {
                    "distribution": {
                        "b:developed_med": {"cell_count": 5},
                        "a:developed_high": {"cell_count": 10},
                        "b:developed_high": {"cell_count": 4},
                        "d:developed_high": {"cell_count": 5},
                        "d:developed_med": {"cell_count": 2},
                        "a:developed_med": {"cell_count": 2}
                    },
                    "cell_count": 28,
                    "change": ":deciduous_forest:"
                }
            ]
        }

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        skipped_tasks = []

        needed_tasks = [
            'start_histograms_job',
            'get_histogram_job_results',
            'histograms_to_censuses',
            'run_tr55'
        ]

        self.assertTrue(all([True if t in str(job_chain)
                             else False for t in skipped_tasks]),
                        'unnecessary job in chain')

        self.assertTrue(all([True if t in str(job_chain)
                            else False for t in needed_tasks]),
                        'missing necessary job in chain')
Example #15
0
    def test_tr55_chain_generates_modification_censuses_if_they_are_old(self):
        """If they modification censuses exist in the model input, but the
        hash stored with the censuses does not match the hash passed in
        with the model input, the modification censuses are regenerated.
        """
        self.model_input['aoi_census'] = {
            "distribution": {
                "b:developed_med": {"cell_count": 155},
                "a:developed_high": {"cell_count": 1044},
                "b:developed_high": {"cell_count": 543},
                "d:developed_high": {"cell_count": 503},
                "d:developed_med": {"cell_count": 164},
                "a:developed_med": {"cell_count": 295}
            },
            "cell_count": 2704
        }
        self.model_input['modification_censuses'] = {
            "modification_hash": "out-of-date-3jk34n9j3knk3kv",
            "censuses": [
                {
                    "distribution": {
                        "b:developed_med": {"cell_count": 5},
                        "a:developed_high": {"cell_count": 10},
                        "b:developed_high": {"cell_count": 4},
                        "d:developed_high": {"cell_count": 5},
                        "d:developed_med": {"cell_count": 2},
                        "a:developed_med": {"cell_count": 2}
                    },
                    "cell_count": 28,
                    "change": ":deciduous_forest:"
                }
            ]
        }

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        skipped_tasks = []

        needed_tasks = [
            'start',
            'finish',
            'nlcd_soil_census',
            'run_tr55'
        ]

        self.assertTrue(all([True if t in str(job_chain)
                             else False for t in skipped_tasks]),
                        'unnecessary job in chain')

        self.assertTrue(all([True if t in str(job_chain)
                            else False for t in needed_tasks]),
                        'missing necessary job in chain')
Example #16
0
    def test_tr55_chain_doesnt_generate_aoi_census_if_it_exists_and_no_mods(
            self):  # noqa
        """If the AoI census exists in the model input, and there are no modifications,
        it should be provided to TR-55 and not generated.
        """
        self.model_input['aoi_census'] = {
            "distribution": {
                "b:developed_med": {
                    "cell_count": 155
                },
                "a:developed_high": {
                    "cell_count": 1044
                },
                "b:developed_high": {
                    "cell_count": 543
                },
                "d:developed_high": {
                    "cell_count": 503
                },
                "d:developed_med": {
                    "cell_count": 164
                },
                "a:developed_med": {
                    "cell_count": 295
                }
            },
            "cell_count": 2704
        }
        self.model_input['modification_censuses'] = None
        self.model_input['modification_pieces'] = []

        skipped_tasks = [
            'start_histograms_job',
            'get_histogram_job_results',
            'histograms_to_censuses',
        ]

        needed_tasks = ['run_tr55']

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        self.assertFalse(
            all([
                True if t in str(job_chain) else False for t in skipped_tasks
            ]), 'unnecessary job in chain')

        self.assertTrue(
            all([True if t in str(job_chain) else False
                 for t in needed_tasks]), 'missing necessary job in chain')
Example #17
0
    def test_tr55_chain_doesnt_generate_aoi_census_if_it_exists_and_mods(self):
        """If the AoI census exists in the model input, and there are modifications,
        it should be provided to TR-55 and not generated.
        """
        self.model_input['aoi_census'] = {
            "distribution": {
                "b:developed_med": {"cell_count": 155},
                "a:developed_high": {"cell_count": 1044},
                "b:developed_high": {"cell_count": 543},
                "d:developed_high": {"cell_count": 503},
                "d:developed_med": {"cell_count": 164},
                "a:developed_med": {"cell_count": 295}
            },
            "cell_count": 2704
        }

        skipped_tasks = []

        # Job chain is the same as if no census exists because
        # we still need to generate modification censuses
        needed_tasks = [
            'start_histograms_job',
            'get_histogram_job_results',
            'histograms_to_censuses',
            'run_tr55'
        ]

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        cached_argument = ("cached_aoi_census={u'distribution': "
                           "{u'b:developed_med'"
                           ": {u'cell_count': 155}, u'a:developed_high': "
                           "{u'cell_count': 1044}, u'b:developed_high': "
                           "{u'cell_count': 543}, u'd:developed_high': "
                           "{u'cell_count': 503}, u'd:developed_med': "
                           "{u'cell_count': 164}, u'a:developed_med': "
                           "{u'cell_count': 295}}, u'cell_count': 2704})")

        self.assertTrue(all([True if t in str(job_chain)
                             else False for t in skipped_tasks]),
                        'unnecessary job in chain')

        self.assertTrue(all([True if t in str(job_chain)
                            else False for t in needed_tasks]),
                        'missing necessary job in chain')

        self.assertTrue(cached_argument in str(job_chain[3]))
Example #18
0
    def test_tr55_chain_doesnt_generate_aoi_census_if_it_exists_and_no_mods(self):  # noqa
        """If the AoI census exists in the model input, and there are no modifications,
        it should be provided to TR-55 and not generated.
        """
        self.model_input['aoi_census'] = {
            "distribution": {
                "b:developed_med": {"cell_count": 155},
                "a:developed_high": {"cell_count": 1044},
                "b:developed_high": {"cell_count": 543},
                "d:developed_high": {"cell_count": 503},
                "d:developed_med": {"cell_count": 164},
                "a:developed_med": {"cell_count": 295}
            },
            "cell_count": 2704
        }
        self.model_input['modification_censuses'] = None
        self.model_input['modification_pieces'] = []

        skipped_tasks = [
            'start_histograms_job',
            'get_histogram_job_results',
            'histograms_to_censuses',
        ]

        needed_tasks = [
            'run_tr55'
        ]

        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        self.assertFalse(all([True if t in str(job_chain)
                             else False for t in skipped_tasks]),
                         'unnecessary job in chain')

        self.assertTrue(all([True if t in str(job_chain)
                            else False for t in needed_tasks]),
                        'missing necessary job in chain')
Example #19
0
    def test_tr55_chain_generates_census_if_census_does_not_exist(self):
        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        self.assertTrue('tasks.prepare_census' in str(job_chain),
                        'Census preparation should not be skipped')
Example #20
0
    def test_tr55_chain_generates_census_if_census_does_not_exist(self):
        job_chain = views._construct_tr55_job_chain(self.model_input,
                                                    self.job.id)

        self.assertTrue('tasks.prepare_census' in str(job_chain),
                        'Census preparation should not be skipped')