diff --git a/.travis.yml b/.travis.yml index d2672c478..738e6067d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,4 +19,5 @@ script: - make nc-release - make tests - ./run-tests db-only shredding + - ./run-tests db-only relational-lenses - ./run-tests unit diff --git a/run-tests b/run-tests index 640fbb65d..acea6bdad 100755 --- a/run-tests +++ b/run-tests @@ -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 @@ -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 diff --git a/tests/relational-lenses/01_cds.links b/tests/relational-lenses/01_cds.links new file mode 100644 index 000000000..3339e6704 --- /dev/null +++ b/tests/relational-lenses/01_cds.links @@ -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() \ No newline at end of file diff --git a/tests/relational-lenses/02_cds.links b/tests/relational-lenses/02_cds.links new file mode 100644 index 000000000..f2c639ec8 --- /dev/null +++ b/tests/relational-lenses/02_cds.links @@ -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() diff --git a/tests/relational-lenses/config.sample b/tests/relational-lenses/config.sample new file mode 100644 index 000000000..2264904bd --- /dev/null +++ b/tests/relational-lenses/config.sample @@ -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 diff --git a/tests/relational-lenses/music_example.sql b/tests/relational-lenses/music_example.sql new file mode 100644 index 000000000..2656448e8 --- /dev/null +++ b/tests/relational-lenses/music_example.sql @@ -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'); + diff --git a/tests/relational-lenses/testsuite.config b/tests/relational-lenses/testsuite.config new file mode 100644 index 000000000..95a18c8c6 --- /dev/null +++ b/tests/relational-lenses/testsuite.config @@ -0,0 +1,2 @@ +01_cds +02_cds