Beispiel #1
0
    def test_adding_mapping_ca_dict(self):
        """Test adding mapping custom attribute values with a dict."""
        cad1 = CAD(
            definition_type="program",
            attribute_type="Map:Person",
            title="CA 1",
        )
        cad2 = CAD(
            definition_type="program",
            attribute_type="Map:Person",
            title="CA 2",
        )
        db.session.commit()

        prog = ProgramFactory()
        prog.custom_attribute_values = [{
            "attribute_value": "Person:1",
            "custom_attribute_id": cad1.id,
        }, {
            "attribute_value": "Person",
            "attribute_object_id": "1",
            "custom_attribute_id": cad2.id,
        }]
        prog.validate_custom_attributes()
        prog = prog.__class__.query.get(prog.id)

        self.assertEqual(
            {"1"},
            set(v.attribute_object_id for v in prog.custom_attribute_values),
        )
        self.assertEqual(
            {"Person"},
            set(v.attribute_value for v in prog.custom_attribute_values),
        )
        self.assertEqual(len(prog.custom_attribute_values), 2)
Beispiel #2
0
    def test_setting_ca_dict(self):
        """Test setting custom attribute values dict."""
        prog = ProgramFactory()
        cad1 = CAD(
            definition_type="program",
            title="CA 1",
        )
        cad2 = CAD(
            definition_type="program",
            title="CA 2",
        )

        prog.custom_attribute_values = [{
            "attribute_value": "55",
            "custom_attribute_id": cad1.id,
        }, {
            "attribute_value": "129aaaaaa",
            "custom_attribute_id": cad2.id,
        }]
        db.session.commit()
        prog = prog.__class__.query.get(prog.id)

        self.assertEqual(
            {"55", "129aaaaaa"},
            set(v.attribute_value for v in prog.custom_attribute_values),
        )
        self.assertEqual(len(prog.custom_attribute_values), 2)
Beispiel #3
0
  def test_changing_custom_attributes_triggers_change_notification(self, _):
    """Test that updating Assessment's CA value results in change notification.
    """
    CAD(definition_type="assessment", title="CA 1",)
    cad2 = CAD(definition_type="assessment", title="CA 2",)

    self.import_file("assessment_with_templates.csv")
    asmts = {asmt.slug: asmt for asmt in Assessment.query}

    self.client.get("/_notifications/send_daily_digest")
    self.assertEqual(self._get_notifications().count(), 0)

    # set initial CA value on the Assessment (also to put it into "In Progress"
    cad2 = CustomAttributeDefinition.query.filter(
        CustomAttributeDefinition.title == "CA 2").one()
    val2 = CustomAttributeValue(attribute_value="1a2b3", custom_attribute=cad2)

    asmt4 = Assessment.query.get(asmts["A 4"].id)
    self.api_helper.modify_object(
        asmt4,
        {
            "custom_attribute_values": [{
                "attributable_id": asmt4.id,
                "attributable_type": "Assessment",
                "id": val2.id,
                "custom_attribute_id": cad2.id,
                "attribute_value": val2.attribute_value,
            }]
        }
    )

    # there should be a notification...
    self.assertEqual(
        self._get_notifications(notif_type="assessment_updated").count(), 1)

    # now change the CA value and check if notification gets generated
    cad2 = CustomAttributeDefinition.query.filter(
        CustomAttributeDefinition.title == "CA 2").one()
    val2 = CustomAttributeValue(attribute_value="NEW", custom_attribute=cad2)

    asmt4 = Assessment.query.get(asmts["A 4"].id)
    self.api_helper.modify_object(
        asmt4,
        {
            "custom_attribute_values": [{
                "attributable_id": asmt4.id,
                "attributable_type": "Assessment",
                "custom_attribute_id": cad2.id,
                "id": val2.id,
                "attribute_value": val2.attribute_value,
            }]
        }
    )

    notifs = self._get_notifications(notif_type="assessment_updated").all()
    self.assertEqual(len(notifs), 1)
Beispiel #4
0
    def test_setting_ca_values(self):
        """Test normal setting of custom attribute values."""
        prog = ProgramFactory()
        cad1 = CAD(
            definition_type="program",
            title="CA 1",
        )
        cad2 = CAD(
            definition_type="program",
            title="CA 2",
        )

        prog = prog.__class__.query.get(prog.id)

        val1 = models.CustomAttributeValue(
            attribute_value="55",
            custom_attribute=cad1,
        )
        val2 = models.CustomAttributeValue(
            attribute_value="129aaaaaa",
            custom_attribute=cad2,
        )

        prog.custom_attribute_values = [val1, val1, val1]
        db.session.commit()
        prog = prog.__class__.query.get(prog.id)
        self.assertEqual(len(prog.custom_attribute_values), 1)

        prog = ProgramFactory()
        prog.custom_attribute_values.append(val1)
        db.session.commit()
        prog = prog.__class__.query.get(prog.id)
        self.assertEqual(len(prog.custom_attribute_values), 1)
        self.assertEqual(
            {"55"},
            set(v.attribute_value for v in prog.custom_attribute_values),
        )

        prog = ProgramFactory()
        prog.custom_attribute_values = [val1, val2]
        db.session.commit()
        prog = prog.__class__.query.get(prog.id)

        self.assertEqual(
            {"55", "129aaaaaa"},
            set(v.attribute_value for v in prog.custom_attribute_values),
        )
        self.assertEqual(len(prog.custom_attribute_values), 2)
