def test_dependency_list(self):
        class FooType(defs.ArtifactType):
            pass

        class BarType(defs.ArtifactType):
            pass

        class TestType(defs.ArtifactType):
            depends_on = defs.ArtifactReferenceList()
            depends_on_self_or_foo = defs.ArtifactReferenceList(
                references=defs.ArtifactReference(['FooType', 'TestType']))

        a = defs.ArtifactType(**get_artifact_fixture(id="1"))
        a_copy = defs.ArtifactType(**get_artifact_fixture(id="1"))
        b = defs.ArtifactType(**get_artifact_fixture(id="2"))

        tt = TestType(**get_artifact_fixture(id="3"))
        foo = FooType(**get_artifact_fixture(id='4'))
        bar = BarType(**get_artifact_fixture(id='4'))

        tt.depends_on.append(a)
        tt.depends_on.append(b)
        self.assertEqual([a, b], tt.depends_on)
        self.assertRaises(exc.InvalidArtifactPropertyValue,
                          tt.depends_on.append, a)
        self.assertRaises(exc.InvalidArtifactPropertyValue,
                          tt.depends_on.append, a_copy)

        tt.depends_on_self_or_foo.append(tt)
        tt.depends_on_self_or_foo.append(foo)
        self.assertRaises(exc.InvalidArtifactPropertyValue,
                          tt.depends_on_self_or_foo.append, bar)
        self.assertEqual([tt, foo], tt.depends_on_self_or_foo)
    def test_dependency_prop(self):
        class DerivedType(defs.ArtifactType):
            depends_on_any = defs.ArtifactReference()
            depends_on_self = defs.ArtifactReference(type_name='DerivedType')
            depends_on_self_version = defs.ArtifactReference(
                type_name='DerivedType', type_version='1.0')

        class DerivedTypeV11(DerivedType):
            __type_name__ = 'DerivedType'
            __type_version__ = '1.1'
            depends_on_self_version = defs.ArtifactReference(
                type_name='DerivedType', type_version='1.1')

        d1 = DerivedType(**get_artifact_fixture())
        d2 = DerivedTypeV11(**get_artifact_fixture())
        a = defs.ArtifactType(**get_artifact_fixture())
        d1.depends_on_any = a
        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, d1,
                          'depends_on_self', a)
        d1.depends_on_self = d2
        d2.depends_on_self = d1
        d1.depends_on_self_version = d1
        d2.depends_on_self_version = d2
        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, d1,
                          'depends_on_self_version', d2)
    def test_create_artifact(self):
        a = defs.ArtifactType(**get_artifact_fixture())
        self.assertIsNotNone(a)
        self.assertEqual("123", a.id)
        self.assertEqual("ArtifactType", a.type_name)
        self.assertEqual("1.0", a.type_version)
        self.assertEqual("11.2", a.version)
        self.assertEqual("Foo", a.name)
        self.assertEqual("private", a.visibility)
        self.assertEqual("creating", a.state)
        self.assertEqual("my_tenant", a.owner)
        self.assertEqual(a.created_at, a.updated_at)
        self.assertIsNone(a.description)
        self.assertIsNone(a.published_at)
        self.assertIsNone(a.deleted_at)

        self.assertIsNone(a.description)

        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, a, "id",
                          "foo")
        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, a,
                          "state", "active")
        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, a,
                          "owner", "some other")
        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, a,
                          "created_at", datetime.datetime.now())
        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, a,
                          "deleted_at", datetime.datetime.now())
        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, a,
                          "updated_at", datetime.datetime.now())
        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, a,
                          "published_at", datetime.datetime.now())
        self.assertRaises(exc.InvalidArtifactPropertyValue, setattr, a,
                          "visibility", "wrong")
    def test_pre_publish_dependency_validation(self):
        class TestType(defs.ArtifactType):
            required_dependency = defs.ArtifactReference(required=True)
            optional_dependency = defs.ArtifactReference()

        tt = TestType(**get_artifact_fixture())
        self.assertRaises(exc.InvalidArtifactPropertyValue, tt.__pre_publish__)
        tt.required_dependency = defs.ArtifactType(**get_artifact_fixture())
        tt.__pre_publish__()
    def test_serialization_to_db(self):
        ref1 = defs.ArtifactType(**get_artifact_fixture(id="1"))
        ref2 = defs.ArtifactType(**get_artifact_fixture(id="2"))
        ref3 = defs.ArtifactType(**get_artifact_fixture(id="3"))

        blob1 = defs.Blob(size=100,
                          locations=['http://example.com/blob1'],
                          item_key='some_key',
                          checksum='abc')
        blob2 = defs.Blob(size=200,
                          locations=['http://example.com/blob2'],
                          item_key='another_key',
                          checksum='fff')
        blob3 = defs.Blob(size=300,
                          locations=['http://example.com/blob3'],
                          item_key='third_key',
                          checksum='123')

        fixture = get_artifact_fixture()
        tt = SerTestType(**fixture)
        tt.some_string = 'bar'
        tt.some_text = 'bazz'
        tt.some_version = '11.22.33-beta'
        tt.some_int = 50
        tt.some_numeric = 10.341
        tt.some_bool = True
        tt.some_array = ['q', 'w', 'e', 'r', 't', 'y']
        tt.another_array = [1, 1.2, False]
        tt.some_dict = {'foobar': "FOOBAR", 'baz': "QUX"}
        tt.another_dict = {'foo': 1, 'bar': True}
        tt.some_ref = ref1
        tt.some_ref_list = [ref2, ref3]
        tt.some_blob = blob1
        tt.some_blob_list = [blob2, blob3]

        results = serialization.serialize_for_db(tt)
        expected = fixture
        expected['type_name'] = 'SerTestType'
        expected['type_version'] = '1.0'
        expected['properties'] = {
            'some_string': {
                'type': 'string',
                'value': 'bar'
            },
            'some_text': {
                'type': 'text',
                'value': 'bazz'
            },
            'some_version': {
                'type': 'string',
                'value': '11.22.33-beta'
            },
            'some_int': {
                'type': 'int',
                'value': 50
            },
            'some_numeric': {
                'type': 'numeric',
                'value': 10.341
            },
            'some_bool': {
                'type': 'bool',
                'value': True
            },
            'some_array': {
                'type':
                'array',
                'value': [{
                    'type': 'string',
                    'value': 'q'
                }, {
                    'type': 'string',
                    'value': 'w'
                }, {
                    'type': 'string',
                    'value': 'e'
                }, {
                    'type': 'string',
                    'value': 'r'
                }, {
                    'type': 'string',
                    'value': 't'
                }, {
                    'type': 'string',
                    'value': 'y'
                }]
            },
            'another_array': {
                'type':
                'array',
                'value': [{
                    'type': 'int',
                    'value': 1
                }, {
                    'type': 'numeric',
                    'value': 1.2
                }, {
                    'type': 'bool',
                    'value': False
                }]
            },
            'some_dict.foobar': {
                'type': 'string',
                'value': 'FOOBAR'
            },
            'some_dict.baz': {
                'type': 'string',
                'value': 'QUX'
            },
            'another_dict.foo': {
                'type': 'int',
                'value': 1
            },
            'another_dict.bar': {
                'type': 'bool',
                'value': True
            }
        }
        expected['dependencies'] = {
            'some_ref': ['1'],
            'some_ref_list': ['2', '3']
        }
        expected['blobs'] = {
            'some_blob': [{
                'size': 100,
                'checksum': 'abc',
                'item_key': 'some_key',
                'locations': ['http://example.com/blob1']
            }],
            'some_blob_list': [{
                'size': 200,
                'checksum': 'fff',
                'item_key': 'another_key',
                'locations': ['http://example.com/blob2']
            }, {
                'size': 300,
                'checksum': '123',
                'item_key': 'third_key',
                'locations': ['http://example.com/blob3']
            }]
        }

        self.assertEqual(expected, results)