-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BootStrap and Domain Class Unit testing examples.
- Loading branch information
Dierk Koenig
committed
Nov 4, 2023
1 parent
63624cb
commit b7c5e3e
Showing
10 changed files
with
223 additions
and
46 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<title> | ||
Jokes | ||
</title> | ||
<script> | ||
function getJokes() { | ||
const url = "https://sv443.net/jokeapi/v2/joke/Programming"; | ||
fetch(url) | ||
.then( resp => resp.json() ) | ||
.then( json => { | ||
console.log(json); | ||
let text = json.joke; | ||
if ("twopart" === json.type) { // Yoda conditional | ||
text = json.setup + " - "+ json.delivery; | ||
} | ||
document.getElementById("target").textContent = text ; | ||
}) | ||
.catch( err => console.log(err)); | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<h1> | ||
Programmer jokes from <a href="https://sv443.net/jokeapi">jokeapi</a> | ||
</h1> | ||
|
||
<button onclick="getJokes();">Next</button> | ||
|
||
<div id="target"> | ||
fetching ... | ||
</div> | ||
|
||
<script> | ||
getJokes(); // initial load | ||
</script> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<title> | ||
Pictures | ||
</title> | ||
<script> | ||
let photoNumber = 0; | ||
let pageNumber = 1; | ||
let photoInfos = undefined; | ||
|
||
function nextPhoto() { | ||
if (undefined === photoInfos) { | ||
findInterestingImages(); // mutually recursive | ||
return; | ||
} | ||
const photo = photoInfos[photoNumber++]; | ||
if (photoNumber > 10) { | ||
// todo: what to do about the page number here? | ||
photoNumber = 0; | ||
findInterestingImages(); | ||
return; | ||
} | ||
const pictureUrl = "http://static.flickr.com/" + | ||
photo.server + "/" + | ||
photo.id + "_" + | ||
photo.secret + "_m.jpg"; | ||
document.getElementById("target").innerHTML = "<img src='" + | ||
pictureUrl + "'><div> " + | ||
photo.title + "</div>"; | ||
} | ||
|
||
function findInterestingImages() { | ||
const url = "https://www.flickr.com/services/rest/?method=flickr.interestingness.getList&per_page=10&page=" + | ||
pageNumber + "&format=json&nojsoncallback=1&api_key=d5d6d15bb498e649b134170184968b61"; | ||
fetch(url) | ||
.then( resp => resp.json() ) | ||
.then( json => { | ||
photoInfos = json.photos.photo; | ||
nextPhoto(); | ||
}) | ||
.catch( err => console.log(err)); | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<h1> | ||
Pictures | ||
</h1> | ||
|
||
<div id="target" onclick="nextPhoto()"> | ||
Click me to find interesting images on flickr. | ||
</div> | ||
|
||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package rooms | ||
|
||
import grails.testing.gorm.DomainUnitTest | ||
import spock.lang.Specification | ||
|
||
import static rooms.BootStrap.today | ||
|
||
class BookingSpec extends Specification implements DomainUnitTest<Booking> { | ||
Person john | ||
Room room_1 | ||
Booking booking_1 | ||
|
||
def setup() { | ||
john = new Person(firstName: "John", lastName: "Smith").save() | ||
room_1 = new Room(name: "Room 1", capacity: 10).save() | ||
booking_1 = new Booking( | ||
person: john, | ||
room: room_1, | ||
bookingDate: today, | ||
timeslot: Booking.AM) | ||
.save() | ||
} | ||
|
||
def cleanup() { | ||
john.delete() | ||
room_1.delete() | ||
booking_1.delete() | ||
} | ||
|
||
void "test constructor"() { | ||
when:"sample data is loaded" | ||
def bookings = Booking.list() | ||
|
||
then:"there is a booking" | ||
bookings.size() == 1 | ||
} | ||
|
||
void "simple finder methods"() { | ||
expect: | ||
[booking_1] == Booking.findAllByPerson(john) | ||
[booking_1] == Booking.findAllByRoom(room_1) | ||
[booking_1] == Booking.findAllByBookingDate(today) | ||
[booking_1] == Booking.findAllByTimeslot(Booking.AM) | ||
[] == Booking.findAllByTimeslot(Booking.PM1) | ||
"Lecture" == Booking.findAllByPurpose("Lecture").first().purpose | ||
} | ||
|
||
void "find all names of all rooms booked by john"() { | ||
expect: | ||
["Room 1"] == Booking.findAllByPerson(john).room.name | ||
} | ||
|
||
void "find the first names of all people that booked rooms today or in the future"() { | ||
expect: | ||
["John"] == Booking.findAllByBookingDateGreaterThanEquals(today).person.firstName | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package rooms | ||
|
||
import grails.testing.gorm.DomainUnitTest | ||
import spock.lang.Specification | ||
|
||
class PersonSpec extends Specification implements DomainUnitTest<Person> { | ||
|
||
def setup() { | ||
} | ||
|
||
def cleanup() { | ||
} | ||
|
||
void "test constructor"() { | ||
given : | ||
new Person(firstName: "John", lastName: "Smith").save() | ||
new Person(firstName: "Jane", lastName: "Doe").save() | ||
when:"sample data is loaded" | ||
def people = Person.list() | ||
|
||
then:"there are 2 people" | ||
people.size() == 2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package rooms | ||
|
||
import grails.testing.gorm.DomainUnitTest | ||
import spock.lang.Specification | ||
|
||
class RoomSpec extends Specification implements DomainUnitTest<Room> { | ||
|
||
def setup() { | ||
} | ||
|
||
def cleanup() { | ||
} | ||
|
||
void "test constructor"() { | ||
given: | ||
new Room(name: "Room 1", capacity: 10).save() | ||
new Room(name: "Room 2", capacity: 20).save() | ||
new Room(name: "Room 3", capacity: 30).save() | ||
when:"sample data is loaded" | ||
def rooms = Room.list() | ||
|
||
then:"there are 3 rooms" | ||
rooms.size() == 3 | ||
} | ||
} |