Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Junming
jio
Commits
3ad7eaba
Commit
3ad7eaba
authored
Dec 05, 2013
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
revisionstorage putAttachment unit tests done
parent
c1a8aff1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
271 additions
and
191 deletions
+271
-191
src/jio.storage/revisionstorage.js
src/jio.storage/revisionstorage.js
+36
-22
test/jio.storage/revisionstorage.tests.js
test/jio.storage/revisionstorage.tests.js
+235
-169
No files found.
src/jio.storage/revisionstorage.js
View file @
3ad7eaba
...
...
@@ -20,6 +20,11 @@
}([
'
jio
'
,
'
sha256
'
],
function
(
jIO
,
sha256
)
{
"
use strict
"
;
var
tool
=
{
"
readBlobAsBinaryString
"
:
jIO
.
util
.
readBlobAsBinaryString
,
"
uniqueJSONStringify
"
:
jIO
.
util
.
uniqueJSONStringify
};
jIO
.
addStorage
(
"
revision
"
,
function
(
spec
)
{
var
that
=
this
,
priv
=
{};
...
...
@@ -225,7 +230,8 @@
delete
doc
.
_rev
;
delete
doc
.
_revs
;
delete
doc
.
_revs_info
;
string
=
JSON
.
stringify
(
doc
)
+
JSON
.
stringify
(
revision_history
)
+
string
=
tool
.
uniqueJSONStringify
(
doc
)
+
tool
.
uniqueJSONStringify
(
revision_history
)
+
JSON
.
stringify
(
deleted_flag
?
true
:
false
);
revision_history
.
start
+=
1
;
revision_history
.
ids
.
unshift
(
priv
.
hashCode
(
string
));
...
...
@@ -389,6 +395,7 @@
delete
doc
.
_attachment
;
delete
doc
.
_data
;
delete
doc
.
_mimetype
;
delete
doc
.
_content_type
;
delete
doc
.
_rev
;
delete
doc
.
_revs
;
delete
doc
.
_revs_info
;
...
...
@@ -428,7 +435,7 @@
result_list
.
push
({
"
_attachment
"
:
attachment_id
,
"
_data
"
:
response
.
data
,
"
_
mime
type
"
:
attachment_meta
.
content_type
"
_
content_
type
"
:
attachment_meta
.
content_type
});
if
(
count
===
0
)
{
state
=
"
finished
"
;
...
...
@@ -625,7 +632,7 @@
"
missing
"
),
undefined
);
}
res_doc
=
{
"
data
"
:{}};
res_doc
=
{
"
data
"
:
{}};
}
else
{
err
.
message
=
"
Cannot get document
"
;
return
onEnd
(
err
,
undefined
);
...
...
@@ -821,6 +828,10 @@
that
.
putAttachment
=
function
(
command
,
param
,
option
)
{
tool
.
readBlobAsBinaryString
(
param
.
_blob
).
then
(
function
(
event
)
{
param
.
_content_type
=
param
.
_blob
.
type
;
param
.
_data
=
event
.
target
.
result
;
delete
param
.
_blob
;
priv
.
revisionGenericRequest
(
command
,
param
,
...
...
@@ -830,18 +841,21 @@
"
attachment_id
"
:
param
.
_attachment
,
"
add_to_attachment_list
"
:
{
"
_attachment
"
:
param
.
_attachment
,
"
_mimetype
"
:
param
.
_blob
.
type
,
"
_data
"
:
param
.
_blob
"
_content_type
"
:
param
.
_content_
type
,
"
_data
"
:
param
.
_data
},
"
putAttachment
"
:
true
},
function
(
err
)
{
function
(
err
,
response
)
{
if
(
err
)
{
return
command
.
error
(
err
);
}
command
.
success
(
);
command
.
success
({
"
rev
"
:
response
.
rev
}
);
}
);
},
function
()
{
command
.
error
(
"
conflict
"
,
"
broken blob
"
,
"
Cannot read data to put
"
);
});
};
that
.
remove
=
function
(
command
,
param
,
option
)
{
...
...
test/jio.storage/revisionstorage.tests.js
View file @
3ad7eaba
...
...
@@ -22,13 +22,20 @@
//////////////////////////////////////////////////////////////////////////////
// Tools
var
tool
=
{
"
deepClone
"
:
jIO
.
util
.
deepClone
,
"
uniqueJSONStringify
"
:
jIO
.
util
.
uniqueJSONStringify
,
"
readBlobAsBinaryString
"
:
jIO
.
util
.
readBlobAsBinaryString
};
function
generateRevisionHash
(
doc
,
revisions
,
deleted_flag
)
{
var
string
;
doc
=
jIO
.
uti
l
.
deepClone
(
doc
);
doc
=
too
l
.
deepClone
(
doc
);
delete
doc
.
_rev
;
delete
doc
.
_revs
;
delete
doc
.
_revs_info
;
string
=
JSON
.
stringify
(
doc
)
+
JSON
.
stringify
(
revisions
)
+
string
=
tool
.
uniqueJSONStringify
(
doc
)
+
tool
.
uniqueJSONStringify
(
revisions
)
+
JSON
.
stringify
(
deleted_flag
?
true
:
false
);
return
sha256
.
hex_sha256
(
string
);
}
...
...
@@ -71,7 +78,6 @@
"
type
"
:
"
local
"
,
"
username
"
:
"
revision post
"
,
"
mode
"
:
"
memory
"
//"mode": "localStorage"
};
jio
=
jIO
.
createJIO
({
...
...
@@ -144,8 +150,7 @@
"
id
"
:
"
post1
"
,
"
method
"
:
"
post
"
,
"
result
"
:
"
success
"
,
"
rev
"
:
"
1-eedaf5235af91978a06d0b0e68b1c16adbc539
"
+
"
5bb9bd4a0cfd632fe03f9d8ef3
"
,
"
rev
"
:
shared
.
rev
,
"
status
"
:
201
,
"
statusText
"
:
"
Created
"
},
"
Post
"
);
...
...
@@ -266,7 +271,7 @@
"
_id
"
:
"
post1.
"
+
shared
.
rev
,
"
_attachment
"
:
"
attachment_test
"
,
"
_data
"
:
"
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"
,
"
_
mime
type
"
:
"
oh/yeah
"
"
_
content_
type
"
:
"
oh/yeah
"
});
}).
then
(
function
()
{
...
...
@@ -299,7 +304,7 @@
}).
then
(
function
(
answer
)
{
return
jIO
.
uti
l
.
readBlobAsBinaryString
(
answer
.
data
);
return
too
l
.
readBlobAsBinaryString
(
answer
.
data
);
}).
then
(
function
(
event
)
{
...
...
@@ -402,7 +407,6 @@
"
type
"
:
"
local
"
,
"
username
"
:
"
revision put
"
,
"
mode
"
:
"
memory
"
//"mode": "localStorage"
};
jio
=
jIO
.
createJIO
({
...
...
@@ -634,12 +638,12 @@
"
_id
"
:
"
put1.3-rh3
"
,
"
_attachment
"
:
"
att1
"
,
"
_data
"
:
"
a
"
,
"
_
mime
type
"
:
"
text/plain
"
"
_
content_
type
"
:
"
text/plain
"
}),
jio_local
.
putAttachment
({
"
_id
"
:
"
put1.3-rh3
"
,
"
_attachment
"
:
"
att2
"
,
"
_data
"
:
"
bc
"
,
"
_
mime
type
"
:
"
dont/care
"
"
_
content_
type
"
:
"
dont/care
"
})]);
}).
then
(
function
()
{
...
...
@@ -687,8 +691,8 @@
deepEqual
(
answers
[
1
].
data
.
type
,
"
dont/care
"
,
"
Check attachment 2 type
"
);
return
RSVP
.
all
([
jIO
.
uti
l
.
readBlobAsBinaryString
(
answers
[
0
].
data
),
jIO
.
uti
l
.
readBlobAsBinaryString
(
answers
[
1
].
data
)
too
l
.
readBlobAsBinaryString
(
answers
[
0
].
data
),
too
l
.
readBlobAsBinaryString
(
answers
[
1
].
data
)
]);
}).
then
(
function
(
answers
)
{
...
...
@@ -714,174 +718,236 @@
});
//
test("Put Attachment", function () {
test
(
"
Put Attachment
"
,
function
()
{
// var o = generateTools()
;
var
shared
=
{},
jio
,
jio_local
;
// o.jio = jIO.newJio({
// "type": "revision",
// "sub_storage": {
// "type": "local",
// "username": "urevputattmt",
// "application_name": "arevputattmt"
// }
// });
shared
.
workspace
=
{};
shared
.
local_storage_description
=
{
"
type
"
:
"
local
"
,
"
username
"
:
"
revision putAttachment
"
,
"
mode
"
:
"
memory
"
};
// // putAttachment without doc id
// // error 20 -> document id required
// o.spy(o, "status", 20, "PutAttachment without doc id" +
// " -> 20 document id required");
// o.jio.putAttachment({}, o.f);
// o.tick(o);
jio
=
jIO
.
createJIO
({
"
type
"
:
"
revision
"
,
"
sub_storage
"
:
shared
.
local_storage_description
},
{
"
workspace
"
:
shared
.
workspace
});
// // putAttachment without attachment id
// // erorr 22 -> attachment id required
// o.spy(o, "status", 22, "PutAttachment without attachment id" +
// " -> 22 attachment id required");
// o.jio.putAttachment({"_id": "putattmt1"}, o.f);
// o.tick(o);
jio_local
=
jIO
.
createJIO
(
shared
.
local_storage_description
,
{
"
workspace
"
:
shared
.
workspace
});
// // putAttachment without document
// o.revisions = {"start": 0, "ids": []};
// o.rev_hash = generateRevisionHash({"_id": "doc1", "_attachment": "attmt1"},
// o.revisions);
// o.rev = "1-" + o.rev_hash;
// o.spy(o, "value",
// {"ok": true, "id": "doc1", "attachment": "attmt1", "rev": o.rev},
// "PutAttachment without document, without data");
// o.jio.putAttachment({"_id": "doc1", "_attachment": "attmt1"}, o.f);
// o.tick(o);
stop
();
// // check document
// deepEqual(
// util.jsonlocalstorage.getItem(
// "jio/localstorage/urevputattmt/arevputattmt/doc1." + o.rev
// ),
// {
// "_id": "doc1." + o.rev,
// "_attachments": {
// "attmt1": {
// "length": 0,
// // md5("")
// "digest": "md5-d41d8cd98f00b204e9800998ecf8427e"
// }
// }
// },
// "Check document"
// );
// putAttachment without document
shared
.
revisions
=
{
"
start
"
:
0
,
"
ids
"
:
[]};
shared
.
rev_hash
=
generateRevisionHash
({
"
_id
"
:
"
doc1
"
,
"
_attachment
"
:
"
attmt1
"
,
"
_data
"
:
""
,
"
_content_type
"
:
""
},
shared
.
revisions
);
shared
.
rev
=
"
1-
"
+
shared
.
rev_hash
;
jio
.
putAttachment
({
"
_id
"
:
"
doc1
"
,
"
_attachment
"
:
"
attmt1
"
,
"
_data
"
:
""
}).
then
(
function
(
answer
)
{
// // check attachment
// deepEqual(util.jsonlocalstorage.getItem(
// "jio/localstorage/urevputattmt/arevputattmt/doc1." + o.rev
// + "/attmt1"
// ), "", "Check attachment");
// // adding a metadata to the document
// o.doc = util.jsonlocalstorage.getItem(
// "jio/localstorage/urevputattmt/arevputattmt/doc1." + o.rev
// );
// o.doc.title = "My Title";
// util.jsonlocalstorage.setItem(
// "jio/localstorage/urevputattmt/arevputattmt/doc1." + o.rev,
// o.doc
// );
deepEqual
(
answer
,
{
"
attachment
"
:
"
attmt1
"
,
"
id
"
:
"
doc1
"
,
"
method
"
:
"
putAttachment
"
,
"
result
"
:
"
success
"
,
"
rev
"
:
shared
.
rev
,
"
status
"
:
204
,
"
statusText
"
:
"
No Content
"
// XXX should be 201 Created
},
"
PutAttachment without document
"
);
// // update attachment
// o.prev_rev = o.rev;
// o.revisions = {"start": 1, "ids": [o.rev_hash]};
// o.rev_hash = generateRevisionHash({
// "_id": "doc1",
// "_data": "abc",
// "_attachment": "attmt1",
// }, o.revisions);
// o.rev = "2-" + o.rev_hash;
// o.spy(o, "value",
// {"ok": true, "id": "doc1", "attachment": "attmt1", "rev": o.rev},
// "Update Attachment, with data");
// o.jio.putAttachment({
// "_id": "doc1",
// "_data": "abc",
// "_attachment": "attmt1",
// "_rev": o.prev_rev
// }, o.f);
// o.tick(o);
return
jio_local
.
get
({
"
_id
"
:
"
doc1.
"
+
shared
.
rev
});
// // check document
// deepEqual(
// util.jsonlocalstorage.getItem(
// "jio/localstorage/urevputattmt/arevputattmt/doc1." + o.rev
// ),
// {
// "_id": "doc1." + o.rev,
// "title": "My Title",
// "_attachments": {
// "attmt1": {
// "length": 3,
// // md5("abc")
// "digest": "md5-900150983cd24fb0d6963f7d28e17f72"
// }
// }
// },
// "Check document"
// );
}).
then
(
function
(
answer
)
{
// // check attachment
// deepEqual(util.jsonlocalstorage.getItem(
// "jio/localstorage/urevputattmt/arevputattmt/doc1." + o.rev +
// "/attmt1"
// ), "abc", "Check attachment");
// check document
deepEqual
(
answer
.
data
,
{
"
_id
"
:
"
doc1.
"
+
shared
.
rev
,
"
_attachments
"
:
{
"
attmt1
"
:
{
"
content_type
"
:
""
,
"
length
"
:
0
,
"
digest
"
:
"
sha256-e3b0c44298fc1c149afbf4c8996fb9242
"
+
"
7ae41e4649b934ca495991b7852b855
"
}
}
},
"
Check document
"
);
// // putAttachment new attachment
// o.prev_rev = o.rev;
// o.revisions = {"start": 2, "ids": [o.rev_hash, o.revisions.ids[0]]};
// o.rev_hash = generateRevisionHash({
// "_id": "doc1",
// "_data": "def",
// "_attachment": "attmt2",
// }, o.revisions);
// o.rev = "3-" + o.rev_hash;
// o.spy(o, "value",
// {"ok": true, "id": "doc1", "attachment": "attmt2", "rev": o.rev},
// "PutAttachment without document, without data");
// o.jio.putAttachment({
// "_id": "doc1",
// "_data": "def",
// "_attachment": "attmt2",
// "_rev": o.prev_rev
// }, o.f);
// o.tick(o);
// check attachment
return
jio_local
.
getAttachment
({
"
_id
"
:
"
doc1.
"
+
shared
.
rev
,
"
_attachment
"
:
"
attmt1
"
});
// // check document
// deepEqual(
// util.jsonlocalstorage.getItem(
// "jio/localstorage/urevputattmt/arevputattmt/doc1." + o.rev
// ),
// {
// "_id": "doc1." + o.rev,
// "title": "My Title",
// "_attachments": {
// "attmt1": {
// "length": 3,
// "digest": "md5-900150983cd24fb0d6963f7d28e17f72"
// },
// "attmt2": {
// "length": 3,
// // md5("def")
// "digest": "md5-4ed9407630eb1000c0f6b63842defa7d"
// }
// }
// },
// "Check document"
// );
}).
then
(
function
(
answer
)
{
// // check attachment
// deepEqual(util.jsonlocalstorage.getItem(
// "jio/localstorage/urevputattmt/arevputattmt/doc1." + o.rev +
// "/attmt2"
// ), "def", "Check attachment");
return
tool
.
readBlobAsBinaryString
(
answer
.
data
);
// util.closeAndcleanUpJio(o.jio);
}).
then
(
function
(
event
)
{
// });
deepEqual
(
event
.
target
.
result
,
""
,
"
Check attachment
"
);
// adding a metadata to the document
return
jio_local
.
get
({
"
_id
"
:
"
doc1.
"
+
shared
.
rev
});
}).
then
(
function
(
answer
)
{
answer
.
data
.
_id
=
"
doc1.
"
+
shared
.
rev
;
answer
.
data
.
title
=
"
My Title
"
;
return
jio_local
.
put
(
answer
.
data
);
}).
then
(
function
()
{
// update attachment
shared
.
prev_rev
=
shared
.
rev
;
shared
.
revisions
=
{
"
start
"
:
1
,
"
ids
"
:
[
shared
.
rev_hash
]};
shared
.
rev_hash
=
generateRevisionHash
({
"
_id
"
:
"
doc1
"
,
"
_data
"
:
"
abc
"
,
"
_content_type
"
:
""
,
"
_attachment
"
:
"
attmt1
"
},
shared
.
revisions
);
shared
.
rev
=
"
2-
"
+
shared
.
rev_hash
;
return
jio
.
putAttachment
({
"
_id
"
:
"
doc1
"
,
"
_data
"
:
"
abc
"
,
"
_attachment
"
:
"
attmt1
"
,
"
_rev
"
:
shared
.
prev_rev
});
}).
then
(
function
(
answer
)
{
deepEqual
(
answer
,
{
"
attachment
"
:
"
attmt1
"
,
"
id
"
:
"
doc1
"
,
"
method
"
:
"
putAttachment
"
,
"
result
"
:
"
success
"
,
"
rev
"
:
shared
.
rev
,
"
status
"
:
204
,
"
statusText
"
:
"
No Content
"
},
"
Update attachment
"
);
// check document
return
jio_local
.
get
({
"
_id
"
:
"
doc1.
"
+
shared
.
rev
});
}).
then
(
function
(
answer
)
{
deepEqual
(
answer
.
data
,
{
"
_id
"
:
"
doc1.
"
+
shared
.
rev
,
"
title
"
:
"
My Title
"
,
"
_attachments
"
:
{
"
attmt1
"
:
{
"
content_type
"
:
""
,
"
length
"
:
3
,
"
digest
"
:
"
sha256-ba7816bf8f01cfea414140de5dae2223b00361a3
"
+
"
96177a9cb410ff61f20015ad
"
}
}
},
"
Check document
"
);
// check attachment
return
jio_local
.
getAttachment
({
"
_id
"
:
"
doc1.
"
+
shared
.
rev
,
"
_attachment
"
:
"
attmt1
"
});
}).
then
(
function
(
answer
)
{
return
tool
.
readBlobAsBinaryString
(
answer
.
data
);
}).
then
(
function
(
event
)
{
deepEqual
(
event
.
target
.
result
,
"
abc
"
,
"
Check attachment
"
);
// putAttachment new attachment
shared
.
prev_rev
=
shared
.
rev
;
shared
.
revisions
=
{
"
start
"
:
2
,
"
ids
"
:
[
shared
.
rev_hash
,
shared
.
revisions
.
ids
[
0
]]
};
shared
.
rev_hash
=
generateRevisionHash
({
"
_id
"
:
"
doc1
"
,
"
_data
"
:
"
def
"
,
"
_attachment
"
:
"
attmt2
"
,
"
_content_type
"
:
""
},
shared
.
revisions
);
shared
.
rev
=
"
3-
"
+
shared
.
rev_hash
;
return
jio
.
putAttachment
({
"
_id
"
:
"
doc1
"
,
"
_data
"
:
"
def
"
,
"
_attachment
"
:
"
attmt2
"
,
"
_rev
"
:
shared
.
prev_rev
});
}).
then
(
function
(
answer
)
{
deepEqual
(
answer
,
{
"
attachment
"
:
"
attmt2
"
,
"
id
"
:
"
doc1
"
,
"
method
"
:
"
putAttachment
"
,
"
result
"
:
"
success
"
,
"
rev
"
:
shared
.
rev
,
"
status
"
:
204
,
"
statusText
"
:
"
No Content
"
// XXX should be 201 Created
},
"
PutAttachment without document
"
);
return
jio_local
.
get
({
"
_id
"
:
"
doc1.
"
+
shared
.
rev
});
}).
then
(
function
(
answer
)
{
deepEqual
(
answer
.
data
,
{
"
_id
"
:
"
doc1.
"
+
shared
.
rev
,
"
title
"
:
"
My Title
"
,
"
_attachments
"
:
{
"
attmt1
"
:
{
"
content_type
"
:
""
,
"
length
"
:
3
,
"
digest
"
:
"
sha256-ba7816bf8f01cfea414140de5dae2223b00361a3
"
+
"
96177a9cb410ff61f20015ad
"
},
"
attmt2
"
:
{
"
content_type
"
:
""
,
"
length
"
:
3
,
"
digest
"
:
"
sha256-cb8379ac2098aa165029e3938a51da0bcecfc008
"
+
"
fd6795f401178647f96c5b34
"
}
}
},
"
Check document
"
);
// check attachment
return
jio_local
.
getAttachment
({
"
_id
"
:
"
doc1.
"
+
shared
.
rev
,
"
_attachment
"
:
"
attmt2
"
});
}).
then
(
function
(
answer
)
{
return
tool
.
readBlobAsBinaryString
(
answer
.
data
);
}).
then
(
function
(
event
)
{
deepEqual
(
event
.
target
.
result
,
"
def
"
,
"
Check attachment
"
);
}).
fail
(
unexpectedError
).
always
(
start
);
});
// test("Get", function () {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment