Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaque Neves committed Aug 16, 2023
1 parent 054a69c commit b01d525
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ final manager = Manager();
## 2.0.2

- add support to postgres driver https://github.com/insinfo/postgresql-dart-1.git
- type correction and other improvements
- type correction and other improvements

## 2.1.0

- change dependencie postgres to my fork postgres_fork
- add more tests
4 changes: 2 additions & 2 deletions lib/src/connectors/postgres_connector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ class PostgresConnector extends Connector implements ConnectorInterface {
//print('using postgres');
} else if (config['driver_implementation'] == 'dargres') {
pdo = DargresPDO(dsn, username, password, options);
// print('using dargres');
//print('using dargres');
} else {
// print('using postgres');
//print('using postgres');
pdo = PostgresPDO(dsn, username, password, options);
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: eloquent
version: 2.0.2
version: 2.1.0
description: eloquent query builder port from PHP Laravel
homepage: https://github.com/insinfo/eloquent_dart
publish_to: none
Expand Down
3 changes: 3 additions & 0 deletions test/postgre_query_test.dart → test/dargres_query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import 'package:eloquent/eloquent.dart';

import 'package:test/test.dart';

/// test DargresPDO
/// config['driver_implementation'] == 'dargres'
void main() {
late Connection db;
setUp(() async {
var manager = new Manager();
manager.addConnection({
'driver': 'pgsql',
'driver_implementation': 'dargres',
'host': 'localhost',
'port': '5432',
'database': 'banco_teste',
Expand Down
146 changes: 146 additions & 0 deletions test/postgres_query_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import 'package:eloquent/eloquent.dart';

import 'package:test/test.dart';

/// test PostgresPDO
/// config['driver_implementation'] == 'postgres'
void main() {
late Connection db;
setUp(() async {
var manager = new Manager();
manager.addConnection({
'driver': 'pgsql',
'driver_implementation': 'postgres',
'host': 'localhost',
'port': '5432',
'database': 'banco_teste',
'username': 'usermd5',
'password': 's1sadm1n',
'charset': 'utf8',
'prefix': '',
'schema': ['public'],
//'sslmode' => 'prefer',
});
manager.setAsGlobal();
db = await manager.connection();
await db.select('DROP SCHEMA IF EXISTS myschema CASCADE;');
await db.select('CREATE SCHEMA IF NOT EXISTS myschema;');
await db.select('SET search_path TO myschema;');
await db.select('''CREATE TABLE "temp_location" (
id int4,
city VARCHAR(80),
street VARCHAR(80),
id_people int4
);''');
await db.select('''CREATE TABLE "people" (
id int4,
name VARCHAR(80),
profession VARCHAR(80)
);''');

await db
.table('people')
.insert({'id': 1, 'name': 'Isaque', 'profession': 'Personal Trainer'});

await db.select(
'''insert into "temp_location" ("id", "city", "street","id_people") values (1, 'Niteroi', 'Rua B',1)''');
});
group('query', () {
test('insert simple', () async {
var query = db.table('temp_location');
var res = await query
.insert({'id': 1, 'city': 'Rio de Janeiro', 'street': 'Rua R'});
expect(res, []);
});

test('insertGetId simple', () async {
var query = db.table('temp_location');
var res = await query
.insertGetId({'id': 1, 'city': 'Rio de Janeiro', 'street': 'Rua R'});
expect(res, 1);
});

test('select first', () async {
var query = db.table('temp_location');
var res = await query.select(['id', 'city', 'street']).first();
expect(res, {'id': 1, 'city': 'Niteroi', 'street': 'Rua B'});
});

test('select limit 1 get', () async {
var query = db.table('temp_location');
var res = await query.select(['id', 'city', 'street']).limit(1).get();
expect(res, [
{'id': 1, 'city': 'Niteroi', 'street': 'Rua B'}
]);
});

test('select limit 1 offset 0 get', () async {
var query = db.table('temp_location');
var res =
await query.select(['id', 'city', 'street']).limit(1).offset(0).get();
expect(res, [
{'id': 1, 'city': 'Niteroi', 'street': 'Rua B'}
]);
});

test('select * where limit 1 offset 0 get', () async {
var query = db.table('temp_location');
var res =
await query.select().where('id', '=', 1).limit(1).offset(0).get();
expect(res, [
{'id': 1, 'city': 'Niteroi', 'street': 'Rua B', 'id_people': 1}
]);
});

test('select inner join simple', () async {
final query = db.table('temp_location');
final res = await query
.select(['temp_location.id', 'city', 'street'])
.where('temp_location.id', '=', 1)
.join('people', 'people.id', '=', 'temp_location.id_people', 'inner')
.limit(1)
.offset(0)
.get();

expect(res, [
{'id': 1, 'city': 'Niteroi', 'street': 'Rua B'}
]);
});

test('select inner join with callback', () async {
final query = db.table('temp_location');
final res = await query
.select(['temp_location.id', 'city', 'street'])
.where('temp_location.id', '=', 1)
.join('people', (JoinClause q) {
q.on('people.id', '=', 'temp_location.id_people');
})
.limit(1)
.offset(0)
.get();

expect(res, [
{'id': 1, 'city': 'Niteroi', 'street': 'Rua B'}
]);
});

test('update simple', () async {
var query = db.table('temp_location');

await query.where('id', '=', 1).update({'city': 'Teresopolis'});

final res = await query
.select(['city'])
.where('temp_location.id', '=', 1)
.first();

expect(res, {'city': 'Teresopolis'});
});

test('delete simple', () async {
var query = db.table('temp_location');
var res = await query.where('id', '=', 1).delete();
expect(res, 1);
});
});
}

0 comments on commit b01d525

Please sign in to comment.