Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from regulaforensics/develop
Browse files Browse the repository at this point in the history
The tests are redesigned to work on the server (with the directory wi…
  • Loading branch information
aliaksandr-drozd authored Feb 21, 2024
2 parents 9d0c119 + 5f5e0c8 commit e95bb3b
Show file tree
Hide file tree
Showing 33 changed files with 744 additions and 151,425 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROCESS_RESPONSE_JSONS_DIR=/Users/cooler/Projects/Regula/document-reader-recipes/testdata
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.idea
docs
testdata

.env
tests/test-data/

# Logs
logs
Expand Down Expand Up @@ -134,3 +134,4 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
/test-data/
1 change: 1 addition & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
transform: {'^.+\\.ts?$': 'ts-jest'},
testEnvironment: 'node',
testRegex: '/.*\\.(test|spec)?\\.(ts|tsx)$',
setupFiles: ['dotenv/config'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
"^@/(.*)": "<rootDir>/src/$1"
Expand Down
238 changes: 129 additions & 109 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.22",
"version": "0.0.23",
"author": "Regula Forensics, Inc.",
"name": "@regulaforensics/document-reader-recipes",
"description": "Document Reader Recipes",
Expand All @@ -16,11 +16,12 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
"@types/node": "^20.11.19",
"dotenv": "^16.4.5",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3",
"vite": "^5.1.0",
"vite": "^5.1.3",
"vite-plugin-dts": "^3.7.2"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { ProcessResponse } from '@regulaforensics/document-reader-typings'
import { readdirSync, readFileSync } from 'fs'
import { join } from 'path'

import rawDocReaderResponse from '@/test-data/1.json'
import { getAuthenticityCheckList } from './get-authenticity-check-list.recipe'


const DIRECTORY = String(process.env.PROCESS_RESPONSE_JSONS_DIR)

describe('getAuthenticityCheckList', () => {
const docReaderResponse = ProcessResponse.fromPlain(rawDocReaderResponse)
const files = readdirSync(DIRECTORY)

files.forEach((file) => {
const filePath = join(DIRECTORY, file)

if (!filePath.endsWith('.json')) {
return
}

const fileContent = readFileSync(filePath, 'utf-8')

let isValidJSON = true
let response = ''

try {
response = JSON.parse(fileContent)
} catch (e) {
isValidJSON = false
}

test(`file '${file}': should be a valid JSON`, () => {
expect(isValidJSON).toBe(true)
})

const docReaderResponse = ProcessResponse.fromPlain(response)

test('should valid result', () => {
const result = getAuthenticityCheckList(docReaderResponse)

console.log(JSON.stringify(result))
test(`file '${file}': should return authenticity check list`, () => {
const result = getAuthenticityCheckList(docReaderResponse)

expect(result).toBeDefined()
expect(result).toBeDefined()
})
})
})
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
import { ProcessResponse } from '@regulaforensics/document-reader-typings'
import { readdirSync, readFileSync } from 'fs'
import { join } from 'path'

import rawDocReaderResponse from '@/test-data/7.json'
import { RDocumentBarcode } from './models'
import { getDocumentBarcodes } from './get-document-barcodes.recipe'


const DIRECTORY = String(process.env.PROCESS_RESPONSE_JSONS_DIR)

describe('getDocumentBarcodes', () => {
const docReaderResponse = ProcessResponse.fromPlain(rawDocReaderResponse)
const result = getDocumentBarcodes(docReaderResponse)
const isValid = RDocumentBarcode.isValid(result)
const files = readdirSync(DIRECTORY)

test('should return non-empty array of images', () => {
expect(result.length).toBeGreaterThan(0)
})
files.forEach((file) => {
const filePath = join(DIRECTORY, file)

if (!filePath.endsWith('.json')) {
return
}

const fileContent = readFileSync(filePath, 'utf-8')

let isValidJSON = true
let response = ''

try {
response = JSON.parse(fileContent)
} catch (e) {
isValidJSON = false
}

test(`file '${file}': should be a valid JSON`, () => {
expect(isValidJSON).toBe(true)
})


const docReaderResponse = ProcessResponse.fromPlain(response)
const result = getDocumentBarcodes(docReaderResponse)
const isValid = RDocumentBarcode.isValid(result)

test('should return valid array of images', () => {
expect(isValid).toBe(true)
test(`file '${file}': should return valid array of images`, () => {
expect(isValid).toBe(true)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
import 'reflect-metadata'
import { join } from 'path'
import { readdirSync, readFileSync } from 'fs'
import { ProcessResponse } from '@regulaforensics/document-reader-typings'

import rawDocReaderResponse from '@/test-data/2.json'
import { RDocumentImage } from './models'
import { getDocumentImages } from './get-document-images.recipe'


const DIRECTORY = String(process.env.PROCESS_RESPONSE_JSONS_DIR)

describe('getDocumentImage', () => {
const docReaderResponse = ProcessResponse.fromPlain(rawDocReaderResponse)
let result: RDocumentImage[] = []
const files = readdirSync(DIRECTORY)

for (let i = 0; i < files.length; i++) {
const file = files[i]
const filePath = join(DIRECTORY, file)

if (!filePath.endsWith('.json')) {
continue
}

const fileContent = readFileSync(filePath, 'utf-8')

let isValidJSON = true
let response = ''

try {
response = JSON.parse(fileContent)
} catch (e) {
isValidJSON = false
}

test(`file '${file}': should be a valid JSON`, () => {
expect(isValidJSON).toBe(true)
})

const docReaderResponse = ProcessResponse.fromPlain(response)

test('should return non-empty array of images', async () => {
result = await getDocumentImages(docReaderResponse)
test(`file '${file}': should return non-empty array of images`, async() => {
let result: RDocumentImage[] = await getDocumentImages(docReaderResponse)

expect(result.length).toBeGreaterThan(0)
})
expect(result.length).toBeGreaterThan(0)
})

test('should return valid array of images', () => {
const isValid = RDocumentImage.isValid(result)
/*
test('should return valid array of images', () => {
const isValid = RDocumentImage.isValid(result)
expect(isValid).toBe(true)
})
expect(isValid).toBe(true)
})
*/
}
})
60 changes: 47 additions & 13 deletions src/recipes/image/graphic-field/get-document-front.recipe.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
import 'reflect-metadata'
import { readdirSync, readFileSync } from 'fs'
import { join } from 'path'
import { ProcessResponse } from '@regulaforensics/document-reader-typings'

import rawDocReaderResponse from '@/test-data/0.json'
import { RGraphicField } from './models'
import { getDocumentFront } from './get-document-front.recipe'


const DIRECTORY = String(process.env.PROCESS_RESPONSE_JSONS_DIR)

describe('getDocumentFront', () => {
let result: RGraphicField
const docReaderResponse = ProcessResponse.fromPlain(rawDocReaderResponse)
const files = readdirSync(DIRECTORY)

test('should be defined', async () => {
result = await getDocumentFront(docReaderResponse)
expect(result).toBeDefined()
})
files.forEach(async (file) => {
const filePath = join(DIRECTORY, file)

test('should return valid image', () => {
expect(result.src).toMatch(/^data:image\/jpeg;base64/)
})
if (!filePath.endsWith('.json')) {
return
}

const fileContent = readFileSync(filePath, 'utf-8')

let isValidJSON = true
let response = ''

try {
response = JSON.parse(fileContent)
} catch (e) {
isValidJSON = false
}

test(`file '${file}': should be a valid JSON`, () => {
expect(isValidJSON).toBe(true)
})

const docReaderResponse = ProcessResponse.fromPlain(response)


test(`file '${file}': should be defined`, async () => {
let result: RGraphicField = await getDocumentFront(docReaderResponse)

expect(result).toBeDefined()
})

test(`file '${file}': should return valid image`, async () => {
let result: RGraphicField = await getDocumentFront(docReaderResponse)

expect(result.src).toMatch(/^data:image\/jpeg;base64/)
})

test(`file '${file}': should return valid model`, async () => {
let result: RGraphicField = await getDocumentFront(docReaderResponse)

test('should return valid model', () => {
const isValid = RGraphicField.isValid(result)
expect(isValid).toBeTruthy()
const isValid = RGraphicField.isValid(result)
expect(isValid).toBeTruthy()
})
})
})
71 changes: 50 additions & 21 deletions src/recipes/image/graphic-field/get-graphic-field.recipe.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,68 @@
import { eGraphicFieldType, eLights, ProcessResponse } from '@regulaforensics/document-reader-typings'
import { readdirSync, readFileSync } from 'fs'
import { join } from 'path'

import rawDocReaderResponse from '@/test-data/0.json'
import { RGraphicField } from './models'
import { getGraphicField } from './get-graphic-field.recipe'


const DIRECTORY = String(process.env.PROCESS_RESPONSE_JSONS_DIR)

describe('getGraphicField', () => {
const docReaderResponse = ProcessResponse.fromPlain(rawDocReaderResponse)
let result: RGraphicField
const files = readdirSync(DIRECTORY)

test('should be defined', async () => {
result = await getGraphicField(docReaderResponse, eGraphicFieldType.DOCUMENT_FRONT)
files.forEach(async (file) => {
const filePath = join(DIRECTORY, file)

expect(result).toBeDefined()
})
if (!filePath.endsWith('.json')) {
return
}

test('should return valid image', async () => {
expect(result.src).toMatch(/^data:image\/jpeg;base64/)
})
const fileContent = readFileSync(filePath, 'utf-8')

test('should be able to return default image', async () => {
const result = await getGraphicField(docReaderResponse, eGraphicFieldType.DOCUMENT_FRONT, true, [eLights.HOLO])
let isValidJSON = true
let response = ''

expect(result.src).toMatch(/^data:image\/png;base64/)
})
try {
response = JSON.parse(fileContent)
} catch (e) {
isValidJSON = false
}

test('should return undefined if no image found', async () => {
const result = await getGraphicField(docReaderResponse, eGraphicFieldType.PORTRAIT, false, [eLights.HOLO])
test(`file '${file}': should be a valid JSON`, () => {
expect(isValidJSON).toBe(true)
})

expect(result).toBeUndefined()
})
const docReaderResponse = ProcessResponse.fromPlain(response)

let result: RGraphicField

test(`file '${file}': should be defined`, async () => {
result = await getGraphicField(docReaderResponse, eGraphicFieldType.DOCUMENT_FRONT)

expect(result).toBeDefined()
})

test(`file '${file}': should return valid image`, async () => {
expect(result.src).toMatch(/^data:image\/jpeg;base64/)
})

test(`file '${file}': should be able to return default image`, async () => {
const result = await getGraphicField(docReaderResponse, eGraphicFieldType.DOCUMENT_FRONT, true, [eLights.HOLO])

expect(result.src).toMatch(/^data:image\/png;base64/)
})

test(`file '${file}': should return undefined if no image found`, async () => {
const result = await getGraphicField(docReaderResponse, eGraphicFieldType.PORTRAIT, false, [eLights.HOLO])

expect(result).toBeUndefined()
})

test('should return valid model', async () => {
const isValid = RGraphicField.isValid(result)
test(`file '${file}': should return valid model`, async () => {
const isValid = RGraphicField.isValid(result)

expect(isValid).toBe(true)
expect(isValid).toBe(true)
})
})
})
Loading

0 comments on commit e95bb3b

Please sign in to comment.