def test_takeover(self): s1 = WorkSession.create(ts_now=test_now - datetime.timedelta(hours=12)) s1.sync() c = Client() c.login(username="******", password="******") # The index shows a session with can_continue = False res = c.get(reverse("session_index")) self.assertEquals(res.status_code, 200) self.assertIsNotNone(res.context["session"]) self.assertFalse(res.context["session"].can_continue) # Take it over: we succeed res = c.get(reverse("session_takeover", kwargs={"name": s1.name})) self.assertRedirects(res, reverse("monitor_show")) self.assertEquals(c.session.get("worksession_name", None), s1.name) self.assertEquals(c.session.get("worksession_readonly", None), False) s = WorkSession.load(s1.name) self.assertEquals(s.info.operator_key, c.session.session_key) # The index shows a session with can_continue = True res = c.get(reverse("session_index")) self.assertEquals(res.status_code, 200) self.assertIsNotNone(res.context["session"]) self.assertTrue(res.context["session"].can_continue)
def test_archive_unarchive(self): s1 = WorkSession.create(ts_now=test_now) s1.close() s1.sync() s2 = WorkSession.create(ts_now=test_now + datetime.timedelta(hours=12)) s2.close() s2.sync() self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s1.name)))) self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s2.name)))) c = Client() c.login(username="******", password="******") res = c.get(reverse("session_archive", kwargs={"name": s1.name})) self.assertRedirects(res, reverse("session_list")) self.assertFalse(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s1.name)))) self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s2.name)))) self.assertTrue(os.path.isfile(os.path.join(self.tmpdir.name, "meteomixer-{}.zip".format(s1.name)))) self.assertFalse(os.path.isfile(os.path.join(self.tmpdir.name, "meteomixer-{}.zip".format(s2.name)))) res = c.get(reverse("session_unarchive", kwargs={"name": s1.name})) self.assertRedirects(res, reverse("session_list")) self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s1.name)))) self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s2.name)))) self.assertFalse(os.path.isfile(os.path.join(self.tmpdir.name, "meteomixer-{}.zip".format(s1.name)))) self.assertFalse(os.path.isfile(os.path.join(self.tmpdir.name, "meteomixer-{}.zip".format(s2.name))))
def test_list(self): s1 = WorkSession.create(ts_now=test_now) s1.close() s2 = WorkSession.create(ts_now=test_now + datetime.timedelta(hours=12)) s2.close() c = Client() res = c.get(reverse("session_list")) self.assertEquals(res.status_code, 200) self.assertContains(res, s1.name) self.assertContains(res, s2.name)
def test_clone(self): s = WorkSession.create(ts_now=test_now - datetime.timedelta(hours=12)) s.sync() self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s.name)))) s1 = s.clone() self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s1.name))))
def test_close_readonly(self): s1 = WorkSession.create(ts_now=test_now - datetime.timedelta(hours=12)) s1.sync() c = Client() c.login(username="******", password="******") res = c.get(reverse("session_view", kwargs={"name": s1.name})) self.assertRedirects(res, reverse("monitor_show")) self.assertEquals(c.session.get("worksession_name", None), s1.name) self.assertEquals(c.session.get("worksession_readonly", None), True) res = c.get(reverse("session_close")) self.assertRedirects(res, reverse("session_index")) s = WorkSession.load(s1.name) self.assertTrue(s.is_open)
def test_abort(self): s1 = WorkSession.create(ts_now=test_now - datetime.timedelta(hours=12)) s1.sync() self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s1.name)))) c = Client() c.login(username="******", password="******") res = c.get(reverse("session_abort", kwargs={"name": s1.name})) self.assertRedirects(res, reverse("session_index"))
def test_join_nonmine(self): # Try joining a worksession one doesn't own s1 = WorkSession.create(ts_now=test_now - datetime.timedelta(hours=12)) s1.sync() c = Client() c.login(username="******", password="******") res = c.get(reverse("session_join", kwargs={"name": s1.name})) self.assertRedirects(res, reverse("session_index")) self.assertEquals(c.session.get("worksession_name", None), None) self.assertEquals(c.session.get("worksession_readonly", None), None)
def test_create(self): c = Client() c.login(username="******", password="******") res = c.get(reverse("session_create")) self.assertRedirects(res, reverse("monitor_show")) name = test_now.strftime("%Y%m%d-%H%M") self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(name)))) s = WorkSession.load(name) self.assertTrue(s.is_open) self.assertEquals(c.session.get("worksession_name", None), name) self.assertEquals(c.session.get("worksession_readonly", None), False)
def test_create_while_active(self): """ Creating a session while there is already an active one, should fail """ s_old = WorkSession.create(ts_now=test_now - datetime.timedelta(days=1)) s_old.sync() c = Client() c.login(username="******", password="******") res = c.get(reverse("session_create")) self.assertEquals(res.status_code, 400) self.assertIn("There is already an active session", res.content.decode("utf-8"))
def test_update_selection_readonly(self): s1 = WorkSession.create(ts_now=test_now) s1.close() c = Client() c.login(username="******", password="******") res = c.get(reverse("session_view", kwargs={"name": s1.name})) self.assertRedirects(res, reverse("monitor_show")) res = c.post(reverse("session_updateselection"), json.dumps({ "selection": [None, None, None] }), content_type="application/json; charset=utf-8") res = self.assertAjaxFailed(res) self.assertEquals(res["error"], "the session is read only")
def _get_session_with_products(self): c = Client() c.login(username="******", password="******") res = c.get(reverse("session_create")) self.assertRedirects(res, reverse("monitor_show")) # Fill in the products directory name = test_now.strftime("%Y%m%d-%H%M") s = WorkSession.load(name) prods = list(s.products(update=True)) self.assertEquals(len(prods), 75) return c, s
def test_index_old_active(self): """ Check that an old active session still shows up in the index """ s_old = WorkSession.create(ts_now=test_now - datetime.timedelta(days=1)) s_old.sync() c = Client() c.login(username="******", password="******") res = c.get(reverse("session_index")) self.assertEquals(res.status_code, 200) self.assertIn("session", res.context) self.assertIsNotNone(res.context["session"]) self.assertEquals(res.context["session"].name, s_old.name)
def test_close_real(self): c = Client() c.login(username="******", password="******") res = c.get(reverse("session_create")) self.assertRedirects(res, reverse("monitor_show")) name = test_now.strftime("%Y%m%d-%H%M") self.assertEquals(c.session.get("worksession_name", None), name) self.assertEquals(c.session.get("worksession_readonly", None), False) res = c.get(reverse("session_close")) self.assertRedirects(res, reverse("session_index")) s = WorkSession.load(name) self.assertFalse(s.is_open)
def get_session(self, request): s = getattr(self, "session", None) if s is None: s = WorkSession.from_session(request) if s is None: return redirect("session_list") return s
def test_workflow_default(self): # Create a session s = WorkSession.create() s.sync() self.assertEquals(s.info.sel_runs.d0.model.name, "CosmoI2") self.assertEquals(s.info.sel_runs.d0.reftime, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) self.assertEquals(s.info.sel_runs.d1.model.name, "CosmoI7") self.assertEquals(s.info.sel_runs.d1.reftime, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) self.assertEquals(s.info.sel_runs.d2.model.name, "ECMWF") self.assertEquals(s.info.sel_runs.d2.reftime, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) # Update models s.info.update_models() s.info.update_runs() s.sync() self.assertEquals(s.info.sel_runs.d0.model.name, "CosmoI2") self.assertEquals(s.info.sel_runs.d0.reftime, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) self.assertEquals(s.info.sel_runs.d1.model.name, "CosmoI7") self.assertEquals(s.info.sel_runs.d1.reftime, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) self.assertEquals(s.info.sel_runs.d2.model.name, "ECMWF") self.assertEquals(s.info.sel_runs.d2.reftime, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) # Read product list prods = [ [], [], [] ] for prod in s.products(update=True): prods[prod.didx].append(prod) # Ensure that the products that come out come from the right model run for p in prods[0]: self.assertEquals(p.run.model.name, "CosmoI2") self.assertEquals(p.run.reftime, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) for p in prods[1]: self.assertEquals(p.run.model.name, "CosmoI7") self.assertEquals(p.run.reftime, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) for p in prods[2]: self.assertEquals(p.run.model.name, "ECMWF") self.assertEquals(p.run.reftime, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) # Ensure that each day has products with the right step and validity for p in prods[0]: if p.var in settings.MMIXER_VARS_24H: self.assertEquals(p.validity, datetime.datetime(2014, 11, 10, 0, tzinfo=utc)) self.assertEquals(p.day_step, 24) self.assertTrue(p.daily) else: self.assertGreaterEqual(p.validity, datetime.datetime(2014, 11, 9, 0, tzinfo=utc)) self.assertLess(p.validity, datetime.datetime(2014, 11, 10, 0, tzinfo=utc)) self.assertGreaterEqual(p.day_step, 0) self.assertLess(p.day_step, 24) self.assertFalse(p.daily) for p in prods[1]: if p.var in settings.MMIXER_VARS_24H: self.assertEquals(p.validity, datetime.datetime(2014, 11, 11, 0, tzinfo=utc)) self.assertEquals(p.day_step, 24) self.assertTrue(p.daily) else: self.assertGreaterEqual(p.validity, datetime.datetime(2014, 11, 10, 0, tzinfo=utc)) self.assertLess(p.validity, datetime.datetime(2014, 11, 11, 0, tzinfo=utc)) self.assertGreaterEqual(p.day_step, 0) self.assertLess(p.day_step, 24) self.assertFalse(p.daily) for p in prods[2]: if p.var in settings.MMIXER_VARS_24H: self.assertEquals(p.validity, datetime.datetime(2014, 11, 12, 0, tzinfo=utc)) self.assertEquals(p.day_step, 24) self.assertTrue(p.daily) else: self.assertGreaterEqual(p.validity, datetime.datetime(2014, 11, 11, 0, tzinfo=utc)) self.assertLess(p.validity, datetime.datetime(2014, 11, 12, 0, tzinfo=utc)) self.assertGreaterEqual(p.day_step, 0) self.assertLess(p.day_step, 24) self.assertFalse(p.daily) # Get the value of a product product = [x for x in prods[0] if x.var == "t2m" and x.day_step == 12][0] vals = { (lat, lon): val for lat, lon, val, orig in s.product_values(product, "staz") } self.assertEquals(vals[43.84, 11.96], "286.769196") self.assertEquals(vals[44.07, 12.57], "289.364502") # Edit the value of a product s.edit_product(product, "staz", 44.07, 12.57, "242.123456") vals = { (lat, lon): val for lat, lon, val, orig in s.product_values(product, "staz") } self.assertEquals(vals[43.84, 11.96], "286.769196") self.assertEquals(vals[44.07, 12.57], "242.123456") # Build a flatlist s.update_flatlist() # Check that the edited value is there flatlist_fname = os.path.join(s.pathname, "flatlist", "t2m+12.staz.csv") flvals = { (float(rec["Latitude"]), float(rec["Longitude"])): rec["Value"] for rec in CSVProduct(flatlist_fname).read() } self.assertEquals(flvals[43.84, 11.96], "286.769196") self.assertEquals(flvals[44.07, 12.57], "242.123456")
def test_load(self): s = WorkSession.create() s.sync() s1 = WorkSession.load(s.name, readonly=True) s2 = WorkSession.load(s.name, readonly=False)
def test_create(self): s = WorkSession.create() s.sync() self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, "meteomixer-{}".format(s.name))))