Skip to content

Commit

Permalink
Merge pull request sequelize#2976 from sequelize/refactor-sql-unit-tests
Browse files Browse the repository at this point in the history
refactor: initial work on sql unit tests refactor
  • Loading branch information
mickhansen committed Jan 22, 2015
2 parents d613c5f + c7691f3 commit 6a0912a
Show file tree
Hide file tree
Showing 77 changed files with 933 additions and 360 deletions.
5 changes: 4 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"before",
"beforeEach",
"after",
"afterEach"
"afterEach",
"suite",
"setup",
"test"
]
}
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ test: codeclimate
else
test:
@if [ "$$GREP" ]; then \
make jshint && make teaser && ./node_modules/mocha/bin/mocha --globals setImmediate,clearImmediate --check-leaks --colors -t 10000 --reporter $(REPORTER) -g "$$GREP" $(TESTS); \
make jshint && make teaser && ./node_modules/mocha/bin/mocha --globals setImmediate,clearImmediate --ui tdd --check-leaks --colors -t 10000 --reporter $(REPORTER) -g "$$GREP" $(TESTS); \
else \
make jshint && make teaser && ./node_modules/mocha/bin/mocha --globals setImmediate,clearImmediate --check-leaks --colors -t 10000 --reporter $(REPORTER) $(TESTS); \
make jshint && make teaser && ./node_modules/mocha/bin/mocha --globals setImmediate,clearImmediate --ui tdd --check-leaks --colors -t 10000 --reporter $(REPORTER) $(TESTS); \
fi
endif

test-only:
./node_modules/mocha/bin/mocha --globals setImmediate,clearImmediate --check-leaks --colors -t 10000 --reporter $(REPORTER) $(TESTS); \
./node_modules/mocha/bin/mocha --globals setImmediate,clearImmediate --ui tdd --check-leaks --colors -t 10000 --reporter $(REPORTER) $(TESTS); \

jshint:
./node_modules/.bin/jshint lib test

cover:
rm -rf coverage \
make teaser && ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -t 10000 $(TESTS); \
make teaser && ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -t 10000 --ui tdd $(TESTS); \

mariadb:
@DIALECT=mariadb make test
Expand Down
73 changes: 39 additions & 34 deletions docs/docs/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ Project
.findAndCountAll({
where: {
title: {
like: 'foo%'
$like: 'foo%'
}
},
offset: 10,
Expand Down Expand Up @@ -689,65 +689,70 @@ Project.findAll({ where: { id: [1,2,3] } }).then(function(projects) {
// this is actually doing an IN query
})

// or
Project.findAll({ where: "name = 'A Project'" }).then(function(projects) {
// the difference between this and the usage of hashes (objects) is, that string usage
// is not sql injection safe. so make sure you know what you are doing!
})

// since v1.7.0 we can now improve our where searches
Project.findAll({
where: {
id: {
gt: 6, // id > 6
gte: 6, // id >= 6
lt: 10, // id < 10
lte: 10, // id
ne: 20, // id != 20
between: [6, 10], // BETWEEN 6 AND 10
nbetween: [11, 15], // NOT BETWEEN 11 AND 15
in: [1, 2], // IN [1, 2]
like: '%hat', // LIKE '%hat'
nlike: '%hat' // NOT LIKE '%hat'
ilike: '%hat' // ILIKE '%hat' (case insensitive)
nilike: '%hat' // NOT ILIKE '%hat'
overlap: [1, 2] // && [1, 2] (PG array overlap operator)
$gt: 6, // id > 6
$gte: 6, // id >= 6
$lt: 10, // id < 10
$lte: 10, // id
$ne: 20, // id != 20
$not: 3, // id NOT 3
$between: [6, 10], // BETWEEN 6 AND 10
$notBetween: [11, 15], // NOT BETWEEN 11 AND 15
$in: [1, 2], // IN [1, 2]
$like: '%hat', // LIKE '%hat'
$notLike: '%hat' // NOT LIKE '%hat'
$iLike: '%hat' // ILIKE '%hat' (case insensitive)
$notILike: '%hat' // NOT ILIKE '%hat'
$overlap: [1, 2] // && [1, 2] (PG array overlap operator)
$contains: [1, 2] // @> [1, 2] (PG array contains operator)
$contained: [1, 2] // <@ [1, 2] (PG array contained by operator)
}
}
})
```

### Complex filtering / OR queries

Since `v1.7.0-rc3`, it is possible to do complex where queries with multiple levels of nested AND and OR conditions. In order to do that you can use `Sequelize.or` and `Sequelize.and` and pass an arbitrary amount of arguments to it. Every argument will get transformed into a proper SQL condition and gets joined with the either `AND` or `OR`.
It's possible to do complex where queries with multiple levels of nested AND and OR conditions. In order to do that you can use `$or` and `$and`:

```js
Project.find({
where: Sequelize.and(
{ name: 'a project' },
Sequelize.or(
where: {
name: 'a project',
$or: [
{ id: [1,2,3] },
{ id: { gt: 10 } }
)
)
{ id: { $gt: 10 } }
]
}
})

Project.find({
where: {
name: 'a project',
id: {
$or: [
[1,2,3],
{ $gt: 10 }
]
}
}
})
```

This code will generate the following query:
Both pieces of code code will generate the following:

```sql
SELECT *
FROM `Projects`
WHERE (
`Projects`.`name`='a project'
`Projects`.`name` = 'a project'
AND (`Projects`.`id` IN (1,2,3) OR `Projects`.`id` > 10)
)
LIMIT 1
;
LIMIT 1;
```

Notice, that instead of `Sequelize.and` you can also use a plain array which will be treated as `Sequelize.and` if it contains objects or hashes or other complex data types. Furthermore you can use `Sequelize.or` as value for the where clause.

### Manipulating the dataset with limit&comma; offset&comma; order and group

To get more relevant data&comma; you can use limit&comma; offset&comma; order and grouping&colon;
Expand Down
Loading

0 comments on commit 6a0912a

Please sign in to comment.