Skip to content

Commit 643b8d4

Browse files
committed
added handler test for #17
done handler tests for VERB handlers changed getDbRef to getApp, more accurate
1 parent 4f6c9ae commit 643b8d4

8 files changed

+151
-15
lines changed

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# Tests
2-
**/functions/tests/results/*
3-
41
# NPM
52
**/package-lock.json
63

functions/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
2+
tests/results/*
23
package-lock.json
34
ottaaproject-flutter-firebase-adminsdk-z2x83-b744263584.json

functions/getter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const admin = require('firebase-admin');
22
const serviceAccount = require('./ottaaproject-flutter-firebase-adminsdk-z2x83-b744263584.json');
33

4-
function getDbRef () {
4+
function getApp () {
55
try{
66
return admin.app('realiser')
77
} catch (e){
@@ -34,4 +34,4 @@ class dbGetter{
3434
}
3535
}
3636

37-
module.exports = { dbGetter, getDbRef }
37+
module.exports = { dbGetter, getApp }

functions/handlers.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ async function handleType(obj, sentence, langRef, src, forces){
4040
obj = await handleADV(obj, langRef);
4141
break;
4242
}
43+
44+
return obj
4345
}
4446

4547
async function handleVERB(obj, sentence, langRef, src, forces){
@@ -52,7 +54,6 @@ async function handleVERB(obj, sentence, langRef, src, forces){
5254
}
5355
/////////////////////////////////////////////////////////
5456
const defaults = await dbGetter.getPersistent(langRef, 'DEFAULTS', {})
55-
obj.meta.PERSON = defaults.PERSON;
5657

5758
const childrenSUBJ = obj.children.filter(c => c.type === 'SUBJ' || c.type === 'NOUN') //HERE: check if include NOUN here is ok
5859
if (forces.PERSON && childrenSUBJ.every(c => !sentence[c.position].words.includes(forces.PERSON))){
@@ -62,21 +63,22 @@ async function handleVERB(obj, sentence, langRef, src, forces){
6263
const personsPlurals = await dbGetter.getPersistent(langRef, 'PERSONS/PLURALS', {})
6364
const foundPerson = personsPlurals.find(person => childrenSUBJpersons.includes(person[0]) || childrenSUBJpersons.includes(person[1]));
6465
obj.meta.PERSON = foundPerson[1];
65-
} else if (childrenSUBJ.length === 1) obj.meta.PERSON = sentence[childrenSUBJ[0].position].meta.PERSON || obj.meta.PERSON;
66+
} else if (childrenSUBJ.length === 1) obj.meta.PERSON = sentence[childrenSUBJ[0].position].meta.PERSON || defaults.PERSON[0];
67+
else obj.meta.PERSON = defaults.PERSON[0];
6668

67-
obj.meta.TIME = defaults.TIME;
6869
const childrenADV = obj.children.filter(c => c.type === 'ADV')
6970
if (forces.TIME){
7071
obj.meta.TIME = forces.TIME;
71-
} else if (childrenADV.length > 1) {
72+
} else if (childrenADV.length > 1) { //HERE: maybe sorting here is worthless since we do a for loop later, maybe it's better to just do the loop and overwrite if position is higher
7273
const sortedChildrenADV = childrenADV.sort((a, b) => Math.abs(a.position - obj.position) - Math.abs(b.position - obj.position))
7374
for (childADV of sortedChildrenADV) {
7475
if (sentence[childADV.position].meta.TIME) {
7576
obj.meta.TIME = sentence[childADV.position].meta.TIME;
7677
break;
7778
}
7879
}
79-
} else if (childrenADV.length === 1) obj.meta.TIME = sentence[childrenADV[0].position].meta.TIME || obj.meta.TIME;
80+
} else if (childrenADV.length === 1) obj.meta.TIME = sentence[childrenADV[0].position].meta.TIME || defaults.TIME;
81+
else obj.meta.TIME = defaults.TIME;
8082

8183
return await conjugateVERB(obj, langRef, src, defaults);
8284
}

functions/methods.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { dbGetter, getDbRef } = require('./getter.js')
1+
const { dbGetter, getApp } = require('./getter.js')
22

3-
const rt = getDbRef().database()
3+
const rt = getApp().database()
44

55
async function addLexiconData(path, data){
66
const newDataRef = rt.ref(path)

functions/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"test": "npm run test-methods && npm run test-prepares && npm run test-dependencies",
1111
"test-methods": "mocha --exit --timeout 5000 --reporter mochawesome --reporter-options reportDir=tests/results/methods tests/methods.test.js",
1212
"test-prepares": "mocha --exit --timeout 5000 --reporter mochawesome --reporter-options reportDir=tests/results/prepares tests/prepares.test.js",
13-
"test-dependencies": "mocha --exit --timeout 5000 --reporter mochawesome --reporter-options reportDir=tests/results/dependencies tests/dependencies.test.js"
13+
"test-dependencies": "mocha --exit --timeout 5000 --reporter mochawesome --reporter-options reportDir=tests/results/dependencies tests/dependencies.test.js",
14+
"test-handlers": "mocha --exit --timeout 5000 --reporter mochawesome --reporter-options reportDir=tests/results/handlers tests/handlers.test.js"
1415
},
1516
"engines": {
1617
"node": "16"

functions/tests/handlers.test.js

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
const expect = require('chai').expect;
2+
3+
describe('Test VERB handlers', () => {
4+
before(() => {
5+
const { getApp } = require('../getter.js')
6+
this.langRef = getApp().database().ref('es')
7+
this.handleType = require('../handlers.js').handleType
8+
})
9+
10+
it('Should get TIME metadata from ADV children (and PERSON be default)', async () => {
11+
const testObj = [
12+
{
13+
words: ['hacer'],
14+
types: ['VERB'],
15+
type: 'VERB',
16+
position: 0,
17+
children: [
18+
{position: 1, type: 'ADV'}
19+
],
20+
meta: {},
21+
composed: false,
22+
props: {}
23+
},
24+
{
25+
words: ['hoy'],
26+
types: ['ADV'],
27+
type: 'ADV',
28+
position: 1,
29+
children: [],
30+
meta: {TIME: 'presente'},
31+
composed: false,
32+
props: {}
33+
}
34+
]
35+
const resultObj = await this.handleType(testObj[0], testObj, this.langRef, 'static', {})
36+
expect(resultObj.meta.TIME).to.equal(testObj[1].meta.TIME)
37+
expect(resultObj.meta.PERSON).to.equal('él') //default PERSON HERE: should generalize this
38+
})
39+
40+
it('Should get PERSON metadata from SUBJ children (and PERSON be default)', async () => {
41+
const testObj = [
42+
{
43+
words: ['hacer'],
44+
types: ['VERB'],
45+
type: 'VERB',
46+
position: 0,
47+
children: [
48+
{position: 1, type: 'SUBJ'}
49+
],
50+
meta: {},
51+
composed: false,
52+
props: {}
53+
},
54+
{
55+
words: ['yo'],
56+
types: ['SUBJ'],
57+
type: 'SUBJ',
58+
position: 1,
59+
children: [],
60+
meta: {PERSON: 'yo'},
61+
composed: false,
62+
props: {}
63+
}
64+
]
65+
const resultObj = await this.handleType(testObj[0], testObj, this.langRef, 'static', {})
66+
expect(resultObj.meta.PERSON).to.equal(testObj[1].meta.PERSON)
67+
expect(resultObj.meta.TIME).to.equal('presente') //default TIME HERE: should generalize this
68+
})
69+
70+
it('Should get metadata from ADV and SUBJ children simultaneously', async () => {
71+
const testObj = [
72+
{
73+
words: ['yo'],
74+
types: ['SUBJ'],
75+
type: 'SUBJ',
76+
position: 0,
77+
children: [],
78+
meta: {PERSON: 'yo'},
79+
composed: false,
80+
props: {}
81+
},
82+
{
83+
words: ['hacer'],
84+
types: ['VERB'],
85+
type: 'VERB',
86+
position: 1,
87+
children: [
88+
{position: 0, type: 'SUBJ'},
89+
{position: 2, type: 'ADV'}
90+
],
91+
meta: {},
92+
composed: false,
93+
props: {}
94+
},
95+
{
96+
words: ['hoy'],
97+
types: ['ADV'],
98+
type: 'ADV',
99+
position: 2,
100+
children: [],
101+
meta: {TIME: 'presente'},
102+
composed: false,
103+
props: {}
104+
}
105+
]
106+
const resultObj = await this.handleType(testObj[1], testObj, this.langRef, 'static', {})
107+
expect(resultObj.meta.PERSON).to.equal(testObj[0].meta.PERSON)
108+
expect(resultObj.meta.TIME).to.equal(testObj[2].meta.TIME)
109+
})
110+
111+
it('Should handle forcing TIME and PERSON', async () => {
112+
const testObj = [
113+
{
114+
words: ['hacer'],
115+
types: ['VERB'],
116+
type: 'VERB',
117+
position: 0,
118+
children: [],
119+
meta: {},
120+
composed: false,
121+
props: {}
122+
}
123+
]
124+
const forces = {PERSON: 'él', TIME: 'pasado'}
125+
const resultObj = await this.handleType(testObj[0], testObj, this.langRef, 'static', forces)
126+
expect(resultObj.meta.PERSON).to.equal(forces.PERSON)
127+
expect(resultObj.meta.TIME).to.equal(forces.TIME)
128+
})
129+
})
130+
131+
describe('Test SUBJ handlers', () => {
132+
133+
})
134+
135+
//For now only SUBJ and VERB are important because ADJ and ADV have mostly no function and NOUN is basically the same as SUBJ

functions/tests/prepares.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const expect = require('chai').expect
22

33
describe('Testing prepare NOUN methods', () => {
44
before(() => {
5-
const { getDbRef } = require('../getter.js')
6-
this.langRef = getDbRef().database().ref('es')
5+
const { getApp } = require('../getter.js')
6+
this.langRef = getApp().database().ref('es')
77
this.prepareMeta = require('../prepares.js').prepareMeta;
88
})
99

0 commit comments

Comments
 (0)