Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrates https://github.com/DickinsonCollege/FD2School-FarmData2/pull/209 #673

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* The tests in this file illustrate how to get seeding logs from
* the database using the functions in the FarmOSAPI library and then
* make assertions about their contents.
*/

/*
* The dayjs library can be imported into a test using the following
* line. This is particularly useful for conversions between dates
* and time stamps.
*/
const dayjs = require('dayjs')

var FarmOSAPI = require("../../resources/FarmOSAPI.js")
var getRecord = FarmOSAPI.getRecord
var getAllPages = FarmOSAPI.getAllPages

describe("Examples of getting seeding logs via the FarmOSAPI functions in a test", () => {

beforeEach(() => {
cy.login("manager1", "farmdata2")

/*
* Load any maps (see dbtest.maps.spec) or get a session token
* (see dbtest.sessionToken.spec.js) as necessary here.
*/

cy.visit("/farm/fd2-example/dbTest")
cy.waitForPage()
})

/**
* If you have the id of a log it can be retrieved using the getRecord
* function from the FarmOSAPI library.
*/
it("Get a seeding log by its id", () => {

/*
* Request the log with id=6. This is a direct seeding of Radish in
* CHUAU-2 on February 04, 2019.
*/
cy.wrap(getRecord("/log.json?id=6")).as("get-log")

/*
* Wait for the promise returned from getRecord to resolve.
*/
cy.get("@get-log")
.then((response) => {

/*
* Once the response is received we can make assertions about the values
* in the response to verify that it is as expected.
*
* To know the structure and content of the log it is helpful to access
* the URL in a browser or through a tool such as Hoppscotch or postman
* and inspect the JSON directly.
*/
expect(response.data.list[0].movement.area[0].name).to.equal("CHUAU-2")
expect(response.data.list[0].type).to.equal("farm_seeding")
expect(response.data.list[0].log_category[0].name).to.equal("Direct Seedings")
})
})

/**
* If you want all of the logs in a date range, they can be retrieved
* using the getAllPAges function from the FarmOSAPI library.
*/
it("Get all of the seeding logs in a date range", () => {

// Get the start and end timestamps for the date range we want.
let start = dayjs("2020-05-01","YYYY-MM-DD").unix()
// Add 1 day here to get to the end of May 5th.
let end = dayjs("2020-05-05","YYYY-MM-DD").add(1,'day').unix()

/*
* Request all of the farm seedings between 2020-05-01 and 2020-05-05. There
* were 14 seeding in this date range.
*/
let url = "/log.json?type=farm_seeding&timestamp[gt]="+start+"&timestamp[lt]="+end
let seedingLogs = []
cy.wrap(getAllPages(url, seedingLogs)).as("get-logs")

/*
* Wait for the promise returned from getAllPages to resolve.
*/
cy.get("@get-logs")
.then((response) => {

/*
* Once the response is received we can make assertions about the values
* in the array that was filled with the results to verify that they are
* as expected.
*
* To know the structure and content of the array it is helpful to access
* the URL in a browser or through a tool such as Hoppscotch or postman
* and inspect the JSON directly.
*/

expect(seedingLogs.length).to.equal(14)
})
})
})
48 changes: 48 additions & 0 deletions farmdata2/farmdata2_modules/fd2_example/dbtest/dbtest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<div id="dbtest" v-cloak>

<p>Unlike other sample modules, this sample module does not have content here.
Instead, the dbTest sample module consists of a number of <code>.spec.js</code>
files that illustrate common patterns used in tests that
interact with the database. The contents of those files should be reviewed
their comments read and studied. In addition, the tests can be run in
the Cypress Test environment.</p>

<p>Each of the files has comments describing what they are doing. It is recommended
to study the files in the order below as later examples may not include comments
describing things that are documented in an earlier example.</p>
<ol>
<li>dbtest.maps.spec.js</li>: Shows how to get maps (e.g. userToIDMap) from the database and use them in tests.
<li>dbtest.get.seedings.js</li>: Shows how to get seeding logs from the database and use them in tests.
<li>dbtest.sessionToken.spec.js</li>: Shows how to get a session token needed for operations that modify the database.
<li>dbtest.modify.seedings.spec.js</li>: Shows how to create, delete and update seeding logs in the database.
</ol>

<!-- See maps.html for description of this line.
While not technically needed in this page it is included
here so that the example tests are more similar to those
that will be created in practice.
-->
<div data-cy="page-loaded" v-show=false>{{ pageLoaded }}</div>
</div>

<script>
new Vue({
el: '#dbtest', // el: must match the id of the <div> for the app above.
data: {
// Provide easy access to the FarmDat2 variables in vue.
userID: fd2UserID,
userName: fd2UserName,

createdCount: 0, // used in created and pageLoaded
},
computed: {
pageLoaded() {
// Check here that the correct number of API calls have completed.
return this.createdCount == 1
},
},
created() {
this.createdCount++
}
})
</script>
106 changes: 106 additions & 0 deletions farmdata2/farmdata2_modules/fd2_example/dbtest/dbtest.maps.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* The tests in this file show how to get a map from the FarmOSAPI and
* use it in tests. This can be useful when the one of the maps is
* needed to translate between ids and names in a test (e.g. from
* crop id to crop name or vice versa).
*/

/**
* Tests that interact with the database will often make use of the
* Functions in the FarmOSAPI library to access and modify the database.
*
* The lines below illustrate how functions from the FarmOSAPI library
* can be brought into a spec.js file for testing.
*
* The farmdata2/README.md contains information about the documentation
* for all of the functions in the FarmOSAPI library.
*/
var FarmOSAPI = require('../../resources/FarmOSAPI.js')
var getUserToIDMap = FarmOSAPI.getUserToIDMap

describe('Example of getting a map to via the FarmOSAPI functions in a test', () => {

/*
* If multiple tests in the same describe will be using the userToIDMap
* then define a variable, like userMap here, and set it in the beforeEach
* as illustrated below. This variable will then be accessible in all of the
* tests (its).
*
* If the map is only needed in a single test (it) then the same approach can
* be used within that test (it) and this variable will not be necessary.
*/
let userMap = null

beforeEach(() => {
// Log in
cy.login("manager1", "farmdata2")
.then (() => {

/*
* Once we are logged in we can Use the getUserToIDMap() function from the FarmOSAPI library
* to get the map.
*
* The getUserToIDMap() function makes an API call to farmOS to get the data and create the map.
* This function returns a promise that resolves to the map when the API call returns.
*
* The map will not be available until the promise returned by the getUserToIDMap()
* resolves. So we need to wait for that promise to resolve.
* The cy.wrap(...).as(...) command is how we setup to wait for a promise to resolve in Cypress.
*
* The name "get-map" used here is just an identifier that describes what we are waiting on.
* It is used below when we actually wait for the promise to resolve.
*/
cy.wrap(getUserToIDMap()).as("get-map")
})

/*
* We use cy.get(...).then(...) in Cypress to wait for a promise that has
* been wrapped to resolve.
*
* Here we wait for the promise wrapped as "get-map" above.
*/
cy.get("@get-map")
.then((map) => {
/*
* The parameter map will be set to the map from the promise. Here we assign
* it to the userMap variable so that it will be accessible to all of the it
* tests.
*/
userMap = map
})

// Visit the page that will be tested.
cy.visit("/farm/fd2-example/dbTest")

/*
* Almost always the page being visited will load maps and other
* content in its created() hook. If the page follows the pattern
* used in FarmData2 pages (see Maps.html for an example with explanation)
* then this causes the tests to wait here until the entire page and
* all of its data is loaded before continuing.
*/
cy.waitForPage()
})

/**
* All tests can then use the userToIDMap that was assigned to the
* userMap variable in the beforeEach.
*/
it("Use the map in a test", () => {

/*
* The tests can then use the userMap in tests.
*
* The expect(...) function is like the .should(...) statement. While
* .should(...) is used to make assertions about elements found with cy.get(...),
* expect(...) is used to make assertions about variables.
*
* For every assertion that can be made with .should(...) there is an equivalent
* assertion for use with expect(...). For a complete reference see:
* https://docs.cypress.io/guides/references/assertions
*/
expect(userMap).to.not.be.null
expect(userMap.get("manager1")).to.equal("3")
expect(userMap.get("worker1")).to.equal("5")
})
})
Loading