Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rgwozdz committed Oct 11, 2023
1 parent b7a46e0 commit 867a93e
Show file tree
Hide file tree
Showing 11 changed files with 792 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
'no-unused-vars': ['error', { ignoreRestSiblings: true }],
indent: ['error', 2, { SwitchCase: 1 }],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single'],
//quotes: ['error', 'single'],
semi: ['error', 'always'],
'no-only-tests/no-only-tests': 'error',
},
Expand Down
19 changes: 19 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: 2
updates:
- package-ecosystem: "npm"
target-branch: "main"
versioning-strategy: "increase"
schedule:
interval: "weekly"
groups:
production-dependencies:
dependency-type: "production"
update-types:
- "minor"
- "patch"
development-dependencies:
dependency-type: "development"
update-types:
- "minor"
- "patch"
rebase-strategy: "auto"
41 changes: 41 additions & 0 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI tests

on:
push:
branches: [ main ]
pull_request:
branches:
- main
paths:
- ".github/**.yml"
- "**/**.js"
- "package.json"

jobs:
pr-tests:
name: Install, lint, test
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [18.x]
os: [ubuntu-latest, windows-latest]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install npm 7
run: npm i -g npm@7 --registry=https://registry.npmjs.org

- name: Install
run: npm ci

- name: Lint
run: npm run lint:ci

- name: Unit tests
run: npm test

73 changes: 73 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
pull_request:
branches: [ "main"]
paths:
- "**/*.js"
schedule:
- cron: '35 13 * * 6'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'javascript' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
37 changes: 37 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/

/** @type {import('jest').Config} */
const config = {
// Indicates which provider should be used to instrument code for coverage
coverageProvider: 'v8',

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],

roots: ['src'],

// The glob patterns Jest uses to detect test files
testMatch: ['**/*.spec.js'],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: ['/node_modules/'],


coverageDirectory: './coverage',
testEnvironment: 'node',
// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: undefined,
};

module.exports = config;
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const packageInfo = require('../package.json');
const provider = {
type: 'provider',
version: packageInfo.version,
name: 'elastic',
name: 'elastic-sql',
Model: require('./model'),
};

Expand Down
10 changes: 10 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { name, type, Model, version } = require('./index');

describe('registration object', () => {
test('has expected properties', () => {
expect(name).toBe('elastic-sql');
expect(type).toBe('provider');
expect(version).toBeDefined();
expect(Model).toBeInstanceOf(Function);
});
});
29 changes: 17 additions & 12 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Model {
#geometryFieldMap;
#idFieldMap;

constructor({ logger }, { conn, geometryFieldMap, idFieldMap }) {
constructor({ logger }, { conn, geometryFieldMap, idFieldMap } = {}) {
this.#logger = logger;

// Validate registration options
Expand Down Expand Up @@ -85,6 +85,7 @@ class Model {
geojson.metadata = { idField };

geojson.filtersApplied = generateFiltersApplied({
...geoserviceParams,
idField,
geometryField,
geometry,
Expand All @@ -99,15 +100,14 @@ class Model {

#handleError(error) {
const messagePrefix = 'Provider error:';
const statusCode = error?.body?.status || 500;

if (error.name === 'ResponseError') {
this.#logger.error(
`${messagePrefix} data-store query failure, ${error.message}`,
);
const message = statusCode === 400 ? 'invalid input' : error.message;
const message = error?.body?.status === 400 ? 'invalid input' : error.message;
const err = new Error(message);
err.code = statusCode;
err.code = error?.body?.status || 500;
return err;
}

Expand Down Expand Up @@ -171,7 +171,7 @@ function buildGeoshapeFilter({ geometryField, geometry, inSR, spatialRel }) {
const { geometry: geometryFilter, relation } = standardizeGeometryFilter({
geometry,
inSR,
reprojecitonSR: 4326,
reprojectionSR: 4326,
spatialRel,
});

Expand Down Expand Up @@ -213,17 +213,22 @@ function convertRowToFeature(featureAttributeKeys, geometryField, row) {
};
}

function generateFiltersApplied({ idField, geometry }) {
const filtersApplied = {
where: true,
orderByFields: true,
};
function generateFiltersApplied({ where, objectIds, orderByFields, idField, geometry, geometryField }) {
const filtersApplied = {};

if (idField) {
if (where) {
filtersApplied.where = true;
}

if (objectIds && idField) {
filtersApplied.objectIds = true;
}

if (geometry) {
if (orderByFields) {
filtersApplied.orderByFields = true;
}

if (geometry && geometryField) {
filtersApplied.geometry = true;
}

Expand Down
Loading

0 comments on commit 867a93e

Please sign in to comment.