|
@@ -1,3 +1,5 @@
|
|
|
+import uuid
|
|
|
+
|
|
|
from flask import url_for
|
|
|
|
|
|
from pycs.database.Collection import Collection
|
|
@@ -155,7 +157,7 @@ class ProjectListTests(_BaseProjectTests):
|
|
|
self.assertTrue(response.is_json)
|
|
|
content = response.json
|
|
|
|
|
|
- self.assertTrue(10, len(content))
|
|
|
+ self.assertEqual(10, len(content))
|
|
|
|
|
|
for entry in content:
|
|
|
project = Project.query.get(entry["id"])
|
|
@@ -165,7 +167,7 @@ class ProjectListTests(_BaseProjectTests):
|
|
|
def test_list_project_collections(self):
|
|
|
project = Project.new(
|
|
|
name="TestProject",
|
|
|
- description="Project for a test case #",
|
|
|
+ description="Project for a test case",
|
|
|
model=self.model,
|
|
|
root_folder="project_folder",
|
|
|
external_data=False,
|
|
@@ -180,7 +182,7 @@ class ProjectListTests(_BaseProjectTests):
|
|
|
name=f"Some collection {i}",
|
|
|
description=f"A description {i}",
|
|
|
position=i,
|
|
|
- autoselect=i is 1
|
|
|
+ autoselect=i == 1
|
|
|
)
|
|
|
self.assertEqual(10, Collection.query.count())
|
|
|
|
|
@@ -190,9 +192,200 @@ class ProjectListTests(_BaseProjectTests):
|
|
|
self.assertTrue(response.is_json)
|
|
|
content = response.json
|
|
|
|
|
|
- self.assertTrue(10, len(content))
|
|
|
+ self.assertEqual(10, len(content))
|
|
|
|
|
|
for entry in content:
|
|
|
collection = Collection.query.get(entry["id"])
|
|
|
self.assertIsNotNone(collection)
|
|
|
self.assertDictEqual(entry, collection.serialize())
|
|
|
+
|
|
|
+ def test_list_all_files(self):
|
|
|
+ project = Project.new(
|
|
|
+ name="TestProject",
|
|
|
+ description="Project for a test case",
|
|
|
+ model=self.model,
|
|
|
+ root_folder="project_folder",
|
|
|
+ external_data=False,
|
|
|
+ data_folder="project_folder/data",
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+ self.assertEqual(0, File.query.count())
|
|
|
+ files = []
|
|
|
+ for i in range(1, 11):
|
|
|
+ file_uuid = str(uuid.uuid1())
|
|
|
+ file, is_new = project.add_file(
|
|
|
+ uuid=file_uuid,
|
|
|
+ file_type="image",
|
|
|
+ name=f"name{i}",
|
|
|
+ filename=f"image_{i:03d}",
|
|
|
+ extension=".jpg",
|
|
|
+ size=32*1024,
|
|
|
+ )
|
|
|
+ self.assertTrue(is_new)
|
|
|
+ files.append(file)
|
|
|
+
|
|
|
+ self.assertEqual(10, File.query.count())
|
|
|
+
|
|
|
+ response = self.get(url_for("list_all_files",
|
|
|
+ project_id=project.id))
|
|
|
+
|
|
|
+ self.assertTrue(response.is_json)
|
|
|
+ _content = response.json
|
|
|
+ count = _content["count"]
|
|
|
+ content = _content["files"]
|
|
|
+
|
|
|
+ self.assertEqual(10, count)
|
|
|
+ self.assertEqual(10, len(content))
|
|
|
+
|
|
|
+ for file, entry in zip(files, content):
|
|
|
+ self.assertDictEqual(entry, file.serialize())
|
|
|
+
|
|
|
+ def test_list_some_files(self):
|
|
|
+ project = Project.new(
|
|
|
+ name="TestProject",
|
|
|
+ description="Project for a test case",
|
|
|
+ model=self.model,
|
|
|
+ root_folder="project_folder",
|
|
|
+ external_data=False,
|
|
|
+ data_folder="project_folder/data",
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+ self.assertEqual(0, File.query.count())
|
|
|
+ files = []
|
|
|
+ for i in range(1, 11):
|
|
|
+ file_uuid = str(uuid.uuid1())
|
|
|
+ file, is_new = project.add_file(
|
|
|
+ uuid=file_uuid,
|
|
|
+ file_type="image",
|
|
|
+ name=f"name{i}",
|
|
|
+ filename=f"image_{i:03d}",
|
|
|
+ extension=".jpg",
|
|
|
+ size=32*1024,
|
|
|
+ )
|
|
|
+ self.assertTrue(is_new)
|
|
|
+ files.append(file)
|
|
|
+
|
|
|
+ self.assertEqual(10, File.query.count())
|
|
|
+
|
|
|
+ for start, length in [(0, 5), (0, 15), (5, 3), (5, 8)]:
|
|
|
+ response = self.get(url_for("list_files",
|
|
|
+ project_id=project.id,
|
|
|
+ start=start, length=length))
|
|
|
+
|
|
|
+ self.assertTrue(response.is_json)
|
|
|
+ _content = response.json
|
|
|
+ count = _content["count"]
|
|
|
+ content = _content["files"]
|
|
|
+
|
|
|
+ self.assertEqual(len(files), count)
|
|
|
+ self.assertEqual(min(len(files), start+length) - start, len(content))
|
|
|
+
|
|
|
+ for file, entry in zip(files[start:start+length], content):
|
|
|
+ self.assertDictEqual(entry, file.serialize())
|
|
|
+
|
|
|
+
|
|
|
+ def test_list_collection_files_of_non_existing_collection(self):
|
|
|
+
|
|
|
+ project = Project.new(
|
|
|
+ name="TestProject",
|
|
|
+ description="Project for a test case",
|
|
|
+ model=self.model,
|
|
|
+ root_folder="project_folder",
|
|
|
+ external_data=False,
|
|
|
+ data_folder="project_folder/data",
|
|
|
+ )
|
|
|
+
|
|
|
+ url = url_for("list_collection_files",
|
|
|
+ project_id=project.id, collection_id=42,
|
|
|
+ start=0, length=30)
|
|
|
+ self.get(url, status_code=404)
|
|
|
+
|
|
|
+
|
|
|
+ def test_list_collection_files(self):
|
|
|
+ project = Project.new(
|
|
|
+ name="TestProject",
|
|
|
+ description="Project for a test case",
|
|
|
+ model=self.model,
|
|
|
+ root_folder="project_folder",
|
|
|
+ external_data=False,
|
|
|
+ data_folder="project_folder/data",
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertEqual(1, Project.query.count())
|
|
|
+
|
|
|
+ collections = {}
|
|
|
+ for i in range(1, 3):
|
|
|
+ collection, is_new = project.create_collection(
|
|
|
+ reference=f"collection_{i}",
|
|
|
+ name=f"Some collection {i}",
|
|
|
+ description=f"A description {i}",
|
|
|
+ position=i,
|
|
|
+ autoselect=i == 1
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertTrue(is_new)
|
|
|
+
|
|
|
+ collection_files = []
|
|
|
+
|
|
|
+ for j in range(1, 4):
|
|
|
+ file_uuid = str(uuid.uuid1())
|
|
|
+ file, is_new = collection.add_file(
|
|
|
+ uuid=file_uuid,
|
|
|
+ file_type="image",
|
|
|
+ name=f"col_{i}_name{j}",
|
|
|
+ filename=f"col_{i}_image_{j:03d}",
|
|
|
+ extension=".jpg",
|
|
|
+ size=32*1024,
|
|
|
+ )
|
|
|
+ self.assertTrue(is_new)
|
|
|
+ collection_files.append(file)
|
|
|
+
|
|
|
+ collections[collection.id] = collection_files
|
|
|
+
|
|
|
+ files = []
|
|
|
+ for j in range(1, 4):
|
|
|
+ file_uuid = str(uuid.uuid1())
|
|
|
+ file, is_new = project.add_file(
|
|
|
+ uuid=file_uuid,
|
|
|
+ file_type="image",
|
|
|
+ name=f"name{j}",
|
|
|
+ filename=f"image_{j:03d}",
|
|
|
+ extension=".jpg",
|
|
|
+ size=32*1024,
|
|
|
+ )
|
|
|
+ self.assertTrue(is_new)
|
|
|
+ files.append(file)
|
|
|
+
|
|
|
+ collections[0] = files
|
|
|
+
|
|
|
+ self.assertEqual(2, Collection.query.filter(Collection.project_id==project.id).count())
|
|
|
+
|
|
|
+ self.assertEqual(6, File.query.filter(
|
|
|
+ File.project_id == project.id,
|
|
|
+ File.collection_id != None,
|
|
|
+ ).count())
|
|
|
+
|
|
|
+ self.assertEqual(3, File.query.filter(
|
|
|
+ File.project_id == project.id,
|
|
|
+ File.collection_id == None,
|
|
|
+ ).count())
|
|
|
+
|
|
|
+
|
|
|
+ for collection_id, files in collections.items():
|
|
|
+ for start, length in [(0, 5), (0, 15), (1, 3), (1, 8)]:
|
|
|
+ response = self.get(url_for("list_collection_files",
|
|
|
+ project_id=project.id, collection_id=collection_id,
|
|
|
+ start=start, length=length))
|
|
|
+
|
|
|
+ self.assertTrue(response.is_json)
|
|
|
+ _content = response.json
|
|
|
+ count = _content["count"]
|
|
|
+ content = _content["files"]
|
|
|
+
|
|
|
+ self.assertEqual(len(files), count)
|
|
|
+ self.assertEqual(min(len(files), start+length) - start, len(content))
|
|
|
+
|
|
|
+ for file, entry in zip(files[start:start+length], content):
|
|
|
+ self.assertDictEqual(entry, file.serialize())
|