Skip to content

Commit

Permalink
Relational Lens Tests (#482)
Browse files Browse the repository at this point in the history
* added relational lenses full tests

* added travis config to run relational lens tests
  • Loading branch information
rudihorn authored and dhil committed Feb 21, 2019
1 parent ab04a9c commit a5b110b
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ script:
- make nc-release
- make tests
- ./run-tests db-only shredding
- ./run-tests db-only relational-lenses
- ./run-tests unit
6 changes: 6 additions & 0 deletions run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

DATABASE_TEST_DIR=tests/database # Location of database tests
SHREDDING_TEST_DIR=tests/shredding # Location of database tests
RELATIONAL_LENSES_TEST_DIR=tests/relational-lenses # Location of database tests
ret=0 # Return code. If <> 0 then some tests failed
STARTCOLOR="\e[31m" # Control sequence to enable red text
ENDCOLOR="\e[0m" # Control sequence to enable normal text
Expand Down Expand Up @@ -118,6 +119,11 @@ if [ "$1" == "shredding" -o "$2" == "shredding" ]; then
run_database_testsuite $SHREDDING_TEST_DIR
fi

# Run relational lenses tests only if user called for them explicitly
if [ "$1" == "relational-lenses" -o "$2" == "relational-lenses" ]; then
run_database_testsuite $RELATIONAL_LENSES_TEST_DIR
fi

#look for custom test scripts in the tests folder and execute them
#they must be called *.testscript
for s in tests/*.testscript; do
Expand Down
61 changes: 61 additions & 0 deletions tests/relational-lenses/01_cds.links
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var db = database "links";

var albumsTable =
table "albums"
with (album: String, quantity: Int)
tablekeys [["album"]]
from db;

var tracksTable =
table "tracks"
with (track: String, date: Int, rating: Int, album: String)
tablekeys [["track", "album"]]
from db;

var albumsLens = lens albumsTable with { album -> quantity };
var tracksLens = lens tracksTable with { track -> date rating };

var joinedLens = lensjoin albumsLens with tracksLens on album delete_left;
var droppedLens = lensdrop date determined by track default 2018 from joinedLens;
var filteredLens = lensselect from droppedLens by quantity > 2;



fun test() {
var filtered = lensget filteredLens;

assertEq(filtered, [
(album = "Wish", quantity = 5, rating = 4, track = "Trust"),
(album = "Paris", quantity = 4, rating = 5, track = "Lovesong"),
(album = "Show", quantity = 3, rating = 3, track = "Lullaby")
]);

# filter out all tracks named "Trust" and change Lullaby's rating to 4.
var newTracks = [
(album = "Show", quantity = 3, rating = 4, track = "Lullaby"),
(album = "Disintegration", quantity = 7, rating = 5, track = "Lovesong")
];

lensput filteredLens with newTracks;

var new = lensget filteredLens;
assertEq(new, newTracks);

assertEq(lensget tracksLens, [
(album = "Show", date = 1989, rating = 4, track = "Lullaby"),
(album = "Galore", date = 1989, rating = 4, track = "Lullaby"),
(album = "Disintegration", date = 1989, rating = 5, track = "Lovesong"),
(album = "Galore", date = 1989, rating = 5, track = "Lovesong")
]);
assertEq(lensget albumsLens, [
(album = "Disintegration", quantity = 7),
(album = "Wish", quantity = 5),
(album = "Paris", quantity = 4),
(album = "Galore", quantity = 1),
(album = "Show", quantity = 3)
]);

lensput filteredLens with filtered;
}

test()
56 changes: 56 additions & 0 deletions tests/relational-lenses/02_cds.links
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var db = database "links";

var albumsTable =
table "albums"
with (album: String, quantity: Int)
tablekeys [["album"]]
from db;

var tracksTable =
table "tracks"
with (track: String, date: Int, rating: Int, album: String)
tablekeys [["track", "album"]]
from db;

var albumsLens = lens albumsTable with { album -> quantity };
var tracksLens = lens tracksTable with { track -> date rating };

var joinedLens = lensjoin albumsLens with tracksLens on album delete_left;

fun test() {
var old = lensget joinedLens;

var newTracks = [
(album = "Wish", date = 1992, quantity = 5, rating = 4, track = "Trust"),
(track="It's the end of the world as we know it",
rating=5, album="Eponymous", date=1988, quantity=42),
(album = "Show", date = 1989, quantity = 3, rating = 3, track = "Lullaby"),
(album = "Galore", date = 1989, quantity = 1, rating = 3, track = "Lullaby"),
(album = "Paris", date = 1989, quantity = 4, rating = 5, track = "Lovesong"),
(album = "Galore", date = 1989, quantity = 1, rating = 5, track = "Lovesong")
];

lensput joinedLens with newTracks;

assertEq(lensget joinedLens, newTracks);
assertEq(lensget albumsLens, [
(album = "Eponymous", quantity = 42),
(album = "Disintegration", quantity = 7),
(album = "Wish", quantity = 5),
(album = "Paris", quantity = 4),
(album = "Galore", quantity = 1),
(album = "Show", quantity = 3)
]);
assertEq(lensget tracksLens, [
(album = "Wish", date = 1992, rating = 4, track = "Trust"),
(album = "Eponymous", date = 1988, rating = 5, track = "It's the end of the world as we know it"),
(album = "Show", date = 1989, rating = 3, track = "Lullaby"),
(album = "Galore", date = 1989, rating = 3, track = "Lullaby"),
(album = "Paris", date = 1989, rating = 5, track = "Lovesong"),
(album = "Galore", date = 1989, rating = 5, track = "Lovesong")
]);

lensput joinedLens with old;
}

test()
5 changes: 5 additions & 0 deletions tests/relational-lenses/config.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
database_driver=postgresql
database_args=localhost:5432::links
show_pre_sugar_typing=off
relax_query_type_constraint=on
relational_lenses=on
32 changes: 32 additions & 0 deletions tests/relational-lenses/music_example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
DROP TABLE IF EXISTS Albums;

CREATE TABLE Albums (
Album VARCHAR(50) NOT NULL,
Quantity INT NOT NULL,
PRIMARY KEY (Album)
);

DROP TABLE IF EXISTS Tracks;

CREATE TABLE Tracks (
Track VARCHAR(50) NOT NULL,
Date INT NOT NULL,
Rating INT NOT NULL,
Album VARCHAR(50) NOT NULL,
PRIMARY KEY (Track, Album)
);

INSERT INTO Albums (Album, Quantity) VALUES
('Disintegration', 6),
('Show', 3),
('Galore', 1),
('Paris', 4),
('Wish', 5);

INSERT INTO Tracks (Track, Date, Rating, Album) VALUES
('Lullaby', 1989, 3, 'Galore'),
('Lullaby', 1989, 3, 'Show'),
('Lovesong', 1989, 5, 'Galore'),
('Lovesong', 1989, 5, 'Paris'),
('Trust', 1992, 4, 'Wish');

2 changes: 2 additions & 0 deletions tests/relational-lenses/testsuite.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
01_cds
02_cds

0 comments on commit a5b110b

Please sign in to comment.