def upsert_organization(self, organization_data): organization = (self.db.query(models.Organization).filter_by( pubid=organization_data["pubid"]).one_or_none()) if not organization: organization = models.Organization() self.db.add(organization) self.setattrs(organization, organization_data)
def test_repr(db_session, factories): organization = models.Organization(name='My Organization', authority='example.com') db_session.add(organization) db_session.flush() assert organization.id is not None assert organization.pubid is not None assert repr(organization) == "<Organization: my-organization>"
def test_init_sets_given_attributes(): logo = """<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /> </svg>""" organization = models.Organization(name="My organization", logo=logo, authority="example.com") assert organization.name == "My organization" assert organization.logo == logo assert organization.authority == "example.com"
def test_init_sets_given_attributes(): logo = """<svg width="100" height="100"> <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /> </svg>""" organization = models.Organization( name='My organization', logo=logo, authority='example.com', ) assert organization.name == 'My organization' assert organization.logo == logo assert organization.authority == 'example.com'
def test_group_organization(db_session): name = "My Hypothesis Group" org = models.Organization(name="My Organization", authority="foobar.com") db_session.add(org) db_session.flush() group = models.Group(name=name, authority="foobar.com", organization=org) db_session.add(group) db_session.flush() assert group.organization == org assert group.organization_id == org.id
def _maybe_create_default_organization(engine, authority): from h import models session = Session(bind=engine) try: default_org = models.Organization.default(session) except exc.NoResultFound: default_org = None if default_org is None: default_org = models.Organization(name="Hypothesis", authority=authority, pubid="__default__") with open("h/static/images/icons/logo.svg", "rb") as h_logo: default_org.logo = h_logo.read().decode("utf-8") session.add(default_org) session.commit() session.close() return default_org
def test_is_default(pubid, is_default): organization = models.Organization(pubid=pubid) assert organization.is_default == is_default
def test_repr(): organization = models.Organization(name="My Organization", authority="example.com", pubid="test_pubid") assert repr(organization) == "<Organization: test_pubid>"
def test_none_logo_is_valid(): org = models.Organization(name="My Organization", logo=None) assert org.logo is None
def test_too_long_name_raises_value_error(): with pytest.raises(ValueError): models.Organization(name="abcdefghijklmnopqrstuvwxyz")
def test_too_short_name_raises_value_error(): with pytest.raises(ValueError): models.Organization(name="")
def test_repr(db_session, factories): organization = models.Organization(name='My Organization', authority='example.com', pubid='test_pubid') assert repr(organization) == "<Organization: test_pubid>"
def test_non_svg_logo_raises_value_error(): with pytest.raises(ValueError): models.Organization(logo='<h>This is not a svg</h>')
def test_malformed_logo_raises_value_error(): with pytest.raises(ValueError): models.Organization(logo='<svg>/svg>')
def test_too_long_logo_raises_value_error(): with pytest.raises(ValueError): models.Organization( logo='<svg>{}</svg>'.format("abcdefghijklmnopqrstuvwxyz" * 400))
def test_null_logo(): organization = models.Organization(name="My Organization") assert organization.name == "My Organization" assert organization.logo is None
def test_slug(): organization = models.Organization(name="My Organization") assert organization.slug == 'my-organization'