# Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: andraz@reciprocitylabs.com
# Maintained By: andraz@reciprocitylabs.com

from ggrc.models.relationship import RelationshipAttr
from tests.ggrc import TestCase


class TestRelationshipAttr(TestCase):

  def test_gather_validators(self):
    class ValidatorParent(object):
      @staticmethod
      def _validate_relationship_attr(cls):
        return True

    class ValidatorChild(ValidatorParent):
      @staticmethod
      def _validate_relationship_attr(cls):
        return False

    validators = RelationshipAttr._gather_validators(ValidatorChild)
    self.assertEqual({True, False}, {v() for v in validators})

@@ -250,84 +250,1903 @@
 ggrc
-.models.relationship import RelationshipAttr%0Afrom tests.ggrc import TestCase
+ import db%0Afrom ggrc.models.mixins import Base%0Afrom ggrc.models.relationship import Relationship%0Afrom ggrc.models.relationship import RelationshipAttr%0Afrom tests.ggrc import TestCase%0Aimport ggrc%0Aimport ggrc.builder%0Aimport ggrc.services%0Aimport random%0Aimport werkzeug.exceptions%0A%0A%0Aclass RelationshipTestMockModel(Base, ggrc.db.Model):%0A  __tablename__ = 'relationship_test_mock_model'%0A  foo = db.Column(db.String)%0A%0A  # REST properties%0A  _publish_attrs = %5B'modified_by_id', 'foo'%5D%0A  _update_attrs = %5B'foo'%5D%0A%0A  @staticmethod%0A  def _validate_relationship_attr(cls, source, destination, name, value):%0A    if cls.__name__ not in %7Bsource.type, destination.type%7D:%0A      return False%0A    if name != %22validated_attr%22:%0A      return False%0A    try:%0A      int(value)%0A    except ValueError:%0A      return False%0A    return True%0A%0A%0Aclass TestRelationship(TestCase):%0A%0A  def setUp(self):%0A    super(TestRelationship, self).setUp()%0A    if RelationshipTestMockModel.__table__.exists(db.engine):%0A      RelationshipTestMockModel.__table__.drop(db.engine)%0A    RelationshipTestMockModel.__table__.create(db.engine)%0A    with self.client.session_transaction() as session:%0A      session%5B'permissions'%5D = %7B%0A          %22__GGRC_ADMIN__%22: %7B%22__GGRC_ALL__%22: %7B%22contexts%22: %5B0%5D%7D%7D%0A      %7D%0A%0A  def mock_model(self, id=None, modified_by_id=1, **kwarg):%0A    if 'id' not in kwarg:%0A      kwarg%5B'id'%5D = random.randint(0, 999999999)%0A    if 'modified_by_id' not in kwarg:%0A      kwarg%5B'modified_by_id'%5D = 1%0A    mock = RelationshipTestMockModel(**kwarg)%0A    return mock%0A%0A  def test_attrs_validation(self):%0A    m1 = self.mock_model()%0A    m2 = self.mock_model()%0A    rel = Relationship(source=m1, destination=m2)%0A    with self.assertRaises(werkzeug.exceptions.BadRequest):%0A      rel.attrs%5B%22foo%22%5D = %22bar%22%0A    with self.assertRaises(werkzeug.exceptions.BadRequest):%0A      rel.attrs%5B%22validated_attr%22%5D = %22wrong value%22%0A    rel.attrs%5B%22validated_attr%22%5D = %22123%22
 %0A%0A%0Ac

Add tests for relationship attributes