Skip to content

Commit

Permalink
ci: 🎡 ci works normal
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewyang committed Dec 12, 2023
1 parent 9266286 commit 58148b0
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 28 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/buildAndTest-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,34 @@ on:
branches: ["main"]
jobs:
buildAndTest:
runs-on: macos-latest
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Checkout repository
uses: actions/checkout@v2

- name: Check docker version
run: |
docker --version
- name: Check docker info
run: |
docker info
- name: Install dependencies
run: |
npm install
- name: Build
run: |
npm run build
- name: Test
run: |
npm run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
dist-test
yarn.lock
.env
5 changes: 3 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loader" : "ts-node/esm",
"spec": "test/**/*.test.ts",
"exclude": ["node_modules"]
"spec": "dist-test/**/*.test.js",
"exclude": ["node_modules"],
"project": "./tsconfig.test.json"
}
19 changes: 9 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"posttest:auth": "npm run mongodb-auth:stop && npm run mongodb-auth:clear && npm run mongodb-auth:delete;",
"pretest": "npm run pretest:anon && npm run pretest:auth;",
"posttest": "npm run posttest:anon && npm run posttest:auth;",
"test": "npm run pretest && mocha test/**/*.test.ts && npm run posttest",
"test": "npm run pretest &&tsc -p tsconfig.test.json; mocha && npm run posttest ",
"readme": "npx readme-md-generator -p ./templates/readme.md",
"commit": "git-cz",
"release": "npm run build && git branch --show-current | grep -q '^main$' && release-it",
Expand All @@ -53,7 +53,7 @@
"homepage": "https://github.com/unipackage/datastore#readme",
"dependencies": {
"@types/node": "20.6.2",
"@unipackage/utils": "^0.1.5",
"@unipackage/utils": "^0.1.8",
"mongoose": "^7.2.2"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions test/helper/mongodb/instance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ExampleMongoDatastore } from "../repo"

export const exampleMongoDb = new ExampleMongoDatastore(
"mongodb://127.0.0.1:27017/fnode"
)
35 changes: 35 additions & 0 deletions test/helper/mongodb/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import mongoose, { Schema, Document } from "mongoose"
import { Example } from "../types"

interface ExampleDocument extends Example, Document {}

const ExampleSchema = new Schema<ExampleDocument>({
boolElement: {
type: Boolean,
required: [true, "Please provide the boolElement"],
index: { unique: true },
},
stringElement: {
type: String,
required: [true, "Please provide the stringElement"],
},
numberElement: {
type: Number,
required: [true, "Please provide the numberElement"],
},
objectElement: {
type: Object,
required: [true, "Please provide the objectElement"],
},
arrayElement: {
type: [Object],
required: [true, "Please provide the arrayElement"],
},
})

const ExampleModel =
mongoose.models.Example ||
mongoose.model<ExampleDocument>("Example", ExampleSchema)

export { ExampleModel }
export type { ExampleDocument }
12 changes: 12 additions & 0 deletions test/helper/mongodb/repo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DataStore } from "../../../../src/datastore"
import { MongooseDataStore } from "../../../../src/implements/mongo/datastore/MongooseDataStore"
import { Example } from "../types"
import { ExampleModel, ExampleDocument } from "../model"

export class ExampleMongoDatastore extends DataStore<Example, ExampleDocument> {
constructor(uri: string) {
super(
new MongooseDataStore<Example, ExampleDocument>(ExampleModel, uri)
)
}
}
11 changes: 11 additions & 0 deletions test/helper/mongodb/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface Example {
boolElement: boolean
stringElement: string
numberElement: number
objectElement: {
boolElement: boolean
stringElement: string
numberElement: number
}
arrayElement: Array<any>
}
15 changes: 9 additions & 6 deletions test/scripts/docker/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,37 @@ ACTION_NAME=${1}
IMAGE_TYPE=${2}
PORT=${3}

HOSTDATAPATH="/var/tmp/containerdata"

BASE_NAME="mongodb"
CONTAINER_NAME="${BASE_NAME}_${IMAGE_TYPE}_container"
IMAGE_NAME="${BASE_NAME}_${IMAGE_TYPE}_image"

function clear() {
clear() {
stop
docker rm ${CONTAINER_NAME}
rm -rf "${HOSTDATAPATH}/*"
}

function delete() {
delete() {
echo $IMAGE_NAME
docker rmi ${IMAGE_NAME}
}

function restart() {
restart() {
stop
start
}

function start() {
start() {
docker start ${CONTAINER_NAME}
}

function stop() {
stop() {
docker stop ${CONTAINER_NAME}
}

function execute() {
execute() {
case $1 in
"start")
start
Expand Down
13 changes: 7 additions & 6 deletions test/scripts/docker/mongodb/execute.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#/bin/sh
source ../common.sh
. ../common.sh

function build() {
docker build -t ${IMAGE_NAME} "../../../docker/mongodb/${IMAGE_TYPE}/"

build() {
docker build -t ${IMAGE_NAME} ../../../docker/mongodb/${IMAGE_TYPE}/
}

function run() {
docker run -d -p ${PORT}:27017 --name ${CONTAINER_NAME} ${IMAGE_NAME}
run() {
docker run -d -p ${PORT}:27017 --name ${CONTAINER_NAME} -v ${HOSTDATAPATH}:/data/db ${IMAGE_NAME}
}

function execute_action() {
execute_action() {
case ${ACTION_NAME} in
"build")
build
Expand Down
61 changes: 61 additions & 0 deletions test/testcase/mongodb/test.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* (c) 2023 unipackage
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

import assert from "assert"
import { it } from "mocha"
import { exampleMongoDb } from "../../helper/mongodb/instance"

//@ts-ignore
describe("Mongo test", () => {
it("connect test", async () => {
const resConnect = await exampleMongoDb.connect()
assert.deepEqual(resConnect.ok, true)
const resDisconnect = await exampleMongoDb.disconnect()
assert.deepEqual(resDisconnect.ok, true)
})
it("xx test", async () => {
const resConnect = await exampleMongoDb.connect()
assert.deepEqual(resConnect.ok, true)
const res = await exampleMongoDb.create({
boolElement: true,
numberElement: 1,
stringElement: "1111",
objectElement: {
boolElement: false,
numberElement: 2,
stringElement: "111",
},
arrayElement: [1, 2],
})
console.log(res)
assert.deepEqual(res.ok, true)
const resDisconnect = await exampleMongoDb.disconnect()
assert.deepEqual(resDisconnect.ok, true)
})
it("yyy test", async () => {
const resConnect = await exampleMongoDb.connect()
assert.deepEqual(resConnect.ok, true)
const res = await exampleMongoDb.find({})
console.log(res)
assert.deepEqual(res.ok, true)
const resDisconnect = await exampleMongoDb.disconnect()
assert.deepEqual(resDisconnect.ok, true)
})
})
24 changes: 24 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"outDir": "./dist-test",
"declaration": true,
"target": "ES2015",
"strict": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"experimentalDecorators": true,
"isolatedModules": false,
"skipLibCheck": true,
"jsx": "react",
"incremental": false,
},
"include": [
"test/**/*"
],
"exclude": [
"node_modules"
],
}

0 comments on commit 58148b0

Please sign in to comment.