const { Restaurant } = require('../src/Restaurant')
const { Database } = require('sqlite3')
const db = new Database(':memory:');
describe('Restaurants', () => {
beforeAll((done) => {
db.run('CREATE TABLE IF NOT EXISTS restaurants(id INTEGER PRIMARY KEY, name TEXT, image TEXT);', ()=> {
done();
})
Restaurant.setDatabase(db);
})
afterAll((done) => {
db.close();
})
test('A Restaurant can be persisted in the database', async(done) => {
const restaurant = new Restaurant('Boo Jangles', 'https://some.image.url')
await restaurant.save();
expect(restaurant.id).toBeGreaterThan(0)
done()
})
test('A Restaurant can found in the database', async(done) => {
const restaurant = new Restaurant('Boo Jangles', 'https://some.image.url')
const savedRestaurant = await restaurant.save();
let foundRestaurant = await Restaurant.findById(savedRestaurant.id);
expect(foundRestaurant.id).toBeGreaterThan(0)
done()
})
test('Multiple Restaurants can found in the database', async(done) => {
await Restaurant.deleteAll();
let restaurant = new Restaurant('Boo Jangles', 'https://some.image.url')
await restaurant.save();
restaurant = new Restaurant('Nandos', 'https://some.otherimage.url')
await restaurant.save();
let foundRestaurants = await Restaurant.findAll();
expect(foundRestaurants.length).toBe(2)
done()
})
})
public class RestaurantTest {
private static Jdbi jdbi = Jdbi.create("jdbc:sqlite:restaurants.sqlite");
@BeforeClass
public static void setup() {
jdbi.useHandle(handle -> {
handle.execute("CREATE TABLE IF NOT EXISTS RESTAURANTS (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, imagelink TEXT)");
});
}
@Test
public void save() {
Restaurant restaurant = new Restaurant("Boo Jangles", "https://some.image.url");
Restaurant savedRestaurant = restaurant.save();
assertTrue(savedRestaurant.getId() > 0);
}
@Test
public void findById() {
Restaurant restaurant = new Restaurant("Boo Jangles", "https://some.image.url");
Restaurant savedRestaurant = restaurant.save();
Restaurant foundRestaurant = Restaurant.findById(savedRestaurant.getId());
assertTrue(foundRestaurant.getId() > 0);
}
@Test
public void findAll() {
Restaurant.deleteAll();
Restaurant restaurant = new Restaurant("Boo Jangles", "https://some.image.url");
Restaurant savedRestaurant = restaurant.save();
restaurant = new Restaurant("Nandos", "https://some.otherimage.url");
savedRestaurant = restaurant.save();
List<Restaurant> foundRestaurants = Restaurant.findAll();
assertTrue(foundRestaurants.size() == 2);
}
}