วันพฤหัสบดีที่ 17 พฤษภาคม พ.ศ. 2561

Commit 14 Nodejs Wordbucket : Add search function and produce the test until the current function

Nodejs : Wordbucket GitHub Link

Commit 14 Nodejs Wordbucket : Add search function and produce the test until the current function

Commits on May 2, 2018



functional test

- เพิ่ม functional_test ตาม project django test เพิ่มคำต่าง และ search
  • อธิบาย test จาก commit django (main : commit 4,5,6 and 8)(search : link)


@@ -1,4 +1,10 @@
+from selenium.common.exceptions import WebDriverException
+
+MAX_WAIT = 10
+
from selenium import webdriver
+from selenium.webdriver.common.keys import Keys
+import time
import unittest
class NewVisitorTest(unittest.TestCase):
@@ -9,15 +15,78 @@ def setUp(self):
def tearDown(self):
self.browser.quit()
- def test_can_start_a_list_and_retrieve_it_later(self):
- # Edith has heard about a cool new online to-do app. She goes
+ def check_for_row_in_list_table(self, row_text):
+ start_time = time.time()
+ while True:
+ try:
+ table = self.browser.find_element_by_id('id_word_table')
+ rows = table.find_elements_by_tag_name('tr')
+ self.assertIn(row_text, [row.text for row in rows])
+ return
+ except (AssertionError, WebDriverException) as e:
+ if time.time() - start_time > MAX_WAIT:
+ raise e
+ time.sleep(0.5)
+
+ def check_for_row_in_explanation_table(self, row_text):
+ start_time = time.time()
+ while True:
+ try:
+ table = self.browser.find_element_by_id('id_explanation_table')
+ rows = table.find_elements_by_tag_name('tr')
+ self.assertIn(row_text, [row.text for row in rows])
+ return
+ except (AssertionError, WebDriverException) as e:
+ if time.time() - start_time > MAX_WAIT:
+ raise e
+ time.sleep(0.5)
+
+ def test_can_start_a_list_and_retrieve_it_later_and_search(self):
+ # Ann has heard about a cool new online word app. She goes
# to check out its homepage
self.browser.get('http://localhost:3000')
- # She notices the page title and header mention to-do lists
- self.assertIn('WordBucket App', self.browser.title)
+ # She notices the page title and header mention Word Bucket lists
+ self.assertIn('Word Bucket', self.browser.title)
+ header_text = self.browser.find_element_by_tag_name('h1').text
+ self.assertIn('Word Bucket', header_text)
+
+ # She is invited to enter a word item straight away
+ inputbox = self.browser.find_element_by_id('id_new_word')
+ self.assertEqual(
+ inputbox.get_attribute('placeholder'),
+ 'Add new word'
+ )
+
+ # She types "weeb" into a text box and "otaku!" in a explanation text box
+ inputbox.send_keys('weeb')
+ explanationbox = self.browser.find_element_by_id('id_new_eplanation')
+ explanationbox.send_keys('otaku!')
+
+ # When she hits enter, the page updates, and now the page word lists
+ # "1: weeb" as an item in a word list table
+ inputbox.send_keys(Keys.ENTER)
+ self.check_for_row_in_list_table('weeb')
+
+ # There is still a text box inviting her to add another item. She
+ # enters "PogChamp" and "awesome!" in a explanation text box
+ inputbox = self.browser.find_element_by_id('id_new_word')
+ inputbox.send_keys('PogChamp')
+ explanationbox = self.browser.find_element_by_id('id_new_eplanation')
+ explanationbox.send_keys('awesome!')
+ inputbox.send_keys(Keys.ENTER)
+
+ # The page updates again, and now shows both items on website's word
+ self.check_for_row_in_list_table('weeb')
+ self.check_for_row_in_list_table('PogChamp')
+
+ # She type "we" in search text box
+ inputbox = self.browser.find_element_by_id('id_search')
+ inputbox.send_keys('we')
+ inputbox.send_keys(Keys.ENTER)
- # She is invited to enter a to-do item straight away
+ # Now html render 'search' page. and 'weeb' url appear on her screen
+ self.check_for_row_in_list_table('weeb')
if __name__ == '__main__':
unittest.main(warnings='ignore')

index.js

- เพิ่ม function searchWord() โดยการทำงานเหมือนที่อธิบายไว้ใน project django (link)

52  index.js

