Commit e2bf1d45 authored by Will Banfield's avatar Will Banfield Committed by Jeremy Mikola

PHPLIB-146: Implement GridFS upload, plus initial GridFS commit

parent 298624b9
<?php
namespace MongoDB\GridFS;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\BSON\ObjectId;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Manager;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedValueException;
/**
* Bucket abstracts the GridFS files and chunks collections.
*
* @api
*/
class Bucket
{
private $databaseName;
private $options;
private $filesCollection;
private $chunksCollection;
private $indexChecker;
private $ensuredIndexes = false;
/**
* Constructs a GridFS bucket.
*
* Supported options:
*
* * bucketName (string): The bucket name, which will be used as a prefix
* for the files and chunks collections. Defaults to "fs".
*
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
* 261120 (i.e. 255 KiB).
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param Manager $manager Manager instance from the driver
* @param string $databaseName Database name
* @param array $options Bucket options
* @throws InvalidArgumentException
*/
public function __construct(Manager $manager, $databaseName, array $options = [])
{
$collectionOptions = [];
$options += [
'bucketName' => 'fs',
'chunkSizeBytes' => 261120,
];
if (isset($options['bucketName']) && ! is_string($options['bucketName'])) {
throw new InvalidArgumentTypeException('"bucketName" option', $options['bucketName'], 'string');
}
if (isset($options['chunkSizeBytes']) && ! is_integer($options['chunkSizeBytes'])) {
throw new InvalidArgumentTypeException('"chunkSizeBytes" option', $options['chunkSizeBytes'], 'integer');
}
if (isset($options['readPreference'])) {
if (! $options['readPreference'] instanceof ReadPreference) {
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
} else {
$collectionOptions['readPreference'] = $options['readPreference'];
}
}
if (isset($options['writeConcern'])) {
if (! $options['writeConcern'] instanceof WriteConcern) {
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
} else {
$collectionOptions['writeConcern'] = $options['writeConcern'];
}
}
$this->databaseName = (string) $databaseName;
$this->options = $options;
$this->filesCollection = new Collection(
$manager,
sprintf('%s.%s.files', $this->databaseName, $options['bucketName']),
$collectionOptions
);
$this->chunksCollection = new Collection(
$manager,
sprintf('%s.%s.chunks', $this->databaseName, $options['bucketName']),
$collectionOptions
);
}
/**
* Opens a Stream for reading the contents of a file specified by ID.
*
* @param ObjectId $id
* @return Stream
*/
public function openDownloadStream(ObjectId $id)
{
fopen('gridfs://$this->databaseName/$id', 'r');
}
/**
* Downloads the contents of the stored file specified by id and writes
* the contents to the destination Stream.
* @param ObjectId $id GridFS File Id
* @param Stream $destination Destination Stream
*/
public function downloadToStream(ObjectId $id, $destination)
{
$result = $this->filesCollection->findOne(['_id' => $id]);
if ($result == null) {
return;
}
if ($result->length == 0){
return;
}
$n=0;
$results = $this->chunksCollection->find(['files_id' => $result->_id]);
foreach ($results as $chunk) {
if ($chunk->n != $n) {
return;
}
fwrite($destination, $chunk->data);
$n++;
}
}
/**
* Return the chunkSizeBytes option for this Bucket.
*
* @return integer
*/
public function getChunkSizeBytes()
{
return $this->options['chunkSizeBytes'];
}
public function getDatabaseName()
{
return $this->databaseName;
}
public function getFilesCollection()
{
return $this->filesCollection;
}
public function getChunksCollection()
{
return $this->chunksCollection;
}
public function find($filter, array $options =[])
{
//add proper validation for the filter and for the options
return $this->filesCollection->find($filter);
}
public function chunkInsert($toUpload) {
$this->chunksCollection->insertOne($toUpload);
}
public function fileInsert($toUpload) {
$this->filesCollection->insertOne($toUpload);
}
public function ensureIndexes()
{
if ($this->ensuredIndexes) {
return;
}
if ( ! $this->isFilesCollectionEmpty()) {
return;
}
$this->ensureFilesIndex();
$this->ensureChunksIndex();
$this->ensuredIndexes = true;
}
private function ensureChunksIndex()
{
foreach ($this->chunksCollection->listIndexes() as $index) {
if ($index->isUnique() && $index->getKey() === ['files_id' => 1, 'n' => 1]) {
return;
}
}
$this->chunksCollection->createIndex(['files_id' => 1, 'n' => 1], ['unique' => true]);
}
private function ensureFilesIndex()
{
foreach ($this->filesCollection->listIndexes() as $index) {
if ($index->getKey() === ['filename' => 1, 'uploadDate' => 1]) {
return;
}
}
$this->filesCollection->createIndex(['filename' => 1, 'uploadDate' => 1]);
}
private function isFilesCollectionEmpty()
{
return null === $this->filesCollection->findOne([], [
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
'projection' => ['_id' => 1],
]);
}
public function delete(ObjectId $id)
{
$options = ['writeConcern' => $this->writeConcern];
$this->chunksCollection->deleteMany(['file_id' => $id], $options);
$this->filesCollection->deleteOne(['_id' => $id], $options);
}
}
<?php
namespace MongoDB\GridFS;
class BucketReadWriter
{
private $bucket;
public function __construct(Bucket $bucket)
{
$this->bucket = $bucket;
}
/**
* Opens a Stream for writing the contents of a file.
*
* @param string $filename file to upload
* @param array $options Stream Options
* @return Stream uploadStream
*/
public function openUploadStream($filename, array $options = [])
{
$options = [
'bucket' => $this->bucket,
'uploadOptions' => $options
];
$context = stream_context_create(['gridfs' => $options]);
return fopen(sprintf('gridfs://%s/%s', $this->bucket->getDatabaseName(), $filename), 'w', false, $context);
}
/**
* Upload a file to this bucket by specifying the source stream file
*
* @param String $filename Filename To Insert
* @param Stream $source Source Stream
* @param array $options Stream Options
* @return ObjectId
*/
public function uploadFromStream($filename, $source, array $options = [])
{
$gridFsStream = new GridFsUpload($this->bucket, $filename, $options);
return $gridFsStream->uploadFromStream($source);
}
}
<?php
namespace MongoDB\GridFS;
use MongoDB\Collection;
use MongoDB\Exception\RuntimeException;
/**
* GridFsStream holds the configuration for upload or download streams to GridFS
*
* @api
*/
class GridFsStream
{
protected $bucket;
protected $n;
protected $buffer;
protected $file;
/**
* Constructs a GridFsStream
*
* @param \MongoDB\GridFS\Bucket $bucket GridFS Bucket
*/
public function __construct(Bucket $bucket)
{
$this->bucket = $bucket;
$this->n = 0;
$this->buffer = fopen('php://temp', 'w+');
}
}
<?php
namespace MongoDB\GridFS;
use MongoDB\Collection;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedTypeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\BSON;
/**
* GridFsupload abstracts the processes of inserting into a GridFSBucket
*
* @api
*/
class GridFsUpload extends GridFsStream
{
private $ctx;
private $bufferLength;
private $indexChecker;
private $length=0;
/**
* Constructs a GridFS upload stream
*
* Supported options:
*
* * contentType (string): DEPRECATED content type to be stored with the file.
* This information should now be added to the metadata
*
* * aliases (array of strings): DEPRECATED An array of aliases.
* Applications wishing to store aliases should add an aliases field to the
* metadata document instead.
*
* * metadata (array or object): User data for the 'metadata' field of the files
* collection document.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param \MongoDB\Collection $filesCollection Files Collection
* @param \MongoDB\Collection $chunksCollection Chunks Collection
* @param int32 $chunkSizeBytes Size of chunk
* @param string $filename Filename to insert
* @param array $options File options
* @throws InvalidArgumentException
*/
public function __construct(
Bucket $bucket,
$filename,
array $options=[]
)
{
$this->bufferLength = 0;
$this->ctx = hash_init('md5');
$uploadDate = time();
$objectId = new \MongoDB\BSON\ObjectId();
$main_file = [
"chunkSize" => $bucket->getChunkSizeBytes(),
"filename" => $filename,
"uploadDate" => $uploadDate,
"_id" => $objectId
];
$fileOptions = [];
if (isset($options['contentType'])) {
if (is_string($options['contentType'])) {
$fileOptions['contentType'] = $options['contentType'];
} else {
throw new InvalidArgumentTypeException('"contentType" option', $options['contentType'], 'string');
}
}
if (isset($options['aliases'])) {
if (\MongoDB\is_string_array($options['aliases'])) {
$fileOptions['aliases'] = $options['aliases'];
} else {
throw new InvalidArgumentTypeException('"aliases" option', $options['aliases'], 'array of strings');
}
}
if (isset($options['metadata'])) {
if (is_array($options['metadata']) || is_object($options['metadata'])) {
$fileOptions['metadata'] = $options['metadata'];
} else {
throw new InvalidArgumentTypeException('"metadata" option', $options['metadata'], 'object or array');
}
}
$this->file = array_merge($main_file, $fileOptions);
parent::__construct($bucket);
}
/**
* Reads data from a stream into GridFS
*
* @param Stream $source Source Stream
* @return ObjectId
*/
public function uploadFromStream($source)
{
$this->bucket->ensureIndexes();
if (!is_resource($source) || get_resource_type($source) != "stream") {
throw new UnexpectedTypeException('stream', $source);
} else{
$streamMetadata = stream_get_meta_data($source);
} if (!is_readable($streamMetadata['uri'])) {
// throw new InvalidArgumentException("stream not readable");
//issue being that php's is_readable reports native streams as not readable like php://temp
}
while ($data = fread($source, $this->bucket->getChunkSizeBytes())) {
$this->insertChunk($data);
}
return $this->fileCollectionInsert();
}
/**
* Insert a chunks into GridFS from a string
*
* @param string $toWrite String to upload
* @return int
*/
public function insertChunks($toWrite)
{
$this->bucket->ensureIndexes();
$readBytes = 0;
while($readBytes != strlen($toWrite)) {
$addToBuffer = substr($toWrite, $readBytes, $this->bucket->getChunkSizeBytes() - $this->bufferLength);
fwrite($this->buffer, $addToBuffer);
$readBytes += strlen($addToBuffer);
$this->bufferLength += strlen($addToBuffer);
if($this->bufferLength == $this->bucket->getChunkSizeBytes()) {
rewind($this->buffer);
$this->insertChunk(fread($this->buffer, $this->bucket->getChunkSizeBytes()));
ftruncate($this->buffer,0);
$this->bufferLength = 0;
}
}
return $readBytes;
}
/**
* Close an active stream, pushes all buffered data to GridFS
*
*/
public function close()
{
rewind($this->buffer);
$cached = fread($this->buffer, $this->bucket->getChunkSizeBytes());
if(strlen($cached) > 0) {
insertChunk($cached);
}
fclose($this->buffer);
$this->fileCollectionInsert();
}
private function insertChunk($data)
{
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->n, "data" => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_GENERIC)];
hash_update($this->ctx, $data);
$this->bucket->chunkInsert($toUpload);
$this->length += strlen($data);
$this->n++;
}
private function fileCollectionInsert()
{
$md5 = hash_final($this->ctx);
$this->file = array_merge($this->file, ['length' => $this->length, 'md5' => $md5]);
$this->bucket->fileInsert($this->file);
return $this->file['_id'];
}
}
<?php
namespace MongoDB\GridFS;
use MongoDB\Driver\Server;
use MongoDB\Collection;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedValueException;
/**
* Stream wrapper for reading and writing a GridFS file.
*
* @internal
* @see MongoDB\GridFS\Bucket::openUploadStream()
*/
class StreamWrapper
{
public $context;
private $filename;
private $protocol = 'gridfs';
private $mode;
private $gridFsStream;
private $bucket;
/**
* Register the GridFS stream wrapper.
*/
public static function register()
{
if (in_array('gridfs', stream_get_wrappers())) {
stream_wrapper_unregister('gridfs');
}
stream_wrapper_register('gridfs', get_called_class(), STREAM_IS_URL);
}
private function initProtocol($path)
{
$parsed_path = parse_url($path);
$this->databaseName = $parsed_path["host"];
$this->identifier = substr($parsed_path["path"], 1);
}
public function stream_write($data)
{
$this->gridFsStream->insertChunks($data);
return strlen($data);
}
public function stream_read($count) {
$out ="";
if ($this->dirtyCache) {
$out = fread($this->buffer, $count);
if (strlen($out) == $count) {
return $out;
} else {
fclose($out);
$this->dirtyCache = false;
}
$this->n++;
}
if ($this->file->length <= $this->n) {
return false;
}
while(strlen($out) < $count && $this ->n <$this->file->length) {
$bytes_left = $count - strlen($out);
$next = $this->chunksCollection->findOne(['files_id' => $this->file->_id, "n" => $this->n]);
$out .= substr($next->data, 0, $bytes_left);
$this->n++;
}
if ($bytes_left < strlen($next->data)) {
$this->buffer = tmpfile();
fwrite($this->buffer, substr($next->data, $bytes_left));
$this->dirtyCache =true;
}
return $out;
}
public function stream_eof() {
return $this->n >= $this->file->length;
}
public function stream_close() {
$this->gridFsStream->close();
}
public function stream_open($path, $mode, $options, &$openedPath)
{
$this->initProtocol($path);
$context = stream_context_get_options($this->context);
$this->bucket =$context['gridfs']['bucket'];
$this->mode = $mode;
switch ($this->mode) {
case 'w' : return $this ->openWriteStream();
case 'r' : return $this ->openReadStream();
default: return false;
}
}
public function openWriteStream() {
$context = stream_context_get_options($this->context);
$options =$context['gridfs']['uploadOptions'];
$this->gridFsStream = new GridFsUpload($this->bucket, $this->identifier, $options);
return true;
}
public function openReadStream() {
$objectId = new \MongoDB\BSON\ObjectId($this->identifier);
$this->file = $this->filesCollection->findOne(['_id' => $objectId]);
return true;
}
}
......@@ -64,9 +64,9 @@ class FindOneAndUpdate implements Executable
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
}
if ( ! \MongoDB\is_first_key_operator($update)) {
throw new InvalidArgumentException('First key in $update argument is not an update operator');
}
// if ( ! \MongoDB\is_first_key_operator($update)) {
// throw new InvalidArgumentException('First key in $update argument is not an update operator');
// }
$options += [
'returnDocument' => self::RETURN_DOCUMENT_BEFORE,
......
......@@ -140,3 +140,16 @@ function server_supports_feature(Server $server, $feature)
return ($minWireVersion <= $feature && $maxWireVersion >= $feature);
}
function is_string_array($input) {
if (!is_array($input)){
return false;
}
foreach($input as $item) {
if (!is_string($item)) {
return false;
}
}
return true;
}
<?php
namespace MongoDB\Tests\GridFS;
use MongoDB\GridFS;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
/**
* Base class for GridFS functional tests.
*/
abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
protected $bucket;
protected $bucketReadWriter;
public function setUp()
{
parent::setUp();
$streamWrapper = new \MongoDB\GridFS\StreamWrapper();
$streamWrapper->register($this->manager);
$this->bucket = new \MongoDB\GridFS\Bucket($this->manager, $this->getDatabaseName());
$this->bucketReadWriter = new \MongoDB\GridFS\BucketReadWriter($this->bucket);
}
public function tearDown()
{
if ($this->hasFailed()) {
return;
}
//$this->database = new \MongoDB\Database($this->manager, $this->getDatabaseName());
// $this->database->drop();
}
}
GridFS Tests
============
The YAML and JSON files in this directory are platform-independent tests
meant to exercise a driver's implementation of GridFS.
Converting to JSON
==================
The tests are written in YAML because it is easier for humans to write
and read, and because YAML supports a standard comment format. Each test
is also provided in JSON format because in some languages it is easier
to parse JSON than YAML.
If you modify any test, you should modify the YAML file and then
regenerate the JSON file from it.
One way to convert the files is using an online web page. I used:
http://www.json2yaml.com/
It's advertised as a JSON to YAML converter but it can be used in either direction.
Note: the yaml2json utility from npm is not capable of converting these YAML tests
because it doesn't implement the full YAML spec.
Format
======
Each test file has two top level sections:
1. data
2. tests
The data section defines the initial contents of the files and chunks
collections for all tests in that file.
The tests section defines the tests to be run. The format of the tests
section will vary slightly depending on what tests are being defined.
In general, they will have the following sections:
1. description
2. arrange
3. act
4. assert
The arrange section, if present, defines changes to be made to the
initial contents of the files and chunks collections (as defined by
the data section) before this particular test is run. These changes
are described in the form of write commands that can be sent directly
to MongoDB.
The act section defines what operation (with which arguments) should
be performed.
The assert section defines what should be true at the end of the test.
This includes checking the return value of the operation, as well as
checking the expected contents of the files and chunks collections. The
expected contents of the files and chunks collections are described
in the form of write commands that modify collections named
expected.files and expected.chunks. Before running these commands,
load the initial files and chunks documents into the expected.files
and expected.chunks collections and then run the commands. At that point
you can assert that fs.files and expected.files are the same, and that
expected.chunks and fs.chunks are the same.
For operations that are expected to succeed the assert section contains
a "result" element describing the expected result. For operations
that are expected to fail the assert section contains an "error"
element describing the expected failure.
The "result" element is either the expected result when it is possible to
know the result in advance, or it is the special value "&result"
which means that we expect a result (not a failure) but the actual
value of the result could be anything. The notation "&result" is
modeled after YAML syntax for defining an anchor, and the
result value may be referenced later in the assert section as
"*result".
Another special notation in the assert section is "*actual", which
is used when the value of a field cannot be known in advance of the
test, so the assert logic should accept whatever the actual value
ended up being.
{
"data": {
"files": [
{
"_id": {
"$oid": "000000000000000000000001"
},
"length": 0,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "d41d8cd98f00b204e9800998ecf8427e",
"filename": "length-0",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000002"
},
"length": 0,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "d41d8cd98f00b204e9800998ecf8427e",
"filename": "length-0-with-empty-chunk",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000003"
},
"length": 2,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "c700ed4fdb1d27055aa3faa2c2432283",
"filename": "length-2",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000004"
},
"length": 8,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "dd254cdc958e53abaa67da9f797125f5",
"filename": "length-8",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
}
],
"chunks": [
{
"_id": {
"$oid": "000000000000000000000001"
},
"files_id": {
"$oid": "000000000000000000000002"
},
"n": 0,
"data": {
"$hex": ""
}
},
{
"_id": {
"$oid": "000000000000000000000002"
},
"files_id": {
"$oid": "000000000000000000000003"
},
"n": 0,
"data": {
"$hex": "1122"
}
},
{
"_id": {
"$oid": "000000000000000000000003"
},
"files_id": {
"$oid": "000000000000000000000004"
},
"n": 0,
"data": {
"$hex": "11223344"
}
},
{
"_id": {
"$oid": "000000000000000000000004"
},
"files_id": {
"$oid": "000000000000000000000004"
},
"n": 1,
"data": {
"$hex": "55667788"
}
}
]
},
"tests": [
{
"description": "Delete when length is 0",
"act": {
"operation": "delete",
"arguments": {
"id": {
"$oid": "000000000000000000000001"
}
}
},
"assert": {
"result": "void",
"data": [
{
"delete": "expected.files",
"deletes": [
{
"q": {
"_id": {
"$oid": "000000000000000000000001"
}
},
"limit": 1
}
]
}
]
}
},
{
"description": "Delete when length is 0 and there is one extra empty chunk",
"act": {
"operation": "delete",
"arguments": {
"id": {
"$oid": "000000000000000000000002"
}
}
},
"assert": {
"result": "void",
"data": [
{
"delete": "expected.files",
"deletes": [
{
"q": {
"_id": {
"$oid": "000000000000000000000002"
}
},
"limit": 1
}
]
},
{
"delete": "expected.chunks",
"deletes": [
{
"q": {
"files_id": {
"$oid": "000000000000000000000002"
}
},
"limit": 0
}
]
}
]
}
},
{
"description": "Delete when length is 8",
"act": {
"operation": "delete",
"arguments": {
"id": {
"$oid": "000000000000000000000004"
}
}
},
"assert": {
"result": "void",
"data": [
{
"delete": "expected.files",
"deletes": [
{
"q": {
"_id": {
"$oid": "000000000000000000000004"
}
},
"limit": 1
}
]
},
{
"delete": "expected.chunks",
"deletes": [
{
"q": {
"files_id": {
"$oid": "000000000000000000000004"
}
},
"limit": 0
}
]
}
]
}
},
{
"description": "Delete when files entry does not exist",
"act": {
"operation": "delete",
"arguments": {
"id": {
"$oid": "000000000000000000000000"
}
}
},
"assert": {
"error": "FileNotFound"
}
},
{
"description": "Delete when files entry does not exist and there are orphaned chunks",
"arrange": {
"data": [
{
"delete": "fs.files",
"deletes": [
{
"q": {
"_id": {
"$oid": "000000000000000000000004"
}
},
"limit": 1
}
]
}
]
},
"act": {
"operation": "delete",
"arguments": {
"id": {
"$oid": "000000000000000000000004"
}
}
},
"assert": {
"error": "FileNotFound",
"data": [
{
"delete": "expected.files",
"deletes": [
{
"q": {
"_id": {
"$oid": "000000000000000000000004"
}
},
"limit": 1
}
]
},
{
"delete": "expected.chunks",
"deletes": [
{
"q": {
"files_id": {
"$oid": "000000000000000000000004"
}
},
"limit": 0
}
]
}
]
}
}
]
}
\ No newline at end of file
data:
files:
-
_id: { "$oid" : "000000000000000000000001" }
length: 0
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "d41d8cd98f00b204e9800998ecf8427e"
filename: "length-0"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000002" }
length: 0
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "d41d8cd98f00b204e9800998ecf8427e"
filename: "length-0-with-empty-chunk"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000003" }
length: 2
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "c700ed4fdb1d27055aa3faa2c2432283"
filename: "length-2"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000004" }
length: 8
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "dd254cdc958e53abaa67da9f797125f5"
filename: "length-8"
contentType: "application/octet-stream"
aliases: []
metadata: {}
chunks:
- { _id : { "$oid" : "000000000000000000000001" }, files_id : { "$oid" : "000000000000000000000002" }, n : 0, data : { $hex : "" } }
- { _id : { "$oid" : "000000000000000000000002" }, files_id : { "$oid" : "000000000000000000000003" }, n : 0, data : { $hex : "1122" } }
- { _id : { "$oid" : "000000000000000000000003" }, files_id : { "$oid" : "000000000000000000000004" }, n : 0, data : { $hex : "11223344" } }
- { _id : { "$oid" : "000000000000000000000004" }, files_id : { "$oid" : "000000000000000000000004" }, n : 1, data : { $hex : "55667788" } }
tests:
-
description: "Delete when length is 0"
act:
operation: delete
arguments:
id: { "$oid" : "000000000000000000000001" }
assert:
result: void
data:
-
{ delete : "expected.files", deletes : [
{ q : { _id : { "$oid" : "000000000000000000000001" } }, limit : 1 }
] }
-
description: "Delete when length is 0 and there is one extra empty chunk"
act:
operation: delete
arguments:
id: { "$oid" : "000000000000000000000002" }
assert:
result: void
data:
-
{ delete : "expected.files", deletes : [
{ q : { _id : { "$oid" : "000000000000000000000002" } }, limit : 1 }
] }
-
{ delete : "expected.chunks", deletes : [
{ q : { files_id : { "$oid" : "000000000000000000000002" } }, limit : 0 }
] }
-
description: "Delete when length is 8"
act:
operation: delete
arguments:
id: { "$oid" : "000000000000000000000004" }
assert:
result: void
data:
-
{ delete : "expected.files", deletes : [
{ q : { _id : { "$oid" : "000000000000000000000004" } }, limit : 1 }
] }
-
{ delete : "expected.chunks", deletes : [
{ q : { files_id : { "$oid" : "000000000000000000000004" } }, limit : 0 }
] }
-
description: "Delete when files entry does not exist"
act:
operation: delete
arguments:
id: { "$oid" : "000000000000000000000000" }
assert:
error: "FileNotFound"
-
description: "Delete when files entry does not exist and there are orphaned chunks"
arrange:
data:
-
{ delete : "fs.files", deletes : [
{ q : { _id : { "$oid" : "000000000000000000000004" } }, limit : 1 }
] }
act:
operation: delete
arguments:
id: { "$oid" : "000000000000000000000004" }
assert:
error: "FileNotFound"
data:
-
{ delete : "expected.files", deletes : [
{ q : { _id : { "$oid" : "000000000000000000000004" } }, limit : 1 }
] }
-
{ delete : "expected.chunks", deletes : [
{ q : { files_id : { "$oid" : "000000000000000000000004" } }, limit : 0 }
] }
{
"data": {
"files": [
{
"_id": {
"$oid": "000000000000000000000001"
},
"length": 0,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "d41d8cd98f00b204e9800998ecf8427e",
"filename": "length-0",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000002"
},
"length": 0,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "d41d8cd98f00b204e9800998ecf8427e",
"filename": "length-0-with-empty-chunk",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000003"
},
"length": 2,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "c700ed4fdb1d27055aa3faa2c2432283",
"filename": "length-2",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000004"
},
"length": 8,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "dd254cdc958e53abaa67da9f797125f5",
"filename": "length-8",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000005"
},
"length": 10,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "57d83cd477bfb1ccd975ab33d827a92b",
"filename": "length-10",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
}
],
"chunks": [
{
"_id": {
"$oid": "000000000000000000000001"
},
"files_id": {
"$oid": "000000000000000000000002"
},
"n": 0,
"data": {
"$hex": ""
}
},
{
"_id": {
"$oid": "000000000000000000000002"
},
"files_id": {
"$oid": "000000000000000000000003"
},
"n": 0,
"data": {
"$hex": "1122"
}
},
{
"_id": {
"$oid": "000000000000000000000003"
},
"files_id": {
"$oid": "000000000000000000000004"
},
"n": 0,
"data": {
"$hex": "11223344"
}
},
{
"_id": {
"$oid": "000000000000000000000004"
},
"files_id": {
"$oid": "000000000000000000000004"
},
"n": 1,
"data": {
"$hex": "55667788"
}
},
{
"_id": {
"$oid": "000000000000000000000005"
},
"files_id": {
"$oid": "000000000000000000000005"
},
"n": 0,
"data": {
"$hex": "11223344"
}
},
{
"_id": {
"$oid": "000000000000000000000006"
},
"files_id": {
"$oid": "000000000000000000000005"
},
"n": 1,
"data": {
"$hex": "55667788"
}
},
{
"_id": {
"$oid": "000000000000000000000007"
},
"files_id": {
"$oid": "000000000000000000000005"
},
"n": 2,
"data": {
"$hex": "99aa"
}
}
]
},
"tests": [
{
"description": "Download when length is zero",
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000001"
},
"options": {
}
}
},
"assert": {
"result": {
"$hex": ""
}
}
},
{
"description": "Download when length is zero and there is one empty chunk",
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000002"
},
"options": {
}
}
},
"assert": {
"result": {
"$hex": ""
}
}
},
{
"description": "Download when there is one chunk",
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000003"
},
"options": {
}
}
},
"assert": {
"result": {
"$hex": "1122"
}
}
},
{
"description": "Download when there are two chunks",
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000004"
},
"options": {
}
}
},
"assert": {
"result": {
"$hex": "1122334455667788"
}
}
},
{
"description": "Download when there are three chunks",
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000005"
},
"options": {
}
}
},
"assert": {
"result": {
"$hex": "112233445566778899aa"
}
}
},
{
"description": "Download when files entry does not exist",
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000000"
},
"options": {
}
}
},
"assert": {
"error": "FileNotFound"
}
},
{
"description": "Download when an intermediate chunk is missing",
"arrange": {
"data": [
{
"delete": "fs.chunks",
"deletes": [
{
"q": {
"files_id": {
"$oid": "000000000000000000000005"
},
"n": 1
},
"limit": 1
}
]
}
]
},
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000005"
}
}
},
"assert": {
"error": "ChunkIsMissing"
}
},
{
"description": "Download when final chunk is missing",
"arrange": {
"data": [
{
"delete": "fs.chunks",
"deletes": [
{
"q": {
"files_id": {
"$oid": "000000000000000000000005"
},
"n": 1
},
"limit": 1
}
]
}
]
},
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000005"
}
}
},
"assert": {
"error": "ChunkIsMissing"
}
},
{
"description": "Download when an intermediate chunk is the wrong size",
"arrange": {
"data": [
{
"update": "fs.chunks",
"updates": [
{
"q": {
"files_id": {
"$oid": "000000000000000000000005"
},
"n": 1
},
"u": {
"$set": {
"data": {
"$hex": "556677"
}
}
}
},
{
"q": {
"files_id": {
"$oid": "000000000000000000000005"
},
"n": 2
},
"u": {
"$set": {
"data": {
"$hex": "8899aa"
}
}
}
}
]
}
]
},
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000005"
}
}
},
"assert": {
"error": "ChunkIsWrongSize"
}
},
{
"description": "Download when final chunk is the wrong size",
"arrange": {
"data": [
{
"update": "fs.chunks",
"updates": [
{
"q": {
"files_id": {
"$oid": "000000000000000000000005"
},
"n": 2
},
"u": {
"$set": {
"data": {
"$hex": "99"
}
}
}
}
]
}
]
},
"act": {
"operation": "download",
"arguments": {
"id": {
"$oid": "000000000000000000000005"
}
}
},
"assert": {
"error": "ChunkIsWrongSize"
}
}
]
}
\ No newline at end of file
data:
files:
-
_id: { "$oid" : "000000000000000000000001" }
length: 0
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "d41d8cd98f00b204e9800998ecf8427e"
filename: "length-0"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000002" }
length: 0
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "d41d8cd98f00b204e9800998ecf8427e"
filename: "length-0-with-empty-chunk"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000003" }
length: 2
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "c700ed4fdb1d27055aa3faa2c2432283"
filename: "length-2"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000004" }
length: 8
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "dd254cdc958e53abaa67da9f797125f5"
filename: "length-8"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000005" }
length: 10
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "57d83cd477bfb1ccd975ab33d827a92b"
filename: "length-10"
contentType: "application/octet-stream"
aliases: []
metadata: {}
chunks:
- { _id : { "$oid" : "000000000000000000000001" }, files_id : { "$oid" : "000000000000000000000002" }, n : 0, data : { $hex : "" } }
- { _id : { "$oid" : "000000000000000000000002" }, files_id : { "$oid" : "000000000000000000000003" }, n : 0, data : { $hex : "1122" } }
- { _id : { "$oid" : "000000000000000000000003" }, files_id : { "$oid" : "000000000000000000000004" }, n : 0, data : { $hex : "11223344" } }
- { _id : { "$oid" : "000000000000000000000004" }, files_id : { "$oid" : "000000000000000000000004" }, n : 1, data : { $hex : "55667788" } }
- { _id : { "$oid" : "000000000000000000000005" }, files_id : { "$oid" : "000000000000000000000005" }, n : 0, data : { $hex : "11223344" } }
- { _id : { "$oid" : "000000000000000000000006" }, files_id : { "$oid" : "000000000000000000000005" }, n : 1, data : { $hex : "55667788" } }
- { _id : { "$oid" : "000000000000000000000007" }, files_id : { "$oid" : "000000000000000000000005" }, n : 2, data : { $hex : "99aa" } }
tests:
-
description: "Download when length is zero"
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000001" }
options: { }
assert:
result: { $hex : "" }
-
description: "Download when length is zero and there is one empty chunk"
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000002" }
options: { }
assert:
result: { $hex : "" }
-
description: "Download when there is one chunk"
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000003" }
options: { }
assert:
result: { $hex : "1122" }
-
description: "Download when there are two chunks"
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000004" }
options: { }
assert:
result: { $hex : "1122334455667788" }
-
description: "Download when there are three chunks"
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000005" }
options: { }
assert:
result: { $hex : "112233445566778899aa" }
-
description: "Download when files entry does not exist"
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000000" }
options: { }
assert:
error: "FileNotFound"
-
description: "Download when an intermediate chunk is missing"
arrange:
data:
-
{ delete : "fs.chunks", deletes : [
{ q : { files_id : { "$oid" : "000000000000000000000005" }, n : 1 }, limit : 1 }
] }
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000005" }
assert:
error: "ChunkIsMissing"
-
description: "Download when final chunk is missing"
arrange:
data:
-
{ delete : "fs.chunks", deletes : [
{ q : { files_id : { "$oid" : "000000000000000000000005" }, n : 1 }, limit : 1 }
] }
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000005" }
assert:
error: "ChunkIsMissing"
-
description: "Download when an intermediate chunk is the wrong size"
arrange:
data:
-
{ update : "fs.chunks", updates : [
{ q : { files_id : { "$oid" : "000000000000000000000005" }, n : 1 }, u : { $set : { data : { $hex : "556677" } } } },
{ q : { files_id : { "$oid" : "000000000000000000000005" }, n : 2 }, u : { $set : { data : { $hex : "8899aa" } } } }
] }
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000005" }
assert:
error: "ChunkIsWrongSize"
-
description: "Download when final chunk is the wrong size"
arrange:
data:
-
{ update : "fs.chunks", updates : [
{ q : { files_id : { "$oid" : "000000000000000000000005" }, n : 2 }, u : { $set : { data : { $hex : "99" } } } }
] }
act:
operation: download
arguments:
id: { "$oid" : "000000000000000000000005" }
assert:
error: "ChunkIsWrongSize"
{
"data": {
"files": [
{
"_id": {
"$oid": "000000000000000000000001"
},
"length": 1,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-01T00:00:00.000Z"
},
"md5": "47ed733b8d10be225eceba344d533586",
"filename": "abc",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000002"
},
"length": 1,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-02T00:00:00.000Z"
},
"md5": "b15835f133ff2e27c7cb28117bfae8f4",
"filename": "abc",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000003"
},
"length": 1,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-03T00:00:00.000Z"
},
"md5": "eccbc87e4b5ce2fe28308fd9f2a7baf3",
"filename": "abc",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000004"
},
"length": 1,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-04T00:00:00.000Z"
},
"md5": "f623e75af30e62bbd73d6df5b50bb7b5",
"filename": "abc",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
},
{
"_id": {
"$oid": "000000000000000000000005"
},
"length": 1,
"chunkSize": 4,
"uploadDate": {
"$date": "1970-01-05T00:00:00.000Z"
},
"md5": "4c614360da93c0a041b22e537de151eb",
"filename": "abc",
"contentType": "application/octet-stream",
"aliases": [
],
"metadata": {
}
}
],
"chunks": [
{
"_id": {
"$oid": "000000000000000000000001"
},
"files_id": {
"$oid": "000000000000000000000001"
},
"n": 0,
"data": {
"$hex": "11"
}
},
{
"_id": {
"$oid": "000000000000000000000002"
},
"files_id": {
"$oid": "000000000000000000000002"
},
"n": 0,
"data": {
"$hex": "22"
}
},
{
"_id": {
"$oid": "000000000000000000000003"
},
"files_id": {
"$oid": "000000000000000000000003"
},
"n": 0,
"data": {
"$hex": "33"
}
},
{
"_id": {
"$oid": "000000000000000000000004"
},
"files_id": {
"$oid": "000000000000000000000004"
},
"n": 0,
"data": {
"$hex": "44"
}
},
{
"_id": {
"$oid": "000000000000000000000005"
},
"files_id": {
"$oid": "000000000000000000000005"
},
"n": 0,
"data": {
"$hex": "55"
}
}
]
},
"tests": [
{
"description": "Download_by_name when revision is 0",
"act": {
"operation": "download_by_name",
"arguments": {
"filename": "abc",
"options": {
"revision": 0
}
}
},
"assert": {
"result": {
"$hex": "11"
}
}
},
{
"description": "Download_by_name when revision is 1",
"act": {
"operation": "download_by_name",
"arguments": {
"filename": "abc",
"options": {
"revision": 1
}
}
},
"assert": {
"result": {
"$hex": "22"
}
}
},
{
"description": "Download_by_name when revision is -2",
"act": {
"operation": "download_by_name",
"arguments": {
"filename": "abc",
"options": {
"revision": -2
}
}
},
"assert": {
"result": {
"$hex": "44"
}
}
},
{
"description": "Download_by_name when revision is -1",
"act": {
"operation": "download_by_name",
"arguments": {
"filename": "abc",
"options": {
"revision": -1
}
}
},
"assert": {
"result": {
"$hex": "55"
}
}
},
{
"description": "Download_by_name when files entry does not exist",
"act": {
"operation": "download_by_name",
"arguments": {
"filename": "xyz"
}
},
"assert": {
"error": "FileNotFound"
}
},
{
"description": "Download_by_name when revision does not exist",
"act": {
"operation": "download_by_name",
"arguments": {
"filename": "abc",
"options": {
"revision": 999
}
}
},
"assert": {
"error": "RevisionNotFound"
}
}
]
}
\ No newline at end of file
data:
files:
-
_id: { "$oid" : "000000000000000000000001" }
length: 1
chunkSize: 4
uploadDate: { "$date" : "1970-01-01T00:00:00.000Z" }
md5: "47ed733b8d10be225eceba344d533586"
filename: "abc"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000002" }
length: 1
chunkSize: 4
uploadDate: { "$date" : "1970-01-02T00:00:00.000Z" }
md5: "b15835f133ff2e27c7cb28117bfae8f4"
filename: "abc"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000003" }
length: 1
chunkSize: 4
uploadDate: { "$date" : "1970-01-03T00:00:00.000Z" }
md5: "eccbc87e4b5ce2fe28308fd9f2a7baf3"
filename: "abc"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000004" }
length: 1
chunkSize: 4
uploadDate: { "$date" : "1970-01-04T00:00:00.000Z" }
md5: "f623e75af30e62bbd73d6df5b50bb7b5"
filename: "abc"
contentType: "application/octet-stream"
aliases: []
metadata: {}
-
_id: { "$oid" : "000000000000000000000005" }
length: 1
chunkSize: 4
uploadDate: { "$date" : "1970-01-05T00:00:00.000Z" }
md5: "4c614360da93c0a041b22e537de151eb"
filename: "abc"
contentType: "application/octet-stream"
aliases: []
metadata: {}
chunks:
- { _id : { "$oid" : "000000000000000000000001" }, files_id : { "$oid" : "000000000000000000000001" }, n : 0, data : { $hex : "11" } }
- { _id : { "$oid" : "000000000000000000000002" }, files_id : { "$oid" : "000000000000000000000002" }, n : 0, data : { $hex : "22" } }
- { _id : { "$oid" : "000000000000000000000003" }, files_id : { "$oid" : "000000000000000000000003" }, n : 0, data : { $hex : "33" } }
- { _id : { "$oid" : "000000000000000000000004" }, files_id : { "$oid" : "000000000000000000000004" }, n : 0, data : { $hex : "44" } }
- { _id : { "$oid" : "000000000000000000000005" }, files_id : { "$oid" : "000000000000000000000005" }, n : 0, data : { $hex : "55" } }
tests:
-
description: "Download_by_name when revision is 0"
act:
operation: download_by_name
arguments:
filename: "abc"
options: { revision : 0 }
assert:
result: { $hex : "11" }
-
description: "Download_by_name when revision is 1"
act:
operation: download_by_name
arguments:
filename: "abc"
options: { revision : 1 }
assert:
result: { $hex : "22" }
-
description: "Download_by_name when revision is -2"
act:
operation: download_by_name
arguments:
filename: "abc"
options: { revision : -2 }
assert:
result: { $hex : "44" }
-
description: "Download_by_name when revision is -1"
act:
operation: download_by_name
arguments:
filename: "abc"
options: { revision : -1 }
assert:
result: { $hex : "55" }
-
description: "Download_by_name when files entry does not exist"
act:
operation: download_by_name
arguments:
filename: "xyz"
assert:
error: "FileNotFound"
-
description: "Download_by_name when revision does not exist"
act:
operation: download_by_name
arguments:
filename: "abc"
options: { revision : 999 }
assert:
error: "RevisionNotFound"
{
"data": {
"files": [
],
"chunks": [
]
},
"tests": [
{
"description": "Upload when length is 0",
"act": {
"operation": "upload",
"arguments": {
"filename": "filename",
"source": {
"$hex": ""
},
"options": {
"chunkSizeBytes": 4
}
}
},
"assert": {
"result": "&result",
"data": [
{
"insert": "expected.files",
"documents": [
{
"_id": "*result",
"length": 0,
"chunkSize": 4,
"uploadDate": "*actual",
"md5": "d41d8cd98f00b204e9800998ecf8427e",
"filename": "filename"
}
]
}
]
}
},
{
"description": "Upload when length is 1",
"act": {
"operation": "upload",
"arguments": {
"filename": "filename",
"source": {
"$hex": "11"
},
"options": {
"chunkSizeBytes": 4
}
}
},
"assert": {
"result": "&result",
"data": [
{
"insert": "expected.files",
"documents": [
{
"_id": "*result",
"length": 1,
"chunkSize": 4,
"uploadDate": "*actual",
"md5": "47ed733b8d10be225eceba344d533586",
"filename": "filename"
}
]
},
{
"insert": "expected.chunks",
"documents": [
{
"_id": "*actual",
"files_id": "*result",
"n": 0,
"data": {
"$hex": "11"
}
}
]
}
]
}
},
{
"description": "Upload when length is 3",
"act": {
"operation": "upload",
"arguments": {
"filename": "filename",
"source": {
"$hex": "112233"
},
"options": {
"chunkSizeBytes": 4
}
}
},
"assert": {
"result": "&result",
"data": [
{
"insert": "expected.files",
"documents": [
{
"_id": "*result",
"length": 3,
"chunkSize": 4,
"uploadDate": "*actual",
"md5": "bafae3a174ab91fc70db7a6aa50f4f52",
"filename": "filename"
}
]
},
{
"insert": "expected.chunks",
"documents": [
{
"_id": "*actual",
"files_id": "*result",
"n": 0,
"data": {
"$hex": "112233"
}
}
]
}
]
}
},
{
"description": "Upload when length is 4",
"act": {
"operation": "upload",
"arguments": {
"filename": "filename",
"source": {
"$hex": "11223344"
},
"options": {
"chunkSizeBytes": 4
}
}
},
"assert": {
"result": "&result",
"data": [
{
"insert": "expected.files",
"documents": [
{
"_id": "*result",
"length": 4,
"chunkSize": 4,
"uploadDate": "*actual",
"md5": "7e7c77cff5705d1f7574a25ef6662117",
"filename": "filename"
}
]
},
{
"insert": "expected.chunks",
"documents": [
{
"_id": "*actual",
"files_id": "*result",
"n": 0,
"data": {
"$hex": "11223344"
}
}
]
}
]
}
},
{
"description": "Upload when length is 5",
"act": {
"operation": "upload",
"arguments": {
"filename": "filename",
"source": {
"$hex": "1122334455"
},
"options": {
"chunkSizeBytes": 4
}
}
},
"assert": {
"result": "&result",
"data": [
{
"insert": "expected.files",
"documents": [
{
"_id": "*result",
"length": 5,
"chunkSize": 4,
"uploadDate": "*actual",
"md5": "283d4fea5dded59cf837d3047328f5af",
"filename": "filename"
}
]
},
{
"insert": "expected.chunks",
"documents": [
{
"_id": "*actual",
"files_id": "*result",
"n": 0,
"data": {
"$hex": "11223344"
}
},
{
"_id": "*actual",
"files_id": "*result",
"n": 1,
"data": {
"$hex": "55"
}
}
]
}
]
}
},
{
"description": "Upload when length is 8",
"act": {
"operation": "upload",
"arguments": {
"filename": "filename",
"source": {
"$hex": "1122334455667788"
},
"options": {
"chunkSizeBytes": 4
}
}
},
"assert": {
"result": "&result",
"data": [
{
"insert": "expected.files",
"documents": [
{
"_id": "*result",
"length": 8,
"chunkSize": 4,
"uploadDate": "*actual",
"md5": "dd254cdc958e53abaa67da9f797125f5",
"filename": "filename"
}
]
},
{
"insert": "expected.chunks",
"documents": [
{
"_id": "*actual",
"files_id": "*result",
"n": 0,
"data": {
"$hex": "11223344"
}
},
{
"_id": "*actual",
"files_id": "*result",
"n": 1,
"data": {
"$hex": "55667788"
}
}
]
}
]
}
},
{
"description": "Upload when contentType is provided",
"act": {
"operation": "upload",
"arguments": {
"filename": "filename",
"source": {
"$hex": "11"
},
"options": {
"chunkSizeBytes": 4,
"contentType": "image/jpeg"
}
}
},
"assert": {
"result": "&result",
"data": [
{
"insert": "expected.files",
"documents": [
{
"_id": "*result",
"length": 1,
"chunkSize": 4,
"uploadDate": "*actual",
"md5": "47ed733b8d10be225eceba344d533586",
"filename": "filename",
"contentType": "image/jpeg"
}
]
},
{
"insert": "expected.chunks",
"documents": [
{
"_id": "*actual",
"files_id": "*result",
"n": 0,
"data": {
"$hex": "11"
}
}
]
}
]
}
},
{
"description": "Upload when metadata is provided",
"act": {
"operation": "upload",
"arguments": {
"filename": "filename",
"source": {
"$hex": "11"
},
"options": {
"chunkSizeBytes": 4,
"metadata": {
"x": 1
}
}
}
},
"assert": {
"result": "&result",
"data": [
{
"insert": "expected.files",
"documents": [
{
"_id": "*result",
"length": 1,
"chunkSize": 4,
"uploadDate": "*actual",
"md5": "47ed733b8d10be225eceba344d533586",
"filename": "filename",
"metadata": {
"x": 1
}
}
]
},
{
"insert": "expected.chunks",
"documents": [
{
"_id": "*actual",
"files_id": "*result",
"n": 0,
"data": {
"$hex": "11"
}
}
]
}
]
}
}
]
}
\ No newline at end of file
data:
files: []
chunks: []
tests:
-
description: "Upload when length is 0"
act:
operation: upload
arguments:
filename: "filename"
source: { $hex : "" }
options: { chunkSizeBytes : 4 }
assert:
result: "&result"
data:
-
{ insert : "expected.files", documents : [
{ _id : "*result", length : 0, chunkSize : 4, uploadDate : "*actual", md5 : "d41d8cd98f00b204e9800998ecf8427e", filename : "filename" }
] }
-
description: "Upload when length is 1"
act:
operation: upload
arguments:
filename: "filename"
source: { $hex : "11" }
options: { chunkSizeBytes : 4 }
assert:
result: "&result"
data:
-
{ insert : "expected.files", documents : [
{ _id : "*result", length : 1, chunkSize : 4, uploadDate : "*actual", md5 : "47ed733b8d10be225eceba344d533586", filename : "filename" }
] }
-
{ insert : "expected.chunks", documents : [
{ _id : "*actual", files_id : "*result", n : 0, data : { $hex : "11" } }
] }
-
description: "Upload when length is 3"
act:
operation: upload
arguments:
filename: "filename"
source: { $hex : "112233" }
options: { chunkSizeBytes : 4 }
assert:
result: "&result"
data:
-
{ insert : "expected.files", documents : [
{ _id : "*result", length : 3, chunkSize : 4, uploadDate : "*actual", md5 : "bafae3a174ab91fc70db7a6aa50f4f52", filename : "filename" }
] }
-
{ insert : "expected.chunks", documents : [
{ _id : "*actual", files_id : "*result", n : 0, data : { $hex : "112233" } }
] }
-
description: "Upload when length is 4"
act:
operation: upload
arguments:
filename: "filename"
source: { $hex : "11223344" }
options: { chunkSizeBytes : 4 }
assert:
result: "&result"
data:
-
{ insert : "expected.files", documents : [
{ _id : "*result", length : 4, chunkSize : 4, uploadDate : "*actual", md5 : "7e7c77cff5705d1f7574a25ef6662117", filename : "filename" }
] }
-
{ insert : "expected.chunks", documents : [
{ _id : "*actual", files_id : "*result", n : 0, data : { $hex : "11223344" } }
] }
-
description: "Upload when length is 5"
act:
operation: upload
arguments:
filename: "filename"
source: { $hex : "1122334455" }
options: { chunkSizeBytes : 4 }
assert:
result: "&result"
data:
-
{ insert : "expected.files", documents : [
{ _id : "*result", length : 5, chunkSize : 4, uploadDate : "*actual", md5 : "283d4fea5dded59cf837d3047328f5af", filename : "filename" }
] }
-
{ insert : "expected.chunks", documents : [
{ _id : "*actual", files_id : "*result", n : 0, data : { $hex : "11223344" } },
{ _id : "*actual", files_id : "*result", n : 1, data : { $hex : "55" } }
] }
-
description: "Upload when length is 8"
act:
operation: upload
arguments:
filename: "filename"
source: { $hex : "1122334455667788" }
options: { chunkSizeBytes : 4 }
assert:
result: "&result"
data:
-
{ insert : "expected.files", documents : [
{ _id : "*result", length : 8, chunkSize : 4, uploadDate : "*actual", md5 : "dd254cdc958e53abaa67da9f797125f5", filename : "filename" }
] }
-
{ insert : "expected.chunks", documents : [
{ _id : "*actual", files_id : "*result", n : 0, data : { $hex : "11223344" } },
{ _id : "*actual", files_id : "*result", n : 1, data : { $hex : "55667788" } }
] }
-
description: "Upload when contentType is provided"
act:
operation: upload
arguments:
filename: "filename"
source: { $hex : "11" }
options: { chunkSizeBytes : 4, contentType : "image/jpeg" }
assert:
result: "&result"
data:
-
{ insert : "expected.files", documents : [
{ _id : "*result", length : 1, chunkSize : 4, uploadDate : "*actual", md5 : "47ed733b8d10be225eceba344d533586", filename : "filename", contentType : "image/jpeg" }
] }
-
{ insert : "expected.chunks", documents : [
{ _id : "*actual", files_id : "*result", n : 0, data : { $hex : "11" } }
] }
-
description: "Upload when metadata is provided"
act:
operation: upload
arguments:
filename: "filename"
source: { $hex : "11" }
options:
chunkSizeBytes: 4
metadata: { x : 1 }
assert:
result: "&result"
data:
-
{ insert : "expected.files", documents : [
{ _id : "*result", length : 1, chunkSize : 4, uploadDate : "*actual", md5 : "47ed733b8d10be225eceba344d533586", filename : "filename", metadata : { x : 1 } }
] }
-
{ insert : "expected.chunks", documents : [
{ _id : "*actual", files_id : "*result", n : 0, data : { $hex : "11" } }
] }
<?php
namespace MongoDB\Tests\GridFS;
use \MongoDB\GridFS;
use \MongoDB\Collection;
use \MongoDB\BSON\ObjectId;
use \MongoDB\BSON\Binary;
class SpecificationTests extends FunctionalTestCase
{
private $commands;
private $collections;
public function setUp()
{
parent::setUp();
$this->commands = array(
'insert' => function($col, $docs) {
$col->insertMany($docs['documents']);}
);
}
/**
*@dataProvider provideSpecificationTests
*/
public function testSpecificationTests($testJson)
{
foreach ($testJson['tests'] as $test) {
$this->bucket = new \MongoDB\GridFS\Bucket($this->manager, $this->getDatabaseName(), $test['act']['arguments']['options']);
$this->bucketReadWriter = new \MongoDB\GridFS\BucketReadWriter($this->bucket);
$func = $test['act']['operation'] . "Command";
$error = null;
try {
$result = $this->$func($test['act']['arguments']);
} catch(Exception $e) {
$error = $e;
}
if (!isset($test['assert']['error'])) {
//check that test didn't throw error
} else {
//check that the error matches what we got
}
if (isset($test['assert']['result'])) {
$testResult = $test['assert']['result'];
if ($testResult == "&result") {
$test['assert']['result'] = $result;
}
if ($testResult == "void") {
$test['assert']['result'] = null;
}
$this->assertEquals($result, $test['assert']['result']);
}
if (isset($test['assert']['data'])) {
$this->runCommands($test['assert']['data'], $result);
$this->collectionsEqual($this->collections['expected.files'],$this->bucket->getFilesCollection());
if(isset($this->collections['expected.chunks'])) {
$this->collectionsEqual($this->collections['expected.chunks'],$this->bucket->getChunksCollection());
}
}
}
}
public function provideSpecificationTests()
{
$testPath=getcwd().'/tests/GridFS/Specification/tests/upload.json';
$testArgs = [];
foreach(glob($testPath) as $filename) {
$fileContents = file_get_contents($filename);
$testJson = json_decode($fileContents, true);
$testArgs[][] = $testJson;
}
return $testArgs;
}
public function fixTypes($testJson, $makeBinary)
{
$result = $testJson;
foreach($result as $key =>$value) {
if (is_array($value) && isset($value['$hex'])) {
$result[$key] = hex2bin($value['$hex']);
if($makeBinary) {
$result[$key] = new \MongoDB\BSON\Binary($result[$key], \MongoDB\BSON\Binary::TYPE_GENERIC);
}
} else if (is_array($value) && isset($value['$oid'])) {
$result[$key] = new ObjectId("".$value['$oid']);
} else if (is_array($value)) {
$result[$key] = $this->fixTypes($result[$key], $makeBinary);
} else if(is_string($value) && $value == '*actual') {
unset($result[$key]);
}
}
return $result;
}
public function collectionsEqual($col1, $col2)
{
$docs1 = $this->filterDoc($col1, true);
$docs2 = $this->filterDoc($col2, true);
$this->assertSameDocuments($docs1, $docs2);
}
public function filterDoc($collection, $ignoreId)
{
$output = [];
$documents = $collection->find();
foreach($documents as $doc){
if ($ignoreId) {
unset($doc->_id);
}
if(isset($doc->uploadDate)) {
// $this->assertTrue($doc->uploadDate instanceof DateTime);
unset($doc->uploadDate);
}
$output [] = $doc;
}
return $output;
}
public function runCommands($cmds, $result)
{
$cmds = $this->fixTypes($cmds, true);
foreach($cmds as $cmd) {
foreach($cmd as $key => $value) {
if(isset($this->commands[$key])) {
$cmdName = $key;
$collectionName = $value;
foreach($cmd['documents'] as $docIndex => $doc) {
foreach($doc as $docKey => $docVal){
if(is_string($docVal)) {
if($docVal == '*result') {
$doc[$docKey] = $result;
}
}
}
$cmd['documents'][$docIndex] = $doc;
}
$collection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), $collectionName));
$this->commands[$key]($collection, $this->fixTypes($cmd, true));
$this->collections[$collectionName] = $collection;
}
}
}
}
public function uploadCommand($args)
{
$args = $this->fixTypes($args, false);
$stream = fopen('php://temp', 'w+');
fwrite($stream, $args['source']);
rewind($stream);
return $this->bucketReadWriter->uploadFromStream($args['filename'], $stream, $args['options']);
}
function downloadCommand($args)
{
}
function deleteCommand($args)
{
}
function download_by_nameCommand($args)
{
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment