Skip to content

Commit

Permalink
Merge pull request #7 from dgodongm/dg_initial_faker_integration
Browse files Browse the repository at this point in the history
Dg initial faker integration
  • Loading branch information
dgodongm authored Feb 3, 2024
2 parents e5866fe + c3e56e8 commit c0ef17d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
16 changes: 16 additions & 0 deletions cypress/e2e/create_bookings.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ describe("Create Bookings spec", () => {
});
});

it("create valid booking with faker generated data", () => {
let newbooking = bookings_generator.generate_booking(true);
bookings_wrapper.create_booking(newbooking).then((response) => {
expect(response.status).to.be.equal(200);
expect(response.body.booking.firstname).to.be.deep.equal(
newbooking.firstname,
);
expect(response.body).to.have.property("bookingid");
cy.log("returned bookingid: " + response.body.bookingid);
bookings_wrapper
.get_booking(response.body.bookingid)
.its("status")
.should("equal", 200);
});
});

it("First name is null should have error response", () => {
let newbooking = bookings_generator.generate_booking();
newbooking.firstname = null;
Expand Down
59 changes: 39 additions & 20 deletions cypress/utils/bookings_generator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bookings_helpers from "../utils/bookings_helpers";
import { faker } from "@faker-js/faker";

let firstNames = [
"Emily",
Expand Down Expand Up @@ -49,30 +50,46 @@ let lastNames = [
let needs = ["breakfast", "lunch", "early checkin", "late checkout", null];

const bookings_generator = {
generate_firstname: function () {
return firstNames[Cypress._.random(firstNames.length - 1)];
generate_firstname: function (useFaker = false) {
return useFaker
? faker.person.firstName()
: firstNames[Cypress._.random(firstNames.length - 1)];
},

generate_lastname: function () {
return lastNames[Cypress._.random(lastNames.length - 1)];
generate_lastname: function (useFaker = false) {
return useFaker
? faker.person.lastName()
: lastNames[Cypress._.random(lastNames.length - 1)];
},

generate_price: function () {
return Cypress._.random(50, 250);
generate_price: function (useFaker = false) {
return useFaker
? faker.number.int({ min: 50, max: 250 })
: Cypress._.random(50, 250);
},

generate_boolean: function () {
return Cypress._.random(1) === 1;
generate_boolean: function (useFaker = false) {
return useFaker ? faker.datatype.boolean() : Cypress._.random(1) === 1;
},

generate_bookingdates: function () {
generate_bookingdates: function (useFaker = false) {
const checkin = new Date();
checkin.setDate(checkin.getDate() + Cypress._.random(1, 180));
checkin.setDate(
checkin.getDate() +
(useFaker
? faker.number.int({ min: 1, max: 180 })
: Cypress._.random(1, 180)),
);
cy.log("checkin: " + checkin.toDateString());
let checkinString = bookings_helpers.convertToBookingDateString(checkin);
cy.log("checkinString: " + checkinString);
const checkout = new Date(
checkin.setDate(checkin.getDate() + Cypress._.random(1, 14)),
checkin.setDate(
checkin.getDate() +
(useFaker
? faker.number.int({ min: 1, max: 14 })
: Cypress._.random(1, 14)),
),
);
cy.log("checkout: " + checkout.toDateString());
let checkoutString = bookings_helpers.convertToBookingDateString(checkout);
Expand All @@ -85,18 +102,20 @@ const bookings_generator = {
return bookingdates;
},

generate_additionalneeds: function () {
return needs[Cypress._.random(needs.length - 1)];
generate_additionalneeds: function (useFaker = false) {
return useFaker
? faker.helpers.arrayElement(needs)
: needs[Cypress._.random(needs.length - 1)];
},

generate_booking: function () {
generate_booking: function (useFaker = false) {
const booking = {
firstname: this.generate_firstname(),
lastname: this.generate_lastname(),
depositpaid: this.generate_boolean(),
totalprice: this.generate_price(),
bookingdates: this.generate_bookingdates(),
additionalneeds: this.generate_additionalneeds(),
firstname: this.generate_firstname(useFaker),
lastname: this.generate_lastname(useFaker),
depositpaid: this.generate_boolean(useFaker),
totalprice: this.generate_price(useFaker),
bookingdates: this.generate_bookingdates(useFaker),
additionalneeds: this.generate_additionalneeds(useFaker),
};
return booking;
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"xml2js": "^0.4.23"
},
"devDependencies": {
"@faker-js/faker": "^8.4.0",
"chai": "^4.3.7",
"cypress": "^13.6.2",
"eslint": "^8.56.0",
Expand Down

0 comments on commit c0ef17d

Please sign in to comment.