1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | const expect = require('chai').expect;
|
9 | const Connector = require('../lib/connector');
|
10 | const testConnector = require('./connectors/test-connector');
|
11 |
|
12 | const juggler = require('loopback-datasource-juggler');
|
13 | const ds = new juggler.DataSource({
|
14 | connector: testConnector,
|
15 | debug: true,
|
16 | });
|
17 |
|
18 |
|
19 | let connector;
|
20 | let Customer;
|
21 | let Order;
|
22 |
|
23 |
|
24 | describe('Name mapping', function() {
|
25 | let connector, builder;
|
26 |
|
27 | before(() => {
|
28 | connector = ds.connector;
|
29 | connector._tables = {};
|
30 | connector._models = {};
|
31 | Customer = ds.createModel(
|
32 | 'Customer',
|
33 | {
|
34 | name: {
|
35 | id: true,
|
36 | type: String,
|
37 | testdb: {
|
38 | column: 'FIRSTNAME',
|
39 | dataType: 'VARCHAR',
|
40 | dataLength: 32,
|
41 | },
|
42 | },
|
43 | middleName: {
|
44 | type: Boolean,
|
45 | name: 'middle_name',
|
46 | postgresql: {
|
47 | column: 'MIDDLENAME',
|
48 | },
|
49 | },
|
50 | lastName: {
|
51 | type: Boolean,
|
52 | testdb: {
|
53 | column: 'LASTNAME',
|
54 | },
|
55 | },
|
56 | primaryAddress: {
|
57 | type: String,
|
58 | name: 'primary_address',
|
59 | },
|
60 | address: String,
|
61 | },
|
62 | {testdb: {table: 'CUSTOMER'}},
|
63 | );
|
64 |
|
65 |
|
66 | Order = ds.createModel(
|
67 | 'Order',
|
68 | {
|
69 | id: {
|
70 | id: true,
|
71 | type: Number,
|
72 | testdb: {
|
73 | field: 'my_id',
|
74 | dataType: 'ObjectId',
|
75 | },
|
76 | },
|
77 | des: {
|
78 | type: String,
|
79 | testdb: {
|
80 | field: 'DESCRIPTION',
|
81 | },
|
82 | },
|
83 | },
|
84 | {testdb: {table: 'ORDER'}},
|
85 | );
|
86 | });
|
87 |
|
88 | context('getIdDbName', function() {
|
89 | it('should map id column name', function() {
|
90 | const idCol = connector.getIdDbName('Customer');
|
91 | expect(idCol).to.eql('FIRSTNAME');
|
92 | });
|
93 |
|
94 | it('alias idColumn should map id column name', function() {
|
95 | const idCol = connector.idColumn('Customer');
|
96 | expect(idCol).to.eql('FIRSTNAME');
|
97 | });
|
98 |
|
99 | it('should map id field name', function() {
|
100 | const idCol = connector.getIdDbName('Order');
|
101 | expect(idCol).to.eql('my_id');
|
102 | });
|
103 | });
|
104 | context('getPropertyDbName', function() {
|
105 | it('prefers property name if the database name is not matched', function() {
|
106 | const column = connector.getPropertyDbName('Customer', 'middleName');
|
107 | expect(column).to.eql('middle_name');
|
108 | });
|
109 |
|
110 | it('prefers database-specific column name over property name', function() {
|
111 | const column = connector.getPropertyDbName('Customer', 'lastName');
|
112 | expect(column).to.eql('LASTNAME');
|
113 | });
|
114 |
|
115 | it('alias column should map the column name', function() {
|
116 | const column = connector.column('Customer', 'lastName');
|
117 | expect(column).to.eql('LASTNAME');
|
118 | });
|
119 |
|
120 | it('propertyMapping should map column name from name attribute', function() {
|
121 | const column = connector.getPropertyDbName('Customer', 'primaryAddress');
|
122 | expect(column).to.eql('primary_address');
|
123 | });
|
124 |
|
125 | it('connector-preffered configuration (UPPERCASE) is applied if no columm/field name is provided', function() {
|
126 | const column = connector.getPropertyDbName('Customer', 'address');
|
127 | expect(column).to.eql('ADDRESS');
|
128 | });
|
129 |
|
130 | it('prefers database-specific field name over property name', function() {
|
131 | const column = connector.getPropertyDbName('Order', 'des');
|
132 | expect(column).to.eql('DESCRIPTION');
|
133 | });
|
134 | });
|
135 | });
|