|
| 1 | +const { entity, field } = require('@herbsjs/gotu') |
| 2 | +const Repository = require('../../../src/repository') |
| 3 | +const db = require('./db') |
| 4 | +const connection = require('../connection') |
| 5 | +const assert = require('assert') |
| 6 | +const { camelCase } = require('lodash') |
| 7 | + |
| 8 | +describe('Query Find with Conventions', () => { |
| 9 | + const table = 'testRepository' |
| 10 | + const schema = 'herbs2knex_testdb' |
| 11 | + |
| 12 | + const givenAnRepositoryClass = options => { |
| 13 | + return class ItemRepositoryBase extends Repository { |
| 14 | + constructor () { |
| 15 | + super(options) |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + const GivenAnEntity = () => { |
| 21 | + return entity('A entity', { |
| 22 | + id: field(Number), |
| 23 | + stringTest: field(String), |
| 24 | + booleanTest: field(Boolean) |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + const GivenAnSnakeCaseEntity = () => { |
| 29 | + return entity('A entity', { |
| 30 | + id: field(Number), |
| 31 | + string_test: field(String), |
| 32 | + boolean_test: field(Boolean) |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + before(async () => { |
| 37 | + const sql = ` |
| 38 | + DROP SCHEMA IF EXISTS ${schema} CASCADE; |
| 39 | + CREATE SCHEMA ${schema}; |
| 40 | + DROP TABLE IF EXISTS ${schema}.${table} CASCADE; |
| 41 | + CREATE TABLE ${schema}."${table}" ( |
| 42 | + "id" INT, |
| 43 | + "stringTest" TEXT, |
| 44 | + "booleanTest" BOOL |
| 45 | + )` |
| 46 | + await db.query(sql) |
| 47 | + |
| 48 | + await db.query( |
| 49 | + `INSERT INTO ${schema}."${table}" ("id", "stringTest", "booleanTest") VALUES (10, 'marie', true)` |
| 50 | + ) |
| 51 | + }) |
| 52 | + |
| 53 | + after(async () => { |
| 54 | + const sql = ` |
| 55 | + DROP SCHEMA IF EXISTS ${schema} CASCADE; |
| 56 | + ` |
| 57 | + await db.query(sql) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should return entities using table field custom convention for camel case', async () => { |
| 61 | + //given |
| 62 | + const toCamelCase = value => camelCase(value) |
| 63 | + |
| 64 | + const anEntity = GivenAnEntity() |
| 65 | + const ItemRepository = givenAnRepositoryClass({ |
| 66 | + entity: anEntity, |
| 67 | + table, |
| 68 | + schema, |
| 69 | + ids: ['id'], |
| 70 | + knex: connection, |
| 71 | + convention: { |
| 72 | + toTableFieldName: field => toCamelCase(field) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + const itemRepo = new ItemRepository() |
| 77 | + |
| 78 | + //when |
| 79 | + const ret = await itemRepo.find({ where: { stringTest: ['marie'] } }) |
| 80 | + |
| 81 | + //then |
| 82 | + assert.deepStrictEqual(ret[0].toJSON(), { |
| 83 | + id: 10, |
| 84 | + stringTest: 'marie', |
| 85 | + booleanTest: true |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + it("should return entities when the convention of the entity's fields is different from the database", async () => { |
| 90 | + //given |
| 91 | + const toCamelCase = value => camelCase(value) |
| 92 | + |
| 93 | + const anEntity = GivenAnSnakeCaseEntity() |
| 94 | + const ItemRepository = givenAnRepositoryClass({ |
| 95 | + entity: anEntity, |
| 96 | + table, |
| 97 | + schema, |
| 98 | + ids: ['id'], |
| 99 | + knex: connection, |
| 100 | + convention: { |
| 101 | + toTableFieldName: field => toCamelCase(field) |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + const itemRepo = new ItemRepository() |
| 106 | + |
| 107 | + //when |
| 108 | + const ret = await itemRepo.find({ where: { stringTest: ['marie'] } }) |
| 109 | + |
| 110 | + //then |
| 111 | + assert.deepStrictEqual(ret[0].toJSON(), { |
| 112 | + id: 10, |
| 113 | + string_test: 'marie', |
| 114 | + boolean_test: true |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + it('should return a error when custom convention throws a exception', async () => { |
| 119 | + //given |
| 120 | + const anEntity = GivenAnSnakeCaseEntity() |
| 121 | + const ItemRepository = givenAnRepositoryClass({ |
| 122 | + entity: anEntity, |
| 123 | + table, |
| 124 | + schema, |
| 125 | + ids: ['id'], |
| 126 | + knex: connection, |
| 127 | + convention: { |
| 128 | + toTableFieldName: _ => { |
| 129 | + throw new Error('error') |
| 130 | + } |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + |
| 135 | + //when & then |
| 136 | + assert.throws(() => new ItemRepository()) |
| 137 | + }) |
| 138 | +}) |
0 commit comments