Source

tddbc-sap02-legacy / TDDBC-Sap02-Library2-Test.st

TestCase subclass: #Book2Test
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TDDBC-Sap02-Library2-Test'!

!Book2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:37'!
test002Bookの初期状態
	|  book |
	book := Book2 new.
	self assert: book id = '0';
		assert: book title = '';
		assert: book author = '';
		assert: book isbn = '';
		assert: book status = #STOCKED.
	!]lang[(11 5 177)0,5,0! !

!Book2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:38'!
test003Attributeを設定するとgetterでその値を得られる
	|  book |
	book := Book2 new.
	book id: '001'.
	self assert: book id = '001'.
	book title: ''.
	self assert: book title = ''.
	book author: ''.
	self assert: book author = ''.
	book isbn: ''.
	self assert: book isbn = ''.
	book status: #STOCKED.
	self assert: book status = #STOCKED.
!]lang[(16 6 6 9 286)0,5,0,5,0! !

!Book2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:38'!
test004等価テスト
	| book1 book2 |
	book1 := Book2 new.
	book2 := Book2 new.
	self assert: book1 = book2.
	self deny: book1 == book2.!]lang[(7 5 116)0,5,0! !

!Book2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:38'!
test005hash
	| book1 book2 |
	book1 := Book2 new id: '001'; yourself.
	book2 := Book2 new id: '001'; yourself.
	self assert: book1 hash = book2 hash.

	book2 id: '002'.
	self deny: book1 hash = book2 hash! !

!Book2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:40'!
test006正しいバイト列を返すことができる
	| book1 book2 book1Bytes book2Bytes |
	book1Bytes := #[48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 99 100 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 48 48 45 48 48 48 0 0 0 0 0 0 0 0 0 83 84 79 67 75 69 68 0 0 0 0 0 0 0 0 0].
	book1 := Book2 new.
	book2 := Book2 new id: '000';
				 title: 'aabb';
				 author: 'ccdd';
				 isbn: '000-000';
				 yourself.
	self assert: book1 asByteArray = book1Bytes.
	self assert: book2 asByteArray = book2Bytes!]lang[(7 16 3053)0,5,0! !

!Book2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:42'!
test007バイト列から対応するBook2を生成できる
	| book1 book2 book1Bytes book2Bytes |
	book1Bytes := #[48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 99 100 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 48 48 45 48 48 48 0 0 0 0 0 0 0 0 0 83 84 79 67 75 69 68 0 0 0 0 0 0 0 0 0].
	book1 := Book2 new.
	book2 := Book2 new id: '000';
				 title: 'aabb';
				 author: 'ccdd';
				 isbn: '000-000';
				 yourself.
	self assert: (Book2 newFrom: book1Bytes) = book1.
	self assert: (Book2 newFrom: book2Bytes) = book2!]lang[(7 10 5 6 3063)0,5,0,5,0! !


TestCase subclass: #Database2Test
	instanceVariableNames: 'savedMethod recordClass db'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TDDBC-Sap02-Library2-Test'!

!Database2Test methodsFor: 'running' stamp: 'sumim 6/9/2011 00:53'!
setUp
	super setUp.
	recordClass := Database2 new recordClass.
	savedMethod := recordClass methodDict at: #datastoreName.
	recordClass methodDict at: #datastoreName put: self class >> #datastoreName.
	FileDirectory default deleteFileNamed: self datastoreName.
	db := Database2 new! !

!Database2Test methodsFor: 'running' stamp: 'sumim 6/9/2011 00:49'!
tearDown
	recordClass methodDict at: #datastoreName put: savedMethod.
	super tearDown! !


!Database2Test methodsFor: 'private' stamp: 'sumim 6/9/2011 00:47'!
datastoreName
	^'testdb.bin'! !


!Database2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:54'!
test001初期状態でリストは空
	self assert: db list isEmpty!]lang[(7 10 30)0,5,0! !

!Database2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:55'!
test002一つ追加するとそれが入ったリストが得られる
	| record |
	record := recordClass new.
	db add: record.
	self assert: db list size = 1.
	self assert: db list first = record!]lang[(7 21 126)0,5,0! !

!Database2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:55'!
test003二つ追加するとそれらがidの順で入ったリストが得られる
	| record1 record2 |
	record1 := recordClass new id: '001';
				 yourself.
	record2 := recordClass new id: '002';
				 yourself.
	db add: record2.
	db add: record1.
	self assert: db list size = 2.
	self assert: db list = {record1. record2}!]lang[(7 11 2 14 240)0,5,0,5,0! !

!Database2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:56'!
test0042つ追加して1つ検索するとそれが返る
	| record1 record2 |
	record1 := recordClass new id: '001';
				 yourself.
	record2 := recordClass new id: '002';
				 yourself.
	db add: record2.
	db add: record1.
	self assert: db list size = 2.
	self assert: db list = {record1. record2}.
	self assert: (db find: '002')
			= record2!]lang[(7 18 285)0,5,0! !

!Database2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 00:58'!
test009検索後追加しても直後のデータが上書きされずリストも正常に機能する
	| record1 record2 record3 |
	record1 := recordClass new id: '001';
				 yourself.
	record2 := recordClass new id: '002';
				 yourself.
	record3 := recordClass new id: '003';
				 yourself.
	db add: record1.
	db add: record2.
	db find: '001'.
	db add: record3.
	self assert: db list size = 3.
	self assert: db list = {record1. record2. record3}.
	db := Database2 new.
	self assert: db list size = 3.
	self assert: db list = {record1. record2. record3}!]lang[(7 32 453)0,5,0! !

!Database2Test methodsFor: 'tests' stamp: 'sumim 6/9/2011 01:04'!
test010同じIDのレコードはDBファイルの同じ位置に書き戻されファイルサイズを増やさない
	| record1 record2 filesize1 filesize2 |
	record1 := recordClass new id: '001';
				 yourself.
	record2 := recordClass new id: '002';
				 yourself.
	db add: record1.
	db add: record2.
	StandardFileStream
		fileNamed: db datastoreName
		do: [:file | filesize1 := file upToEnd; position].
	db add: record1.
	StandardFileStream
		fileNamed: db datastoreName
		do: [:file | filesize2 := file upToEnd; position].
	self assert: filesize1 = filesize2!]lang[(7 2 2 6 2 28 445)0,5,0,5,0,5,0! !