Skip to content

Commit

Permalink
Add unit test for category matching. (project-chip#831)
Browse files Browse the repository at this point in the history
0. Add two zap files with intentionally wrong package paths, but correct categories.
1. Add a test that loads both matter and zigbee metadata.
2. Load a matter zap file and a zigbee zap file, and test that correct packages are attached.
  • Loading branch information
tecimovic authored Nov 23, 2022
1 parent cfdf9e1 commit 6a1948e
Show file tree
Hide file tree
Showing 6 changed files with 2,169 additions and 4 deletions.
4 changes: 0 additions & 4 deletions test/gen-dotdot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ const zclLoader = require('../src-electron/zcl/zcl-loader.js')
const importJs = require('../src-electron/importexport/import.js')
const testUtil = require('./test-util.js')
const queryPackage = require('../src-electron/db/query-package.js')
const {
exportClustersFromEndpointType,
} = require('../src-electron/db/query-impexp.js')
const querySession = require('../src-electron/db/query-session.js')

let db
const testFile = path.join(__dirname, 'resource/three-endpoint-device.zap')
Expand Down
1 change: 1 addition & 0 deletions test/gen-template/matter/gen-test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "Matter test template",
"version": "test-matter",
"category": "matter",
"options": {
"testOption": {
"code1": "label1",
Expand Down
1 change: 1 addition & 0 deletions test/gen-template/zigbee/gen-templates.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "Test templates",
"version": "test-v1",
"category": "zigbee",
"requiredFeatureLevel": 2,
"options": {
"testOption": {
Expand Down
111 changes: 111 additions & 0 deletions test/multi-package.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
*
* Copyright (c) 2022 Silicon Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* @jest-environment node
*/
const path = require('path')
const genEngine = require('../src-electron/generator/generation-engine')
const env = require('../src-electron/util/env')
const dbApi = require('../src-electron/db/db-api')
const queryAttribute = require('../src-electron/db/query-attribute')
const querySession = require('../src-electron/db/query-session')
const queryPackage = require('../src-electron/db/query-package')
const queryZcl = require('../src-electron/db/query-zcl')
const zclLoader = require('../src-electron/zcl/zcl-loader')
const importJs = require('../src-electron/importexport/import')
const testUtil = require('./test-util')
const testQuery = require('./test-query')
const dbEnum = require('../src-shared/db-enum')

let db

// These two files have intentionally wrong paths in the package, so
// that it tests the ability of the zap files that got moved around
// to attach to proper packages via category.
const testFileMatter = path.join(__dirname, 'resource/mp-matter.zap')
const testFileZigbee = path.join(__dirname, 'resource/mp-zigbee.zap')

beforeAll(async () => {
env.setDevelopmentEnv()
let file = env.sqliteTestFile('multi-package')
db = await dbApi.initDatabaseAndLoadSchema(
file,
env.schemaFile(),
env.zapVersion()
)
await zclLoader.loadZcl(db, env.builtinMatterZclMetafile())
await zclLoader.loadZcl(db, env.builtinSilabsZclMetafile())
await genEngine.loadTemplates(db, testUtil.testTemplate.matter)
await genEngine.loadTemplates(db, testUtil.testTemplate.zigbee)
}, testUtil.timeout.medium())

afterAll(() => dbApi.closeDatabase(db), testUtil.timeout.short())

test('Validate loaded packages', async () => {
expect(
await testQuery.selectCountFrom(
db,
"PACKAGE WHERE PACKAGE.TYPE = 'zcl-properties'"
)
).toBe(2)
expect(
await testQuery.selectCountFrom(
db,
"PACKAGE WHERE PACKAGE.TYPE = 'gen-templates-json'"
)
).toBe(2)
})

test('Validate Matter zap file.', async () => {
let sessionId = await querySession.createBlankSession(db)
await importJs.importDataFromFile(db, testFileMatter, {
sessionId: sessionId,
})
let pkgs = await queryPackage.getSessionPackagesWithTypes(db, sessionId)
expect(pkgs.length).toBe(2)
for (let pkg of pkgs) {
if (pkg.type === dbEnum.packageType.zclProperties) {
let p = await queryPackage.getPackageByPackageId(db, pkg.packageRef)
expect(p.category).toBe('matter')
} else if (pkg.type === dbEnum.packageType.genTemplatesJson) {
let p = await queryPackage.getPackageByPackageId(db, pkg.packageRef)
expect(p.category).toBe('matter')
} else {
fail('Unknown package type.')
}
}
})

test('Validate Zigbee zap file.', async () => {
let sessionId = await querySession.createBlankSession(db)
await importJs.importDataFromFile(db, testFileZigbee, {
sessionId: sessionId,
})
let pkgs = await queryPackage.getSessionPackagesWithTypes(db, sessionId)
expect(pkgs.length).toBe(2)
for (let pkg of pkgs) {
if (pkg.type === dbEnum.packageType.zclProperties) {
let p = await queryPackage.getPackageByPackageId(db, pkg.packageRef)
expect(p.category).toBe('zigbee')
} else if (pkg.type === dbEnum.packageType.genTemplatesJson) {
let p = await queryPackage.getPackageByPackageId(db, pkg.packageRef)
expect(p.category).toBe('zigbee')
} else {
fail('Unknown package type.')
}
}
})
Loading

0 comments on commit 6a1948e

Please sign in to comment.