Skip to content

Commit

Permalink
BootStrap and Domain Class Unit testing examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dierk Koenig committed Nov 4, 2023
1 parent 63624cb commit b7c5e3e
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 46 deletions.
6 changes: 0 additions & 6 deletions grails-app/controllers/rooms/AdminController.groovy

This file was deleted.

21 changes: 4 additions & 17 deletions grails-app/controllers/rooms/RoomController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@ class RoomController {

static scaffold = Room

def tryRoom() {
// List<Room> rooms = Room.list()
// List<Room> rooms = Room.listOrderByCapacity()

Admin hans = new Admin(name: "Hans", tel: "1234", cardNumber: "1234").save()
Admin grete = new Admin(name: "Grete", tel: "4321", cardNumber: "4321").save()

new Room(name: "5.1C56", capacity: 40, admin: hans).save()
// java style
// for (int i = 0; i < 10; i++) {
// new Room(name: "Room " + i, capacity: i * 5).save()
// }
// groovy style
10.times { n ->
new Room(name: "Room " + n, capacity: n * 5, admin: grete).save()
}
def tryRoom() { // experiment with dynamic finder methods

List<Room> rooms = Room.list()
// List<Room> rooms = Room.listOrderByCapacity()
// List<Room> rooms = Room.findAllByCapacityGreaterThan(30)
List<Room> rooms = Room.findAllByAdmin(grete)

String result = rooms.toString()
render text: result
}
Expand Down
18 changes: 0 additions & 18 deletions grails-app/domain/rooms/Admin.groovy

This file was deleted.

13 changes: 12 additions & 1 deletion grails-app/domain/rooms/Booking.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ package rooms

class Booking {

String purpose = "Lecture"
Person person
Room room
Date bookingDate
Date bookingDate // a date where we ignore the time and assume 00:00:00
String timeslot // one of AM, PM1, PM2

public static final String AM = "08:15 - 11:00"
public static final String PM1 = "12:15 - 15:00"
public static final String PM2 = "15:15 - 18:00"

static constraints = {
purpose ()
person nullable: false
room nullable: false
bookingDate nullable: false
timeslot nullable: false, inList: [AM, PM1, PM2]
}
}
6 changes: 2 additions & 4 deletions grails-app/domain/rooms/Room.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package rooms
class Room {
String name
int capacity
Admin admin

String toString() {
"$name ($capacity ${admin?.name})" // groovy style
// return name + " (" + capacity + ")"; // java style
"$name ($capacity)" // groovy style
// return name + " (" + capacity + ")"; // java style
}

static constraints = {
name nullable: false, blank: false
capacity min: 0
admin nullable: true
}
}
41 changes: 41 additions & 0 deletions src/main/resources/public/Jokes.html
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>
58 changes: 58 additions & 0 deletions src/main/resources/public/Pictures.html
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>
57 changes: 57 additions & 0 deletions src/test/groovy/rooms/BookingSpec.groovy
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
}
}
24 changes: 24 additions & 0 deletions src/test/groovy/rooms/PersonSpec.groovy
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
}
}
25 changes: 25 additions & 0 deletions src/test/groovy/rooms/RoomSpec.groovy
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
}
}

0 comments on commit b7c5e3e

Please sign in to comment.