Skip to content

Commit

Permalink
Merge pull request #1 from nicjansma/master
Browse files Browse the repository at this point in the history
Tests for where() fix with 0-values and .as() function
  • Loading branch information
xavierlacot committed Nov 2, 2011
2 parents 5b72d8b + 286df72 commit 0fb6fe5
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Resources/test/query.js
Expand Up @@ -53,6 +53,40 @@
expect(q.getQuery()).toBe('select * from human left outer join city on city.id = human.city_id');
});

it('joli.query.as()', function() {
// test without using as()
q = new joli.query().select()
.from('human')
.join('city', 'id', 'human.city_id');

// query validation
expect(q.getQuery()).toBe('select * from human left outer join city on city.id = human.city_id');

// recs should be for humans
recs = q.execute();
expect(recs.length).toBe(1);
human = recs[0];
expect(human.first_name).toBe('Sarah');
expect(human.last_name).toBe('Sure');
expect(human.city_id).toBe(models.city.findOneBy('name', 'New York').get('id'));

// test a projection of the same query using as('city')
q = new joli.query().select('city.*')
.from('human')
.join('city', 'id', 'human.city_id')
.as('city');

// query validation
expect(q.getQuery()).toBe('select city.* from human left outer join city on city.id = human.city_id');

// recs should be for cities
recs = q.execute();
expect(recs.length).toBe(1);
city = recs[0];
expect(city.name).toBe('New York');
expect(city.id).toBe(models.city.findOneBy('name', 'New York').get('id'));
});

it('joli.query.limit()', function() {
q = new joli.query().select().from('human').limit(10);
expect(q.getQuery()).toBe('select * from human limit 10');
Expand Down Expand Up @@ -121,6 +155,14 @@
q = new joli.query().select().from('view_count').where('nb_views between ? and ?', [1000]);
expect(q.getQuery()).toBe('select * from view_count where nb_views between "1000" and ?');

// check that replacements work with 0 values
q = new joli.query().select().from('view_count').where('nb_views between ? and ?', [1000, 0]);
expect(q.getQuery()).toBe('select * from view_count where nb_views between "1000" and "0"');

// check that replacements work for values after 0
q = new joli.query().select().from('view_count').where('nb_views between ? and ? and ?', [1000, 0, 2000]);
expect(q.getQuery()).toBe('select * from view_count where nb_views between "1000" and "0" and "2000"');

// check with several chained calls
q = new joli.query().select().from('human').where('last_name = ?', 'Doe').where('first_name = ?', 'John');
expect(q.getQuery()).toBe('select * from human where last_name = "Doe" and first_name = "John"');
Expand Down

0 comments on commit 0fb6fe5

Please sign in to comment.