Beispiel #5
0
  def test_assessment_reopen_notifications_on_ca_edit(self, send_email):
    """Test if updating assessment's CA value in reopen notification."""
    CAD(definition_type="assessment", title="CA_misc_remarks")

    self.import_file("assessment_with_templates.csv")

    asmts = {asmt.slug: asmt for asmt in Assessment.query}
    asmt = Assessment.query.get(asmts["A 1"].id)
    asmt_id, asmt_slug = asmt.id, asmt.slug

    for i, new_state in enumerate(Assessment.DONE_STATES):
      asmt = Assessment.query.get(asmt_id)
      asmt.status = new_state
      db.session.commit()

      self.client.get("/_notifications/send_daily_digest")
      self.assertEqual(self._get_notifications().count(), 0)

      self.import_data(OrderedDict([
          (u"object_type", u"Assessment"),
          (u"Code*", asmt_slug),
          (u"CA_misc_remarks", u"CA new value" + unicode(i)),
      ]))

      query = self._get_notifications(notif_type="assessment_reopened")
      self.assertEqual(query.count(), 1)

      self.client.get("/_notifications/send_daily_digest")
      recipient, _, content = send_email.call_args[0]
      self.assertEqual(recipient, u"*****@*****.**")
      self.assertIn(u"Reopened assessments", content)
Beispiel #6
0
  def test_assessment_ca_updated_notifications(self, send_email):
    """Test if updating assessment custom attr. results in a notification."""
    CAD(definition_type="assessment", title="CA_misc_remarks")

    self.import_file("assessment_with_templates.csv")
    self.client.get("/_notifications/send_daily_digest")
    self.assertEqual(self._get_notifications().count(), 0)

    asmts = {asmt.slug: asmt for asmt in Assessment.query}
    asmt = Assessment.query.get(asmts["A 1"].id)

    self.import_data(OrderedDict([
        (u"object_type", u"Assessment"),
        (u"Code*", asmt.slug),
        (u"CA_misc_remarks", u"CA new value"),
    ]))

    asmt = Assessment.query.get(asmts["A 1"].id)

    query = self._get_notifications(notif_type="assessment_updated")
    self.assertEqual(query.count(), 1)

    # check email content
    self.client.get("/_notifications/send_daily_digest")
    recipient, _, content = send_email.call_args[0]

    self.assertEqual(recipient, u"*****@*****.**")
    self.assertIn(u"Assessments have been updated", content)
Beispiel #7
0
    def test_updating_ca_values(self):
        """Test updating custom attribute values."""
        cad1 = CAD(
            definition_type="program",
            title="CA 1",
        )

        val1 = models.CustomAttributeValue(
            attribute_value="55",
            custom_attribute=cad1,
        )

        prog = ProgramFactory()
        prog.custom_attribute_values = [val1]
        db.session.commit()

        prog = prog.__class__.query.get(prog.id)
        self.assertEqual(prog.custom_attribute_values[0].attribute_value, "55")

        val2 = models.CustomAttributeValue(
            attribute_value="129",
            custom_attribute=cad1,
        )

        prog.custom_attribute_values = [val2]
        db.session.commit()

        prog = prog.__class__.query.get(prog.id)
        self.assertEqual(prog.custom_attribute_values[0].attribute_value,
                         "129")
  def test_ca_setattr(self):
    """Test setting custom attribute values with setattr."""
    prog = ProgramFactory()
    cad1 = CAD(definition_type="program", title="CA 1",)

    setattr(prog, "custom_attribute_values", [{
            "attribute_value": "55",
            "custom_attribute_id": cad1.id,
            }])
    db.session.commit()
    prog = prog.__class__.query.get(prog.id)

    self.assertEqual(
        {"55"},
        set(v.attribute_value for v in prog.custom_attribute_values),
    )
    self.assertEqual(len(prog.custom_attribute_values), 1)
  def test_adding_bad_ca_dict(self):
    """Test setting invalid custom attribute values."""
    prog = ProgramFactory()
    cad1 = CAD(definition_type="section", title="CA 1",)

    with self.assertRaises(ValueError):
      prog.custom_attribute_values = [{
          "attribute_value": "55",
          "custom_attribute_id": -1
      }]
      prog.validate_custom_attributes()

    with self.assertRaises(ValueError):
      prog.custom_attribute_values = [{
          "attribute_value": "55",
          "custom_attribute_id": cad1.id,
      }]
      prog.validate_custom_attributes()
  def test_updating_ca_dict(self):
    """Test updating custom attribute values with a dict."""
    prog = ProgramFactory()
    cad1 = CAD(definition_type="program", title="CA 1",)

    prog.custom_attribute_values = [{
        "attribute_value": "55",
        "custom_attribute_id": cad1.id,
    }]
    db.session.commit()
    prog = prog.__class__.query.get(prog.id)

    prog.custom_attribute_values = [{
        "attribute_value": "57",
        "custom_attribute_id": cad1.id,
    }]

    self.assertEqual(len(prog.custom_attribute_values), 1)
    self.assertEqual(prog.custom_attribute_values[0].attribute_value, "57")