package com.example.serversidebbssample import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.test.context.jdbc.Sql import org.springframework.test.context.junit.jupiter.SpringExtension import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.ResultMatcher import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* import org.springframework.test.web.servlet.setup.MockMvcBuilders @ExtendWith(SpringExtension::class) @SpringBootTest class PersonControllerTasks { lateinit var mockMvc: MockMvc @Autowired lateinit var target: PersonController @BeforeEach fun setup() { mockMvc = MockMvcBuilders.standaloneSetup(target).build() } @Test fun getAllPersonsTest() { val response = mockMvc.perform( get("/all") ).andReturn().response assertThat(response.status).isEqualTo(HttpStatus.OK.value()) assertThat(response.contentType).isEqualTo(MediaType.APPLICATION_JSON.toString()) } @Test fun addNewPersonTest() { val response = mockMvc.perform( post("/add").param("name", "aaa") ).andReturn().response assertThat(response.status).isEqualTo(HttpStatus.OK.value()) assertThat(response.contentAsString).isEqualTo("Saved") } // メソッドの前に実行するSQLを指定 @Test @Sql(statements = ["INSERT INTO person (name) VALUES ('update_data');"]) fun updatePersonTest() { val lastPerson: Person = target.personRepository.findAll().last() val response = mockMvc.perform( post("/update") .param("id", lastPerson.id.toString()) .param("name", "update_data") ).andReturn().response assertThat(response.status).isEqualTo(HttpStatus.OK.value()) assertThat(response.contentAsString).isEqualTo("Updated") } @Test fun deleteNoPersonTest() { var response = mockMvc.perform( post("/delete") .param("id", "-1") ).andReturn().response println(response) assertThat(response.status).isEqualTo(HttpStatus.OK.value()) assertThat(response.contentAsString).isEqualTo("No User") } }
【Kotlin】VSCodeでkotlin拡張を使う
IntelliJを入れたほうが快適だと思うが、VSCodeを使えという指示なので試してみる。
コンパイラをいれる
https://kotlinlang.org/docs/command-line.html#install-the-compiler
Githubよりkotlin-compiler-1.9.23.zipをダウンロードする。
CodeRunner拡張をいれる
設定追加
"code-runner.executorMap" に下記を追加。
"kotlin": "cd $dir && kotlinc-jvm $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar"
jarを作って実行するまでをやってくれる。
Kotlin拡張をいれる
コンパイルエラーのチェック、候補の表示をしてくれる。
デフォルトではLanguageServerとか設定がオフになってるっぽく何もしてくれれない。
設定を開いてそれぞれ必要そうなのをONにしてく。
VSCodeの再起動を忘れずに。
"kotlin.externalSources.autoConvertToKotlin": true "kotlin.inlayHints.chainedHints": true "kotlin.inlayHints.parameterHints": true "kotlin.inlayHints.typeHints": true "kotlin.languageServer.enabled": true "spring.initializr.defaultLanguage": "Kotlin"
IntelliJみたいに引数の名前や型を出してくれたり、型チェックの警告などを出してくれる。関数の候補もでる。
F12で定義に飛ぶことも出来る。いい感じ。
ただちゃんと開発する場合はGradleを使うほうが良いと思われる。
postgresqlメモ
PostgreSQLの使い方 JavaDrive
https://www.javadrive.jp/postgresql/
PostgreSQL日本語ドキュメント
https://www.postgresql.jp/document/
PostgreSQL】データ型一覧
https://plus-info-tech.com/postgresql-typelist
# 文字列や数値の値の入力方法
https://www.javadrive.jp/postgresql/basic/index1.html
エスケープ文字を使う場合はeかEをつける
e'abc\ndef' → 改行がはいる
'abc\ndef' → ただの文字列
'を入れる場合は''にする → isn''t
# データ型
https://www.javadrive.jp/postgresql/type/
数値 integer(int), bigint, numeric(or decimal), real, double
連番 serial, bigserial (手動で指定できるが使用済みとして考慮されないので)
文字列 varchar(255)可変長, char(8)固定長なので空白で埋める, text(無制限)
日付 timestamp, date, time ''で囲む '2024-4-20 10:23:54'
論理値 bool TRUE/FALSEのほか、 true, 't', 'yes', 'y', 'on', 1 が使える
その他 money, bytea, enum, point, cidr, xml, json, integer, text
# スキーマ
https://www.javadrive.jp/postgresql/schema/index1.html
データベースの中にスキーマを作って、テーブルなどのオブジェクトをグループ化出来る
postgres, template0はもともと存在しているデータベース
publicはもともとあるスキーマ。どのロールでも(誰でも)テーブル作成できる。
# ロール
https://www.javadrive.jp/postgresql/role/
ユーザーやグループ。パスワードを設定する。
何が出来るか権限を設定できる。
GRANT SELECT ON ALL TABLES IN SCHEMA schema_name To role_name;
GRANT SELECT, INSERT ON table_name To role_name;
グループ(ロールをロールに所属させる)
grant admin to kuma with admin option;
# テーブル
https://www.javadrive.jp/postgresql/table/
カラムに対して
id serial PRIMARY KEY
id serial UNIQUE
code integer REFERENCES some_table (code) on delete cascade
price integer CHECK (price > 0)
テーブルに対して
PRIMARY KEY (id, branch)
UNIQUE (id, branch)
FOREIGN KEY (code) REFERENCES some_table (code) on delete cascade
CHECK (price > 0)
# 一時テーブル
https://www.javadrive.jp/postgresql/table/index12.html
# 継承
https://www.javadrive.jp/postgresql/table/index13.html
# 変更
https://www.javadrive.jp/postgresql/table/index16.html
ALTER TABLE sample_tbl ADD COLUMN address varchar(10);
ALTER TABLE sample_tbl ALTER address SET DEFAULT '';
ALTER TABLE sample_tbl ALTER create_date SET NOT NULL;
ALTER TABLE sample_tbl ALTER address TYPE text;
ALTER TABLE sample_tbl ADD PRIMARY KEY (u_id);
ALTER TABLE sample_tbl ADD CONSTRAINT pk_sample_tbl PRIMARY KEY (u_id); notnulは勝手につく
ALTER TABLE sample_tbl DROP CONSTRAINT xxx
IDENTITY 自動で値を入れてくれる(serial, AUTO_INCREMENT みたいなもの)
(id int PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, data text)
## インデックス
https://www.javadrive.jp/postgresql/index/index1.html
CREATE INDEX ON sample_tbl (address);
【Python】JSONの要素の実際と期待値の差異を取得する
jsonを並び替える。
片方にしか存在しないオブジェクトを返す。
まあ並び替えなくてもいいんだけど。
with open("actual.json") as f: act = json.load(f) with open("expected.json") as f: exp = json.load(f) # 並び替えたい場合 act.sort(key=operator.itemgetter("Name", "Age", "Birthday")) exp.sort(key=operator.itemgetter("Name", "Age", "Birthday")) # exp.sort(key=lambda x: (x["Name"], x["Age"])) # print(json.dumps(act)) # print(json.dumps(exp)) print("only in act. something is missing in expected.json ") print([x for x in act if x not in exp]) # for x in act: # if x not in exp: # print(x) print("only in exp. too much items in expected.json") print([x for x in exp if x not in act]) # for x in exp: # if x not in act: # print(x)
actual.json
[ { "Name": "Bob", "Age": 30, "Birthday": "1994-08-20" }, { "Name": "Alice", "Age": 25, "Birthday": "1999-05-15" }, { "Name": "David", "Age": 35, "Birthday": "1989-03-25" }, { "Name": "Eve", "Age": 28, "Birthday": "1996-11-05" } ]
expected.json
[ { "Name": "Alice", "Age": 25, "Birthday": "1999-05-15" }, { "Name": "Bob", "Age": 30, "Birthday": "1994-08-20" }, { "Name": "Charlie", "Age": 40, "Birthday": "1984-12-10" }, { "Name": "David", "Age": 35, "Birthday": "1989-03-25" }, { "Name": "Eve", "Age": 28, "Birthday": "1996-11-05" } ]
VSCodeでAWS EC2に接続する
1.キーペアをローカルに保存
EC2を作成時にローカルに保存する
場所 %userprofile%\.ssh\sshkey.pem
2.ターミナルから接続確認
%userprofile%\.sshに移動しターミナルから実行
ssh -i "sshkey.pem" ec2-user@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.amazonaws.com
4.configファイル作成
場所 %userprofile%\.ssh
Hostはただの名前なので自由につける
HostName EC2の情報から取得する
User AmazonLinuxならec2-user
IdentityFile 相対パスだとダメっぽい
Host ec2-test HostName ec2-user@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.amazonaws.com User ec2-user IdentityFile ~/.ssh/sshkey.pem
5.接続
F1 または 左下の青い><ボタン -> Connect to host
上記のconfigファイルを選択
6.ファイルをVSCodeに展開
フォルダを開くで、任意のフォルダを選択
※注意点
EC2を停止・起動をするとIPアドレスが変わってしまう可能性があるため、
その場合はconfigのHostNameを再設定する。
速習Kotlin
【Kotlin】Gradleのdependanciesを反映させる方法
IntelliJ IDEA初心者です。
問題
build.gradle.ktsのdependanciesにarrowを追加したがimportが認識しない。
ビルドしなおしてもダメ。
ChatGPTに教えてもらった。
方法
画面右上の歯車マークの下のベルマークの下の謎アイコンをクリックする。
(よく見るとゾウさんマーク?)
Gradleのタブが開く。
一番左の更新ボタンを押すとリロードされ反映される。
方法2
変更すると右上に更新ボタンが出現するのでこれを押す。