def test_namespace_list_packages(self, app): n = Namespace(name='Hello').save() assert n.packages.all() == [] p = Package(name='Dog Bog', namespace=n) p.save() assert n.packages.all() == [p]
def test_lookup_path(self, app): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) p.save() expected_path = 'hello/dog-bog' assert Package.lookup_path(expected_path) == p
def test_get_all_packages(self, app, client): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) p.save() response = client.get(f"/api/v1/namespaces/{n.slug}/") assert response.status_code == 200 assert response.json[0] == {"name": p.name, "path": p.path}
def test_package_path(self, app): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) expected_path = 'hello/dog-bog' assert p.path == expected_path p.save() assert p.path == expected_path
def test_get_single_version_when_version_does_not_exist_returns_404( self, app, client): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) p.save() response = client.get(f"/api/v1/namespaces/{n.slug}/{p.slug}/1.0.03") assert response.status_code == 404 assert response.json['errors'][0]['status'] == 404 assert response.json['errors'][0]['title'] == "Version Not Found"
def test_suites_have_packages(self, app): n1 = Namespace(name='Hello') p1 = Package(name='Dog Bog', namespace=n1) n2 = Namespace(name='Bello') p2 = Package(name='Trog Mog', namespace=n2) s = Suite(name='Everything', packages=[p1, p2]) s.save() assert set(s.packages) == set([p1, p2])
def test_query_packages_belonging_to_suite(self, app): n1 = Namespace(name='Hello') p1 = Package(name='Dog Bog', namespace=n1) n2 = Namespace(name='Bello') p2 = Package(name='Trog Mog', namespace=n2) s = Suite(name='Everything', packages=[p1, p2]) s.save() packages = Package.query.filter(Package.suites.contains(s)).all() assert set(packages) == set([p1, p2])
def route_create_package(namespace, package): match = get_namespace(namespace) existing = Package.get(namespace=match, name=package) if existing is not None: package_slug = slugify(package) raise PackageAlreadyExistsErrorResponse(f'{namespace}/{package_slug}') package = Package(namespace=match, name=package).save() return package_schema.jsonify(package, many=True)
def test_packages_have_suites(self, app): """Packages belong to suites through a backref.""" n1 = Namespace(name='Hello') p1 = Package(name='Dog Bog', namespace=n1) n2 = Namespace(name='Bello') p2 = Package(name='Trog Mog', namespace=n2) s = Suite(name='Everything', packages=[p1, p2]) s.save() assert p1.suites == [s] assert p2.suites == [s]
def test_delete_namespace(self, app, client): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) p.save() v = PackageVersion(package=p, version='1.0.0', local='C:/dog-bog') v.save() assert Namespace.query.count() == 1 response = client.delete(f"/api/v1/namespaces/{n.slug}/") assert response.status_code == 200 assert Namespace.query.count() == 0
def test_append_package_after_creation(self, app): n1 = Namespace(name='Hello') p1 = Package(name='Dog Bog', namespace=n1) n2 = Namespace(name='Bello') p2 = Package(name='Trog Mog', namespace=n2) s = Suite(name='Everything', packages=[p1]) s.save() assert s.packages == [p1] s.packages.append(p2) s.save() assert s.packages == [p1, p2]
def test_delete_single_version_200_deletes_s3_file(self, app, client, test_media_directory): n = Namespace(name='Hello') p = Package.create(name='Dog Bog', namespace=n) version = '1.0.0' bytes1 = b'not a zip' response = client.put( f"/api/v1/namespaces/{n.slug}/{p.slug}/{version}", data={"file": (BytesIO(bytes1), "workstation.zip")}) assert response.status_code == 200 v = PackageVersion.query.first() assert v.cubby().exists() is True response = client.delete( f"/api/v1/namespaces/{n.slug}/{p.slug}/{v.version}") assert response.status_code == 200 assert PackageVersion.query.count() == 0 assert v.cubby().exists() is False
def test_lookup_paths(self, app): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) p.save() n2 = Namespace(name='Molten') n2.save() p2 = Package(name='Dog Bog', namespace=n2) p2.save() expected_path = 'hello/dog-bog' assert Package.lookup_paths([expected_path]) == [p]
def get_package(namespace_slug, package_slug): path = f'{namespace_slug}/{package_slug}' match = Package.lookup_path(path) if match is None: raise PackageNotFoundErrorResponse(path) return match
def test_update_suite_packages_packages_must_exist(self, app, client): s = Suite(name="Suite", packages=[]) s.save() n = Namespace(name='Hello') p1 = Package(name='Dog Bog', namespace=n) p1.save() response = client.put(f"/api/v1/suites/{s.slug}/packages", json=["hello/dog-bog", "hello/mog"]) assert response.status_code == 400 assert s.packages == [] response = client.put(f"/api/v1/suites/{s.slug}/packages", json=["hello/dog-bog"]) assert response.status_code == 200 assert s.packages == [p1]
def test_suites_have_packages_once(self, app): """A package can either belong to a suite or not; it cannot belong twice.""" n1 = Namespace(name='Hello') p1 = Package(name='Dog Bog', namespace=n1) with pytest.raises(sqlalchemy.exc.IntegrityError): s = Suite(name='Everything', packages=[p1, p1]) s.save()
def test_delete_suite(self, app, client): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) s = Suite(name="Suite", packages=[p]) s.save() response = client.delete(f"/api/v1/suites/{s.slug}/") assert response.status_code == 200 assert Suite.query.count() == 0
def test_package_duplicates_allowed_in_seperate_namespaces(self, app): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) p.save() n2 = Namespace(name='Yellow') p2 = Package(name='Dog Bog', namespace=n2) p2.save()
def test_package_version_local_is_pathtype(self, app): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) v = PackageVersion(package=p, version='1.0.0', local='C:/dog-bog') assert type(v.local) is str v.save() assert type(v.local) is PosixPath
def test_get_single_suite(self, app, client): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) s = Suite(name="Suite", packages=[p]) s.save() response = client.get(f"/api/v1/suites/{s.slug}/") assert response.status_code == 200 assert response.json['packages'] == packages_schema.dump([p]).data assert response.json['slug'] == s.slug
def test_update_suite_packages(self, app, client): s = Suite(name="Suite", packages=[]) s.save() n = Namespace(name='Hello') p1 = Package(name='Dog Bog', namespace=n) p2 = Package(name='Mog', namespace=n) app.db.session.add(p1) app.db.session.add(p2) app.db.session.add(n) app.db.session.commit() response = client.put(f"/api/v1/suites/{s.slug}/packages", json=["hello/dog-bog", "hello/mog"]) assert response.status_code == 200 assert response.json['packages'] == packages_schema.dump([p1, p2]).data assert p1.suites == [s] assert p2.suites == [s] assert set(s.packages) == set([p1, p2])
def test_update_suite_remove_packages(self, app, client): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) s = Suite(name="Suite", packages=[p]) s.save() assert s.packages == [p] response = client.put(f"/api/v1/suites/{s.slug}/packages", json=[]) assert response.status_code == 200 assert s.packages == []
def test_get_all_versions(self, app, client): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) p.save() response = client.get(f"/api/v1/namespaces/{n.slug}/{p.slug}/") assert response.status_code == 200 assert response.json == { "name": p.name, "path": p.path, "versions": [] } v = PackageVersion(package=p, version='1.0.0', local='C:/dog-bog') v.save() response = client.get(f"/api/v1/namespaces/{n.slug}/{p.slug}/") print(response.json) assert response.status_code == 200 assert response.json['versions'][0]['local'] == str(v.local) assert response.json['versions'][0]['version'] == str(v.version)
def test_delete_single_version_200_with_no_s3_file(self, app, client): n = Namespace(name='Hello') p = Package.create(name='Dog Bog', namespace=n) v = PackageVersion.create(package=p, version='1.0.0', local='C:/dog-bog') response = client.delete( f"/api/v1/namespaces/{n.slug}/{p.slug}/{v.version}") assert response.status_code == 200 assert PackageVersion.query.count() == 0
def test_package_versions_are_versiontype(self, app): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) assert p.versions.all() == [] v = PackageVersion(package=p, version='1.0.0', local='C:/dog-bog') assert type(v.version) is str v.save() assert type(v.version) is LooseVersion
def test_package_lookup(self, app): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) p.save() assert Package.lookup("Hello", "dog bog") == p assert Package.lookup("Hello sop", "dog bog") is None assert Package.lookup("Hello", "dog nope") is None
def test_package_duplicates_not_allowed_in_same_namespace(self, app): n = Namespace(name='Hello') p = Package(name='Dog Bog', namespace=n) p.save() p2 = Package(name='Dog Bog', namespace=n) with pytest.raises(sqlalchemy.exc.IntegrityError): p2.save() app.db.session.rollback()
def test_get_single_version_200(self, app, client): n = Namespace(name='Hello') p = Package.create(name='Dog Bog', namespace=n) v = PackageVersion.create(package=p, version='1.0.0', local='C:/dog-bog') response = client.get( f"/api/v1/namespaces/{n.slug}/{p.slug}/{v.version}") assert response.status_code == 200 assert response.json['local'] == str(v.local) assert response.json['version'] == str(v.version)
def test_post_twice_fails(self, app, client, test_media_directory): n = Namespace(name='Hello') p = Package.create(name='Dog Bog', namespace=n) version = '1.0.0' response = client.post( f"/api/v1/namespaces/{n.slug}/{p.slug}/{version}", data={"file": (BytesIO(b"not a zip!"), "workstation.zip")}) assert response.status_code == 200 response = client.post( f"/api/v1/namespaces/{n.slug}/{p.slug}/{version}", data={"file": (BytesIO(b"not a zip!"), "workstation.zip")}) assert response.status_code == 400 assert response.json['errors'][0]['title'] == 'Version Already Exists'
def test_put_cannot_update_package_version_version(self, app, client, test_media_directory): n = Namespace(name='Hello') p = Package.create(name='Dog Bog', namespace=n) version = '1.0.0' bytes1 = b'not a zip' response = client.put( f"/api/v1/namespaces/{n.slug}/{p.slug}/{version}", data={"file": (BytesIO(bytes1), "workstation.zip")}) assert response.status_code == 200 response = client.put( f"/api/v1/namespaces/{n.slug}/{p.slug}/{version}", json={"version": "C:/bobo/"}) assert response.status_code == 400