@@ -1,6 +1,7 @@
//------------------------- setting and import -------------------------
var express = require('express'); //require the just installed express app
var app = express(); //then we call express
+var Op = require('sequelize').Op;
var db = require('./models');
var Word = db.Word;
var Explanation = db.Explanation;
@@ -97,11 +98,11 @@ function renderDisplayWithMessage(req, res) { //render the ejs and display adde
var like = [];
var dislike = [];
var wordID = [];
+ wordID.push(wordID_ref);
ref.forEach(function(item) {
explanation.push(item.explanation_text);
like.push(item.like);
dislike.push(item.dislike);
- wordID.push(item.ExplanationWordId);
});
res.render("detail", { word: word , wordID: wordID , explanation: explanation , message: message });
});
@@ -142,23 +143,68 @@ function view_word(req, res) { //view word detail
var like = [];
var dislike = [];
var wordID = [];
+ wordID.push(wordID_ref);
ref.forEach(function(item) {
explanation.push(item.explanation_text);
like.push(item.like);
dislike.push(item.dislike);
- wordID.push(item.ExplanationWordId);
});
res.render("detail", { word: word , wordID: wordID , explanation: explanation , message: message });
});
});
}
-// ------------------------- call function -------------------------
+function searchWord(req, res) { //search word
+ if (req.body.search_input)
+ var search_ref = req.body.search_input.toString();
+ else var search_ref = req.params.word.toString();
+ var word = [];
+ var id = [];
+ if(search_ref == ""){
+ message = "Please enter the word.";
+ return res.render("search", { word: word , id: id , message: message });
+ }
+ else if (search_ref != ""){
+ message = ""
+ Word.findAll({
+ where: {
+ word: {
+ $like: "%"+search_ref+"%"
+ }
+ },
+ attributes: ['id','word'],
+ raw : true
+ }).then(function(query) {
+ query.forEach(function(item) {
+ word.push(item.word);
+ id.push(item.id);
+ });
+ return res.render("search", { word: word , id: id , message: message })
+ });
+ }
+}
+
+function likeExplanation(req, res) { //like explanation function
+ pass;
+}
+
+function dislikeExplanation(req, res) { //dislike explanation function
+ pass;
+}
+
+
+// ------------------------- path and call function -------------------------
app.use('/addword', addingNewWord); //call function add word
app.use('/word/:id/addexplanation', addingNewExplanation);
+app.use('/word/:id/like', likeExplanation);
+
+app.use('/word/:id/dislike', dislikeExplanation);
+
+app.use('/search/:word', searchWord);
+
app.get('/message', renderDisplayWithMessage);
app.get('/', renderDisplay)

test/test.js

- เพิ่ม unit test เน้นไปทางสถานะ http และ test model


@@ -59,47 +59,56 @@ describe('Unit testing', function(){
});
});
-/*
- //------------AllAroundModelsTest------------//
- it('test_saving_and_retrieving_words', function(){
- chai.request(server)
- .get('/')
- .end(function(err, res){
- res.status.should.equal(200);
- res.should.be.json;
- res.body.should.be.a('array');
+ //------------NewExplanationTest------------//
+ it('test_can_save_a_POST_request_to_an_existing_word', function(done){
+ server
+ .post("word/1/addexplanation")
+ .send({word_input: "JA",explanation_input: "VA"})
+ .expect("Content-type",/json/)
+ .expect(302)
+ .end(function(err, res){
+ res.status.should.equal(302);
+
+ Explanation.create({
+ ExplanationWordId: 1,
+ explanation_text: "Test",
+ like: 0,
+ dislike: 0,
+ }).then( function (test) {
+ // do some tests on article here
+ test.destroy();
done();
});
+ });
});
-
-/*
- //------------NewWordTest------------//
- it('test_can_save_a_POST_request', function(){
-
- });
-
- //------------NewExplanationTest------------//
- it('test_can_save_a_POST_request_to_an_existing_word', function(){
-
- });
- it('test_redirects_to_word_view', function(){
-
+ it('test_redirects_to_word_view', function(done){
+ server
+ .post("word/1/addexplanation")
+ .send({word_input: "JA",explanation_input: "VA"})
+ .expect("Content-type",/json/)
+ .expect(302)
+ .end(function(err, res){
+ res.status.should.equal(302);
+ done();
+ });
});
-/* ================== future test ==================
-
//------------SearchAndBrowseTest------------//
- it('test_uses_search_template', function(){
-
- });
it('test_render_after_POST', function(){
-
- });
- it('test_return_correct_text', function(){
-
+ server
+ .post("search/x")
+ .send({search_input: "x"})
+ .expect("Content-type",/json/)
+ .expect(302)
+ .end(function(err, res){
+ res.status.should.equal(302);
+ done();
+ });
});
+/* ================== future test ==================
+
//------------VoteTest------------//
it('test_redirects_like_to_word_view', function(){

views/detail.ejs

- update ปุ่ม like dislike เผื่ออนาคต หน้าตาเหมือนของ project django


@@ -21,7 +21,12 @@
<table id="id_word_table">
<% for( var i = 0; i < explanation.length; i++){ %>
- <tr><td id="word.word">explanation <%= i+1 + " : " + explanation[i] %></td></tr>
+ <tr><td id="word.word">explanation <%= i+1 + " : " + explanation[i] %><br>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+ <a href="/word/<%= wordID %>/like">LIKE</a> &nbsp;
+ <a href="/word/<%= wordID %>/dislike">DISLIKE</a>
+ </td></tr>
<% } %>
</table>
<br><br><br>

views/search.ejs

- สร้างหน้าผล search โดยดัดแปลงมาจาก project django

@@ -0,0 +1,36 @@
+<html>
+
+ <head>
+ <title> WordBucket App </title>
+ <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet">
+ <link href="/styles.css" rel="stylesheet">
+ </head>
+
+<body>
+ <div class="container">
+
+ <h1>Search Word</h1>
+
+ <h4><%= message %></h4>
+ <form action ="/search/byword" id="form1" method="POST">
+ <input name="search_input" id="id_search" placeholder="Search the word" />
+ </form>
+ <button type="submit" form="form1" value="Submit">Submit</button>
+
+ <% if(word.length == 0){ %>
+ <h4>Word not found</h4>
+ <% } %>
+
+ <table id="id_word_table">
+ <% for( var i = word.length-5; i < word.length; i++){ %>
+ <tr><td id="word.word"><a href="/word/<%= id[i] %>"><%= word[i] %></a></td></tr>
+ <% } %>
+ </table>
+ <br><br><br>
+ <a href="/">home</a>
+
+
+</div>
+
+</body>
+</html>


ไม่มีความคิดเห็น:

แสดงความคิดเห็น