Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
jio_mebibou
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Alexandra Rogova
jio_mebibou
Commits
96d5d045
Commit
96d5d045
authored
Feb 16, 2015
by
Romain Courteaud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add test for LocalStorage
parent
073ebc39
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
247 additions
and
700 deletions
+247
-700
src/jio.storage/localstorage.js
src/jio.storage/localstorage.js
+11
-20
test/jio.storage/localstorage.tests.js
test/jio.storage/localstorage.tests.js
+234
-678
test/tests.html
test/tests.html
+2
-2
No files found.
src/jio.storage/localstorage.js
View file @
96d5d045
...
@@ -5,7 +5,7 @@
...
@@ -5,7 +5,7 @@
*/
*/
/*jslint nomen: true */
/*jslint nomen: true */
/*global jIO, localStorage,
window, Blob, Uint8Array
, RSVP */
/*global jIO, localStorage,
Blob
, RSVP */
/**
/**
* JIO Local Storage. Type = 'local'.
* JIO Local Storage. Type = 'local'.
...
@@ -20,7 +20,7 @@
...
@@ -20,7 +20,7 @@
* @class LocalStorage
* @class LocalStorage
*/
*/
(
function
(
jIO
)
{
(
function
(
jIO
,
localStorage
,
Blob
,
RSVP
)
{
"
use strict
"
;
"
use strict
"
;
function
LocalStorage
()
{
function
LocalStorage
()
{
...
@@ -34,47 +34,38 @@
...
@@ -34,47 +34,38 @@
}
}
}
}
/**
* Get a document
*
* @method get
* @param {Object} param The given parameters
* @param {Object} options The command options
*/
LocalStorage
.
prototype
.
get
=
function
(
param
)
{
LocalStorage
.
prototype
.
get
=
function
(
param
)
{
restrictDocumentId
(
param
.
_id
);
restrictDocumentId
(
param
.
_id
);
var
doc
=
{},
var
doc
=
{},
attachments
=
{},
attachments
=
{},
found
=
false
,
key
;
key
;
for
(
key
in
localStorage
)
{
for
(
key
in
localStorage
)
{
if
(
localStorage
.
hasOwnProperty
(
key
))
{
if
(
localStorage
.
hasOwnProperty
(
key
))
{
attachments
[
key
]
=
{};
attachments
[
key
]
=
{};
found
=
true
;
}
}
}
}
if
(
attachments
.
length
!==
0
)
{
if
(
found
)
{
doc
.
_attachments
=
attachments
;
doc
.
_attachments
=
attachments
;
}
}
return
doc
;
return
doc
;
};
};
/**
* Get an attachment
*
* @method getAttachment
* @param {Object} param The given parameters
* @param {Object} options The command options
*/
LocalStorage
.
prototype
.
getAttachment
=
function
(
param
)
{
LocalStorage
.
prototype
.
getAttachment
=
function
(
param
)
{
restrictDocumentId
(
param
.
_id
);
restrictDocumentId
(
param
.
_id
);
var
textstring
=
localStorage
.
getItem
(
param
.
_attachment
);
var
textstring
=
localStorage
.
getItem
(
param
.
_attachment
);
if
(
textstring
===
null
)
{
if
(
textstring
===
null
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find attachment
"
,
404
);
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find attachment
"
+
param
.
_attachment
,
404
);
}
}
return
new
Blob
([
textstring
])
;
return
{
data
:
new
Blob
([
textstring
])}
;
};
};
LocalStorage
.
prototype
.
putAttachment
=
function
(
param
)
{
LocalStorage
.
prototype
.
putAttachment
=
function
(
param
)
{
...
@@ -110,4 +101,4 @@
...
@@ -110,4 +101,4 @@
jIO
.
addStorage
(
'
local
'
,
LocalStorage
);
jIO
.
addStorage
(
'
local
'
,
LocalStorage
);
}(
jIO
));
}(
jIO
,
localStorage
,
Blob
,
RSVP
));
test/jio.storage/localstorage.tests.js
View file @
96d5d045
/*jslint
maxlen: 120,
nomen: true */
/*jslint nomen: true */
/*global localStorage,
test_util, console, Blob
*/
/*global localStorage,
Blob, document
*/
(
function
(
jIO
,
localStorage
,
QUnit
)
{
(
function
(
jIO
,
localStorage
,
QUnit
,
Blob
,
document
)
{
"
use strict
"
;
"
use strict
"
;
var
test
=
QUnit
.
test
,
var
test
=
QUnit
.
test
,
stop
=
QUnit
.
stop
,
stop
=
QUnit
.
stop
,
...
@@ -11,7 +11,10 @@
...
@@ -11,7 +11,10 @@
equal
=
QUnit
.
equal
,
equal
=
QUnit
.
equal
,
module
=
QUnit
.
module
;
module
=
QUnit
.
module
;
module
(
"
localStorage
"
,
{
/////////////////////////////////////////////////////////////////
// localStorage.get
/////////////////////////////////////////////////////////////////
module
(
"
localStorage.get
"
,
{
setup
:
function
()
{
setup
:
function
()
{
localStorage
.
clear
();
localStorage
.
clear
();
this
.
jio
=
jIO
.
createJIO
({
this
.
jio
=
jIO
.
createJIO
({
...
@@ -20,21 +23,17 @@
...
@@ -20,21 +23,17 @@
}
}
});
});
/////////////////////////////////////////////////////////////////
test
(
"
get non valid document ID
"
,
function
()
{
// localStorage.get
/////////////////////////////////////////////////////////////////
test
(
"
get inexistent document
"
,
function
()
{
stop
();
stop
();
expect
(
3
);
expect
(
3
);
this
.
jio
.
get
({
"
_id
"
:
"
inexistent
"
})
this
.
jio
.
get
({
"
_id
"
:
"
inexistent
"
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
error
instanceof
jIO
.
util
.
jIOError
);
ok
(
error
instanceof
jIO
.
util
.
jIOError
);
equal
(
error
.
message
,
"
Cannot find document
"
);
equal
(
error
.
message
,
"
id inexistent is forbidden (!== /)
"
);
equal
(
error
.
status_code
,
40
4
);
equal
(
error
.
status_code
,
40
0
);
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
console
.
error
(
error
);
ok
(
false
,
error
);
ok
(
false
,
error
);
})
})
.
always
(
function
()
{
.
always
(
function
()
{
...
@@ -42,18 +41,15 @@
...
@@ -42,18 +41,15 @@
});
});
});
});
test
(
"
get document
"
,
function
()
{
test
(
"
get document without attachment
"
,
function
()
{
var
id
=
"
post1
"
;
var
id
=
"
/
"
;
localStorage
[
id
]
=
JSON
.
stringify
({
title
:
"
myPost1
"
});
stop
();
stop
();
expect
(
1
);
expect
(
1
);
this
.
jio
.
get
({
"
_id
"
:
id
})
this
.
jio
.
get
({
"
_id
"
:
id
})
.
then
(
function
(
result
)
{
.
then
(
function
(
result
)
{
deepEqual
(
result
,
{
deepEqual
(
result
,
{
"
title
"
:
"
myPost1
"
"
_id
"
:
"
/
"
},
"
Check document
"
);
},
"
Check document
"
);
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
...
@@ -65,33 +61,19 @@
...
@@ -65,33 +61,19 @@
});
});
test
(
"
get document with attachment
"
,
function
()
{
test
(
"
get document with attachment
"
,
function
()
{
var
id
=
"
putattmt1
"
;
var
id
=
"
/
"
,
attachment
=
"
foo
"
;
stop
();
stop
();
expect
(
1
);
expect
(
1
);
localStorage
[
id
]
=
JSON
.
stringify
({
localStorage
[
attachment
]
=
"
bar
"
;
"
_id
"
:
id
,
"
_attachments
"
:
{
"
putattmt2
"
:
{
content_type
:
""
,
digest
:
"
sha256-e3b0c44298fc1c149afbf4c8996fb9242
"
+
"
7ae41e4649b934ca495991b7852b855
"
,
length
:
0
}
}
});
this
.
jio
.
get
({
"
_id
"
:
id
})
this
.
jio
.
get
({
"
_id
"
:
id
})
.
then
(
function
(
result
)
{
.
then
(
function
(
result
)
{
deepEqual
(
result
,
{
deepEqual
(
result
,
{
"
_id
"
:
id
,
"
_id
"
:
id
,
"
_attachments
"
:
{
"
_attachments
"
:
{
"
putattmt2
"
:
{
"
foo
"
:
{}
content_type
:
""
,
digest
:
"
sha256-e3b0c44298fc1c149afbf4c8996fb9242
"
+
"
7ae41e4649b934ca495991b7852b855
"
,
length
:
0
}
}
}
},
"
Check document
"
);
},
"
Check document
"
);
})
})
...
@@ -104,39 +86,29 @@
...
@@ -104,39 +86,29 @@
});
});
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
// localStorage.
pos
t
// localStorage.
getAttachmen
t
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
test
(
"
post without id
"
,
function
()
{
module
(
"
localStorage.getAttachment
"
,
{
expect
(
1
);
setup
:
function
()
{
stop
();
localStorage
.
clear
();
this
.
jio
=
jIO
.
createJIO
({
this
.
jio
.
post
({})
"
type
"
:
"
local
"
.
then
(
function
(
uuid
)
{
// ok(util.isUuid(uuid), "Uuid should look like " +
// "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx : " + uuid);
equal
(
localStorage
[
uuid
],
JSON
.
stringify
({
"
_id
"
:
uuid
}));
})
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
})
.
always
(
function
()
{
start
();
});
});
}
});
});
test
(
"
post non empty document
"
,
function
()
{
test
(
"
get attachment from inexistent document
"
,
function
()
{
expect
(
2
);
stop
();
stop
();
expect
(
3
);
this
.
jio
.
post
({
"
_id
"
:
"
post1
"
,
"
title
"
:
"
myPost1
"
})
this
.
jio
.
getAttachment
({
.
then
(
function
(
uuid
)
{
"
_id
"
:
"
inexistent
"
,
equal
(
uuid
,
"
post1
"
);
"
_attachment
"
:
"
a
"
equal
(
localStorage
.
post1
,
JSON
.
stringify
({
})
"
_id
"
:
"
post1
"
,
.
fail
(
function
(
error
)
{
"
title
"
:
"
myPost1
"
ok
(
error
instanceof
jIO
.
util
.
jIOError
);
}));
equal
(
error
.
message
,
"
id inexistent is forbidden (!== /)
"
);
equal
(
error
.
status_code
,
400
);
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
ok
(
false
,
error
);
...
@@ -146,48 +118,49 @@
...
@@ -146,48 +118,49 @@
});
});
});
});
test
(
"
post but document already exists
"
,
function
()
{
test
(
"
get inexistent attachment from document
"
,
function
()
{
var
id
=
"
post1
"
;
var
id
=
"
/
"
;
localStorage
[
id
]
=
JSON
.
stringify
({
"
_id
"
:
id
,
title
:
"
myPost1
"
});
expect
(
4
);
stop
();
stop
();
expect
(
3
);
this
.
jio
.
post
({
"
_id
"
:
"
post1
"
,
"
title
"
:
"
myPost2
"
})
this
.
jio
.
getAttachment
({
.
then
(
function
(
result
)
{
"
_id
"
:
id
,
ok
(
false
,
result
);
"
_attachment
"
:
"
inexistent
"
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
error
instanceof
jIO
.
util
.
jIOError
);
ok
(
error
instanceof
jIO
.
util
.
jIOError
);
equal
(
error
.
message
,
"
Cannot create a new document
"
);
equal
(
error
.
message
,
"
Cannot find attachment inexistent
"
);
equal
(
error
.
status_code
,
409
);
equal
(
error
.
status_code
,
404
);
})
equal
(
localStorage
.
post1
,
JSON
.
stringify
({
.
fail
(
function
(
error
)
{
"
_id
"
:
"
post1
"
,
ok
(
false
,
error
);
"
title
"
:
"
myPost1
"
}));
})
})
.
always
(
function
()
{
.
always
(
function
()
{
start
();
start
();
});
});
});
});
/////////////////////////////////////////////////////////////////
test
(
"
get string attachment from document
"
,
function
()
{
// localStorage.put
var
id
=
"
/
"
,
/////////////////////////////////////////////////////////////////
value
=
"
azertyuio
\n
pàç_è-('é&
"
,
test
(
"
put non empty document
"
,
function
()
{
attachment
=
"
stringattachment
"
;
expect
(
2
);
stop
();
stop
();
expect
(
4
);
localStorage
[
attachment
]
=
value
;
this
.
jio
.
put
({
"
_id
"
:
"
put1
"
,
"
title
"
:
"
myPut1
"
})
this
.
jio
.
getAttachment
({
.
then
(
function
(
uuid
)
{
"
_id
"
:
id
,
equal
(
uuid
,
"
put1
"
);
"
_attachment
"
:
attachment
equal
(
localStorage
.
put1
,
JSON
.
stringify
({
})
"
_id
"
:
"
put1
"
,
.
then
(
function
(
result
)
{
"
title
"
:
"
myPut1
"
ok
(
result
.
data
instanceof
Blob
,
"
Data is Blob
"
);
}));
deepEqual
(
result
.
data
.
type
,
""
,
"
Check mimetype
"
);
deepEqual
(
result
.
data
.
size
,
24
,
"
Check size
"
);
return
jIO
.
util
.
readBlobAsText
(
result
.
data
);
})
.
then
(
function
(
result
)
{
equal
(
result
.
target
.
result
,
value
,
"
Attachment correctly fetched
"
);
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
ok
(
false
,
error
);
...
@@ -197,22 +170,44 @@
...
@@ -197,22 +170,44 @@
});
});
});
});
test
(
"
put when document already exists
"
,
function
()
{
test
(
"
get binary string attachment from document
"
,
function
()
{
var
id
=
"
put1
"
;
var
id
=
"
/
"
,
localStorage
[
id
]
=
JSON
.
stringify
({
context
=
this
,
"
_id
"
:
id
,
imgCanvas
=
document
.
createElement
(
"
canvas
"
),
title
:
"
myPut1
"
imgContext
=
imgCanvas
.
getContext
(
"
2d
"
),
});
data_url
,
expect
(
2
);
value
,
attachment
=
"
stringattachment
"
;
stop
();
stop
();
expect
(
2
);
imgCanvas
.
width
=
200
;
imgCanvas
.
height
=
200
;
imgContext
.
fillStyle
=
"
blue
"
;
imgContext
.
font
=
"
bold 16px Arial
"
;
imgContext
.
fillText
(
"
Zibri
"
,
100
,
100
);
this
.
jio
.
put
({
"
_id
"
:
id
,
"
title
"
:
"
myPut2
"
})
data_url
=
imgCanvas
.
toDataURL
(
"
image/png
"
);
.
then
(
function
(
uuid
)
{
equal
(
uuid
,
"
put1
"
);
return
jIO
.
util
.
ajax
({
equal
(
localStorage
.
put1
,
JSON
.
stringify
({
url
:
data_url
"
_id
"
:
"
put1
"
,
})
"
title
"
:
"
myPut2
"
.
then
(
function
(
result
)
{
}));
value
=
result
.
target
.
response
;
localStorage
[
attachment
]
=
value
;
return
context
.
jio
.
getAttachment
({
"
_id
"
:
id
,
"
_attachment
"
:
attachment
});
})
.
then
(
function
(
result
)
{
ok
(
result
.
data
instanceof
Blob
,
"
Data is Blob
"
);
return
jIO
.
util
.
readBlobAsText
(
result
.
data
);
})
.
then
(
function
(
result
)
{
equal
(
result
.
target
.
result
,
value
,
"
Attachment correctly fetched
"
);
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
ok
(
false
,
error
);
...
@@ -223,20 +218,30 @@
...
@@ -223,20 +218,30 @@
});
});
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
// localStorage.
ge
tAttachment
// localStorage.
pu
tAttachment
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
test
(
"
get attachment from inexistent document
"
,
function
()
{
module
(
"
localStorage.putAttachment
"
,
{
setup
:
function
()
{
localStorage
.
clear
();
this
.
jio
=
jIO
.
createJIO
({
"
type
"
:
"
local
"
});
}
});
test
(
"
put an attachment to an inexistent document
"
,
function
()
{
stop
();
stop
();
expect
(
3
);
expect
(
3
);
this
.
jio
.
ge
tAttachment
({
this
.
jio
.
pu
tAttachment
({
"
_id
"
:
"
inexistent
"
,
"
_id
"
:
"
inexistent
"
,
"
_attachment
"
:
"
a
"
"
_attachment
"
:
"
putattmt2
"
,
"
_data
"
:
""
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
error
instanceof
jIO
.
util
.
jIOError
);
ok
(
error
instanceof
jIO
.
util
.
jIOError
);
equal
(
error
.
message
,
"
Cannot find document
"
);
equal
(
error
.
message
,
"
id inexistent is forbidden (!== /)
"
);
equal
(
error
.
status_code
,
40
4
);
equal
(
error
.
status_code
,
40
0
);
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
ok
(
false
,
error
);
...
@@ -246,23 +251,21 @@
...
@@ -246,23 +251,21 @@
});
});
});
});
test
(
"
get inexistent attachment from document
"
,
function
()
{
test
(
"
put string attachment from document
"
,
function
()
{
var
id
=
"
b
"
;
var
id
=
"
/
"
,
value
=
"
azertyuio
\n
pàç_è-('é&
"
,
attachment
=
"
stringattachment
"
;
stop
();
stop
();
expect
(
3
);
expect
(
1
);
localStorage
[
id
]
=
JSON
.
stringify
({
"
_id
"
:
id
});
this
.
jio
.
ge
tAttachment
({
this
.
jio
.
pu
tAttachment
({
"
_id
"
:
id
,
"
_id
"
:
id
,
"
_attachment
"
:
"
inexistent
"
"
_attachment
"
:
attachment
,
"
_data
"
:
value
})
})
.
fail
(
function
(
error
)
{
.
then
(
function
()
{
ok
(
error
instanceof
jIO
.
util
.
jIOError
);
equal
(
localStorage
[
attachment
],
value
);
equal
(
error
.
message
,
"
Cannot find attachment
"
);
equal
(
error
.
status_code
,
404
);
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
ok
(
false
,
error
);
...
@@ -272,39 +275,41 @@
...
@@ -272,39 +275,41 @@
});
});
});
});
test
(
"
get attachment from document
"
,
function
()
{
test
(
"
put binary string attachment from document
"
,
function
()
{
var
id
=
"
putattmt1
"
,
var
id
=
"
/
"
,
attachment
=
"
putattmt2
"
;
context
=
this
,
imgCanvas
=
document
.
createElement
(
"
canvas
"
),
imgContext
=
imgCanvas
.
getContext
(
"
2d
"
),
data_url
,
original_blob
,
value
,
attachment
=
"
stringattachment
"
;
stop
();
stop
();
expect
(
4
);
expect
(
1
);
localStorage
[
id
]
=
JSON
.
stringify
({
imgCanvas
.
width
=
200
;
"
_id
"
:
id
,
imgCanvas
.
height
=
200
;
"
_attachments
"
:
{
"
putattmt2
"
:
{
content_type
:
""
,
digest
:
"
sha256-e3b0c44298fc1c149afbf4c8996fb9242
"
+
"
7ae41e4649b934ca495991b7852b855
"
,
length
:
0
}
}
});
localStorage
[
id
+
'
/
'
+
attachment
]
=
JSON
.
stringify
(
""
);
this
.
jio
.
getAttachment
({
imgContext
.
fillStyle
=
"
blue
"
;
"
_id
"
:
id
,
imgContext
.
font
=
"
bold 16px Arial
"
;
"
_attachment
"
:
attachment
imgContext
.
fillText
(
"
Zibri
"
,
100
,
100
);
data_url
=
imgCanvas
.
toDataURL
(
"
image/png
"
);
return
jIO
.
util
.
ajax
({
url
:
data_url
})
})
.
then
(
function
(
result
)
{
.
then
(
function
(
result
)
{
ok
(
result
.
data
instanceof
Blob
,
"
Data is Blob
"
);
value
=
result
.
target
.
response
;
deepEqual
(
result
.
data
.
type
,
""
,
"
Check mimetype
"
);
original_blob
=
new
Blob
([
result
.
target
.
response
],
deepEqual
(
result
.
data
.
size
,
0
,
"
Check size
"
);
{
type
:
"
image/png
"
});
return
context
.
jio
.
putAttachment
({
delete
result
.
data
;
"
_id
"
:
id
,
deepEqual
(
result
,
{
"
_attachment
"
:
attachment
,
"
digest
"
:
"
sha256-e3b0c44298fc1c149afbf4c8996fb9242
"
+
"
_blob
"
:
original_blob
"
7ae41e4649b934ca495991b7852b855
"
});
},
"
Get Attachment, Check Response
"
);
})
.
then
(
function
()
{
equal
(
localStorage
[
attachment
],
value
);
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
ok
(
false
,
error
);
...
@@ -314,23 +319,29 @@
...
@@ -314,23 +319,29 @@
});
});
});
});
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
// localStorage.
put
Attachment
// localStorage.
remove
Attachment
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
test
(
"
put an attachment to an inexistent document
"
,
function
()
{
module
(
"
localStorage.removeAttachment
"
,
{
setup
:
function
()
{
this
.
jio
=
jIO
.
createJIO
({
"
type
"
:
"
local
"
});
}
});
test
(
"
remove an attachment to an inexistent document
"
,
function
()
{
stop
();
stop
();
expect
(
3
);
expect
(
3
);
this
.
jio
.
put
Attachment
({
this
.
jio
.
remove
Attachment
({
"
_id
"
:
"
inexistent
"
,
"
_id
"
:
"
inexistent
"
,
"
_attachment
"
:
"
putattmt2
"
,
"
_attachment
"
:
"
removeattmt2
"
"
_data
"
:
""
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
error
instanceof
jIO
.
util
.
jIOError
);
ok
(
error
instanceof
jIO
.
util
.
jIOError
,
error
);
equal
(
error
.
message
,
"
Cannot find document
"
);
equal
(
error
.
message
,
"
id inexistent is forbidden (!== /)
"
);
equal
(
error
.
status_code
,
40
4
);
equal
(
error
.
status_code
,
40
0
);
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
ok
(
false
,
error
);
...
@@ -340,38 +351,74 @@
...
@@ -340,38 +351,74 @@
});
});
});
});
test
(
"
put an attachment to a document
"
,
function
()
{
test
(
"
remove an attachment to a document
"
,
function
()
{
var
id
=
"
putattmt1
"
;
var
id
=
"
/
"
,
localStorage
[
id
]
=
JSON
.
stringify
({
attachment
=
"
foo
"
;
"
_id
"
:
id
,
"
title
"
:
"
myPutAttmt1
"
localStorage
[
attachment
]
=
"
bar
"
;
});
stop
();
stop
();
expect
(
3
);
expect
(
1
);
this
.
jio
.
put
Attachment
({
this
.
jio
.
remove
Attachment
({
"
_id
"
:
id
,
"
_id
"
:
id
,
"
_attachment
"
:
"
putattmt2
"
,
"
_attachment
"
:
attachment
"
_data
"
:
""
})
})
.
then
(
function
()
{
ok
(
!
localStorage
.
hasOwnProperty
(
attachment
));
})
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
})
.
always
(
function
()
{
start
();
});
});
/////////////////////////////////////////////////////////////////
// localStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module
(
"
localStorage.hasCapacity
"
,
{
setup
:
function
()
{
this
.
jio
=
jIO
.
createJIO
({
"
type
"
:
"
local
"
});
}
});
test
(
"
can list documents
"
,
function
()
{
ok
(
this
.
jio
.
hasCapacity
(
"
list
"
));
});
/////////////////////////////////////////////////////////////////
// localStorage.buildQuery
/////////////////////////////////////////////////////////////////
module
(
"
localStorage.buildQuery
"
,
{
setup
:
function
()
{
this
.
jio
=
jIO
.
createJIO
({
"
type
"
:
"
local
"
});
}
});
test
(
"
only return one document
"
,
function
()
{
stop
();
expect
(
1
);
this
.
jio
.
allDocs
()
.
then
(
function
(
result
)
{
.
then
(
function
(
result
)
{
deepEqual
(
result
,
{
"
digest
"
:
"
sha256-e3b0c44298fc1c149afbf4c8996fb9242
"
+
deepEqual
(
result
,
{
"
7ae41e4649b934ca495991b7852b855
"
"
data
"
:
{
});
"
rows
"
:
[
equal
(
localStorage
[
id
],
JSON
.
stringify
({
{
"
_id
"
:
id
,
"
id
"
:
"
/
"
,
"
title
"
:
"
myPutAttmt1
"
,
"
value
"
:
{}
"
_attachments
"
:
{
}
"
putattmt2
"
:
{
],
content_type
:
""
,
"
total_rows
"
:
1
digest
:
"
sha256-e3b0c44298fc1c149afbf4c8996fb9242
"
+
"
7ae41e4649b934ca495991b7852b855
"
,
length
:
0
}
}
}
}));
});
equal
(
localStorage
[
id
+
'
/putattmt2
'
],
JSON
.
stringify
(
""
));
})
})
.
fail
(
function
(
error
)
{
.
fail
(
function
(
error
)
{
ok
(
false
,
error
);
ok
(
false
,
error
);
...
@@ -382,495 +429,4 @@
...
@@ -382,495 +429,4 @@
});
});
});
});
// test("Remove & RemoveAttachment", function () {
}(
jIO
,
localStorage
,
QUnit
,
Blob
,
document
));
// expect(4);
// var jio = jIO.createJIO({
// "type": "local",
// "username": "uremove",
// "application_name": "aremove"
// }, {
// "workspace": {}
// });
//
// stop();
//
// jio.put({"_id": "a"}).then(function () {
//
// return jio.putAttachment({"_id": "a", "_attachment": "b", "_data": "c"});
//
// }).then(function () {
//
// return jio.removeAttachment({"_id": "a", "_attachment": "b"});
//
// }).always(function (answer) {
//
// deepEqual(answer, {
// "attachment": "b",
// "id": "a",
// "method": "removeAttachment",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// }, "Remove existent attachment");
//
// }).then(function () {
//
// // Promise.all always return success
// return RSPV.all([jio.removeAttachment({
// "_id": "a",
// "_attachment": "b"
// })]);
//
// }).always(function (answers) {
//
// deepEqual(answers[0], {
// "attachment": "b",
// "error": "not_found",
// "id": "a",
// "message": "Attachment not found",
// "method": "removeAttachment",
// "reason": "missing attachment",
// "result": "error",
// "status": 404,
// "statusText": "Not Found"
// }, "Remove removed attachment");
//
// }).then(function () {
//
// return jio.remove({"_id": "a"});
//
// }).always(function (answer) {
//
// deepEqual(answer, {
// "id": "a",
// "method": "remove",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// }, "Remove existent document");
//
// }).then(function () {
//
// return jio.remove({"_id": "a"});
//
// }).always(function (answer) {
//
// deepEqual(answer, {
// "error": "not_found",
// "id": "a",
// "message": "Document not found",
// "method": "remove",
// "reason": "missing",
// "result": "error",
// "status": 404,
// "statusText": "Not Found"
// }, "Remove removed document");
//
// }).always(start);
//
// });
//
// test("AllDocs", function () {
// expect(3);
// var o = {}, jio = jIO.createJIO({
// "type": "local",
// "username": "ualldocs",
// "application_name": "aalldocs"
// }, {
// "workspace": {}
// });
//
// stop();
//
// o.date_a = new Date(0);
// o.date_b = new Date();
//
// // put some document before list them
// RSVP.all([
// jio.put({
// "_id": "a",
// "title": "one",
// "date": o.date_a
// }).then(function () {
// return jio.putAttachment({
// "_id": "a",
// "_attachment": "aa",
// "_data": "aaa"
// });
// }),
// jio.put({"_id": "b", "title": "two", "date": o.date_a}),
// jio.put({"_id": "c", "title": "one", "date": o.date_b}),
// jio.put({"_id": "d", "title": "two", "date": o.date_b})
// ]).then(function () {
//
// // get a list of documents
// return jio.allDocs();
//
// }).always(function (answer) {
//
// // sort answer rows for comparison
// if (answer.data && answer.data.rows) {
// answer.data.rows.sort(function (a, b) {
// return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
// });
// }
//
// deepEqual(answer, {
// "data": {
// "rows": [{
// "id": "a",
// "key": "a",
// "value": {}
// }, {
// "id": "b",
// "key": "b",
// "value": {}
// }, {
// "id": "c",
// "key": "c",
// "value": {}
// }, {
// "id": "d",
// "key": "d",
// "value": {}
// }],
// "total_rows": 4
// },
// "method": "allDocs",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// }, "AllDocs");
//
// }).then(function () {
//
// // get a list of documents
// return jio.allDocs({
// "include_docs": true,
// "sort_on": [['title', 'ascending'], ['date', 'descending']],
// "select_list": ['title', 'date'],
// "limit": [1] // ==> equal [1, 3] in this case
// });
//
// }).always(function (answer) {
//
// deepEqual(answer, {
// "data": {
// "rows": [{
// "doc": {
// "_attachments": {
// "aa": {
// "content_type": "",
// "digest": "sha256-9834876dcfb05cb167a5c24953eba58c4" +
// "ac89b1adf57f28f2f9d09af107ee8f0",
// "length": 3
// }
// },
// "_id": "a",
// "date": o.date_a.toJSON(),
// "title": "one"
// },
// "id": "a",
// "key": "a",
// "value": {
// "date": o.date_a.toJSON(),
// "title": "one"
// }
// }, {
// "doc": {
// "_id": "d",
// "date": o.date_b.toJSON(),
// "title": "two"
// },
// "id": "d",
// "key": "d",
// "value": {
// "date": o.date_b.toJSON(),
// "title": "two"
// }
// }, {
// "doc": {
// "_id": "b",
// "date": o.date_a.toJSON(),
// "title": "two"
// },
// "id": "b",
// "key": "b",
// "value": {
// "date": o.date_a.toJSON(),
// "title": "two"
// }
// }],
// "total_rows": 3
// },
// "method": "allDocs",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// }, "AllDocs include docs + sort on + limit + select_list");
//
// }).then(function () {
//
// // use a query
// return jio.allDocs({
// "query": "title: \"two\"",
// "sort_on": [["date", "descending"]]
// });
//
// }).always(function (answer) {
//
// deepEqual(answer, {
// "data": {
// "rows": [{
// "id": "d",
// "key": "d",
// "value": {}
// }, {
// "id": "b",
// "key": "b",
// "value": {}
// }],
// "total_rows": 2
// },
// "method": "allDocs",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// }, "AllDocs sort on + query");
//
// }).always(start);
//
// });
//
// test("Check & Repair", function () {
// expect(18);
// var o = {}, jio = jIO.createJIO({
// "type": "local",
// "username": "urepair",
// "application_name": "arepair"
// }, {
// "workspace": {}
// });
//
// stop();
//
// o.putCorruptedDocuments = function () {
// // put a document with a wrong attachment reference
// util.json_localStorage.setItem(
// "jio/localstorage/urepair/arepair/war",
// {"_id": "war", "title": "b", "_attachments": {"aa": {}}}
// );
//
// // put a document with a wrong metadata
// util.json_localStorage.setItem(
// "jio/localstorage/urepair/arepair/meta",
// {"_id": "meta", "title": ["b", ["c", {}], {"blue": "blue"}]}
// );
//
// // put a corrupted document
// util.json_localStorage.setItem(
// "jio/localstorage/urepair/arepair/cor",
// "blue"
// );
// };
//
// // put an unreferenced attachment
// util.json_localStorage.setItem(
// "jio/localstorage/urepair/arepair/unref/aa",
// "attachment content"
// );
// o.putCorruptedDocuments();
//
// RSVP.all([
// jio.check({"_id": "war"}),
// jio.check({"_id": "meta"}),
// jio.check({"_id": "cor"}),
// jio.check({"_id": "inexistent"})
// ]).always(function (answers) {
//
// deepEqual(answers[0], {
// "error": "conflict",
// "id": "war",
// "message": "Attachment \"aa\" of \"war\" is missing",
// "method": "check",
// "reason": "missing attachment",
// "result": "error",
// "status": 409,
// "statusText": "Conflict"
// }, "Check a document with one missing attachment");
//
// deepEqual(answers[1], {
// "error": "conflict",
// "id": "meta",
// "message": "Some metadata might be lost",
// "method": "check",
// "reason": "corrupted",
// "result": "error",
// "status": 409,
// "statusText": "Conflict"
// }, "Check document with wrong metadata");
//
// deepEqual(answers[2], {
// "error": "conflict",
// "id": "cor",
// "message": "Document is unrecoverable",
// "method": "check",
// "reason": "corrupted",
// "result": "error",
// "status": 409,
// "statusText": "Conflict"
// }, "Check corrupted document");
//
// deepEqual(answers[3], {
// "id": "inexistent",
// "method": "check",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// }, "Check inexistent document");
//
// }).then(function () {
//
// return RSVP.all([
// jio.repair({"_id": "war"}),
// jio.repair({"_id": "meta"}),
// jio.repair({"_id": "cor"}),
// jio.repair({"_id": "inexistent"})
// ]);
//
// }).always(function (answers) {
//
// deepEqual(answers[0], {
// "id": "war",
// "method": "repair",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// }, "Repair a document with one missing attachment");
//
// deepEqual(answers[1], {
// "id": "meta",
// "method": "repair",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// }, "Repair document with wrong metadata");
//
// deepEqual(answers[2], {
// "id": "cor",
// "method": "repair",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// }, "Repair corrupted document");
//
// deepEqual(answers[3], {
// "id": "inexistent",
// "method": "repair",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// }, "Repair inexistent document");
//
// }).then(function () {
//
// o.getCorruptedDocuments = function () {
// return RSVP.all([
// jio.get({"_id": "war"}),
// jio.get({"_id": "meta"}),
// jio.get({"_id": "cor"}),
// jio.get({"_id": "inexistent"})
// ]);
// };
//
// return o.getCorruptedDocuments();
//
// }).always(function (answers) {
//
// o.testGetAnswers = function (answers) {
//
// deepEqual(answers[0], {
// "data": {
// "_id": "war",
// "title": "b"
// },
// "id": "war",
// "method": "get",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// }, "Get repaired document with one missing attachment");
//
// deepEqual(answers[1], {
// "data": {
// "_id": "meta",
// "title": "b"
// },
// "id": "meta",
// "method": "get",
// "result": "success",
// "status": 200,
// "statusText": "Ok"
// }, "Get repaired document with wrong metadata");
//
// deepEqual(answers[2], {
// "error": "not_found",
// "id": "cor",
// "message": "Cannot find document",
// "method": "get",
// "reason": "missing",
// "result": "error",
// "status": 404,
// "statusText": "Not Found"
// }, "Get repaired corrupted document");
//
// deepEqual(answers[3], {
// "error": "not_found",
// "id": "inexistent",
// "message": "Cannot find document",
// "method": "get",
// "reason": "missing",
// "result": "error",
// "status": 404,
// "statusText": "Not Found"
// }, "Get repaired inexistent document");
//
// };
//
// o.testGetAnswers(answers);
//
// }).then(function () {
//
// o.putCorruptedDocuments();
//
// return jio.repair({});
//
// }).always(function (answer) {
//
// deepEqual(answer, {
// "method": "repair",
// "result": "success",
// "status": 204,
// "statusText": "No Content"
// }, "Repair all the database");
//
// }).then(function () {
//
// return o.getCorruptedDocuments();
//
// }).always(function (answers) {
//
// o.testGetAnswers(answers);
//
// }).then(function () {
//
// // unreferenced attachment must be removed
// deepEqual(util.json_localStorage.getItem(
// "jio/localstorage/urepair/arepair/unref/aa"
// ), null, "Unreferenced attachment removed");
//
// }).always(start);
//
// });
}(
jIO
,
localStorage
,
QUnit
));
test/tests.html
View file @
96d5d045
...
@@ -27,9 +27,9 @@
...
@@ -27,9 +27,9 @@
<script
src=
"jio.storage/memorystorage.tests.js"
></script>
<script
src=
"jio.storage/memorystorage.tests.js"
></script>
<script
src=
"jio.storage/querystorage.tests.js"
></script>
<script
src=
"jio.storage/querystorage.tests.js"
></script>
<script
src=
"jio.storage/localstorage.tests.js"
></script>
<!--script src="jio.storage/localstorage.tests.js"></script>
<!--script src="jio.storage/davstorage.tests.js"></script>
<script src="jio.storage/davstorage.tests.js"></script>
<script src="jio.storage/unionstorage.tests.js"></script>
<script src="jio.storage/unionstorage.tests.js"></script>
<script src="jio.storage/querystorage.tests.js"></script-->
<script src="jio.storage/querystorage.tests.js"></script-->
...
...
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