def test_get_data_period(self):
        w = Workflow('test')
        w.add_primary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_primary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_primary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.add_secondary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_secondary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_secondary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))

        data_period = w._get_data_period()
        self.assertEqual(datetime.date(1988, 11, 8), data_period.get_start_date())
        self.assertEqual(datetime.date(1994, 12, 31), data_period.get_end_date())
    def test_get_inp_preconditions_for_one_month_minus_one_day(self):
        w = Workflow('test', Period('1991-01-02', '1991-02-01'))
        w.add_primary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_primary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_primary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.add_secondary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_secondary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_secondary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))

        preconditions = []
        preconditions = w._add_inp_preconditions(preconditions)
        self.assertEqual(1, len(preconditions))
        self.assertEqual('/inp/1991/01', preconditions[0])
    def test_get_sensor_pairs(self):
        w = Workflow('test')
        w.add_primary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_primary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_primary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.add_secondary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_secondary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_secondary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))

        sensor_pairs = w._get_sensor_pairs()
        self.assertEqual(2, len(sensor_pairs))
        self.assertEqual('avhrr.n12', sensor_pairs[0].get_primary_name())
        self.assertEqual('avhrr.n11', sensor_pairs[1].get_primary_name())
    def test_add_obs_preconditions_for_one_year_and_one_day(self):
        w = Workflow('test', Period('1991-01-01', '1992-01-02'))
        w.add_primary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_primary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_primary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.add_secondary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_secondary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_secondary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))

        preconditions = list()
        preconditions = w._add_obs_preconditions(preconditions)
        self.assertEqual(2, len(preconditions))
        self.assertEqual('/obs/1990/12', preconditions[0])
        self.assertEqual('/obs/1992/02', preconditions[1])
    def test_add_obs_preconditions_for_all_years(self):
        w = Workflow('test')
        w.add_primary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_primary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_primary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.add_secondary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_secondary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_secondary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))

        preconditions = list()
        preconditions = w._add_obs_preconditions(preconditions)
        self.assertEqual(2, len(preconditions))
        self.assertEqual('/obs/1988/10', preconditions[0])
        self.assertEqual('/obs/1995/01', preconditions[1])
    def test_get_inp_preconditions_for_all_years(self):
        w = Workflow('test')
        w.add_primary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_primary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_primary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.add_secondary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_secondary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_secondary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))

        preconditions = []
        preconditions = w._add_inp_preconditions(preconditions)
        self.assertEqual(74, len(preconditions))
        self.assertEqual('/inp/1988/11', preconditions[0])
        self.assertEqual('/inp/1994/12', preconditions[73])
    def test_add_smp_preconditions_for_one_year(self):
        w = Workflow('test', Period('1991-01-01', '1992-01-01'))
        w.add_primary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_primary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_primary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.add_secondary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_secondary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_secondary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))

        preconditions = list()
        preconditions = w._add_smp_preconditions(preconditions)
        self.assertEqual(4, len(preconditions))
        self.assertEqual('/smp/avhrr.n12,avhrr.n11/1991/08', preconditions[0])
        self.assertEqual('/smp/avhrr.n12,avhrr.n11/1992/01', preconditions[1])
        self.assertEqual('/smp/avhrr.n11,avhrr.n10/1990/12', preconditions[2])
        self.assertEqual('/smp/avhrr.n11,avhrr.n10/1991/10', preconditions[3])
    def test_run_dual_sensor_usecase_without_arc(self):
        usecase = 'mms1'
        mmdtype = 'mmd1'
        w = Workflow(usecase, Period('1991-01-01', '1992-01-01'))
        w.add_primary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_primary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_primary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.add_secondary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_secondary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_secondary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.run(mmdtype, log_dir='.', simulation=True, without_arc=True)

        with open('mms1.status', 'r') as status:
            self.assertEqual('203 created, 0 running, 0 backlog, 203 processed, 0 failed\n', status.readline())
        with open('mms1.report', 'r') as report:
            self.assertEqual(203, len(report.readlines()))

        os.remove('mms1.status')
        os.remove('mms1.report')
    def test_get_primary_sensors_by_period(self):
        w = Workflow('test')
        w.add_primary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_primary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_primary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))
        w.add_secondary_sensor('avhrr.n10', (1986, 11, 17), (1991, 9, 16))
        w.add_secondary_sensor('avhrr.n11', (1988, 11, 8), (1994, 12, 31))
        w.add_secondary_sensor('avhrr.n12', (1991, 9, 16), (1998, 12, 14))

        sensors = w._get_primary_sensors_by_period()
        """:type : list"""
        self.assertEqual(2, len(sensors))
        sensor_1 = sensors[0]
        """:type : Sensor"""
        sensor_2 = sensors[1]
        """:type : Sensor"""
        self.assertEqual("avhrr.n11", sensor_1.get_name())
        self.assertEqual(Period('1988-11-08', '1991-09-16'), sensor_1.get_period())
        self.assertEqual("avhrr.n12", sensor_2.get_name())
        self.assertEqual(Period('1991-09-16', '1994-12-31'), sensor_2.get_period())
    def test_add_secondary_sensors_to_workflow(self):
        w = Workflow('test')
        w.add_secondary_sensor('avhrr.n10', (2007, 1, 1), (2008, 1, 1))
        self.assertEqual(1, len(w._get_secondary_sensors()))

        try:
            w.add_secondary_sensor('avhrr.n10', (2007, 1, 1), (2008, 1, 1))
            self.fail()
        except ValueError:
            pass

        try:
            w.add_secondary_sensor('avhrr.n10', (2007, 7, 1), (2008, 1, 1))
            self.fail()
        except ValueError:
            pass

        w.add_secondary_sensor('avhrr.n11', (2007, 1, 1), (2008, 1, 1))
        self.assertEqual(2, len(w._get_secondary_sensors()))
        w.add_secondary_sensor('avhrr.n12', (2008, 1, 1), (2009, 1, 1))
        self.assertEqual(3, len(w._get_secondary_sensors()))