/*
Unit testing
*/
const assert = require("assert");
const fs=require('fs-extra');
const path= require('path');
const Odmdb = require("../Odmdb.js");
const {generemdp} = require('../../nationchains/socialworld/contracts/toolsbox.js');

const ut = { name: "Odmdb" };
/*
We test only search and indexation here
Create Update Read and Delete are unit testing with specificities of each Object.

To do that we create in tmp a dummy data folder for a dummy schema object 
*/
const schema = {
    $schema: "http://json-schema.org/schema#",
    title: "Dummy schema to test Checkjson.js",
    description: "Checkjson is use on server as well as into a browser",
    $comment: "We change schema type on the fly to simplify the test",
    type: "object",
    properties: {
      uuid: {
        type:"string",
        format:"uuid",
        default:"=uuid.v4()"
      },
      dtcreate:{
        type:"string",
        format:"datetime",
        default:"=date.now()"
      },
      tag:{
        type:"string",
        enum:["t1","t2","t3"],
        default:"t1"
      },
      info:{
        type:"string",
        minLength: 10,
        default:"=generemdp(255,'ABCDEFGHIJKLM 12340')"
      }
    },
    required:["uuid"],
    apxprimarykey:"uuid",
    apxuniquekey:["info"],
    apxsearchindex:{
        "uuid":{"list":[],"taginfo":['tag','info'],"all":""},
        "info":{"uuid":['uuid']}
    }
  };    

  const obj={tag:"t1",info:"Lorem ipsum A"}

ut.createanobject=(schema,obj)=>{

    const res={status:200,err:[]}
    return res
} 

ut.run = (options) => { 
    const objectPath=path.resolve(__dirname,'../../tmp/testobjects');
    const schemaPath=path.resolve(__dirname,'../../tmp/testschema');
    if (!fs.existsSync(objectPath)) fs.ensureDirSync(objectPath);
    if (!fs.existsSync(schemaPath)) fs.ensureDirSync(schemaPath);
    const createenvobj=Odmdb.setObject(schemaPath,objectPath,"objtest",schema,{},"en");
    assert.deepEqual(createenvobj,{status:200},JSON.stringify(createenvobj));
    const checkschema= Odmdb.schema(schemaPath,"objtest",true)
    assert.deepEqual(checkschema.status,200,JSON.stringify(checkschema))
  };
module.exports = ut;