From 9b1351d3cd8996f8c749a94448043bec887f0bba Mon Sep 17 00:00:00 2001 From: Manabu Matsuzaki Date: Mon, 10 May 2021 04:31:43 +0900 Subject: [PATCH] improve tests --- Makefile | 103 ++++++++++++++++++ test/data/project1.json | 3 + test/data/project1_repo1.json | 3 + test/data/project1_repo1_content1.json | 15 +++ test/data/project1_repo1_content2.json | 15 +++ test/data/project1_repo2.json | 3 + test/data/project1_repo2_content1.json | 15 +++ test/data/project2.json | 3 + test/data/project2_repo1.json | 3 + test/data/project2_repo1_content1.json | 15 +++ test/data/project2_repo2.json | 3 + test/data/project2_repo2_content1.json | 15 +++ test/data/project2_repo2_content2.json | 15 +++ .../data/project2_repo2_content2_update1.json | 16 +++ .../data/project2_repo2_content2_update2.json | 16 +++ .../data/project2_repo2_content2_update3.json | 16 +++ test/watchService.test.ts | 27 ++++- 17 files changed, 281 insertions(+), 5 deletions(-) create mode 100644 Makefile create mode 100644 test/data/project1.json create mode 100644 test/data/project1_repo1.json create mode 100644 test/data/project1_repo1_content1.json create mode 100644 test/data/project1_repo1_content2.json create mode 100644 test/data/project1_repo2.json create mode 100644 test/data/project1_repo2_content1.json create mode 100644 test/data/project2.json create mode 100644 test/data/project2_repo1.json create mode 100644 test/data/project2_repo1_content1.json create mode 100644 test/data/project2_repo2.json create mode 100644 test/data/project2_repo2_content1.json create mode 100644 test/data/project2_repo2_content2.json create mode 100644 test/data/project2_repo2_content2_update1.json create mode 100644 test/data/project2_repo2_content2_update2.json create mode 100644 test/data/project2_repo2_content2_update3.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..580ba92 --- /dev/null +++ b/Makefile @@ -0,0 +1,103 @@ +.PHONY: docker-compose-up +docker-compose-up: + docker-compose up -d + +.PHONY: docker-compose-down +docker-compose-down: + docker-compose down + +.PHONY: setup-test-data +setup-test-data: + # project1 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project1.json \ + http://localhost:36462/api/v1/projects + # project1 - repo1 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project1_repo1.json \ + http://localhost:36462/api/v1/projects/project1/repos + # project1 - repo1 - content1 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project1_repo1_content1.json \ + http://localhost:36462/api/v0/projects/project1/repositories/repo1/files/revisions/head + # project1 - repo1 - content2 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project1_repo1_content2.json \ + http://localhost:36462/api/v0/projects/project1/repositories/repo1/files/revisions/head + # project1 - repo2 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project1_repo2.json \ + http://localhost:36462/api/v1/projects/project1/repos + # project1 - repo2 - content1 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project1_repo2_content1.json \ + http://localhost:36462/api/v0/projects/project1/repositories/repo2/files/revisions/head + # ---------- + # project2 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project2.json \ + http://localhost:36462/api/v1/projects + # project2 - repo1 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project2_repo1.json \ + http://localhost:36462/api/v1/projects/project2/repos + # project2 - repo1 - content1 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project2_repo1_content1.json \ + http://localhost:36462/api/v0/projects/project2/repositories/repo1/files/revisions/head + # project2 - repo2 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project2_repo2.json \ + http://localhost:36462/api/v1/projects/project2/repos + # project2 - repo2 - content1 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project2_repo2_content1.json \ + http://localhost:36462/api/v0/projects/project2/repositories/repo2/files/revisions/head + # project2 - repo2 - content2 + curl -X POST \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project2_repo2_content2.json \ + http://localhost:36462/api/v0/projects/project2/repositories/repo2/files/revisions/head + +.PHONY: update-test-data +update-test-data: + curl -X PUT \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project2_repo2_content2_update1.json \ + http://localhost:36462/api/v0/projects/project2/repositories/repo2/files/revisions/head + sleep 3 + curl -X PUT \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project2_repo2_content2_update2.json \ + http://localhost:36462/api/v0/projects/project2/repositories/repo2/files/revisions/head + sleep 3 + curl -X PUT \ + -H 'Authorization: Bearer anonymous' \ + -H 'Content-Type: application/json' \ + -d @test/data/project2_repo2_content2_update3.json \ + http://localhost:36462/api/v0/projects/project2/repositories/repo2/files/revisions/head diff --git a/test/data/project1.json b/test/data/project1.json new file mode 100644 index 0000000..471c47d --- /dev/null +++ b/test/data/project1.json @@ -0,0 +1,3 @@ +{ + "name": "project1" +} diff --git a/test/data/project1_repo1.json b/test/data/project1_repo1.json new file mode 100644 index 0000000..4b8de8c --- /dev/null +++ b/test/data/project1_repo1.json @@ -0,0 +1,3 @@ +{ + "name": "repo1" +} diff --git a/test/data/project1_repo1_content1.json b/test/data/project1_repo1_content1.json new file mode 100644 index 0000000..078a026 --- /dev/null +++ b/test/data/project1_repo1_content1.json @@ -0,0 +1,15 @@ +{ + "file": { + "name": "test1.json", + "type": "JSON", + "path": "/test1.json", + "content": "{\"field1\": \"foo\"}" + }, + "commitMessage": { + "summary": "Add /test1.json", + "detail": { + "content": "", + "markup": "PLAINTEXT" + } + } +} diff --git a/test/data/project1_repo1_content2.json b/test/data/project1_repo1_content2.json new file mode 100644 index 0000000..fe8ebd7 --- /dev/null +++ b/test/data/project1_repo1_content2.json @@ -0,0 +1,15 @@ +{ + "file": { + "name": "test2.json", + "type": "JSON", + "path": "/test2.json", + "content": "{\"field1\": \"bar\"}" + }, + "commitMessage": { + "summary": "Add /test2.json", + "detail": { + "content": "", + "markup": "PLAINTEXT" + } + } +} diff --git a/test/data/project1_repo2.json b/test/data/project1_repo2.json new file mode 100644 index 0000000..278fdca --- /dev/null +++ b/test/data/project1_repo2.json @@ -0,0 +1,3 @@ +{ + "name": "repo2" +} diff --git a/test/data/project1_repo2_content1.json b/test/data/project1_repo2_content1.json new file mode 100644 index 0000000..ba87bfc --- /dev/null +++ b/test/data/project1_repo2_content1.json @@ -0,0 +1,15 @@ +{ + "file": { + "name": "test3.json", + "type": "JSON", + "path": "/test3.json", + "content": "{\"field1\": \"baz\"}" + }, + "commitMessage": { + "summary": "Add /test3.json", + "detail": { + "content": "", + "markup": "PLAINTEXT" + } + } +} diff --git a/test/data/project2.json b/test/data/project2.json new file mode 100644 index 0000000..0dce5f3 --- /dev/null +++ b/test/data/project2.json @@ -0,0 +1,3 @@ +{ + "name": "project2" +} diff --git a/test/data/project2_repo1.json b/test/data/project2_repo1.json new file mode 100644 index 0000000..4b8de8c --- /dev/null +++ b/test/data/project2_repo1.json @@ -0,0 +1,3 @@ +{ + "name": "repo1" +} diff --git a/test/data/project2_repo1_content1.json b/test/data/project2_repo1_content1.json new file mode 100644 index 0000000..2be59b7 --- /dev/null +++ b/test/data/project2_repo1_content1.json @@ -0,0 +1,15 @@ +{ + "file": { + "name": "test4.json", + "type": "JSON", + "path": "/test4.json", + "content": "{\"field1\": \"qux\"}" + }, + "commitMessage": { + "summary": "Add /test4.json", + "detail": { + "content": "", + "markup": "PLAINTEXT" + } + } +} diff --git a/test/data/project2_repo2.json b/test/data/project2_repo2.json new file mode 100644 index 0000000..278fdca --- /dev/null +++ b/test/data/project2_repo2.json @@ -0,0 +1,3 @@ +{ + "name": "repo2" +} diff --git a/test/data/project2_repo2_content1.json b/test/data/project2_repo2_content1.json new file mode 100644 index 0000000..69c209b --- /dev/null +++ b/test/data/project2_repo2_content1.json @@ -0,0 +1,15 @@ +{ + "file": { + "name": "test5.json", + "type": "JSON", + "path": "/test5.json", + "content": "{\"field1\": \"quux\"}" + }, + "commitMessage": { + "summary": "Add /test5.json", + "detail": { + "content": "", + "markup": "PLAINTEXT" + } + } +} diff --git a/test/data/project2_repo2_content2.json b/test/data/project2_repo2_content2.json new file mode 100644 index 0000000..271c3dc --- /dev/null +++ b/test/data/project2_repo2_content2.json @@ -0,0 +1,15 @@ +{ + "file": { + "name": "test6.json", + "type": "JSON", + "path": "/test6.json", + "content": "{\"field1\": \"corge\"}" + }, + "commitMessage": { + "summary": "Add /test6.json", + "detail": { + "content": "", + "markup": "PLAINTEXT" + } + } +} diff --git a/test/data/project2_repo2_content2_update1.json b/test/data/project2_repo2_content2_update1.json new file mode 100644 index 0000000..3af4022 --- /dev/null +++ b/test/data/project2_repo2_content2_update1.json @@ -0,0 +1,16 @@ +{ + "file": { + "revision": "HEAD", + "name": "test6.json", + "type": "JSON", + "path": "/test6.json", + "content": "{\"field1\": \"corge1\"}" + }, + "commitMessage": { + "summary": "Edit /test6.json", + "detail": { + "content": "", + "markup": "PLAINTEXT" + } + } +} diff --git a/test/data/project2_repo2_content2_update2.json b/test/data/project2_repo2_content2_update2.json new file mode 100644 index 0000000..4090d21 --- /dev/null +++ b/test/data/project2_repo2_content2_update2.json @@ -0,0 +1,16 @@ +{ + "file": { + "revision": "HEAD", + "name": "test6.json", + "type": "JSON", + "path": "/test6.json", + "content": "{\"field1\": \"corge2\"}" + }, + "commitMessage": { + "summary": "Edit /test6.json", + "detail": { + "content": "", + "markup": "PLAINTEXT" + } + } +} diff --git a/test/data/project2_repo2_content2_update3.json b/test/data/project2_repo2_content2_update3.json new file mode 100644 index 0000000..770fad2 --- /dev/null +++ b/test/data/project2_repo2_content2_update3.json @@ -0,0 +1,16 @@ +{ + "file": { + "revision": "HEAD", + "name": "test6.json", + "type": "JSON", + "path": "/test6.json", + "content": "{\"field1\": \"corge3\"}" + }, + "commitMessage": { + "summary": "Edit /test6.json", + "detail": { + "content": "", + "markup": "PLAINTEXT" + } + } +} diff --git a/test/watchService.test.ts b/test/watchService.test.ts index e1063fa..b4597aa 100644 --- a/test/watchService.test.ts +++ b/test/watchService.test.ts @@ -1,4 +1,5 @@ import http2 from 'http2'; +import { exec } from 'child_process'; import { CentralDogmaClient, ContentService, WatchService } from '../lib'; const { HTTP_STATUS_NOT_MODIFIED } = http2.constants; @@ -47,15 +48,31 @@ describe('WatchService', () => { }); const sut = new WatchService(client); - const project = 'project1'; - const repo = 'repo1'; - const filePath = '/test3.json'; + const project = 'project2'; + const repo = 'repo2'; + const filePath = '/test6.json'; const emitter = sut.watchFile(project, repo, filePath); + + let count = 0; + emitter.on('data', (data) => { + count++; console.log(`data=${JSON.stringify(data)}`); }); - await sleep(90_000); - }, 90_000); + setTimeout(() => { + // The target updates the json three times + exec('make update-test-data', (e) => { + if (e) { + // fail + expect(true).toBe(false); + } + }); + }, 1_000); + + await sleep(15_000); + + expect(count).toBe(3); + }, 30_000); });