diff --git a/packages/bookshop/demo/demo-magic.sh b/packages/bookshop/demo/demo-magic.sh new file mode 100755 index 00000000..9a84deb1 --- /dev/null +++ b/packages/bookshop/demo/demo-magic.sh @@ -0,0 +1,205 @@ +#!/usr/bin/env bash + +############################################################################### +# +# demo-magic.sh +# +# Copyright (c) 2015 Paxton Hare +# +# This script lets you script demos in bash. It runs through your demo script when you press +# ENTER. It simulates typing and runs commands. +# +############################################################################### + +# the speed to "type" the text +TYPE_SPEED=20 + +# no wait after "p" or "pe" +NO_WAIT=false + +# if > 0, will pause for this amount of seconds before automatically proceeding with any p or pe +PROMPT_TIMEOUT=0 + +# don't show command number unless user specifies it +SHOW_CMD_NUMS=false + + +# handy color vars for pretty prompts +BLACK="\033[0;30m" +BLUE="\033[0;34m" +GREEN="\033[0;32m" +GREY="\033[0;90m" +CYAN="\033[0;36m" +RED="\033[0;31m" +PURPLE="\033[0;35m" +BROWN="\033[0;33m" +WHITE="\033[1;37m" +COLOR_RESET="\033[0m" + +C_NUM=0 + +# prompt and command color which can be overriden +DEMO_PROMPT="$ " +DEMO_CMD_COLOR=$WHITE +DEMO_COMMENT_COLOR=$GREY + +## +# prints the script usage +## +function usage() { + echo -e "" + echo -e "Usage: $0 [options]" + echo -e "" + echo -e "\tWhere options is one or more of:" + echo -e "\t-h\tPrints Help text" + echo -e "\t-d\tDebug mode. Disables simulated typing" + echo -e "\t-n\tNo wait" + echo -e "\t-w\tWaits max the given amount of seconds before proceeding with demo (e.g. '-w5')" + echo -e "" +} + +## +# wait for user to press ENTER +# if $PROMPT_TIMEOUT > 0 this will be used as the max time for proceeding automatically +## +function wait() { + if [[ "$PROMPT_TIMEOUT" == "0" ]]; then + read -rs + else + read -rst "$PROMPT_TIMEOUT" + fi +} + +## +# print command only. Useful for when you want to pretend to run a command +# +# takes 1 param - the string command to print +# +# usage: p "ls -l" +# +## +function p() { + if [[ ${1:0:1} == "#" ]]; then + cmd=$DEMO_COMMENT_COLOR$1$COLOR_RESET + else + cmd=$DEMO_CMD_COLOR$1$COLOR_RESET + fi + + # render the prompt + x=$(PS1="$DEMO_PROMPT" "$BASH" --norc -i &1 | sed -n '${s/^\(.*\)exit$/\1/p;}') + + # show command number is selected + if $SHOW_CMD_NUMS; then + printf "[$((++C_NUM))] $x" + else + printf "$x" + fi + + # wait for the user to press a key before typing the command + if !($NO_WAIT); then + wait + fi + + if [[ -z $TYPE_SPEED ]]; then + echo -en "$cmd" + else + echo -en "$cmd" | pv -qL $[$TYPE_SPEED+(-2 + RANDOM%5)]; + fi + + # wait for the user to press a key before moving on + if !($NO_WAIT); then + wait + fi + echo "" +} + +## +# Prints and executes a command +# +# takes 1 parameter - the string command to run +# +# usage: pe "ls -l" +# +## +function pe() { + # print the command + p "$@" + + # execute the command + eval "$@" +} + +## +# executes a command without print +# +# takes 1 parameter - the string command to run +# +# usage: pe "ls -l" +# +## +function e() { + # execute the command + eval "$@" +} + +## +# Enters script into interactive mode +# +# and allows newly typed commands to be executed within the script +# +# usage : cmd +# +## +function cmd() { + # render the prompt + x=$(PS1="$DEMO_PROMPT" "$BASH" --norc -i &1 | sed -n '${s/^\(.*\)exit$/\1/p;}') + printf "$x\033[0m" + read command + eval "${command}" +} + + +function check_pv() { + command -v pv >/dev/null 2>&1 || { + + echo "" + echo -e "${RED}##############################################################" + echo "# HOLD IT!! I require pv but it's not installed. Aborting." >&2; + echo -e "${RED}##############################################################" + echo "" + echo -e "${COLOR_RESET}Installing pv:" + echo "" + echo -e "${BLUE}Mac:${COLOR_RESET} $ brew install pv" + echo "" + echo -e "${BLUE}Other:${COLOR_RESET} http://www.ivarch.com/programs/pv.shtml" + echo -e "${COLOR_RESET}" + exit 1; + } +} + +check_pv +# +# handle some default params +# -h for help +# -d for disabling simulated typing +# +while getopts ":dhncw:" opt; do + case $opt in + h) + usage + exit 1 + ;; + d) + unset TYPE_SPEED + ;; + n) + NO_WAIT=true + ;; + c) + SHOW_CMD_NUMS=true + ;; + w) + PROMPT_TIMEOUT=$OPTARG + ;; + esac +done \ No newline at end of file diff --git a/packages/bookshop/demo/demo_Week2_Unit4.sh b/packages/bookshop/demo/demo_Week2_Unit4.sh new file mode 100755 index 00000000..9d339bd7 --- /dev/null +++ b/packages/bookshop/demo/demo_Week2_Unit4.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash + +######################## +# include the magic +######################## +. ./demo-magic.sh + + +######################## +# Configure the options +######################## + +# +# speed at which to simulate typing. bigger num = faster +# +TYPE_SPEED=30 + +# +# custom prompt +# +# see http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html for escape sequences +# +DEMO_PROMPT='\[\033[33;1m\]${PWD#"${PWD%/*/*}/"} \[\033[m\]\$ ' + +# DEMO_PROMPT='${PWD#"${PWD%/*/*}/"} $ ' + +# DEMO_PROMPT="${CYAN}➜ ${GREEN} ${PWD#"${PWD%/*/*}/"} $" + + + +# hide the evidence +clear + +p "Demo: Week 2 Unit 4 Using Database" + +pe "cd ../" + +p "********************************" +p "Demo 4.1: Deploy to your data to different database" + +p " let's check model definition in schema.cds in VSCode" + +p " let's check CSV files" +pe "ls -l db/data" + +p "As the quickest option to get started, you can deploy your data to in-memory SQLite database" +p "cds run --in-memory" +e "cds run --in-memory | grep --color=always -E '^.*in-memory.*$' -C 10000" +#pe "cds run --in-memory | grep --color=always -E '^.*in-memory.*$' -C 10000" + +p " Typically you might also start your project locally and choose to deploy the CSV files to local SQLite database file" +p "cds deploy --to sqlite:bookshop.db" +e "cds deploy --to sqlite:bookshop.db | grep --color=always -E '^.*successfully.*$' -C 10000" + +p " the package.json file is udpated accordingly" + +p "cat package.json" +e "cat package.json | grep --color -E bookshop.db -C 10000 " + +p "Inspect database schema with sqlite cli or VSCode extension" + +p "sqlite3 bookshop.db .tables " +e "sqlite3 bookshop.db .tables | grep -v localized" + +pe "sqlite3 bookshop.db \".schema sap_capire_bookshop_Authors \"" + +p "sqlite3 bookshop.db \".schema sap_capire_bookshop_Books \" " +e "sqlite3 bookshop.db \".schema sap_capire_bookshop_Books \" | grep --color -E author_ID -C 10000" +# author_ID INTEGER, + + +p "Or deploy to HANA database in Cloud Foundry (logged in Cloud Foundry already)" + +pe "cds deploy --to hana" + +# highlight the first hana in the output +p "cat default-env.json | less" +e "cat default-env.json | grep --color=always -E "^.*\"hana\".*$" -C 10000 | less" + + +p "Behind the scene, cds CLI compiles the cds model definition and looks up corrsponding CSV files for each table" +p "Check out the gen/src folder in VSCode" + +p "cds compile --to hdbtabledata db/schema.cds" +e "cds compile --to hdbtabledata db/schema.cds | grep --color=always 'target_table\|source_data\|column_mappings' -C 10000 | less" + +p "********************************" + +p "Q: How CDS model maps to SQL ?" +p "cds compile --to sql db/schema.cds" +e "cds compile --to sql db/schema.cds | grep --color=always -E '^.*CREATE TABLE.*$' -C 10000 | less" + +p "cds compile --to hana db/schema.cds" +e "cds compile --to hana db/schema.cds | grep --color=always -E '^.*entity.*$' -B 4 -A 50 | less" + + +# Following part will demostrated in later units +# p "Q: How annotation @cds.persistence.exist works? " +# pe "cat db/schema.cds | grep --color -E 'cds.persistence.exists|$' " +# p "It denotes that there already exists a native database object, and should be used during runtime." +# p "The CDS artifact merely acts as a proxy artifact, representing the signature of the native database artifact in the CDS model." +# pe "cds compile --to sql db/schema.cds | grep --color -E ISBN -A 5" + +# p "Now removing annotation @cds.persistence.exist and check again" +# pe "cds compile --to sql db/schema.cds | grep --color -E ISBN -A 5" + + +# p "Q: How annotation @cds.persistence.skip works? " +# pe "cat srv/services.cds | grep --color -E cds.persistence.skip -A 5 -B 5" +# p "It denotes that the artifact isn't available in the database but eventually implemented by custom code." + +# pe "cds compile --to serviceinfo srv/services.cds" + +# the demo has concluded +p "" \ No newline at end of file diff --git a/packages/bookshop/demo/demo_Week2_Unit56.sh b/packages/bookshop/demo/demo_Week2_Unit56.sh new file mode 100755 index 00000000..8b4e7bae --- /dev/null +++ b/packages/bookshop/demo/demo_Week2_Unit56.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +######################## +# include the magic +######################## +. ./demo-magic.sh + + +######################## +# Configure the options +######################## + +# +# speed at which to simulate typing. bigger num = faster +# +TYPE_SPEED=30 + +# +# custom prompt +# +# see http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html for escape sequences +# +DEMO_PROMPT='\[\033[33;1m\]${PWD#"${PWD%/*/*}/"} \[\033[m\]\$ ' + +# DEMO_PROMPT='${PWD#"${PWD%/*/*}/"} $ ' + +# DEMO_PROMPT="${CYAN}➜ ${GREEN} ${PWD#"${PWD%/*/*}/"} $" + + + +# hide the evidence +clear + +p "Demo: Week 2 Unit 5 Custom Handlers & Unit 6 Access Data From Code" + +pe "cd .." + +p "First check srv.before and srv.after in bookshop/srv/cat-service.js. \n" + +p " - srv.after : apply discount in case of overstock in case of READ entity Books \n" + +p " - srv.before : ensure sufficient stock in case of CREATE Orders \n" + +# p " - srv.on : call external review services in case of READ entity Reviews \n" + +p "Now launch Bookshop, and run a few tests in VSCode. \n" + + +# pe "PORT=4004 cds run reviews-service --in-memory & " +pe "PORT=4004 cds run --in-memory " + + +e "pkill node" +# the demo has concluded +p "" \ No newline at end of file diff --git a/packages/bookshop/demo/demo_Week2_Unit7.sh b/packages/bookshop/demo/demo_Week2_Unit7.sh new file mode 100755 index 00000000..396aeeb0 --- /dev/null +++ b/packages/bookshop/demo/demo_Week2_Unit7.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +######################## +# include the magic +######################## +. ./demo-magic.sh + + +######################## +# Configure the options +######################## + +# +# speed at which to simulate typing. bigger num = faster +# +TYPE_SPEED=30 + +# +# custom prompt +# +# see http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html for escape sequences +# +DEMO_PROMPT='\[\033[33;1m\]${PWD#"${PWD%/*/*}/"} \[\033[m\]\$ ' + +# DEMO_PROMPT='${PWD#"${PWD%/*/*}/"} $ ' + +# DEMO_PROMPT="${CYAN}➜ ${GREEN} ${PWD#"${PWD%/*/*}/"} $" + + + +# hide the evidence +clear + +p "Demo: Week 2 Unit 7 Testing" + +pe "cd .." + +p "How Javascript testing framework Jest helps to run test" +p "Take a look at the example test/bookshop.test.js" +p "Now run the test" +pe " cd ../.. && npm run test bookshop" + +p " Enable debug switch for CDS and run a few tests with Rest Client tool" +pe "cd packages/bookshop" +pe "DEBUG=true PORT=4004 cds run --in-memory " + + +e "pkill node" +# the demo has concluded +p "" \ No newline at end of file diff --git a/packages/bookshop/tests/bookshop.test.js b/packages/bookshop/tests/bookshop.test.js index b9851ee1..03740b2f 100644 --- a/packages/bookshop/tests/bookshop.test.js +++ b/packages/bookshop/tests/bookshop.test.js @@ -1,13 +1,15 @@ const cds = require('@sap/cds/lib/cds') -const { setup, close } = require('./utils') -const request = require('supertest') +const app = require('express')() +const request = require('supertest')(app) describe('Samples: Bookshop', () => { - beforeAll(done => setup('packages/bookshop', done)) - afterAll(close) - test('Service $metadata document', async () => { + it ('should serve BooksShop', async ()=>{ + await cds.serve('CatalogService').from(__dirname+'/browse') .in (app) + }) + + it('Service $metadata document', async () => { const response = await request(cds.serve.app) .get('/browse/$metadata') .expect('Content-Type', /^application\/xml/) @@ -20,7 +22,7 @@ describe('Samples: Bookshop', () => { }) - test('Get with select, expand and localized', async () => { + it('Get with select, expand and localized', async () => { const response = await request(cds.serve.app) .get('/browse/Books?$select=title,author&$expand=currency&sap-language=de') .expect('Content-Type', /^application\/json/)