linfo2335-programming-parad.../project-1/tests_dcg.pl

99 lines
2.8 KiB
Perl
Raw Normal View History

2024-03-01 13:22:27 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HELPERS
% plunit is a Prolog unit-test framework initially developed for SWI-Prolog
:- use_module(library(plunit)).
:- discontiguous test/2.
% Run the tests.
test_dcg :-
run_tests(dcg).
% 'clean' drop: call drop/1 and swallow any thrown exceptions
cdrop(Table) :-
catch(drop(Table), _, true).
% Checks that two lists have the same content.
same_content(L1, L2) :-
msort(L1, S),
msort(L2, S).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- begin_tests(dcg).
%%not tested because not specified.
%%test(query3) :-
%% query("SELECT +id FROM cities;", _/Z), !,
%% assertion(Z == []).
test(selec_with_selectors) :-
findall(Z,
query("SELECT +first, +age FROM persons;",
_/Z), Zs),
same_content(Zs, [
["Jeffrey",30],
["Lorena",50],
["Joseph",37],
["Stewart",83],
["Thomas",75],
["Tameka",25],
["Annette",40],
["David",36],
["Anthony",20],
["Lindsay",70],
["Claudette",32],
["Betty",47],
["Richard",47],
["Robert",69],
["Candace",61],
["Sandra",67]]).
test(selec_selectors_conditions) :-
findall(Z,
query("SELECT +state, +name FROM cities WHERE +temp>=20;",
_/Z), Zs),
same_content(Zs, [
["Florida", "Daytona Beach"],
["Texas", "Houston"],
["Florida", "Orlando"]]).
test(selec_multi_conditions) :-
findall(Z,
query(`SELECT +last FROM persons WHERE +city="Orlando", +age>30;`,
_/Z), Zs),
same_content(Zs, [["Smith"]]).
test(selec_all) :-
findall(Z,
query("SELECT * FROM cities;"
, _/Z), Zs),
same_content(Zs, [["Daytona Beach", "Florida", 22],
["Boardman", "Oregon", 12],
["Washington", "District of Columbia", 11],
["Grand Island", "New York", 10],
["Houston", "Texas", 20],
["Orlando", "Florida", 23],
["Dallas", "Texas", 17],
["Pittsburgh", "Pennsylvania", 11],
["Atlanta", "Georgia", 16]]).
test(selec_after_insert, cleanup(delete(cities, [+name="Tempa"]))) :-
query(`INSERT INTO cities (+name, +state) VALUES ("Tempa", "Florida");`),
findall(Z,
query("SELECT * FROM cities;"
, _/Z), Zs),
same_content(Zs, [["Daytona Beach", "Florida", 22],
["Boardman", "Oregon", 12],
["Washington", "District of Columbia", 11],
["Grand Island", "New York", 10],
["Houston", "Texas", 20],
["Orlando", "Florida", 23],
["Dallas", "Texas", 17],
["Pittsburgh", "Pennsylvania", 11],
["Atlanta", "Georgia", 16],
["Tempa", "Florida", _]]).
:- end_tests(dcg).