Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 80b9887b
authored
Sep 23, 2014
by
Sveinn Fannar Kristjansson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests for nested conditions
1 parent
6a091558
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
13 deletions
test/postgres/dao.test.js
test/utils.test.js
test/postgres/dao.test.js
View file @
80b9887
...
...
@@ -4,6 +4,7 @@ var chai = require('chai')
,
dialect
=
Support
.
getTestDialect
()
,
DataTypes
=
require
(
__dirname
+
"/../../lib/data-types"
)
,
_
=
require
(
'lodash'
)
,
sequelize
=
require
(
__dirname
+
'/../../lib/sequelize'
);
chai
.
config
.
includeStack
=
true
...
...
@@ -72,25 +73,25 @@ if (dialect.match(/^postgres/)) {
it
(
'should be able retrieve json value as object'
,
function
()
{
var
self
=
this
;
var
emergency
_c
ontact
=
{
name
:
'kate'
,
phone
:
1337
};
var
emergency
C
ontact
=
{
name
:
'kate'
,
phone
:
1337
};
return
this
.
User
.
create
({
username
:
'swen'
,
emergency_contact
:
emergency
_c
ontact
})
return
this
.
User
.
create
({
username
:
'swen'
,
emergency_contact
:
emergency
C
ontact
})
.
then
(
function
(
user
)
{
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
_c
ontact
);
// .eql does deep value comparison instead of strict equal comparison
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
C
ontact
);
// .eql does deep value comparison instead of strict equal comparison
return
self
.
User
.
find
({
where
:
{
username
:
'swen'
},
attributes
:
[
'emergency_contact'
]
});
})
.
then
(
function
(
user
)
{
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
_c
ontact
);
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
C
ontact
);
});
});
it
(
'should be able to retrieve element of array by index'
,
function
()
{
var
self
=
this
;
var
emergency
_c
ontact
=
{
name
:
'kate'
,
phones
:
[
1337
,
42
]
};
var
emergency
C
ontact
=
{
name
:
'kate'
,
phones
:
[
1337
,
42
]
};
return
this
.
User
.
create
({
username
:
'swen'
,
emergency_contact
:
emergency
_c
ontact
})
return
this
.
User
.
create
({
username
:
'swen'
,
emergency_contact
:
emergency
C
ontact
})
.
then
(
function
(
user
)
{
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
_c
ontact
);
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
C
ontact
);
return
self
.
User
.
find
({
where
:
{
username
:
'swen'
},
attributes
:
[[
'emergency_contact->\'phones\'->1'
,
'emergency_contact'
]]
});
})
.
then
(
function
(
user
)
{
...
...
@@ -100,11 +101,11 @@ if (dialect.match(/^postgres/)) {
it
(
'should be able to retrieve root level value of an object by key'
,
function
()
{
var
self
=
this
;
var
emergency
_c
ontact
=
{
kate
:
1337
};
var
emergency
C
ontact
=
{
kate
:
1337
};
return
this
.
User
.
create
({
username
:
'swen'
,
emergency_contact
:
emergency
_c
ontact
})
return
this
.
User
.
create
({
username
:
'swen'
,
emergency_contact
:
emergency
C
ontact
})
.
then
(
function
(
user
)
{
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
_c
ontact
);
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
C
ontact
);
return
self
.
User
.
find
({
where
:
{
username
:
'swen'
},
attributes
:
[[
'emergency_contact->\'kate\''
,
'emergency_contact'
]]
});
})
.
then
(
function
(
user
)
{
...
...
@@ -114,11 +115,11 @@ if (dialect.match(/^postgres/)) {
it
(
'should be able to retrieve nested value of an object by path'
,
function
()
{
var
self
=
this
;
var
emergency
_c
ontact
=
{
kate
:
{
email
:
'kate@kate.com'
,
phones
:
[
1337
,
42
]
}
};
var
emergency
C
ontact
=
{
kate
:
{
email
:
'kate@kate.com'
,
phones
:
[
1337
,
42
]
}
};
return
this
.
User
.
create
({
username
:
'swen'
,
emergency_contact
:
emergency
_c
ontact
})
return
this
.
User
.
create
({
username
:
'swen'
,
emergency_contact
:
emergency
C
ontact
})
.
then
(
function
(
user
)
{
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
_c
ontact
);
expect
(
user
.
emergency_contact
).
to
.
eql
(
emergency
C
ontact
);
return
self
.
User
.
find
({
where
:
{
username
:
'swen'
},
attributes
:
[[
'emergency_contact#>\'{kate,email}\''
,
'emergency_contact'
]]
});
})
.
then
(
function
(
user
)
{
...
...
@@ -145,6 +146,22 @@ if (dialect.match(/^postgres/)) {
expect
(
user
.
emergency_contact
.
name
).
to
.
equal
(
'kate'
);
});
});
it
(
'should be able to query using the nested query language'
,
function
()
{
var
self
=
this
;
return
this
.
sequelize
.
Promise
.
all
([
this
.
User
.
create
({
username
:
'swen'
,
emergency_contact
:
{
name
:
'kate'
}
}),
this
.
User
.
create
({
username
:
'anna'
,
emergency_contact
:
{
name
:
'joe'
}
})])
.
then
(
function
()
{
return
self
.
User
.
find
({
where
:
sequelize
.
json
({
emergency_contact
:
{
name
:
'kate'
}
}),
attributes
:
[
'username'
,
'emergency_contact'
]
});
})
.
then
(
function
(
user
)
{
expect
(
user
.
emergency_contact
.
name
).
to
.
equal
(
'kate'
);
});
});
});
describe
(
'hstore'
,
function
()
{
...
...
test/utils.test.js
View file @
80b9887
...
...
@@ -147,6 +147,19 @@ describe(Support.getTestDialectTeaser("Utils"), function() {
});
});
describe
(
'json'
,
function
()
{
it
(
'successfully parses a complex nested condition hash'
,
function
()
{
var
conditions
=
{
metadata
:
{
language
:
'icelandic'
,
pg_rating
:
{
'dk'
:
'G'
}
},
another_json_field
:
{
x
:
1
}
};
expect
((
new
Utils
.
json
(
conditions
)).
toString
()).
to
.
deep
.
equal
(
"metadata#>>'{language}' = 'icelandic' and metadata#>>'{pg_rating,dk}' = 'G' and another_json_field#>>'{x}' = '1'"
);
})
});
describe
(
'inflection'
,
function
()
{
it
(
'works better than lingo ;)'
,
function
()
{
expect
(
Utils
.
pluralize
(
'buy'
)).
to
.
equal
(
'buys'
);
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment