From: Will Budic Date: Thu, 18 Sep 2025 02:00:28 +0000 (+1000) Subject: upd. X-Git-Url: https://lifelog.hopto.org/gitweb/?a=commitdiff_plain;h=64782f5f9927eec20e5303db788cbcafbb4cdae9;p=PerlCNF.git upd. --- diff --git a/tests/testSQL map_macro.pl b/tests/testSQL map_macro.pl new file mode 100644 index 0000000..5df5440 --- /dev/null +++ b/tests/testSQL map_macro.pl @@ -0,0 +1,113 @@ +#!/usr/bin/env perl +use warnings; use strict; +use Syntax::Keyword::Try; +use Benchmark; + +use lib "tests"; +use lib '/home/will/dev_new/PerlCNF/system/modules'; +# use lib "system/modules"; + + +require TestManager; +require CNFParser; +require CNFSQL; + +my $test = TestManager -> new($0); +my $cnf; + + `rm -f tests/test_db_central.db`; + +try{ + + die $test->failed() if not $cnf = CNFParser->new(); + $test->case("Passed new instance CNFParser."); + $test->subcase('CNFParser->VERSION is '.CNFParser->VERSION); + # + $test->nextCase(); + # + + + $test->case("Test CNFSQL script to data synch and map."); + $cnf = CNFParser->new(undef,{DO_ENABLED=>1,DEBUG=>1,'%LOG'=>{console=>1}}); + ### + # Here the table is mapped to CNF data property that is auto-numbered. + # But the table itself is not reflected to this so inserts and updates are different. + # Then directly making an SQL table from an CNF Data header. + $cnf->parse(undef,q( + + <<>> + + + + < _MAP_TO_ TBL_A_DATA + "name" varchar(28) NOT NULL, + "ID" INTEGER NOT NULL, + "Gender" varchar(10), + PRIMARY KEY ("ID") + >> + < __HAS_HEADER__ __AUTONUMBER__ + ID`NAME`Gender~ + #`Mickey Mouse`rat~ + 5`Donald Duck`food~ + #`Captain Cook`crook~ + >> + )); + # + $test->nextCase(); + # + + + my $db = CNFSQL::_connectDB('test','test',$cnf->{DB_SQL_SOURCE},$cnf->{DB}); + my $sql = $cnf->SQL(); + $sql -> {data } = $sql->{parser}->data(); + $sql -> initDatabase($db,0); + $db -> disconnect(); + + # + $test->nextCase(); + # + + $test->case("Test db view to existing database."); + $cnf = CNFParser->new(undef,{DO_ENABLED=>1,DEBUG=>1,'%LOG'=>{console=>1}}); + $cnf->parse(undef,qq( +< + DB = tests/test_db_central.db + DB_CREDENTIALS = admin/admin + DB_SQL_SOURCE = DBI:SQLite: + package : DatabaseCentralPlugin + subroutine : executeStatements + property : VIEW_TBL_A +>> + +# This is the table spec for an unknown to this script physical SQL database table. +# Its selection statement is mapped with __MAP_CNF_DB_VIEW__ VIEW_TBL_A, without this meta instruction +# auto resolving will not work. +# +< __MAP_CNF_DB_VIEW__ VIEW_TBL_A +ID _INT_`NAME _TEXT_~ +>> + +# It is more readable not to map the above to an CNF instruction. +# The following statement can be both table and a view, which is recommended. +# +<select ID, Name from TBL_A;>> +)); + my $central = $cnf->property('DB_CENTRAL'); + my $view = $cnf->data()->{VIEW_TBL_A}; + my @data = @{$$view->{data}}; + $test->evaluate("Mapped view has selected three records?",3,scalar @data); + $test->evaluate("Mapped view has record 5 -Donald Duck at index 2?",'Donald Duck', $data[2][1]); #<-view is ID-0, Name is 1. +# +# +$test->done(); +# +} +catch{ + $test -> dumpTermination($@); + $test -> doneFailed(); +} diff --git a/tests/testSQL.pl b/tests/testSQL.pl index e557843..4966c3b 100644 --- a/tests/testSQL.pl +++ b/tests/testSQL.pl @@ -3,7 +3,7 @@ use warnings; use strict; use Syntax::Keyword::Try; use Benchmark; -use lib::relative 'system/modules'; +# use lib::relative 'system/modules'; use lib "tests"; diff --git a/tests/testSQLPostgres_on_elite.pl b/tests/testSQLPostgres_on_elite.pl new file mode 100644 index 0000000..5337c05 --- /dev/null +++ b/tests/testSQLPostgres_on_elite.pl @@ -0,0 +1,126 @@ +#!/usr/bin/env perl +use warnings; use strict; +use Syntax::Keyword::Try; +use Benchmark; +use lib "tests"; +use lib '/home/will/dev_new/PerlCNF/system/modules'; +# use lib "system/modules"; + +require TestManager; +require CNFParser; +require CNFSQL; + +my $test = TestManager -> new($0); +my $cnf; +my $DB_SETTINGS = qq( +<<>> +); +try{ + + die $test->failed() if not $cnf = CNFParser->new(undef,{'%LOG'=>{console=>1}}); + $test->case("Passed new instance CNFParser."); + # + our $sql; + $cnf->parse(undef,$DB_SETTINGS); + my($user,$pass,$source,$store)=(CNFSQL::_credentialsToArray($cnf->{DB_CREDENTIALS}),$cnf->{DB_SQL_SOURCE},$cnf->{DB}); + our ($db,$test_further) = (undef,1); + try{ + $db = CNFSQL::_connectDB($user,$pass,$source,$store); + }catch($e){ + $test_further = 0; + $test->unsuited()->passed("Skipping further testing unable to connect to <<{DB_SQL_SOURCE} >>> \n$e"); + $test->done(); + exit; + + } + if($test_further){ + $test->case("initDatabase"); + my $content = do {local $/;}; + $cnf->parse(undef, $content); + + $cnf->SQL()->initDatabase($db); + } + + my %schema = $cnf->SQL()->schema(); + + if(!$schema{CMD_HISTORY}){ + my $tbl_cmd_history_statement = $cnf->SQL()->{CMD_HISTORY}; + $test->fail("SQL Statement for CMD_HISTORY"); + $db ->do($tbl_cmd_history_statement); + }else{ + $cnf->warn('Skipping SQL Statement for CMD_HISTORY, table exists!'); + } + # + $test->done(); + # +} +catch{ + $test -> dumpTermination($@); + $test -> doneFailed(); +} + + +=begin postgreSQL setup +$> psql -U postgres -p 5433 -h localhost + +CREATE USER admin SUPERUSER LOGIN PASSWORD 'admin'; +CREATE DATABASE test_db_central; +grant all privileges on database test_db_central to admin; +=cut + +__DATA__ +!CNF3.0 +// The spaced out data column header and its meta type settings are spaced out, +// bellow for your readability, this is allowed in CNF. not in any other scripted data format. +<< TASKS __SQL_TABLE__ __SQL_PostgreSQL__ +ID _CNF_ID_ + `Date _DATE_ + `Due _DATE_ + `Task _TEXT_ + `Completed _BOOL_ + `Priority _ID__~ +#`2023-10-18`2023-11-22`Write test.`0`1~ +#`2023-10-18`2023-12-01`Implement HSHContact.`0`5~ +#`2023-12-1`2023-12-21`Deploy HSHContact.`0`5~ +>> + +<< TASKS_AUTO_VARIANT __SQL_TABLE__ __SQL_PostgreSQL__ +ID`Date _DATE_`Due _DATE_ + `Task _TEXT_ + `Completed _BOOL_ + `Priority _ID__~ +#`2023-10-18`2023-11-22`Write test.`0`1~ +#`2023-10-18`2023-12-01`Implement HSHContact.`0`5~ +#`2023-12-1`2023-12-21`Deploy HSHContact.`0`5~ +>> + +< + +CREATE TABLE IF NOT EXISTS public.cmd_history +( + "ID" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ), + "DATE" timestamp without time zone, + "CMD" character varying(2024) COLLATE pg_catalog."default" NOT NULL, + "COUNT" integer, + CONSTRAINT cmd_history_pkey PRIMARY KEY ("ID"), + CONSTRAINT "cmd_history_CMD_key" UNIQUE ("CMD") +) + +WITH ( + autovacuum_enabled = TRUE +) +TABLESPACE pg_default; +CREATE INDEX idx_cmd_history_cmd ON public.cmd_history ("CMD"); + + + +ALTER TABLE IF EXISTS public.cmd_history + OWNER to tester; + +GRANT ALL ON TABLE public.cmd_history TO tester; + +>> \ No newline at end of file